Fixed AFLOW-26 and AFLOW-14 in storageLocation API route

This commit is contained in:
Sören Oesterwind 2023-07-09 21:28:31 +02:00
parent abb7e7bab3
commit 534cc3055f

View File

@ -1,12 +1,13 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js'; import { prisma, __path, log } from '../../../index.js';
import { parseIntOrUndefined } from '../../../assets/helper.js';
// Get storageLocation. // Get storageLocation.
function get(req: Request, res: Response) { function get(req: Request, res: Response) {
if (req.query.getAll === undefined) { if (req.query.getAll === undefined) {
// Check if required fields are present. // Check if required fields are present.
if (!req.query.id) { if (!req.query.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return; return;
} }
prisma.storageLocation prisma.storageLocation
@ -23,12 +24,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(items); res.status(200).json(items);
} else { } else {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}); });
} else { } else {
prisma.storageLocation prisma.storageLocation
@ -42,12 +43,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(items); res.status(200).json(items);
} else { } else {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}); });
} }
} }
@ -56,7 +57,7 @@ function get(req: Request, res: Response) {
function post(req: Request, res: Response) { function post(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.name) { if (!req.body.name) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return; return;
} }
// Create storageLocation with existing storageUnit. // Create storageLocation with existing storageUnit.
@ -71,22 +72,22 @@ function post(req: Request, res: Response) {
} }
}) })
.then((data) => { .then((data) => {
res.status(201).json({ status: 'created', id: data.id }); res.status(201).json({ status: 'CREATED', message: 'Successfully created storageLocation', id: data.id });
}) })
.catch((err) => { .catch((err) => {
// Check if an entry already exists. // Check if an entry already exists.
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' }); res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageLocation already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right? // FIXME: Is this errormessage right?
res.status(404).json({ error: 'specified storageUnitId does not exist' }); res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnitId does not exist' });
} else { } else {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
} }
}); });
} }
@ -95,7 +96,7 @@ function post(req: Request, res: Response) {
async function patch(req: Request, res: Response) { async function patch(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.id || !req.body.name || !req.body.storageUnitId) { if (!req.body.id || !req.body.name || !req.body.storageUnitId) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return; return;
} }
@ -108,12 +109,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(404).json({ error: 'storageLocation does not exist.' }); res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
} }
prisma.storageLocation prisma.storageLocation
@ -123,26 +124,29 @@ async function patch(req: Request, res: Response) {
}, },
data: { data: {
name: req.body.name, name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined storageUnitId: parseIntOrUndefined(req.body.storageUnitId)
},
select: {
id: true
} }
}) })
.then(() => { .then((data) => {
res.status(201).json({ status: 'updated' }); res.status(201).json({ status: 'UPDATED', message: 'Successfully updated storageLocation', id: data.id });
}) })
.catch((err) => { .catch((err) => {
// Check if an entry already exists. // Check if an entry already exists.
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' }); res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageLocation already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right? // FIXME: Is this errormessage right?
res.status(404).json({ error: 'specified storageUnitId does not exist' }); res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnitId does not exist' });
} else { } else {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
} }
}); });
} }
@ -151,7 +155,7 @@ async function patch(req: Request, res: Response) {
async function del(req: Request, res: Response) { async function del(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.id) { if (!req.body.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return; return;
} }
@ -164,12 +168,12 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
} }
prisma.storageLocation prisma.storageLocation
@ -179,11 +183,11 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' }); res.status(200).json({ status: 'DELETED', message: 'Successfully deleted storageLocation' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}); });
} }