Skip to content

Commit

Permalink
refactor: duplicate project
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Jun 27, 2024
1 parent 394f927 commit 843cae7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
18 changes: 9 additions & 9 deletions apps/server/src/api-data/db/db.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,23 @@ export async function loadProject(req: Request, res: Response<MessageResponse |
* or a 500 status with an error message in case of an exception.
*/
export async function duplicateProjectFile(req: Request, res: Response<MessageResponse | ErrorResponse>) {
try {
const { filename } = req.params;
const { newFilename } = req.body;

const errors = validateProjectFiles({ filename, newFilename });

if (errors.length) {
return res.status(409).send({ message: errors.join(', ') });
}
// file to copy from
const { filename } = req.params;
// new file name
const { newFilename } = req.body;

try {
await projectService.duplicateProjectFile(filename, newFilename);

res.status(201).send({
message: `Duplicated project ${filename} to ${newFilename}`,
});
} catch (error) {
const message = getErrorMessage(error);
if (message.startsWith('Project file')) {
return res.status(403).send({ message });
}

res.status(500).send({ message });
}
}
Expand Down
14 changes: 11 additions & 3 deletions apps/server/src/services/project-service/ProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ export async function getProjectList(): Promise<ProjectFileListResponse> {
/**
* Duplicates an existing project file
*/
export async function duplicateProjectFile(existingProjectFile: string, newProjectFile: string) {
const projectFilePath = getPathToProject(existingProjectFile);
const duplicateProjectFilePath = getPathToProject(newProjectFile);
export async function duplicateProjectFile(originalFile: string, newFileName: string) {
if (!doesProjectExist(originalFile)) {
throw new Error('Project file not found');
}

if (doesProjectExist(newFileName)) {
throw new Error(`Project file with name ${newFileName} already exists`);
}

const projectFilePath = getPathToProject(originalFile);
const duplicateProjectFilePath = getPathToProject(newFileName);

return copyFile(projectFilePath, duplicateProjectFilePath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { deleteProjectFile } from '../ProjectService.js';
import { deleteProjectFile, duplicateProjectFile } from '../ProjectService.js';
import { appStateProvider } from '../../app-state-service/AppStateService.js';
import { doesProjectExist, getPathToProject } from '../projectServiceUtils.js';
import { doesProjectExist } from '../projectServiceUtils.js';
import { Mock } from 'vitest';
import { deleteFile } from '../../../utils/parserUtils.js';

vi.mock('./appStateProvider');
vi.mock('./fileSystem');
// stop the database loading from initiating
vi.mock('../../../setup/loadDb.js', () => {
return {
switchDb: vi.fn(),
};
});

vi.mock('../../app-state-service/AppStateService.js', () => ({
appStateProvider: {
isLastLoadedProject: vi.fn(),
},
}));

vi.mock('../projectServiceUtils.js', () => ({
doesProjectExist: vi.fn(),
getPathToProject: vi.fn(),
}));
vi.mock('../../../utils/parserUtils.js', () => ({
deleteFile: vi.fn(),
}));

/**
* tests only assert errors since the
* controller depend on these to send the right responses
*/
describe('deleteProjectFile', () => {
it('throws an error if trying to delete the currently loaded project', async () => {
(appStateProvider.isLastLoadedProject as Mock).mockResolvedValue(true);
Expand All @@ -30,16 +36,21 @@ describe('deleteProjectFile', () => {
(doesProjectExist as Mock).mockReturnValue(false);
await expect(deleteProjectFile('nonexistentProject')).rejects.toThrow('Project file not found');
});
});

it('deletes the project file successfully', async () => {
(appStateProvider.isLastLoadedProject as Mock).mockResolvedValue(false);
(doesProjectExist as Mock).mockReturnValue(true);
(getPathToProject as Mock).mockReturnValue('/path/to/project');
(deleteFile as Mock).mockResolvedValue(undefined);

await deleteProjectFile('existingProject');
describe('duplicateProjectFile', () => {
it('throws an error if origin project does not exist', async () => {
(doesProjectExist as Mock).mockReturnValue(false);
await expect(duplicateProjectFile('does not exist', 'doesnt matter')).rejects.toThrow('Project file not found');
});

expect(getPathToProject).toHaveBeenCalledWith('existingProject');
expect(deleteFile).toHaveBeenCalledWith('/path/to/project');
it('throws an error if new file name is already a project', async () => {
// current project exists
(doesProjectExist as Mock).mockReturnValueOnce(true);
// new project exists
(doesProjectExist as Mock).mockReturnValueOnce(true);
expect(duplicateProjectFile('nonexistentProject', 'existingproject')).rejects.toThrow(
'Project file with name existingproject already exists',
);
});
});

0 comments on commit 843cae7

Please sign in to comment.