Ride-The-Lightning-RTL/server/controllers/shared/pageSettings.ts

37 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-10-14 22:53:36 +02:00
import { Database, DatabaseService } from '../../utils/database.js';
import { Logger, LoggerService } from '../../utils/logger.js';
import { Common, CommonService } from '../../utils/common.js';
2022-11-02 23:59:37 +01:00
import { CollectionsEnum, PageSettings } from '../../models/database.model.js';
2022-10-14 22:53:36 +02:00
const logger: LoggerService = Logger;
const common: CommonService = Common;
const databaseService: DatabaseService = Database;
export const getPageSettings = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Page Settings', msg: 'Getting Page Settings..' });
2022-12-28 03:05:42 +01:00
databaseService.find(req.session.selectedNode, CollectionsEnum.PAGE_SETTINGS).then((settings: any) => {
2022-10-14 22:53:36 +02:00
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Page Settings', msg: 'Page Settings Received', data: settings });
2022-12-28 04:04:23 +01:00
res.status(200).json(settings);
2022-10-14 22:53:36 +02:00
}).catch((errRes) => {
const err = common.handleError(errRes, 'Page Settings', 'Page Settings Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};
export const savePageSettings = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Page Settings', msg: 'Saving Page Settings..' });
2022-10-16 02:25:46 +02:00
// eslint-disable-next-line arrow-body-style
return Promise.all(req.body.map((page) => databaseService.validateDocument(CollectionsEnum.PAGE_SETTINGS, page))).then((values) => {
2022-10-28 23:20:34 +02:00
return databaseService.insert(req.session.selectedNode, CollectionsEnum.PAGE_SETTINGS, req.body).then((insertRes) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Page Settings', msg: 'Page Settings Updated', data: insertRes });
res.status(201).json(insertRes);
}).catch((insertErrRes) => {
const err = common.handleError(insertErrRes, 'Page Settings', 'Page Settings Update Error', req.session.selectedNode);
2022-11-02 23:59:37 +01:00
return res.status(err.statusCode).json({ message: err.message, error: err.error });
2022-10-28 23:20:34 +02:00
});
2022-10-16 02:25:46 +02:00
}).catch((errRes) => {
const err = common.handleError(errRes, 'Page Settings', 'Page Settings Validation Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
2022-10-14 22:53:36 +02:00
};