refactor: move logic to service

This commit is contained in:
Carlos Valente
2025-02-25 21:02:21 +01:00
committed by Carlos Valente
parent 8c593b3487
commit 4cd2dcbab0
2 changed files with 31 additions and 18 deletions
@@ -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 });