Compare commits

..

5 Commits

Author SHA1 Message Date
b785dd8ca7 Mitigated frontend selection issues 2023-06-27 23:47:00 +02:00
4c0be6d87b Implemented api validation and item route patch
- Implemented api field validation in item api routes
- Implemented PATCH api route for item endpoint
2023-06-27 23:45:58 +02:00
87e1c55553 Added comment 2023-06-27 23:43:23 +02:00
a4d697265b Added middleware to handle empty strings as null 2023-06-27 23:42:29 +02:00
ad84e6a3a0 ALT+CTRL+C Keybind!
Don't try to stop me writing in a textbox with the keybind C again....
2023-06-27 23:40:52 +02:00
9 changed files with 116 additions and 54 deletions

View File

@ -70,9 +70,21 @@ export function parseIntRelation(data: string, relation_name: string = 'id') {
// This function is perfect. If data is not a valid number, return `undefined` // This function is perfect. If data is not a valid number, return `undefined`
// If it is a valid number return `{connect: {relation_name: yourNumber}}}` // If it is a valid number return `{connect: {relation_name: yourNumber}}}`
// This can be used by prisma to connect relations // 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 === '') {
return JSON.parse(`{
"disconnect": true
}`);
}
return isNaN(parseInt(data)) ? undefined : JSON.parse(`{ return isNaN(parseInt(data)) ? undefined : JSON.parse(`{
"connect": { "connect": {
"${relation_name}": ${parseInt(data)} "${relation_name}": ${parseInt(data)}
} }
}`); }`);
} }
export function parseIntOrUndefined(data: string) {
return isNaN(parseInt(data)) ? undefined : parseInt(data);
}

View File

@ -21,8 +21,8 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="itemModifyModalStorageLocation" class="form-label">Select a storage location</label> <label for="itemModifyModalStorageLocation" class="form-label">Select a storage location</label>
<select class="form-select" id="itemModifyModalStorageLocation" name="storageLocation" required> <select class="form-select" id="itemModifyModalStorageLocation" name="storageLocation">
<option value="undefined"><i>Do not assign a storage location</i></option> <option value=""><i>Do not assign a storage location</i></option>
<% it.storeLocs.forEach(function(locs){ %> <% it.storeLocs.forEach(function(locs){ %>
<option value="<%= locs.id %>"><%= locs.name %></option> <option value="<%= locs.id %>"><%= locs.name %></option>
<% }) %> <% }) %>
@ -46,8 +46,8 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="itemModifyModalCategory" class="form-label">Select a category</label> <label for="itemModifyModalCategory" class="form-label">Select a category</label>
<select class="form-select" id="itemModifyModalCategory" name="category" required> <select class="form-select" id="itemModifyModalCategory" name="category">
<option value="undefined"><i>Do not assign a category</i></option> <option value=""><i>Do not assign a category</i></option>
<% it.categories.forEach(function(cat){ %> <% it.categories.forEach(function(cat){ %>
<option value="<%= cat.id %>"><%= cat.name %></option> <option value="<%= cat.id %>"><%= cat.name %></option>
<% }) %> <% }) %>
@ -61,13 +61,20 @@
<option value="normal" class="text-success">Normal</option> <option value="normal" class="text-success">Normal</option>
<option value="borrowed" class="text-info">Borrowed</option> <option value="borrowed" class="text-info">Borrowed</option>
<option value="stolen" class="text-danger">Stolen</option> <option value="stolen" class="text-danger">Stolen</option>
<option value="lost" class="text-warning"">Lost</option> <option value="lost" class="text-warning">Lost</option>
</select> </select>
<div id="storageLocationModalLocationText" class="form-text">You have to create a storage location beforehand.</div> <div id="storageLocationModalLocationText" class="form-text">You have to create a storage location beforehand.</div>
</div> </div>
<div class="mb-3">
<label for="itemModifyModalContact" class="form-label">Contact Info</label>
<select class="form-select" id="itemModifyModalContact" name="contactInfoId" onchange="handleSelector()">
<option value=""><i>Do not assign contact info</i></option>
<% it.contactInfo.forEach(function(address){ %>
<option value="<%= address.id %>"><%= address.street %> <%= address.houseNumber %>, <%= address.city %> <%= address.country %></option>
<% }) %>
</select>
</div>
<input type="text" id="itemModifyModalId" name="id" hidden /> <input type="text" id="itemModifyModalId" name="id" hidden />
</div> </div>
<div class="modal-footer"> <div class="modal-footer">

View File

@ -9,10 +9,19 @@ import storageLocationRoute from './storageLocations.js';
import search_routes from './search/index.js'; import search_routes from './search/index.js';
// Router base is '/api/v1' // Router base is '/api/v1'
const Router = express.Router({ strict: false }); const Router = express.Router({ strict: false });
// All empty strings are null values.
Router.use('*', function (req, res, next) {
for (let key in req.body) {
if (req.body[key] === '') {
req.body[key] = null;
}
}
next();
});
Router.route('/items').get(itemRoute.get).post(itemRoute.post).patch(itemRoute.patch).delete(itemRoute.del); Router.route('/items').get(itemRoute.get).post(itemRoute.post).patch(itemRoute.patch).delete(itemRoute.del);
Router.route('/categories').get(categoryRoute.get).post(categoryRoute.post).patch(categoryRoute.patch).delete(categoryRoute.del); Router.route('/categories').get(categoryRoute.get).post(categoryRoute.post).patch(categoryRoute.patch).delete(categoryRoute.del);
// TODO: Migrate routes to lowercase. // TODO: Migrate routes to lowercase.

View File

@ -1,21 +1,27 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js'; import { prisma, __path, log } from '../../../index.js';
//import { itemStatus } from '@prisma/client'; import { itemStatus } from '@prisma/client';
import { parseIntRelation, parseIntOrUndefined } from '../../../assets/helper.js';
// Get item. // Get item.
function get(req: Request, res: Response) { function get(req: Request, res: Response) {
if (req.query.getAll === undefined) { if (req.query.getAll === undefined) {
// Check if required fields are present. // Check if required fields are present
if (!req.query.id) { if (!req.query.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
// 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' });
return;
}
prisma.item prisma.item
.findUnique({ .findUnique({
where: { where: {
id: parseInt(req.query.id.toString()) id: parseInt(req.query.id.toString())
}, },
// Get contactInfo, category, storageLocation( storageUnit<contactInfo> ) from relations. // Get contactInfo, category, storageLocation( storageUnit<contactInfo> ) from relations
include: { include: {
contactInfo: true, contactInfo: true,
category: true, category: true,
@ -38,7 +44,7 @@ function get(req: Request, res: Response) {
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} else { } else {
@ -67,7 +73,7 @@ function get(req: Request, res: Response) {
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }
@ -81,19 +87,24 @@ function post(req: Request, res: Response) {
return; return;
} }
// Check if status is valid.
if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` });
return;
}
prisma.item prisma.item
.create({ .create({
data: { data: {
SKU: req.body.sku, SKU: req.body.sku,
amount: req.body.ammount, amount: parseIntOrUndefined(req.body.ammount), // FIXME: This is silently failing if NaN..
name: req.body.name, name: req.body.name,
comment: req.body.comment, comment: req.body.comment,
status: req.body.status, //itemStatus.normal, status: req.body.status, // Only enum(itemStatus) values are valid
// Relations // Relations
contactInfoId: req.body.contactInfoId, contactInfo: parseIntRelation(req.body.contactInfoId),
categoryId: req.body.categoryId, category: parseIntRelation(req.body.categoryId),
storageLocationId: req.body.storageLocationId, storageLocation: parseIntRelation(req.body.storageLocationId),
manufacturer: req.body.manufacturer, manufacturer: req.body.manufacturer,
@ -109,7 +120,6 @@ function post(req: Request, res: Response) {
} }
}) })
.then((data) => { .then((data) => {
// TODO: Check if id is returned correctly
res.status(201).json({ status: 'created', id: data.id }); res.status(201).json({ status: 'created', id: data.id });
}) })
.catch((err) => { .catch((err) => {
@ -117,12 +127,12 @@ function post(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' }); res.status(409).json({ errorcode: 'EXISTING', error: 'Item already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right? // FIXME: Is this errormessage right?
res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'specified storageUnitId does not exist' }); res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' });
} else { } else {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
@ -132,39 +142,51 @@ function post(req: Request, res: Response) {
// Update storageLocation. -> Only existing contactInfo. // Update storageLocation. -> Only existing contactInfo.
async function patch(req: Request, res: Response) { async function patch(req: Request, res: Response) {
res.status(501).json({ errorcode: 'NOT_IMPLEMENTED', error: 'This endpoint is not yet implemented' });
/*
// Check if required fields are present. // Check if required fields are present.
if (!req.body.id || !req.body.name || !req.body.storageUnitId) { if (!req.body.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' }); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
// Check if the storageLocation id exists. If not return 410 Gone. // Check if number is a valid integer
try { if (!Number.isInteger(parseInt(req.body.id.toString()))) {
const result = await prisma.storageLocation.findUnique({ res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'The id field must be an integer' });
where: {
id: parseInt(req.body.id)
}
});
if (result === null) {
res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'specified storageUnitId does not exist' });
return; return;
} }
} catch (err) {
log.db.error(err); // Check if status is valid.
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` });
return;
} }
prisma.storageLocation prisma.item
.update({ .update({
where: { where: {
id: parseInt(req.body.id) id: parseInt(req.body.id)
}, },
data: { data: {
SKU: req.body.sku,
amount: parseIntOrUndefined(req.body.ammount), // FIXME: This is silently failing if NaN..
name: req.body.name, name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined 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),
manufacturer: req.body.manufacturer,
//contents: {
// connect: [{ id: 1 }, { id: 2 }, { id: 3 }]
//},
//baseItem: {
// connect: {
// id: req.body.baseitemId
// }
//},
createdBy: req.body.createdBy
} }
}) })
.then(() => { .then(() => {
@ -175,17 +197,16 @@ async function patch(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' }); res.status(409).json({ errorcode: 'EXISTING', error: 'Item already exists', err: err });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(404).json({ error: 'specified storageUnitId does not exist' }); res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' });
} else { } else {
log.db.error(err); log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
*/
} }
// Delete item. // Delete item.

View File

@ -188,7 +188,7 @@ async function patch(req: Request, res: Response) {
name: req.body.name, name: req.body.name,
contactInfo: { contactInfo: {
connect: { connect: {
id: parseInt(req.body.locationId) id: parseInt(req.body.locationId) // TODO: Rename to contactInfoId
} }
} }
} }

View File

@ -27,7 +27,10 @@ async function get(req: Request, res: Response) {
.then((items) => { .then((items) => {
prisma.storageLocation.findMany({}).then((locations) => { prisma.storageLocation.findMany({}).then((locations) => {
prisma.itemCategory.findMany({}).then((categories) => { prisma.itemCategory.findMany({}).then((categories) => {
res.render(__path + '/src/frontend/items.eta.html', { items: items, currentPage: page, maxPages: pageSize, storeLocs: locations, categories: categories }); prisma.contactInfo.findMany({}).then((contactInfo) => {
res.render(__path + '/src/frontend/items.eta.html', { items: items, currentPage: page, maxPages: pageSize, storeLocs: locations, categories: categories, contactInfo: contactInfo });
})
}); });
}); });
}) })

View File

@ -32,6 +32,7 @@ function getDataForEdit(id) {
const modal_itemCategory = document.getElementById('itemModifyModalCategory'); const modal_itemCategory = document.getElementById('itemModifyModalCategory');
const modal_itemStatus = document.getElementById('itemModifyModalStatus'); const modal_itemStatus = document.getElementById('itemModifyModalStatus');
const modal_itemid = document.getElementById('itemModifyModalId'); const modal_itemid = document.getElementById('itemModifyModalId');
const modal_userinfo = document.getElementById('itemModifyModalContact');
modal_itemName.value = result.name; modal_itemName.value = result.name;
modal_itemComment.value = result.comment; modal_itemComment.value = result.comment;
@ -42,7 +43,7 @@ function getDataForEdit(id) {
// Select the correct option in the dropdown // Select the correct option in the dropdown
const modal_itemCategoryOptions = modal_itemCategory.options; const modal_itemCategoryOptions = modal_itemCategory.options;
for (let i = 0; i < modal_itemCategoryOptions.length; i++) { for (let i = 0; i < modal_itemCategoryOptions.length; i++) {
if (modal_itemCategoryOptions[i].value == result.category.id) { if (modal_itemCategoryOptions[i].value == result.categoryId) {
modal_itemCategoryOptions[i].selected = true; modal_itemCategoryOptions[i].selected = true;
} }
} }
@ -50,7 +51,7 @@ function getDataForEdit(id) {
// Select the correct option in the dropdown // Select the correct option in the dropdown
const modal_itemStatusOptions = modal_itemStatus.options; const modal_itemStatusOptions = modal_itemStatus.options;
for (let i = 0; i < modal_itemStatusOptions.length; i++) { for (let i = 0; i < modal_itemStatusOptions.length; i++) {
if (modal_itemStatusOptions[i].value == result.status.id) { if (modal_itemStatusOptions[i].value == result.statusId) {
modal_itemStatusOptions[i].selected = true; modal_itemStatusOptions[i].selected = true;
} }
} }
@ -58,11 +59,20 @@ function getDataForEdit(id) {
// Select the correct option in the dropdown // Select the correct option in the dropdown
const modal_itemStorageLocationOptions = modal_itemStorageLocation.options; const modal_itemStorageLocationOptions = modal_itemStorageLocation.options;
for (let i = 0; i < modal_itemStorageLocationOptions.length; i++) { for (let i = 0; i < modal_itemStorageLocationOptions.length; i++) {
if (modal_itemStorageLocationOptions[i].value == result.storageLocation.id) { if (modal_itemStorageLocationOptions[i].value == result.storageLocationId) {
modal_itemStorageLocationOptions[i].selected = true; modal_itemStorageLocationOptions[i].selected = true;
} }
} }
modal_userinfo.selectedIndex = 0;
// Select the correct option in the dropdown
const modal_userInfoOptions = modal_userinfo.options;
for (let i = 0; i < modal_userInfoOptions.length; i++) {
if (modal_userInfoOptions[i].value == result.contactInfoId) {
modal_userInfoOptions[i].selected = true;
}
}
modal_itemid.value = result.id; modal_itemid.value = result.id;
}, },
error: function (data) { error: function (data) {

View File

@ -45,7 +45,7 @@ function primeEdit() {
document.getElementById('storageUnitModalLocationSelectText').innerText= "While editing you can only select already existing locations. Use the settings to create new ones."; document.getElementById('storageUnitModalLocationSelectText').innerText= "While editing you can only select already existing locations. Use the settings to create new ones.";
document.getElementById('storageUnitModalLabel').innerText = "Edit a storage unit"; document.getElementById('storageUnitModalLabel').innerText = "Edit a storage unit";
document.getElementById('storageLocationModalTitle').innerText = "Edit a storage location" document.getElementById('storageLocationModalTitle').innerText = "Edit a storage location"
document.getElementById('storageUnitModalLocationSelect').value = 1 document.getElementById('storageUnitModalLocationSelect').selectedIndex = 1
handleSelector() handleSelector()
form.setAttribute('method', 'PATCH'); form.setAttribute('method', 'PATCH');
form2.setAttribute('method', 'PATCH'); form2.setAttribute('method', 'PATCH');
@ -121,7 +121,7 @@ function getDataForEditLoc(id) {
// Select the correct location from the select based on the value of the option // Select the correct location from the select based on the value of the option
for(var i, j = 0; i = modal_locationUnitSel.options[j]; j++) { for(var i, j = 0; i = modal_locationUnitSel.options[j]; j++) {
if(i.value == result.storageUnit) { if(i.value == result.storageUnitId) {
console.log("Found it"); console.log("Found it");
modal_locationUnitSel.selectedIndex = j; modal_locationUnitSel.selectedIndex = j;
break; break;

View File

@ -105,7 +105,7 @@ function handleSearchSubmit(e) {
function handleHotKey(e) { function handleHotKey(e) {
// If c is pressed, focus on the search box // If c is pressed, focus on the search box
if(e.key == 'c') { if(e.key == 'c' && e.altKey && e.ctrlKey) {
// Show search_modal modal // Show search_modal modal
bootstrap.Modal.getOrCreateInstance($('#search_modal')).show() bootstrap.Modal.getOrCreateInstance($('#search_modal')).show()
document.getElementById('SearchBoxInput').focus(); document.getElementById('SearchBoxInput').focus();