From af8a5c5ffe8391d2c7f9cf40d79151369f5eede0 Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Thu, 28 Aug 2025 08:28:02 +0200 Subject: [PATCH] Fix jwt-token --- client/src/pages/Devices.jsx | 24 +++++++++++++++++++----- server/routes/device.js | 16 ++++++++-------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/client/src/pages/Devices.jsx b/client/src/pages/Devices.jsx index 9167ad1..a6dad0e 100644 --- a/client/src/pages/Devices.jsx +++ b/client/src/pages/Devices.jsx @@ -555,16 +555,30 @@ const DeviceModal = ({ device, onClose, onSave }) => { try { if (device) { - // Update existing device - exclude read-only fields + // Update existing device - exclude read-only fields and filter out empty strings const updateData = { name: formData.name, - location_description: formData.location_description, - notes: formData.notes + location_description: formData.location_description || null, + notes: formData.notes || null }; + + // Remove null values to avoid sending unnecessary data + Object.keys(updateData).forEach(key => { + if (updateData[key] === null || updateData[key] === '') { + delete updateData[key]; + } + }); + await api.put(`/devices/${device.id}`, updateData); } else { - // Create new device - include all fields - await api.post('/devices', formData); + // Create new device - include all fields, convert empty strings to null + const createData = { ...formData }; + Object.keys(createData).forEach(key => { + if (createData[key] === '') { + createData[key] = null; + } + }); + await api.post('/devices', createData); } onSave(); } catch (error) { diff --git a/server/routes/device.js b/server/routes/device.js index 59e52bf..d7eacc0 100644 --- a/server/routes/device.js +++ b/server/routes/device.js @@ -9,26 +9,26 @@ const { Op } = require('sequelize'); // Validation schema for device const deviceSchema = Joi.object({ id: Joi.number().integer().required(), - name: Joi.string().max(255).optional(), + name: Joi.string().max(255).allow('').optional(), geo_lat: Joi.number().min(-90).max(90).optional(), geo_lon: Joi.number().min(-180).max(180).optional(), - location_description: Joi.string().optional(), + location_description: Joi.string().allow('').optional(), heartbeat_interval: Joi.number().integer().min(60).max(3600).optional(), - firmware_version: Joi.string().optional(), + firmware_version: Joi.string().allow('').optional(), installation_date: Joi.date().optional(), - notes: Joi.string().optional() + notes: Joi.string().allow('').optional() }); const updateDeviceSchema = Joi.object({ - name: Joi.string().max(255).optional(), + name: Joi.string().max(255).allow('').optional(), geo_lat: Joi.number().min(-90).max(90).optional(), geo_lon: Joi.number().min(-180).max(180).optional(), - location_description: Joi.string().optional(), + location_description: Joi.string().allow('').optional(), is_active: Joi.boolean().optional(), heartbeat_interval: Joi.number().integer().min(60).max(3600).optional(), - firmware_version: Joi.string().optional(), + firmware_version: Joi.string().allow('').optional(), installation_date: Joi.date().optional(), - notes: Joi.string().optional() + notes: Joi.string().allow('').optional() }); // GET /api/devices - Get all devices