From fbd03aeffca9820f1905678ca298448a7767868f Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Tue, 16 Sep 2025 07:11:32 +0200 Subject: [PATCH] Fix jwt-token --- server/models/index.js | 2 ++ server/routes/detectors.js | 12 ++++++++++++ server/tests/setup.js | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/server/models/index.js b/server/models/index.js index 232fcb3..841616b 100644 --- a/server/models/index.js +++ b/server/models/index.js @@ -7,8 +7,10 @@ if (process.env.NODE_ENV !== 'test') { // Check if models are already initialized (for tests) if (global.__TEST_MODELS__) { + console.log(`🔧 DEBUG: Using global test models from models/index.js`); module.exports = global.__TEST_MODELS__; } else { + console.log(`🔧 DEBUG: Creating new models instance in models/index.js`); // Configure database based on environment let sequelize; if (process.env.NODE_ENV === 'test') { diff --git a/server/routes/detectors.js b/server/routes/detectors.js index e194bd2..4a4dda3 100644 --- a/server/routes/detectors.js +++ b/server/routes/detectors.js @@ -207,7 +207,19 @@ async function handleDetection(req, res) { console.log('🔍 Complete detection data:', JSON.stringify(detectionData, null, 2)); // Check if device exists and is approved + console.log(`🔍 DEBUG: Looking for device with ID: ${detectionData.device_id} (type: ${typeof detectionData.device_id})`); + console.log(`🔍 DEBUG: Device model being used:`, Device); + console.log(`🔍 DEBUG: Sequelize instance:`, Device.sequelize.constructor.name); + + // Get all devices to see what's actually in the database + const allDevices = await Device.findAll(); + console.log(`🔍 DEBUG: Total devices in database: ${allDevices.length}`); + allDevices.forEach(d => { + console.log(` - Device ID: ${d.id} (type: ${typeof d.id}), name: ${d.name}, approved: ${d.is_approved}, active: ${d.is_active}`); + }); + let device = await Device.findOne({ where: { id: detectionData.device_id } }); + console.log(`🔍 DEBUG: Device lookup result:`, device ? `Found device ${device.id}` : 'Device not found'); if (!device) { // Device not found - reject detection and require manual registration diff --git a/server/tests/setup.js b/server/tests/setup.js index 41b28e4..164e13d 100644 --- a/server/tests/setup.js +++ b/server/tests/setup.js @@ -94,6 +94,11 @@ async function setupTestEnvironment() { // Store models globally for routes to access global.__TEST_MODELS__ = models; + // Debug info + console.log(`🔧 DEBUG: Test sequelize instance: ${sequelize.constructor.name}`); + console.log(`🔧 DEBUG: Test database storage: ${sequelize.options.storage}`); + console.log(`🔧 DEBUG: Global models set:`, Object.keys(global.__TEST_MODELS__)); + // Sync database await sequelize.sync({ force: true }); @@ -197,14 +202,25 @@ async function createTestDevice(deviceData = {}) { // If a specific ID is provided, use upsert to ensure it's respected if (deviceData.id) { + console.log(`🔧 DEBUG: Creating device with specific ID: ${deviceData.id} (type: ${typeof deviceData.id})`); + console.log(`🔧 DEBUG: Device data:`, defaultDeviceData); const [device, created] = await Device.upsert(defaultDeviceData, { returning: true }); + console.log(`🔧 DEBUG: Device ${created ? 'created' : 'updated'}: ID=${device.id} (type: ${typeof device.id}), approved=${device.is_approved}`); + + // Verify the device exists immediately after creation + const verification = await Device.findByPk(device.id); + console.log(`🔧 DEBUG: Verification lookup: ${verification ? `Found device ${verification.id}` : 'Device not found after creation!'}`); + return device; } else { // Auto-generate ID when none provided defaultDeviceData.id = Math.floor(Math.random() * 1000000000); - return await Device.create(defaultDeviceData); + console.log(`🔧 DEBUG: Creating device with auto-generated ID: ${defaultDeviceData.id}`); + const device = await Device.create(defaultDeviceData); + console.log(`🔧 DEBUG: Auto-generated device created: ID=${device.id}, approved=${device.is_approved}`); + return device; } }