Implemented storageUnit api / Added error handling
- AFLOW-13 - Added check if an category entry already exists. - Update precheck if all IDs are existing
This commit is contained in:
parent
b514e81764
commit
e4295493f2
@ -1,18 +1,19 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { prisma, __path, log } from '../../../index.js';
|
||||
import { contactType } from '@prisma/client';
|
||||
|
||||
// Get storageUnit.
|
||||
function get(req: Request, res: Response) {
|
||||
if (req.query.getAll === undefined) {
|
||||
// Check if required fields are present.
|
||||
if (!req.query.name) {
|
||||
if (!req.query.id) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
prisma.storageUnit
|
||||
.findUnique({
|
||||
where: {
|
||||
name: req.query.name.toString()
|
||||
id: parseInt(req.query.id.toString())
|
||||
},
|
||||
// Get category name from relation.
|
||||
include: {
|
||||
@ -54,37 +55,121 @@ function get(req: Request, res: Response) {
|
||||
|
||||
// Create storageUnit.
|
||||
function post(req: Request, res: Response) {
|
||||
log.web.debug(JSON.stringify(req.body));
|
||||
/* // Check if required fields are present.
|
||||
if (!req.body.name) {
|
||||
// If the frontend wants to create a StorageUnit with non-existing ContactInfo.
|
||||
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).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
// Save data.
|
||||
// Create storageUnit and location.
|
||||
prisma.storageUnit
|
||||
.create({
|
||||
data: {
|
||||
name: req.body.name
|
||||
name: req.body.name,
|
||||
contactInfo: {
|
||||
create: {
|
||||
type: contactType.storageUnit,
|
||||
street: req.body.street,
|
||||
houseNumber: req.body.houseNumber,
|
||||
zipCode: req.body.zipCode,
|
||||
city: req.body.city,
|
||||
country: req.body.country
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'created' });
|
||||
})
|
||||
.catch((err) => {
|
||||
// TODO Catch if is a duplicate error and show a message to the user
|
||||
// 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({ error: 'storageUnit already exists.' });
|
||||
} else {
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
}); */
|
||||
}
|
||||
|
||||
// Update category.
|
||||
function patch(req: Request, res: Response) {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Check if required fields are present.
|
||||
if (!req.body.id || !req.body.name) {
|
||||
if (!req.body.name || !req.body.locationId) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
// Create storageUnit with existing location.
|
||||
prisma.storageUnit
|
||||
.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
contactInfo: {
|
||||
connect: {
|
||||
id: parseInt(req.body.locationId)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'created' });
|
||||
})
|
||||
.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({ error: 'storageUnit already exists.' });
|
||||
} else {
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Update storageUnit. -> Only existing contactInfo.
|
||||
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).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the storageUnit id exists. If not return 410 Gone.
|
||||
try {
|
||||
const result = await prisma.storageUnit.findUnique({
|
||||
where: {
|
||||
id: parseInt(req.body.id)
|
||||
}
|
||||
});
|
||||
|
||||
if (result === null) {
|
||||
res.status(410).json({ error: 'storageUnit does not exist.' });
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
}
|
||||
|
||||
// Check if the locationId(contactInfo) exists. If not return 410 Gone.
|
||||
try {
|
||||
const result = await prisma.contactInfo.findUnique({
|
||||
where: {
|
||||
id: parseInt(req.body.locationId)
|
||||
}
|
||||
});
|
||||
|
||||
if (result === null) {
|
||||
res.status(410).json({ error: 'locationId does not exist.' });
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
}
|
||||
|
||||
prisma.storageUnit
|
||||
.update({
|
||||
@ -92,20 +177,31 @@ function patch(req: Request, res: Response) {
|
||||
id: parseInt(req.body.id)
|
||||
},
|
||||
data: {
|
||||
name: req.body.name
|
||||
name: req.body.name,
|
||||
contactInfo: {
|
||||
connect: {
|
||||
id: parseInt(req.body.locationId)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'updated' });
|
||||
})
|
||||
.catch((err) => {
|
||||
// TODO Catch if is a duplicate error and show a message to the user
|
||||
// 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({ error: 'storageUnit already exists.' });
|
||||
} else {
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Delete category.
|
||||
// Delete storageUnit.
|
||||
async function del(req: Request, res: Response) {
|
||||
// Check if required fields are present.
|
||||
if (!req.body.id) {
|
||||
@ -122,7 +218,7 @@ async function del(req: Request, res: Response) {
|
||||
});
|
||||
|
||||
if (result === null) {
|
||||
res.status(410).json({ error: 'Storage Unit does not exist.' });
|
||||
res.status(410).json({ error: 'storageUnit does not exist.' });
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
@ -137,7 +233,7 @@ async function del(req: Request, res: Response) {
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'deleted' });
|
||||
res.status(200).json({ status: 'deleted' });
|
||||
})
|
||||
.catch((err) => {
|
||||
log.db.error(err);
|
||||
|
Loading…
Reference in New Issue
Block a user