Files
ontime/apps/server/src/api-data/custom-views/customViews.router.ts
T
Alex Christoffer Rasmussen 7c1d2f4554 refactor: small cleanups (#2059)
* refactor: remove unneeded async

* chore: add express Router type to all routes

* refactor: don't use index in react key

* refactor: correctly get error message in excel route

* refactor: avoid exporting muteable values
2026-04-26 17:32:50 +02:00

73 lines
2.3 KiB
TypeScript

import express from 'express';
import type { Request, Response, Router } from 'express';
import { type CustomViewsListResponse, type ErrorResponse, type MessageResponse } from 'ontime-types';
import { handleCustomViewsError } from './customViews.errors.js';
import { uploadCustomViewFile } from './customViews.middleware.js';
import {
deleteCustomView,
getCustomViewDownloadPath,
listCustomViews,
restoreDemoView,
uploadCustomView,
} from './customViews.service.js';
import { validateCustomViewSlugParam } from './customViews.validation.js';
export const router: Router = express.Router();
router.get('/', async (_req: Request, res: Response<CustomViewsListResponse | ErrorResponse>) => {
try {
const views = await listCustomViews();
res.status(200).send({ views });
} catch (error) {
handleCustomViewsError(error, res);
}
});
router.post('/restore-demo', async (_req: Request, res: Response<MessageResponse | ErrorResponse>) => {
try {
const view = await restoreDemoView();
res.status(201).send({ message: `Restored demo view "${view.slug}"` });
} catch (error) {
handleCustomViewsError(error, res);
}
});
router.post(
'/:slug/upload',
validateCustomViewSlugParam,
uploadCustomViewFile,
async (req: Request, res: Response<MessageResponse | ErrorResponse>) => {
try {
const view = await uploadCustomView(req.params.slug, req.file);
res.status(201).send({ message: `Uploaded custom view "${view.slug}"` });
} catch (error) {
handleCustomViewsError(error, res);
}
},
);
router.get('/:slug/download', validateCustomViewSlugParam, async (req: Request, res: Response<ErrorResponse>) => {
try {
const pathToFile = await getCustomViewDownloadPath(req.params.slug);
const fileName = `${req.params.slug}-index.html`;
res.download(pathToFile, fileName, (error: Error | null) => {
if (error && !res.headersSent) {
res.status(500).send({ message: 'Could not download custom view' });
}
});
} catch (error) {
handleCustomViewsError(error, res);
}
});
router.delete('/:slug', validateCustomViewSlugParam, async (req: Request, res: Response<ErrorResponse>) => {
try {
await deleteCustomView(req.params.slug);
res.status(204).send();
} catch (error) {
handleCustomViewsError(error, res);
}
});