Skip to content

Commit

Permalink
refactor: rename project
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Jun 27, 2024
1 parent 843cae7 commit df4aecb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 64 deletions.
2 changes: 1 addition & 1 deletion apps/client/src/common/api/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export async function renameProject(filename: string, newFilename: string): Prom
const url = `${dbPath}/${filename}/rename`;
const decodedUrl = decodeURIComponent(url);
const res = await axios.put(decodedUrl, {
filename: newFilename,
newFilename,
});
return res.data;
}
Expand Down
17 changes: 7 additions & 10 deletions apps/server/src/api-data/db/db.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import type { Request, Response } from 'express';
import { failEmptyObjects } from '../../utils/routerUtils.js';
import { resolveDbDirectory } from '../../setup/index.js';

import * as projectService from '../../services/project-service/ProjectService.js';
import { doesProjectExist, upload, validateProjectFiles } from '../../services/project-service/projectServiceUtils.js';
import { doesProjectExist, upload } from '../../services/project-service/projectServiceUtils.js';
import { oscIntegration } from '../../services/integration-service/OscIntegration.js';
import { httpIntegration } from '../../services/integration-service/HttpIntegration.js';
import { DataProvider } from '../../classes/data-provider/DataProvider.js';
import * as projectService from '../../services/project-service/ProjectService.js';

export async function patchPartialProjectFile(req: Request, res: Response<DatabaseModel | ErrorResponse>) {
// all fields are optional in validation
Expand Down Expand Up @@ -208,23 +208,20 @@ export async function duplicateProjectFile(req: Request, res: Response<MessageRe
*/
export async function renameProjectFile(req: Request, res: Response<MessageResponse | ErrorResponse>) {
try {
const { filename: newFilename } = req.body;
const { newFilename } = req.body;
const { filename } = req.params;

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

if (errors.length) {
return res.status(409).send({ message: errors.join(', ') });
}

// Rename the file
await projectService.renameProjectFile(filename, newFilename);

res.status(201).send({
message: `Renamed 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
2 changes: 1 addition & 1 deletion apps/server/src/api-data/db/db.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ router.get('/all', listProjects);

router.post('/load', validateFilenameBody, loadProject);
router.post('/:filename/duplicate', validateFilenameParam, validateNewFilenameBody, duplicateProjectFile);
router.put('/:filename/rename', validateFilenameParam, validateFilenameBody, renameProjectFile);
router.put('/:filename/rename', validateFilenameParam, validateNewFilenameBody, renameProjectFile);
router.delete('/:filename', validateFilenameParam, deleteProjectFile);

router.get('/info', getInfo);
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AppState {
return data.lastLoadedProject;
}

async updateDatabaseConfig(filename: string): Promise<void> {
async setLastLoadedProject(filename: string): Promise<void> {
if (isTest) return;

if (!this.didInit) {
Expand Down
38 changes: 21 additions & 17 deletions apps/server/src/services/project-service/ProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { dbModel } from '../../models/dataModel.js';
import { deleteFile } from '../../utils/parserUtils.js';
import { switchDb } from '../../setup/loadDb.js';
import { doesProjectExist, getPathToProject, getProjectFiles } from './projectServiceUtils.js';
import { parseJson } from '../../utils/parser.js';
import { generateUniqueFileName } from '../../utils/generateUniqueFilename.js';

// init dependencies
Expand All @@ -38,16 +37,14 @@ export async function applyProjectFile(name: string, options?: Options) {
const filePath = getPathToProject(name);
const data = parseProjectFile(filePath);

const result = parseJson(data);

// change LowDB to point to new file
await switchDb(filePath, result.data);
await switchDb(filePath);

// apply data model
await applyDataModel(data, options);

// persist the project selection
await appStateProvider.updateDatabaseConfig(name);
await appStateProvider.setLastLoadedProject(name);
}

/**
Expand All @@ -66,35 +63,42 @@ export async function getProjectList(): Promise<ProjectFileListResponse> {
/**
* Duplicates an existing project file
*/
export async function duplicateProjectFile(originalFile: string, newFileName: string) {
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`);
if (doesProjectExist(newFilename)) {
throw new Error(`Project file with name ${newFilename} already exists`);
}

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

return copyFile(projectFilePath, duplicateProjectFilePath);
}

/**
* Renames an existing project file
*/
export async function renameProjectFile(existingProjectFile: string, newName: string) {
const projectFilePath = getPathToProject(existingProjectFile);
const newProjectFilePath = getPathToProject(newName);
export async function renameProjectFile(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 newProjectFilePath = getPathToProject(newFilename);

await rename(projectFilePath, newProjectFilePath);

// Update the last loaded project config if current loaded project is the one being renamed
const lastLoadedProject = await appStateProvider.getLastLoadedProject();

if (lastLoadedProject === existingProjectFile) {
await appStateProvider.updateDatabaseConfig(newName);
const isLoaded = await appStateProvider.isLastLoadedProject(originalFile);
if (isLoaded) {
await applyProjectFile(newFilename);
}
}

Expand All @@ -121,7 +125,7 @@ export async function createProject(filename: string, projectData: ProjectData)
await applyDataModel(data);

// update app state to point to new value
appStateProvider.updateDatabaseConfig(uniqueFileName);
appStateProvider.setLastLoadedProject(uniqueFileName);

return uniqueFileName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { deleteProjectFile, duplicateProjectFile } from '../ProjectService.js';
import { deleteProjectFile, duplicateProjectFile, renameProjectFile } from '../ProjectService.js';
import { appStateProvider } from '../../app-state-service/AppStateService.js';
import { doesProjectExist } from '../projectServiceUtils.js';
import { Mock } from 'vitest';
import { rename } from 'fs';

Check failure on line 5 in apps/server/src/services/project-service/__tests__/ProjectService.test.ts

View workflow job for this annotation

GitHub Actions / unit-test

'rename' is defined but never used. Allowed unused vars must match /^_/u

// stop the database loading from initiating
vi.mock('../../../setup/loadDb.js', () => {
Expand Down Expand Up @@ -54,3 +55,20 @@ describe('duplicateProjectFile', () => {
);
});
});

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

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(renameProjectFile('nonexistentProject', 'existingproject')).rejects.toThrow(
'Project file with name existingproject already exists',
);
});
});
29 changes: 0 additions & 29 deletions apps/server/src/services/project-service/projectServiceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,6 @@ export function doesProjectExist(name: string): boolean {
return existsSync(projectFilePath);
}

/**
* @description Validates the existence of project files.
* @param {object} projectFiles
* @param {string} projectFiles.projectFilename
* @param {string} projectFiles.newFilename
*
* @returns {Promise<Array<string>>} Array of errors
*
*/
export const validateProjectFiles = (projectFiles: { filename?: string; newFilename?: string }): Array<string> => {
const errors: string[] = [];

// current project must exist
if (projectFiles.filename) {
if (!doesProjectExist(projectFiles.filename)) {
errors.push('Project file does not exist');
}
}

// new project must NOT exist
if (projectFiles.newFilename) {
if (doesProjectExist(projectFiles.newFilename)) {
errors.push('New project file already exists');
}
}

return errors;
};

/**
* Returns the absolute path to a project file
*/
Expand Down
13 changes: 9 additions & 4 deletions apps/server/src/setup/loadDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ const populateDb = (directory: string, filename: string): string => {
async function loadDb(directory: string, filename: string) {
const dbInDisk = populateDb(directory, filename);

// TODO: should this be passed in somewhere?
let newData: DatabaseModel = dbModel;

try {
const maybeProjectFile = parseProjectFile(dbInDisk);
const result = parseJson(maybeProjectFile);

await appStateProvider.updateDatabaseConfig(filename);
await appStateProvider.setLastLoadedProject(filename);

newData = result.data;
} catch (error) {
Expand Down Expand Up @@ -88,10 +89,14 @@ const init = async () => {
/**
* Allows to switch the database to a new file
*/
export const switchDb = async (filePath: string, data: DatabaseModel) => {
const newDb = await JSONFilePreset<DatabaseModel>(filePath, data);
export const switchDb = async (filePath: string, initialData: DatabaseModel = dbModel) => {
const newDb = await JSONFilePreset<DatabaseModel>(filePath, initialData);

// Read the database to initialize it
await newDb.read();

db = newDb;
data = newDb.data;
data = db.data;
};

init();

0 comments on commit df4aecb

Please sign in to comment.