From 5aeec6fb28c9ceef6e8ff2a4a3cf9ab4fa88ab61 Mon Sep 17 00:00:00 2001 From: grey Date: Sun, 9 Jul 2023 23:03:15 +0200 Subject: [PATCH] Fixed AFLOW-26 and AFLOW-14 in items API route --- src/routes/api/v1/items.ts | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/routes/api/v1/items.ts b/src/routes/api/v1/items.ts index 0ee500d..9b8e99f 100644 --- a/src/routes/api/v1/items.ts +++ b/src/routes/api/v1/items.ts @@ -11,14 +11,14 @@ async function get(req: Request, res: Response) { if (req.query.order === undefined) { req.query.order = 'asc'; } - if(req.query.search === undefined) { + if (req.query.search === undefined) { req.query.search = ''; } if (req.query.id) { // Check if number is a valid integer if (!Number.isInteger(parseInt(req.query.id.toString()))) { - res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'The id field must be an integer' }); + res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' }); return; } prisma.item @@ -45,12 +45,12 @@ async function get(req: Request, res: Response) { if (items) { res.status(200).json(items); } else { - res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' }); + res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item 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 { // Get all items @@ -119,12 +119,12 @@ async function get(req: Request, res: Response) { if (items) { res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items }); } else { - res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' }); + res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item 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' }); }); } } @@ -133,13 +133,13 @@ async 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; } // Check if status is valid. if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) { - res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` }); + res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` }); return; } @@ -170,22 +170,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 item', 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: 'Item already exists' }); + res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item 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({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' }); + res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item 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' }); } }); } @@ -194,19 +194,19 @@ function post(req: Request, res: Response) { async function patch(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; } // Check if number is a valid integer if (!Number.isInteger(parseInt(req.body.id.toString()))) { - res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'The id field must be an integer' }); + res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'id field must be an integer' }); return; } // Check if status is valid. if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) { - res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` }); + res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` }); return; } @@ -237,24 +237,27 @@ async function patch(req: Request, res: Response) { // } //}, createdBy: req.body.createdBy + }, + select: { + id: true } }) - .then(() => { - res.status(201).json({ status: 'updated' }); + .then((data) => { + res.status(201).json({ status: 'UPDATED', message: 'Successfully updated item', 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: 'Item already exists', err: err }); + res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item 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 - res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' }); + res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item 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' }); } }); } @@ -263,7 +266,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; } @@ -276,12 +279,12 @@ async function del(req: Request, res: Response) { }); if (result === null) { - res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' }); + res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item 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.item @@ -291,11 +294,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 item' }); }) .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' }); }); }