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 { prisma, __path, log } from '../../../index.js';
import { parseIntOrUndefined } from '../../../assets/helper.js';
// Get storageLocation.
function get(req: Request, res: Response) {
if (req.query.getAll === undefined) {
// Check if required fields are present.
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;
}
prisma.storageLocation
@ -23,12 +24,12 @@ function get(req: Request, res: Response) {
if (items) {
res.status(200).json(items);
} 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) => {
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 {
prisma.storageLocation
@ -42,12 +43,12 @@ function get(req: Request, res: Response) {
if (items) {
res.status(200).json(items);
} 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) => {
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) {
// Check if required fields are present.
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;
}
// Create storageLocation with existing storageUnit.
@ -71,22 +72,22 @@ function post(req: Request, res: Response) {
}
})
.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) => {
// Check if an entry already exists.
if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}"
// 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') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
// 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 {
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) {
// Check if required fields are present.
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;
}
@ -108,12 +109,12 @@ async function patch(req: Request, res: Response) {
});
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;
}
} catch (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
@ -123,26 +124,29 @@ async function patch(req: Request, res: Response) {
},
data: {
name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined
storageUnitId: parseIntOrUndefined(req.body.storageUnitId)
},
select: {
id: true
}
})
.then(() => {
res.status(201).json({ status: 'updated' });
.then((data) => {
res.status(201).json({ status: 'UPDATED', message: 'Successfully updated storageLocation', id: data.id });
})
.catch((err) => {
// Check if an entry already exists.
if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}"
// 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') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
// 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 {
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) {
// Check if required fields are present.
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;
}
@ -164,12 +168,12 @@ async function del(req: Request, res: Response) {
});
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;
}
} catch (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
@ -179,11 +183,11 @@ async function del(req: Request, res: Response) {
}
})
.then(() => {
res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted storageLocation' });
})
.catch((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' });
});
}