diff --git a/server/routes/detections.js b/server/routes/detections.js index 6608d0d..b62e42e 100644 --- a/server/routes/detections.js +++ b/server/routes/detections.js @@ -1,8 +1,15 @@ const express = require('express'); const { Op } = require('sequelize'); -const models = global.__TEST_MODELS__ || require('../models'); -console.log('🔧 DEBUG: detections route using models:', global.__TEST_MODELS__ ? 'global test models' : 'regular models'); -const { DroneDetection, Device, Tenant } = models; + +// Dynamic model injection for testing +function getModels() { + if (global.__TEST_MODELS__) { + console.log('🔧 DEBUG: Using global test models in detections route'); + return global.__TEST_MODELS__; + } + return require('../models'); +} + const { authenticateToken } = require('../middleware/auth'); const { getDroneTypeInfo } = require('../utils/droneTypes'); const MultiTenantAuth = require('../middleware/multi-tenant-auth'); @@ -17,6 +24,9 @@ const multiAuth = new MultiTenantAuth(); */ router.get('/', authenticateToken, async (req, res) => { try { + const models = getModels(); + const { DroneDetection, Device, Tenant } = models; + // Get tenant from authenticated user context const tenantId = req.tenantId; console.log('🔍 Looking for tenant with slug:', tenantId); @@ -168,6 +178,9 @@ router.get('/', authenticateToken, async (req, res) => { */ router.get('/debug', authenticateToken, async (req, res) => { try { + const models = getModels(); + const { DroneDetection, Device } = models; + // Check if user is admin if (req.user.role !== 'admin') { return res.status(403).json({ @@ -270,6 +283,9 @@ router.get('/debug', authenticateToken, async (req, res) => { */ router.get('/:id', authenticateToken, async (req, res) => { try { + const models = getModels(); + const { DroneDetection, Device, Tenant } = models; + // Get tenant from authenticated user context const tenantId = req.tenantId; if (!tenantId) { @@ -340,6 +356,9 @@ router.get('/:id', authenticateToken, async (req, res) => { */ router.delete('/:id', authenticateToken, async (req, res) => { try { + const models = getModels(); + const { DroneDetection, Device, Tenant } = models; + // Check if user is admin if (req.user.role !== 'admin') { return res.status(403).json({