Add alertContact POST validation
This commit is contained in:
parent
d4da439542
commit
94034fa29f
@ -2,16 +2,16 @@ import { Request, Response } from 'express';
|
|||||||
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
|
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
|
||||||
import log from '../../../handlers/log.js';
|
import log from '../../../handlers/log.js';
|
||||||
import { parseIntOrUndefined, parseDynamicSortBy } from '../../../helpers/prisma_helpers.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) {
|
async function get(req: Request, res: Response) {
|
||||||
const { error, value } = schema_get.validate(req.query);
|
const { error, value } = schema_get.validate(req.query);
|
||||||
if (error) {
|
if (error) {
|
||||||
log.api?.debug('Error:', req.query, value);
|
log.api?.debug('alertContact GET Error:', req.query, value, error.details[0].message);
|
||||||
res.status(400).json({ error: error.details[0].message });
|
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
|
||||||
} else {
|
} else {
|
||||||
log.api?.debug('Success:', req.query, value);
|
log.api?.debug('alertContact GET Success:', req.query, value);
|
||||||
|
|
||||||
// Query with FullTextSearch
|
// Query with FullTextSearch
|
||||||
const query_fts = {
|
const query_fts = {
|
||||||
@ -55,16 +55,23 @@ async function get(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: CREATE AlertContact
|
// MARK: CREATE alertContact
|
||||||
async function post(req: Request, res: Response) {
|
async function post(req: Request, res: Response) {
|
||||||
// Check if undefined or null
|
const { error, value } = schema_post.validate(req.body);
|
||||||
if (req.body.name != null && req.body.phone != null) {
|
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 {
|
||||||
|
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
|
await db.alertContacts
|
||||||
.create({
|
.create({
|
||||||
data: {
|
data: {
|
||||||
name: req.body.name,
|
name: value.name,
|
||||||
phone: req.body.phone,
|
phone: value.phone,
|
||||||
comment: req.body.comment
|
comment: value.comment
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true
|
id: true
|
||||||
@ -73,15 +80,13 @@ async function post(req: Request, res: Response) {
|
|||||||
.then((result) => {
|
.then((result) => {
|
||||||
res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id });
|
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' });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: UPDATE AlertContact
|
// MARK: UPDATE alertContact
|
||||||
async function patch(req: Request, res: Response) {}
|
async function patch(req: Request, res: Response) {}
|
||||||
|
|
||||||
// MARK: DELETE AlertContact
|
// MARK: DELETE alertContact
|
||||||
async function del(req: Request, res: Response) {}
|
async function del(req: Request, res: Response) {}
|
||||||
|
|
||||||
export default { get, post, patch, del };
|
export default { get, post, patch, del };
|
||||||
|
Loading…
Reference in New Issue
Block a user