atas/src/routes/api/v1/alertContacts.ts

151 lines
5.1 KiB
TypeScript

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, schema_post, schema_patch, schema_del } from './alertContacts_schema.js';
// MARK: GET alertContact
async function get(req: Request, res: Response) {
const { error, value } = schema_get.validate(req.query);
if (error) {
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('alertContact GET Success:', req.query, value);
// Query with FullTextSearch
if (value.search !== undefined || value.id !== undefined) {
// with FullTextSearch
if (!value.count) {
// get all entrys
log.api?.trace('get all entrys - with FullTextSearch');
await db.alertContacts
.findMany({
where: {
OR: [{ id: parseIntOrUndefined(value.id) }, { name: { search: value.search } }, { phone: { search: value.search } }, { comment: { search: value.search } }]
},
orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()),
skip: parseIntOrUndefined(value.skip),
take: parseIntOrUndefined(value.take)
})
.then((result) => {
if (result.length !== 0) {
res.status(200).json(result);
} else {
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
}
});
} else {
// count all entrys
log.api?.trace('count all entrys - with FullTextSearch');
await db.alertContacts
.count({
where: {
OR: [{ id: parseIntOrUndefined(value.id) }, { name: { search: value.search } }, { phone: { search: value.search } }, { comment: { search: value.search } }]
},
orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString())
})
.then((result) => {
res.status(200).json(result);
});
}
} else {
// without FullTextSearch
if (!value.count) {
// get all entrys
log.api?.trace('get all entrys - without FullTextSearch');
await db.alertContacts
.findMany({ orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()), take: parseIntOrUndefined(value.take), skip: parseIntOrUndefined(value.skip) })
.then((result) => {
if (result.length !== 0) {
res.status(200).json(result);
} else {
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
}
});
} else {
// count all entrys without FullTextSearch
log.api?.trace('count all entrys - without FullTextSearch');
await db.alertContacts.count().then((result) => {
res.status(200).json(result);
});
}
}
}
}
// MARK: CREATE alertContact
async function post(req: Request, res: Response) {
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 {
log.api?.debug('alertContact POST Success:', req.body, value);
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
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
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 };