import { Request, Response } from 'express'; import db, { handlePrismaError } from '../../../handlers/db.js'; // Database import log from '../../../handlers/log.js'; ///api/v1/alertContacts?action=count&filter=... // GET without args -> Get all alertContacts /** * A function to create a sortBy compatible object from a string * * @export * @param {string} SortField * @param {string} Order * @returns {object} */ export function parseDynamicSortBy(SortField: string, Order: string) { return JSON.parse(`{ "${SortField}": "${Order}" }`); } /** * Function to parse a string into a number or return undefined if it is not a number * * @export * @param {string || any} data * @returns {object} */ export function parseIntOrUndefined(data: any) { return isNaN(parseInt(data)) ? undefined : parseInt(data); } // GET AlertContact async function get(req: Request, res: Response) { // Set sane defaults if undefined for sort if (req.query.sort === undefined) { req.query.sort = 'id'; } if (req.query.order === undefined) { req.query.order = 'asc'; } // Prio 1 -> Get count (with or without filter) // Prio 2 -> Get by id // Prio 3 -> Get with filter if ((req.query.search !== undefined && req.query.search.length > 0) || (req.query.id !== undefined && req.query.id.length > 0)) { if (req.query.search !== undefined && req.query.search === '*') { log.db.debug('Single * does not work with FullTextSearch'); req.query.search = ''; } // When an ID is set, remove(disable) the search query if (req.query.id !== undefined && req.query.id.length > 0) { req.query.search = undefined; } const query = { where: { OR: [{ id: parseIntOrUndefined(req.query.id) }, { name: { search: req.query.search } }, { phone: { search: req.query.search } }, { comment: { search: req.query.search } }] }, orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()) }; if (req.query.count === undefined) { // get all entrys await db.alertContacts.findMany(query).then((result) => { res.status(200).json(result); }); } else { // count all entrys (filtered or not) await db.alertContacts.count(query).then((result) => { res.status(200).json(result); }); } } else { if (req.query.count === undefined) { await db.alertContacts.findMany({ orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()) }).then((result) => { res.status(200).json(result); }); } else { await db.alertContacts.count().then((result) => { res.status(200).json(result); }); } } } // CREATE AlertContact async function post(req: Request, res: Response) { // Check if undefined or null if (req.body.name != null && req.body.phone != null) { await db.alertContacts .create({ data: { name: req.body.name, phone: req.body.phone, comment: req.body.comment, }, select: { id: true } }).then((result) => { res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id }); }) } else { res.status(400).json({ status: 'ERROR', errorcode: "VALIDATION_ERROR", message: 'One or more required fields are missing or invalid' }); } } // UPDATE AlertContact async function patch(req: Request, res: Response) {} // DELETE AlertContact async function del(req: Request, res: Response) {} export default { get, post, patch, del };