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

This commit is contained in:
Sören Oesterwind 2023-07-09 21:59:35 +02:00
parent 534cc3055f
commit 3f55b22ede

View File

@ -7,7 +7,7 @@ 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.storageUnit prisma.storageUnit
@ -25,12 +25,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: 'storageUnit does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnit 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.storageUnit prisma.storageUnit
@ -45,12 +45,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: 'storageUnit does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnit 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' });
}); });
} }
} }
@ -61,7 +61,7 @@ function post(req: Request, res: Response) {
if (req.body.locationId === 'META_CREATENEW') { if (req.body.locationId === 'META_CREATENEW') {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.street || !req.body.houseNumber || !req.body.zipCode || !req.body.city || !req.body.country || !req.body.name) { if (!req.body.street || !req.body.houseNumber || !req.body.zipCode || !req.body.city || !req.body.country || !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;
} }
@ -86,23 +86,23 @@ 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 storageUnit', 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: 'storageUnit already exists' }); res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageUnit already exists' });
} 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' });
} }
}); });
} else { } else {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.name || !req.body.locationId) { if (!req.body.name || !req.body.locationId) {
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 storageUnit with existing location. // Create storageUnit with existing location.
@ -121,17 +121,17 @@ 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 storageUnit', 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: 'storageUnit already exists' }); res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageUnit already exists' });
} 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' });
} }
}); });
} }
@ -141,7 +141,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.locationId) { if (!req.body.id || !req.body.name || !req.body.locationId) {
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;
} }
@ -154,12 +154,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnit 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' });
} }
// Check if the locationId(contactInfo) exists. If not return 410 Gone. // Check if the locationId(contactInfo) exists. If not return 410 Gone.
@ -171,12 +171,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'locationId does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'locationId 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.storageUnit prisma.storageUnit
@ -191,20 +191,23 @@ async function patch(req: Request, res: Response) {
id: parseInt(req.body.locationId) // TODO: Rename to contactInfoId id: parseInt(req.body.locationId) // TODO: Rename to contactInfoId
} }
} }
},
select: {
id: true
} }
}) })
.then(() => { .then((data) => {
res.status(201).json({ status: 'updated' }); res.status(201).json({ status: 'UPDATED', message: 'Successfully updated storageUnit', 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: 'storageUnit already exists' }); res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageUnit already exists' });
} 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' });
} }
}); });
} }
@ -213,7 +216,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;
} }
@ -226,13 +229,13 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' }); res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnit 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.storageUnit prisma.storageUnit
@ -242,11 +245,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 storageUnit' });
}) })
.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' });
}); });
} }