From 94034fa29fced6388fe74b209dab0dec301a2c8b Mon Sep 17 00:00:00 2001 From: Spacelord Date: Thu, 30 Jan 2025 01:00:24 +0100 Subject: [PATCH] Add alertContact POST validation --- src/routes/api/v1/alertContacts.ts | 55 ++++++++++++++++-------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/routes/api/v1/alertContacts.ts b/src/routes/api/v1/alertContacts.ts index 45a006c..8f3f008 100644 --- a/src/routes/api/v1/alertContacts.ts +++ b/src/routes/api/v1/alertContacts.ts @@ -2,16 +2,16 @@ import { Request, Response } from 'express'; import db, { handlePrismaError } from '../../../handlers/db.js'; // Database import log from '../../../handlers/log.js'; import { parseIntOrUndefined, parseDynamicSortBy } from '../../../helpers/prisma_helpers.js'; -import { schema_get } from './alertContacts_schema.js'; +import { schema_get, schema_post } from './alertContacts_schema.js'; -// MARK: GET AlertContact +// MARK: GET alertContact async function get(req: Request, res: Response) { const { error, value } = schema_get.validate(req.query); if (error) { - log.api?.debug('Error:', req.query, value); - res.status(400).json({ error: error.details[0].message }); + log.api?.debug('alertContact GET Error:', req.query, value, error.details[0].message); + res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message }); } else { - log.api?.debug('Success:', req.query, value); + log.api?.debug('alertContact GET Success:', req.query, value); // Query with FullTextSearch const query_fts = { @@ -55,33 +55,38 @@ async function get(req: Request, res: Response) { } } -// MARK: CREATE AlertContact +// MARK: 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 }); - }); + const { error, value } = schema_post.validate(req.body); + if (error) { + log.api?.debug('alertContact POST Error:', req.body, value, error.details[0].message); + res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message }); } else { - res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing or invalid' }); + log.api?.debug('alertContact POST Success:', req.body, value); + + // // Check if undefined or null + // if (req.body.name != null && req.body.phone != null) { + await db.alertContacts + .create({ + data: { + name: value.name, + phone: value.phone, + comment: value.comment + }, + select: { + id: true + } + }) + .then((result) => { + res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id }); + }); } } -// MARK: UPDATE AlertContact +// MARK: UPDATE alertContact async function patch(req: Request, res: Response) {} -// MARK: DELETE AlertContact +// MARK: DELETE alertContact async function del(req: Request, res: Response) {} export default { get, post, patch, del };