mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 14:39:06 +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 { getErrorMessage } from 'ontime-utils';
|
||||||
|
|
||||||
import type { Request, Response } from 'express';
|
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 { failEmptyObjects } from '../../utils/routerUtils.js';
|
||||||
import { getDataProvider } from '../../classes/data-provider/DataProvider.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>) {
|
export function getProjectData(_req: Request, res: Response<ProjectData>) {
|
||||||
res.json(getDataProvider().getProjectData());
|
res.json(getDataProvider().getProjectData());
|
||||||
@@ -19,9 +18,7 @@ export async function postProjectData(req: Request, res: Response<ProjectData |
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const currentProjectData = getDataProvider().getProjectData();
|
const newData: Partial<ProjectData> = removeUndefined({
|
||||||
|
|
||||||
const newEvent: Partial<ProjectData> = removeUndefined({
|
|
||||||
title: req.body?.title,
|
title: req.body?.title,
|
||||||
description: req.body?.description,
|
description: req.body?.description,
|
||||||
publicUrl: req.body?.publicUrl,
|
publicUrl: req.body?.publicUrl,
|
||||||
@@ -32,18 +29,9 @@ export async function postProjectData(req: Request, res: Response<ProjectData |
|
|||||||
projectLogo: req.body?.projectLogo,
|
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
|
res.status(200).send(updatedData);
|
||||||
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);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = getErrorMessage(error);
|
const message = getErrorMessage(error);
|
||||||
res.status(400).send({ message });
|
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 { getErrorMessage } from 'ontime-utils';
|
||||||
|
|
||||||
|
import { join } from 'path';
|
||||||
import { copyFile } from 'fs/promises';
|
import { copyFile } from 'fs/promises';
|
||||||
|
|
||||||
import { logger } from '../../classes/Logger.js';
|
import { logger } from '../../classes/Logger.js';
|
||||||
@@ -311,3 +312,27 @@ export async function patchCurrentProject(data: Partial<DatabaseModel>) {
|
|||||||
|
|
||||||
return newData;
|
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