mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-04 23:18:01 +00:00
refactor: duplicate project
This commit is contained in:
committed by
Carlos Valente
parent
ad0e821cc0
commit
c9ef8d55c1
@@ -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.
|
||||
*/
|
||||
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 {
|
||||
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);
|
||||
|
||||
res.status(201).send({
|
||||
@@ -192,6 +188,10 @@ export async function duplicateProjectFile(req: Request, res: Response<MessageRe
|
||||
});
|
||||
} catch (error) {
|
||||
const message = getErrorMessage(error);
|
||||
if (message.startsWith('Project file')) {
|
||||
return res.status(403).send({ message });
|
||||
}
|
||||
|
||||
res.status(500).send({ message });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
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');
|
||||
});
|
||||
|
||||
await deleteProjectFile('existingProject');
|
||||
|
||||
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',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user