diff --git a/apps/server/src/api-data/db/db.controller.ts b/apps/server/src/api-data/db/db.controller.ts index f406d96e1..a9eec754c 100644 --- a/apps/server/src/api-data/db/db.controller.ts +++ b/apps/server/src/api-data/db/db.controller.ts @@ -13,11 +13,10 @@ import { existsSync } from 'fs'; import type { Request, Response } from 'express'; import { failEmptyObjects } from '../../utils/routerUtils.js'; -import { resolveDbDirectory, resolveProjectsDirectory } from '../../setup/index.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 { generateUniqueFileName } from '../../utils/generateUniqueFilename.js'; import { appStateProvider } from '../../services/app-state-service/AppStateService.js'; import { oscIntegration } from '../../services/integration-service/OscIntegration.js'; import { httpIntegration } from '../../services/integration-service/HttpIntegration.js'; @@ -53,27 +52,20 @@ export async function patchPartialProjectFile(req: Request, res: Response) { + const newProjectData: ProjectData = { + title: req.body?.title ?? '', + description: req.body?.description ?? '', + publicUrl: req.body?.publicUrl ?? '', + publicInfo: req.body?.publicInfo ?? '', + backstageUrl: req.body?.backstageUrl ?? '', + backstageInfo: req.body?.backstageInfo ?? '', + }; + try { - const filename = generateUniqueFileName(resolveProjectsDirectory, req.body.filename); - const errors = validateProjectFiles({ newFilename: filename }); - - if (errors.length) { - return res.status(409).send({ message: 'Project with title already exists' }); - } - - const newProjectData: ProjectData = { - title: req.body?.title ?? '', - description: req.body?.description ?? '', - publicUrl: req.body?.publicUrl ?? '', - publicInfo: req.body?.publicInfo ?? '', - backstageUrl: req.body?.backstageUrl ?? '', - backstageInfo: req.body?.backstageInfo ?? '', - }; - - await projectService.createProjectFile(filename, newProjectData); + const newFileName = await projectService.createProject(req.body.filename, newProjectData); res.status(200).send({ - filename, + filename: newFileName, }); } catch (error) { const message = getErrorMessage(error); diff --git a/apps/server/src/services/project-service/ProjectService.ts b/apps/server/src/services/project-service/ProjectService.ts index 96980893f..e98544aa9 100644 --- a/apps/server/src/services/project-service/ProjectService.ts +++ b/apps/server/src/services/project-service/ProjectService.ts @@ -14,6 +14,8 @@ import { dbModel } from '../../models/dataModel.js'; import { deleteFile } from '../../utils/parserUtils.js'; import { switchDb } from '../../setup/loadDb.js'; import { getPathToProject, getProjectFiles } from './projectServiceUtils.js'; +import { parseJson } from '../../utils/parser.js'; +import { generateUniqueFileName } from '../../utils/generateUniqueFilename.js'; // init dependencies init(); @@ -36,8 +38,10 @@ 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(name); + await switchDb(filePath, result.data); // apply data model await applyDataModel(data, options); @@ -89,8 +93,8 @@ export async function renameProjectFile(existingProjectFile: string, newName: st /** * Creates a new project file and applies its result */ -export async function createProjectFile(filename: string, projectData: ProjectData) { - const data = { +export async function createProject(filename: string, projectData: ProjectData) { + const data: DatabaseModel = { ...dbModel, project: { ...dbModel.project, @@ -98,18 +102,20 @@ export async function createProjectFile(filename: string, projectData: ProjectDa }, }; - // create new file - await writeFile(newFile, JSON.stringify(data)); - const newFile = getPathToProject(filename); + const uniqueFileName = generateUniqueFileName(resolveProjectsDirectory, filename); + const newFile = getPathToProject(uniqueFileName); // change LowDB to point to new file - await switchDb(filename); + await switchDb(newFile, data); - // apply its data + // apply data to running services + // we dont need to parse since we are creating a new file await applyDataModel(data); // update app state to point to new value - appStateProvider.updateDatabaseConfig(filename); + appStateProvider.updateDatabaseConfig(uniqueFileName); + + return uniqueFileName; } /** @@ -144,6 +150,7 @@ export async function getInfo(): Promise { /** * applies a partial database model */ +// TODO: should be private as part of a load export async function applyDataModel(data: Partial, _options?: Options) { runtimeService.stop(); diff --git a/apps/server/src/setup/loadDb.ts b/apps/server/src/setup/loadDb.ts index 1a8dd9824..9bd928ba5 100644 --- a/apps/server/src/setup/loadDb.ts +++ b/apps/server/src/setup/loadDb.ts @@ -88,10 +88,10 @@ const init = async () => { /** * Allows to switch the database to a new file */ -export const switchDb = async (newFileName: string) => { - const { db: newDb, data: newData } = await loadDb(resolveDbDirectory, newFileName); +export const switchDb = async (filePath: string, data: DatabaseModel) => { + const newDb = await JSONFilePreset(filePath, data); db = newDb; - data = newData; + data = newDb.data; }; init();