Implemented POST / PATCH / DELETE for alertcontact api route

This commit is contained in:
Leon Meier 2025-01-30 01:37:32 +01:00
parent 94034fa29f
commit 7715672802
2 changed files with 78 additions and 42 deletions

View File

@ -2,7 +2,7 @@ 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, schema_post } from './alertContacts_schema.js'; import { schema_get, schema_post, schema_patch, schema_del } from './alertContacts_schema.js';
// MARK: GET alertContact // MARK: GET alertContact
async function get(req: Request, res: Response) { async function get(req: Request, res: Response) {
@ -63,9 +63,6 @@ async function post(req: Request, res: Response) {
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message }); res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else { } else {
log.api?.debug('alertContact POST Success:', req.body, value); 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: {
@ -84,9 +81,51 @@ async function post(req: Request, res: Response) {
} }
// MARK: UPDATE alertContact // MARK: UPDATE alertContact
async function patch(req: Request, res: Response) {} async function patch(req: Request, res: Response) {
const { error, value } = schema_patch.validate(req.body);
if (error) {
log.api?.debug('alertContact PATCH 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 PATCH Success:', req.body, value);
await db.alertContacts
.update({
where: {
id: value.id
},
data: {
name: value.name,
phone: value.phone,
comment: value.comment
},
select: {
id: true
}
})
.then((result) => {
res.status(200).json({ status: 'UPDATED', message: 'Successfully updated alertContact', id: result.id });
});
}
}
// MARK: DELETE alertContact // MARK: DELETE alertContact
async function del(req: Request, res: Response) {} async function del(req: Request, res: Response) {
const { error, value } = schema_del.validate(req.body);
if (error) {
log.api?.debug('alertContact DELETE 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 DELETE Success:', req.body, value);
await db.alertContacts
.delete({
where: {
id: value.id
}
})
.then((result) => {
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted alertContact', id: result.id });
});
}
}
export default { get, post, patch, del }; export default { get, post, patch, del };

View File

@ -1,8 +1,7 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import validator from 'joi'; // DOCS: https://joi.dev/api import validator from 'joi'; // DOCS: https://joi.dev/api
import log from '../../../handlers/log.js';
// MARK: GET alertContact
const schema_get = validator.object({ const schema_get = validator.object({
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'), sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'),
order: validator.string().valid('asc', 'desc').default('asc'), order: validator.string().valid('asc', 'desc').default('asc'),
@ -11,42 +10,40 @@ const schema_get = validator.object({
count: validator.boolean() count: validator.boolean()
}).nand('id', 'search'); // Allow id or search. not both. }).nand('id', 'search'); // Allow id or search. not both.
// MARK: CREATE alertContact
const schema_post = validator.object({ const schema_post = validator.object({
name: validator.string().min(1).max(32).required(), name: validator.string().min(1).max(32).required(),
phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')).required(), phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')).required(),
comment: validator.string().max(64), comment: validator.string().max(64),
}) })
// MARK: UPDATE alertContact
const schema_patch = validator.object({ const schema_patch = validator.object({
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'), id: validator.number().positive().precision(0).required(),
order: validator.string().valid('asc', 'desc').default('asc'), name: validator.string().min(1).max(32),
search: validator.string().min(3).max(20), // TODO: Check if * or ** or *** -> Due to crashes.. phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')),
id: validator.number().positive().precision(0), comment: validator.string().max(64),
count: validator.boolean() }).or('name', 'phone', 'comment')
}).nand('id', 'search'); // Allow id or search. not both.
// MARK: DELETE alertContact
const schema_del = validator.object({ const schema_del = validator.object({
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'), id: validator.number().positive().precision(0).required()
order: validator.string().valid('asc', 'desc').default('asc'), })
search: validator.string().min(3).max(20), // TODO: Check if * or ** or *** -> Due to crashes..
id: validator.number().positive().precision(0),
count: validator.boolean()
}).nand('id', 'search'); // Allow id or search. not both.
// Describe all schemas // Describe all schemas
const schema_get_describe = schema_get.describe(); const schema_get_desc = schema_get.describe();
const schema_post_describe = schema_post.describe(); const schema_post_desc = schema_post.describe();
const schema_patch_describe = schema_patch.describe(); const schema_patch_desc = schema_patch.describe();
const schema_del_describe = schema_del.describe(); const schema_del_desc = schema_del.describe();
// GET route // GET route
export default async function get(req: Request, res: Response) { export default async function get(req: Request, res: Response) {
res.status(200).json({ res.status(200).json({
GET: schema_get_describe, GET: schema_get_desc,
POST: schema_post_describe, POST: schema_post_desc,
PATCH: schema_patch_describe, PATCH: schema_patch_desc,
DELETE: schema_del_describe DELETE: schema_del_desc
}); });
} }