diff --git a/src/assets/helper.ts b/src/assets/helper.ts index 5b6701d..a7311ad 100644 --- a/src/assets/helper.ts +++ b/src/assets/helper.ts @@ -66,13 +66,16 @@ function returnAllModelFieldData() { * @param {string} [relation_name='id'] * @returns {undefined || object} undefined or prisma connect object */ -export function parseIntRelation(data: string, relation_name: string = 'id') { +export function parseIntRelation(data: string, relation_name: string = 'id', doNotDisconnect: boolean = false) { // This function is perfect. If data is not a valid number, return `undefined` // If it is a valid number return `{connect: {relation_name: yourNumber}}}` // This can be used by prisma to connect relations // If the incoming data is null or empty, return a prisma disconnect object instead of a connect one if (data === null || data === '') { + if (doNotDisconnect) { + return undefined; + } return JSON.parse(`{ "disconnect": true }`); diff --git a/src/routes/api/v1/items.ts b/src/routes/api/v1/items.ts index 2e5ed29..2d42f1d 100644 --- a/src/routes/api/v1/items.ts +++ b/src/routes/api/v1/items.ts @@ -102,9 +102,9 @@ function post(req: Request, res: Response) { comment: req.body.comment, status: req.body.status, // Only enum(itemStatus) values are valid // Relations - contactInfo: parseIntRelation(req.body.contactInfoId), - category: parseIntRelation(req.body.categoryId), - storageLocation: parseIntRelation(req.body.storageLocationId), + contactInfo: parseIntRelation(req.body.contactInfoId, undefined, true), + category: parseIntRelation(req.body.categoryId, undefined, true), + storageLocation: parseIntRelation(req.body.storageLocationId, undefined, true), manufacturer: req.body.manufacturer,