Add alertContact POST validation

This commit is contained in:
Leon Meier 2025-01-30 01:00:24 +01:00
parent d4da439542
commit 94034fa29f

View File

@ -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 };