update all storagelocation / unit routes to support new tables (AFLOW-23)

This commit is contained in:
2023-07-10 15:34:04 +02:00
parent a79a1eab81
commit 1605987952
5 changed files with 189 additions and 84 deletions

View File

@ -1,9 +1,19 @@
import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js';
import { parseIntOrUndefined } from '../../../assets/helper.js';
import { parseIntOrUndefined, parseDynamicSortBy, parseIntRelation } from '../../../assets/helper.js';
// Get storageLocation.
function get(req: Request, res: Response) {
async function get(req: Request, res: Response) {
if (req.query.sort === undefined) {
req.query.sort = 'id';
}
if (req.query.order === undefined) {
req.query.order = 'asc';
}
if (req.query.search === undefined) {
req.query.search = '';
}
if (req.query.getAll === undefined) {
// Check if required fields are present.
if (!req.query.id) {
@ -32,16 +42,39 @@ function get(req: Request, res: Response) {
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
} else {
// Get all items
const itemCountNotFiltered = await prisma.storageLocation.count({});
// Get all items (filtered)
const itemCountFiltered = await prisma.storageLocation.count({
where: {
name: {
// @ts-ignore
contains: req.query.search.length > 0 ? req.query.search : ''
}
},
orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString())
});
prisma.storageLocation
.findMany({
take: parseIntOrUndefined(req.query.limit),
skip: parseIntOrUndefined(req.query.offset),
where: {
name: {
// @ts-ignore
contains: req.query.search.length > 0 ? req.query.search : ''
}
},
// Get storageUnit from relation.
include: {
storageUnit: true
}
},
orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()),
})
.then((items) => {
if (items) {
res.status(200).json(items);
res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items });
} else {
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
}
@ -124,7 +157,7 @@ async function patch(req: Request, res: Response) {
},
data: {
name: req.body.name,
storageUnitId: parseIntOrUndefined(req.body.storageUnitId)
storageUnit: parseIntRelation(req.body.storageUnitId)
},
select: {
id: true

View File

@ -1,9 +1,19 @@
import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js';
import { contactType } from '@prisma/client';
import { parseDynamicSortBy, parseIntOrUndefined } from '../../../assets/helper.js';
// Get storageUnit.
function get(req: Request, res: Response) {
async function get(req: Request, res: Response) {
if (req.query.sort === undefined) {
req.query.sort = 'id';
}
if (req.query.order === undefined) {
req.query.order = 'asc';
}
if (req.query.search === undefined) {
req.query.search = '';
}
if (req.query.getAll === undefined) {
// Check if required fields are present.
if (!req.query.id) {
@ -33,17 +43,40 @@ function get(req: Request, res: Response) {
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
} else {
// Get all items
const itemCountNotFiltered = await prisma.storageUnit.count({});
// Get all items (filtered)
const itemCountFiltered = await prisma.storageUnit.count({
where: {
name: {
// @ts-ignore
contains: req.query.search.length > 0 ? req.query.search : ''
}
},
orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString())
});
prisma.storageUnit
.findMany({
take: parseIntOrUndefined(req.query.limit),
skip: parseIntOrUndefined(req.query.offset),
// Get contactInfo and StorageLocation from relation.
include: {
contactInfo: true,
StorageLocation: true
}
},
where: {
name: {
// @ts-ignore
contains: req.query.search.length > 0 ? req.query.search : ''
}
},
orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString())
})
.then((items) => {
if (items) {
res.status(200).json(items);
res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items });
} else {
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnit does not exist' });
}