Fix jwt-token
This commit is contained in:
@@ -1,16 +1,37 @@
|
||||
const express = require('express');
|
||||
const { Op } = require('sequelize');
|
||||
const { DroneDetection, Device } = require('../models');
|
||||
const { DroneDetection, Device, Tenant } = require('../models');
|
||||
const { authenticateToken } = require('../middleware/auth');
|
||||
const { getDroneTypeInfo } = require('../utils/droneTypes');
|
||||
const MultiTenantAuth = require('../middleware/multi-tenant-auth');
|
||||
const router = express.Router();
|
||||
|
||||
// Initialize multi-tenant auth
|
||||
const multiAuth = new MultiTenantAuth();
|
||||
|
||||
/**
|
||||
* GET /api/detections
|
||||
* Get all drone detections with filtering and pagination
|
||||
* Get all drone detections with filtering and pagination (tenant-filtered)
|
||||
*/
|
||||
router.get('/', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
// Determine tenant from request
|
||||
const tenantId = await multiAuth.determineTenant(req);
|
||||
if (!tenantId) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Unable to determine tenant'
|
||||
});
|
||||
}
|
||||
|
||||
const tenant = await Tenant.findOne({ where: { slug: tenantId } });
|
||||
if (!tenant) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Tenant not found'
|
||||
});
|
||||
}
|
||||
|
||||
const {
|
||||
device_id,
|
||||
drone_id,
|
||||
@@ -47,12 +68,13 @@ router.get('/', authenticateToken, async (req, res) => {
|
||||
// Calculate offset for pagination
|
||||
const offset = (parseInt(page) - 1) * parseInt(limit);
|
||||
|
||||
// Query detections with device information
|
||||
// Query detections with device information (filtered by tenant)
|
||||
const detections = await DroneDetection.findAll({
|
||||
where: whereClause,
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
where: { tenant_id: tenant.id }, // Filter by tenant
|
||||
attributes: ['id', 'name', 'geo_lat', 'geo_lon', 'location_description', 'is_approved']
|
||||
}],
|
||||
order: [[sort, order.toUpperCase()]],
|
||||
@@ -60,9 +82,14 @@ router.get('/', authenticateToken, async (req, res) => {
|
||||
offset: offset
|
||||
});
|
||||
|
||||
// Get total count for pagination
|
||||
// Get total count for pagination (also filtered by tenant)
|
||||
const totalCount = await DroneDetection.count({
|
||||
where: whereClause
|
||||
where: whereClause,
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
where: { tenant_id: tenant.id }
|
||||
}]
|
||||
});
|
||||
|
||||
// Calculate pagination info
|
||||
@@ -154,13 +181,13 @@ router.get('/debug', authenticateToken, async (req, res) => {
|
||||
// Calculate offset for pagination
|
||||
const offset = (parseInt(page) - 1) * parseInt(limit);
|
||||
|
||||
// Query ALL detections including type 0 for debugging
|
||||
// Query ALL detections including type 0 for debugging (with tenant filtering)
|
||||
const detections = await DroneDetection.findAndCountAll({
|
||||
where: whereClause,
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
attributes: ['id', 'name', 'location_description', 'geo_lat', 'geo_lon']
|
||||
attributes: ['id', 'name', 'location_description', 'geo_lat', 'geo_lon', 'tenant_id']
|
||||
}],
|
||||
limit: parseInt(limit),
|
||||
offset: offset,
|
||||
@@ -205,22 +232,40 @@ router.get('/debug', authenticateToken, async (req, res) => {
|
||||
|
||||
/**
|
||||
* GET /api/detections/:id
|
||||
* Get a specific detection by ID
|
||||
* Get a specific detection by ID (tenant-filtered)
|
||||
*/
|
||||
router.get('/:id', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
// Determine tenant from request
|
||||
const tenantId = await multiAuth.determineTenant(req);
|
||||
if (!tenantId) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Unable to determine tenant'
|
||||
});
|
||||
}
|
||||
|
||||
const tenant = await Tenant.findOne({ where: { slug: tenantId } });
|
||||
if (!tenant) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Tenant not found'
|
||||
});
|
||||
}
|
||||
|
||||
const { id } = req.params;
|
||||
|
||||
const detection = await DroneDetection.findByPk(id, {
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
where: { tenant_id: tenant.id }, // Filter by tenant
|
||||
attributes: ['id', 'name', 'geo_lat', 'geo_lon', 'location_description', 'is_approved']
|
||||
}]
|
||||
});
|
||||
|
||||
if (!detection) {
|
||||
return res.status(404).json({ error: 'Detection not found' });
|
||||
return res.status(404).json({ error: 'Detection not found in your tenant' });
|
||||
}
|
||||
|
||||
// Enhance detection with drone type information
|
||||
@@ -271,93 +316,4 @@ router.delete('/:id', authenticateToken, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/detections/debug
|
||||
* Get all drone detections including drone_type 0 for debugging
|
||||
*/
|
||||
router.get('/debug', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
device_id,
|
||||
start_date,
|
||||
end_date,
|
||||
page = 1,
|
||||
limit = 100,
|
||||
sort = 'server_timestamp',
|
||||
order = 'desc'
|
||||
} = req.query;
|
||||
|
||||
// Build where clause for filtering (includes drone_type 0)
|
||||
const whereClause = {};
|
||||
|
||||
if (device_id) {
|
||||
whereClause.device_id = device_id;
|
||||
}
|
||||
|
||||
if (start_date) {
|
||||
whereClause.server_timestamp = { ...whereClause.server_timestamp, [Op.gte]: new Date(start_date) };
|
||||
}
|
||||
|
||||
if (end_date) {
|
||||
whereClause.server_timestamp = { ...whereClause.server_timestamp, [Op.lte]: new Date(end_date) };
|
||||
}
|
||||
|
||||
// Calculate offset for pagination
|
||||
const offset = (parseInt(page) - 1) * parseInt(limit);
|
||||
|
||||
// Query ALL detections including drone_type 0
|
||||
const { count, rows: detections } = await DroneDetection.findAndCountAll({
|
||||
where: whereClause,
|
||||
include: [{
|
||||
model: Device,
|
||||
attributes: ['id', 'name', 'geo_lat', 'geo_lon', 'location_description']
|
||||
}],
|
||||
order: [[sort, order.toUpperCase()]],
|
||||
limit: parseInt(limit),
|
||||
offset: offset
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(count / parseInt(limit));
|
||||
const hasNextPage = parseInt(page) < totalPages;
|
||||
const hasPrevPage = parseInt(page) > 1;
|
||||
|
||||
// Enhance detections with drone type information and debug flags
|
||||
const enhancedDetections = detections.map(detection => {
|
||||
const droneTypeInfo = getDroneTypeInfo(detection.drone_type);
|
||||
const isDebugDetection = detection.drone_type === 0;
|
||||
|
||||
return {
|
||||
...detection.toJSON(),
|
||||
drone_type_info: droneTypeInfo,
|
||||
is_debug_detection: isDebugDetection,
|
||||
debug_note: isDebugDetection ? 'Debug detection (drone_type 0) - no alerts triggered' : null
|
||||
};
|
||||
});
|
||||
|
||||
res.json({
|
||||
detections: enhancedDetections,
|
||||
pagination: {
|
||||
currentPage: parseInt(page),
|
||||
totalPages,
|
||||
totalCount: count,
|
||||
limit: parseInt(limit),
|
||||
hasNextPage,
|
||||
hasPrevPage
|
||||
},
|
||||
debug_info: {
|
||||
includes_drone_type_0: true,
|
||||
total_detections: count,
|
||||
debug_detections: enhancedDetections.filter(d => d.is_debug_detection).length
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching debug detections:', error);
|
||||
res.status(500).json({
|
||||
error: 'Failed to fetch debug detections',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user