mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-07 08:23:55 +00:00
refactor: move logic to service
This commit is contained in:
committed by
Carlos Valente
parent
8c593b3487
commit
4cd2dcbab0
@@ -2,12 +2,11 @@ import { ErrorResponse, ProjectData } from 'ontime-types';
|
||||
import { getErrorMessage } from 'ontime-utils';
|
||||
|
||||
import type { Request, Response } from 'express';
|
||||
import { join } from 'path';
|
||||
|
||||
import { deleteFile, removeUndefined } from '../../utils/parserUtils.js';
|
||||
import { removeUndefined } from '../../utils/parserUtils.js';
|
||||
import { failEmptyObjects } from '../../utils/routerUtils.js';
|
||||
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
|
||||
import { publicDir } from '../../setup/index.js';
|
||||
import { editCurrentProjectData } from '../../services/project-service/ProjectService.js';
|
||||
|
||||
export function getProjectData(_req: Request, res: Response<ProjectData>) {
|
||||
res.json(getDataProvider().getProjectData());
|
||||
@@ -19,9 +18,7 @@ export async function postProjectData(req: Request, res: Response<ProjectData |
|
||||
}
|
||||
|
||||
try {
|
||||
const currentProjectData = getDataProvider().getProjectData();
|
||||
|
||||
const newEvent: Partial<ProjectData> = removeUndefined({
|
||||
const newData: Partial<ProjectData> = removeUndefined({
|
||||
title: req.body?.title,
|
||||
description: req.body?.description,
|
||||
publicUrl: req.body?.publicUrl,
|
||||
@@ -32,18 +29,9 @@ export async function postProjectData(req: Request, res: Response<ProjectData |
|
||||
projectLogo: req.body?.projectLogo,
|
||||
});
|
||||
|
||||
const newData = await getDataProvider().setProjectData(newEvent);
|
||||
const updatedData = await editCurrentProjectData(newData);
|
||||
|
||||
// Delete the old logo if the new logo is empty
|
||||
if (!newData.projectLogo && currentProjectData.projectLogo) {
|
||||
const filePath = join(publicDir.logoDir, currentProjectData.projectLogo);
|
||||
|
||||
deleteFile(filePath).catch((_error) => {
|
||||
/** we do not handle this error */
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).send(newData);
|
||||
res.status(200).send(updatedData);
|
||||
} catch (error) {
|
||||
const message = getErrorMessage(error);
|
||||
res.status(400).send({ message });
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { DatabaseModel, LogOrigin, ProjectFileListResponse } from 'ontime-types';
|
||||
import { DatabaseModel, LogOrigin, ProjectData, ProjectFileListResponse } from 'ontime-types';
|
||||
import { getErrorMessage } from 'ontime-utils';
|
||||
|
||||
import { join } from 'path';
|
||||
import { copyFile } from 'fs/promises';
|
||||
|
||||
import { logger } from '../../classes/Logger.js';
|
||||
@@ -311,3 +312,27 @@ export async function patchCurrentProject(data: Partial<DatabaseModel>) {
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the title of a project
|
||||
* it handles invalidating the necessary data
|
||||
*/
|
||||
export async function editCurrentProjectData(newData: Partial<ProjectData>) {
|
||||
const currentProjectData = getDataProvider().getProjectData();
|
||||
const updatedProjectData = await getDataProvider().setProjectData(newData);
|
||||
|
||||
if (currentProjectData.title !== updatedProjectData.title) {
|
||||
// something
|
||||
}
|
||||
|
||||
// Delete the old logo if the logo has been removed
|
||||
if (!updatedProjectData.projectLogo && currentProjectData.projectLogo) {
|
||||
const filePath = join(publicDir.logoDir, currentProjectData.projectLogo);
|
||||
|
||||
deleteFile(filePath).catch((_error) => {
|
||||
/** we do not handle this error */
|
||||
});
|
||||
}
|
||||
|
||||
return updatedProjectData;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user