refactor: duplicate project

This commit is contained in:
Carlos Valente
2024-06-26 22:30:15 +02:00
committed by Carlos Valente
parent ad0e821cc0
commit c9ef8d55c1
3 changed files with 48 additions and 29 deletions
+9 -9
View File
@@ -175,16 +175,12 @@ export async function loadProject(req: Request, res: Response<MessageResponse |
* or a 500 status with an error message in case of an exception. * or a 500 status with an error message in case of an exception.
*/ */
export async function duplicateProjectFile(req: Request, res: Response<MessageResponse | ErrorResponse>) { export async function duplicateProjectFile(req: Request, res: Response<MessageResponse | ErrorResponse>) {
// file to copy from
const { filename } = req.params;
// new file name
const { newFilename } = req.body;
try { 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(', ') });
}
await projectService.duplicateProjectFile(filename, newFilename); await projectService.duplicateProjectFile(filename, newFilename);
res.status(201).send({ res.status(201).send({
@@ -192,6 +188,10 @@ export async function duplicateProjectFile(req: Request, res: Response<MessageRe
}); });
} catch (error) { } catch (error) {
const message = getErrorMessage(error); const message = getErrorMessage(error);
if (message.startsWith('Project file')) {
return res.status(403).send({ message });
}
res.status(500).send({ message }); res.status(500).send({ message });
} }
} }
@@ -66,9 +66,17 @@ export async function getProjectList(): Promise<ProjectFileListResponse> {
/** /**
* Duplicates an existing project file * Duplicates an existing project file
*/ */
export async function duplicateProjectFile(existingProjectFile: string, newProjectFile: string) { export async function duplicateProjectFile(originalFile: string, newFileName: string) {
const projectFilePath = getPathToProject(existingProjectFile); if (!doesProjectExist(originalFile)) {
const duplicateProjectFilePath = getPathToProject(newProjectFile); 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); return copyFile(projectFilePath, duplicateProjectFilePath);
} }
@@ -1,24 +1,30 @@
import { deleteProjectFile } from '../ProjectService.js'; import { deleteProjectFile, duplicateProjectFile } from '../ProjectService.js';
import { appStateProvider } from '../../app-state-service/AppStateService.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 { Mock } from 'vitest';
import { deleteFile } from '../../../utils/parserUtils.js';
vi.mock('./appStateProvider'); // stop the database loading from initiating
vi.mock('./fileSystem'); vi.mock('../../../setup/loadDb.js', () => {
return {
switchDb: vi.fn(),
};
});
vi.mock('../../app-state-service/AppStateService.js', () => ({ vi.mock('../../app-state-service/AppStateService.js', () => ({
appStateProvider: { appStateProvider: {
isLastLoadedProject: vi.fn(), isLastLoadedProject: vi.fn(),
}, },
})); }));
vi.mock('../projectServiceUtils.js', () => ({ vi.mock('../projectServiceUtils.js', () => ({
doesProjectExist: vi.fn(), doesProjectExist: vi.fn(),
getPathToProject: 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', () => { describe('deleteProjectFile', () => {
it('throws an error if trying to delete the currently loaded project', async () => { it('throws an error if trying to delete the currently loaded project', async () => {
(appStateProvider.isLastLoadedProject as Mock).mockResolvedValue(true); (appStateProvider.isLastLoadedProject as Mock).mockResolvedValue(true);
@@ -30,16 +36,21 @@ describe('deleteProjectFile', () => {
(doesProjectExist as Mock).mockReturnValue(false); (doesProjectExist as Mock).mockReturnValue(false);
await expect(deleteProjectFile('nonexistentProject')).rejects.toThrow('Project file not found'); await expect(deleteProjectFile('nonexistentProject')).rejects.toThrow('Project file not found');
}); });
});
it('deletes the project file successfully', async () => { describe('duplicateProjectFile', () => {
(appStateProvider.isLastLoadedProject as Mock).mockResolvedValue(false); it('throws an error if origin project does not exist', async () => {
(doesProjectExist as Mock).mockReturnValue(true); (doesProjectExist as Mock).mockReturnValue(false);
(getPathToProject as Mock).mockReturnValue('/path/to/project'); await expect(duplicateProjectFile('does not exist', 'doesnt matter')).rejects.toThrow('Project file not found');
(deleteFile as Mock).mockResolvedValue(undefined); });
await deleteProjectFile('existingProject'); it('throws an error if new file name is already a project', async () => {
// current project exists
expect(getPathToProject).toHaveBeenCalledWith('existingProject'); (doesProjectExist as Mock).mockReturnValueOnce(true);
expect(deleteFile).toHaveBeenCalledWith('/path/to/project'); // new project exists
(doesProjectExist as Mock).mockReturnValueOnce(true);
expect(duplicateProjectFile('nonexistentProject', 'existingproject')).rejects.toThrow(
'Project file with name existingproject already exists',
);
}); });
}); });