mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-11 10:23:54 +00:00
refactor: create project
This commit is contained in:
committed by
Carlos Valente
parent
ad69c0ff80
commit
21454947e0
@@ -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<Databa
|
||||
* or a 500 status with an error message in case of an exception.
|
||||
*/
|
||||
export async function createProjectFile(req: Request, res: Response<{ filename: string } | ErrorResponse>) {
|
||||
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);
|
||||
|
||||
@@ -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<GetInfo> {
|
||||
/**
|
||||
* applies a partial database model
|
||||
*/
|
||||
// TODO: should be private as part of a load
|
||||
export async function applyDataModel(data: Partial<DatabaseModel>, _options?: Options) {
|
||||
runtimeService.stop();
|
||||
|
||||
|
||||
@@ -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<DatabaseModel>(filePath, data);
|
||||
db = newDb;
|
||||
data = newData;
|
||||
data = newDb.data;
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
Reference in New Issue
Block a user