diff --git a/src/routes/api/v1/storageUnits.ts b/src/routes/api/v1/storageUnits.ts index 4884407..5249a19 100644 --- a/src/routes/api/v1/storageUnits.ts +++ b/src/routes/api/v1/storageUnits.ts @@ -7,7 +7,7 @@ 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.storageUnit @@ -25,12 +25,12 @@ function get(req: Request, res: Response) { if (items) { res.status(200).json(items); } 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) => { 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.storageUnit @@ -45,12 +45,12 @@ function get(req: Request, res: Response) { if (items) { res.status(200).json(items); } 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) => { 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') { // 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) { - 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; } @@ -86,23 +86,23 @@ 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 storageUnit', 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: 'storageUnit already exists' }); + res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageUnit already exists' }); } 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' }); } }); } else { // Check if required fields are present. 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; } // Create storageUnit with existing location. @@ -121,17 +121,17 @@ 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 storageUnit', 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: 'storageUnit already exists' }); + res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageUnit already exists' }); } 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' }); } }); } @@ -141,7 +141,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.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; } @@ -154,12 +154,12 @@ async function patch(req: Request, res: Response) { }); 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; } } 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' }); } // 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) { - 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; } } 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.storageUnit @@ -191,20 +191,23 @@ async function patch(req: Request, res: Response) { id: parseInt(req.body.locationId) // TODO: Rename to contactInfoId } } + }, + select: { + id: true } }) - .then(() => { - res.status(201).json({ status: 'updated' }); + .then((data) => { + res.status(201).json({ status: 'UPDATED', message: 'Successfully updated storageUnit', 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: 'storageUnit already exists' }); + res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageUnit already exists' }); } 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' }); } }); } @@ -213,7 +216,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; } @@ -226,13 +229,13 @@ async function del(req: Request, res: Response) { }); 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; } } 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.storageUnit @@ -242,11 +245,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 storageUnit' }); }) .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' }); }); }