- introduced table view with client side loading
- improved /items/ with support for pagination - improved helper functions for tooltips and popovers - removed console log residue from handleSidebarTriangles - introduction of version route
This commit is contained in:
@ -1,16 +1,18 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { prisma, __path, log } from '../../../index.js';
|
||||
import { itemStatus } from '@prisma/client';
|
||||
import { parseIntRelation, parseIntOrUndefined } from '../../../assets/helper.js';
|
||||
import { parseIntRelation, parseIntOrUndefined, parseDynamicSortBy } from '../../../assets/helper.js';
|
||||
// Get item.
|
||||
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' });
|
||||
return;
|
||||
}
|
||||
async function get(req: Request, res: Response) {
|
||||
// Set sane defaults if undefined.
|
||||
if (req.query.sort === undefined) {
|
||||
req.query.sort = 'id';
|
||||
}
|
||||
if (req.query.order === undefined) {
|
||||
req.query.order = 'asc';
|
||||
}
|
||||
|
||||
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' });
|
||||
@ -48,8 +50,53 @@ function get(req: Request, res: Response) {
|
||||
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
|
||||
});
|
||||
} else {
|
||||
// Get all items
|
||||
const itemCountNotFiltered = await prisma.item.count({});
|
||||
|
||||
// Get all items (filtered)
|
||||
const itemCountFiltered = await prisma.item.count({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
SKU: {
|
||||
// Probably use prisma's Full-text search if it's out of beta
|
||||
// @ts-ignore
|
||||
contains: req.query.search.length > 0 ? req.query.search : ''
|
||||
}
|
||||
},
|
||||
{
|
||||
name: {
|
||||
// @ts-ignore
|
||||
contains: req.query.search.length > 0 ? req.query.search : ''
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString())
|
||||
});
|
||||
// log.core.debug('Dynamic relation:', parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()));
|
||||
|
||||
prisma.item
|
||||
.findMany({
|
||||
take: parseIntOrUndefined(req.query.limit),
|
||||
skip: parseIntOrUndefined(req.query.offset),
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
SKU: {
|
||||
// @ts-ignore
|
||||
contains: req.query.search.length > 0 ? req.query.search : ''
|
||||
}
|
||||
},
|
||||
{
|
||||
name: {
|
||||
// @ts-ignore
|
||||
contains: req.query.search.length > 0 ? req.query.search : ''
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()),
|
||||
// Get contactInfo, category, storageLocation( storageUnit<contactInfo> ) from relations.
|
||||
include: {
|
||||
contactInfo: true,
|
||||
@ -67,7 +114,7 @@ function get(req: Request, res: Response) {
|
||||
})
|
||||
.then((items) => {
|
||||
if (items) {
|
||||
res.status(200).json(JSON.stringify(items));
|
||||
res.status(200).json(JSON.stringify({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items }));
|
||||
} else {
|
||||
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
|
||||
}
|
||||
@ -97,7 +144,7 @@ function post(req: Request, res: Response) {
|
||||
.create({
|
||||
data: {
|
||||
SKU: req.body.sku,
|
||||
amount: parseIntOrUndefined(req.body.ammount), // FIXME: This is silently failing if NaN..
|
||||
amount: parseIntOrUndefined(req.body.amount), // FIXME: This is silently failing if NaN..
|
||||
name: req.body.name,
|
||||
comment: req.body.comment,
|
||||
status: req.body.status, // Only enum(itemStatus) values are valid
|
||||
@ -167,7 +214,7 @@ async function patch(req: Request, res: Response) {
|
||||
},
|
||||
data: {
|
||||
SKU: req.body.sku,
|
||||
amount: parseIntOrUndefined(req.body.ammount), // FIXME: This is silently failing if NaN..
|
||||
amount: parseIntOrUndefined(req.body.amount), // FIXME: This is silently failing if NaN..
|
||||
name: req.body.name,
|
||||
comment: req.body.comment,
|
||||
status: req.body.status, // Only enum(itemStatus) values are valid
|
||||
|
Reference in New Issue
Block a user