Fix jwt-token
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { DroneDetection, Device, Heartbeat } = require('../models');
|
||||
const { DroneDetection, Device, Heartbeat, Tenant } = require('../models');
|
||||
const { Op } = require('sequelize');
|
||||
const { sequelize } = require('../models');
|
||||
const { authenticateToken } = require('../middleware/auth');
|
||||
const { MultiTenantAuth } = require('../middleware/multiTenantAuth');
|
||||
|
||||
// GET /api/dashboard/overview - Get dashboard overview statistics
|
||||
router.get('/overview', authenticateToken, async (req, res) => {
|
||||
@@ -11,7 +12,21 @@ router.get('/overview', authenticateToken, async (req, res) => {
|
||||
const { hours = 24 } = req.query;
|
||||
const timeWindow = new Date(Date.now() - hours * 60 * 60 * 1000);
|
||||
|
||||
// Get basic statistics
|
||||
// Initialize multi-tenant auth to determine tenant
|
||||
const multiTenantAuth = new MultiTenantAuth();
|
||||
const tenantId = await multiTenantAuth.determineTenant(req);
|
||||
|
||||
if (!tenantId) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: 'Access denied: No tenant context'
|
||||
});
|
||||
}
|
||||
|
||||
// Create base filter for tenant devices
|
||||
const tenantDeviceFilter = { tenant_id: tenantId };
|
||||
|
||||
// Get basic statistics - filtered by tenant
|
||||
const [
|
||||
totalDevices,
|
||||
activeDevices,
|
||||
@@ -19,16 +34,33 @@ router.get('/overview', authenticateToken, async (req, res) => {
|
||||
recentDetections,
|
||||
uniqueDronesDetected
|
||||
] = await Promise.all([
|
||||
Device.count(),
|
||||
Device.count({ where: { is_active: true } }),
|
||||
DroneDetection.count({ where: { drone_type: { [Op.ne]: 0 } } }),
|
||||
Device.count({ where: tenantDeviceFilter }),
|
||||
Device.count({ where: { ...tenantDeviceFilter, is_active: true } }),
|
||||
DroneDetection.count({
|
||||
include: [{
|
||||
model: Device,
|
||||
where: tenantDeviceFilter,
|
||||
attributes: []
|
||||
}],
|
||||
where: { drone_type: { [Op.ne]: 0 } }
|
||||
}),
|
||||
DroneDetection.count({
|
||||
include: [{
|
||||
model: Device,
|
||||
where: tenantDeviceFilter,
|
||||
attributes: []
|
||||
}],
|
||||
where: {
|
||||
server_timestamp: { [Op.gte]: timeWindow },
|
||||
drone_type: { [Op.ne]: 0 }
|
||||
}
|
||||
}),
|
||||
DroneDetection.count({
|
||||
include: [{
|
||||
model: Device,
|
||||
where: tenantDeviceFilter,
|
||||
attributes: []
|
||||
}],
|
||||
where: {
|
||||
server_timestamp: { [Op.gte]: timeWindow },
|
||||
drone_type: { [Op.ne]: 0 }
|
||||
@@ -38,8 +70,9 @@ router.get('/overview', authenticateToken, async (req, res) => {
|
||||
})
|
||||
]);
|
||||
|
||||
// Get device status breakdown
|
||||
// Get device status breakdown - filtered by tenant
|
||||
const devices = await Device.findAll({
|
||||
where: tenantDeviceFilter,
|
||||
attributes: ['id', 'last_heartbeat', 'heartbeat_interval', 'is_active']
|
||||
});
|
||||
|
||||
@@ -110,7 +143,18 @@ router.get('/activity', authenticateToken, async (req, res) => {
|
||||
const { limit = 50, hours = 24 } = req.query;
|
||||
const timeWindow = new Date(Date.now() - hours * 60 * 60 * 1000);
|
||||
|
||||
// Get recent detections with device info
|
||||
// Initialize multi-tenant auth to determine tenant
|
||||
const multiTenantAuth = new MultiTenantAuth();
|
||||
const tenantId = await multiTenantAuth.determineTenant(req);
|
||||
|
||||
if (!tenantId) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: 'Access denied: No tenant context'
|
||||
});
|
||||
}
|
||||
|
||||
// Get recent detections with device info - filtered by tenant
|
||||
const recentDetections = await DroneDetection.findAll({
|
||||
where: {
|
||||
server_timestamp: { [Op.gte]: timeWindow },
|
||||
@@ -119,18 +163,20 @@ router.get('/activity', authenticateToken, async (req, res) => {
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
where: { tenant_id: tenantId },
|
||||
attributes: ['id', 'name', 'geo_lat', 'geo_lon', 'location_description']
|
||||
}],
|
||||
limit: Math.min(parseInt(limit), 200),
|
||||
order: [['server_timestamp', 'DESC']]
|
||||
});
|
||||
|
||||
// Get recent heartbeats
|
||||
// Get recent heartbeats - filtered by tenant
|
||||
const recentHeartbeats = await Heartbeat.findAll({
|
||||
where: { received_at: { [Op.gte]: timeWindow } },
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
where: { tenant_id: tenantId },
|
||||
attributes: ['id', 'name', 'geo_lat', 'geo_lon']
|
||||
}],
|
||||
limit: Math.min(parseInt(limit), 50),
|
||||
@@ -191,6 +237,17 @@ router.get('/charts/detections', authenticateToken, async (req, res) => {
|
||||
const { hours = 24, interval = 'hour' } = req.query;
|
||||
const timeWindow = new Date(Date.now() - hours * 60 * 60 * 1000);
|
||||
|
||||
// Initialize multi-tenant auth to determine tenant
|
||||
const multiTenantAuth = new MultiTenantAuth();
|
||||
const tenantId = await multiTenantAuth.determineTenant(req);
|
||||
|
||||
if (!tenantId) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: 'Access denied: No tenant context'
|
||||
});
|
||||
}
|
||||
|
||||
let groupBy;
|
||||
switch (interval) {
|
||||
case 'minute':
|
||||
@@ -207,6 +264,11 @@ router.get('/charts/detections', authenticateToken, async (req, res) => {
|
||||
}
|
||||
|
||||
const detectionCounts = await DroneDetection.findAll({
|
||||
include: [{
|
||||
model: Device,
|
||||
where: { tenant_id: tenantId },
|
||||
attributes: []
|
||||
}],
|
||||
where: {
|
||||
server_timestamp: { [Op.gte]: timeWindow },
|
||||
drone_type: { [Op.ne]: 0 }
|
||||
@@ -244,6 +306,17 @@ router.get('/charts/devices', authenticateToken, async (req, res) => {
|
||||
const { hours = 24 } = req.query;
|
||||
const timeWindow = new Date(Date.now() - hours * 60 * 60 * 1000);
|
||||
|
||||
// Initialize multi-tenant auth to determine tenant
|
||||
const multiTenantAuth = new MultiTenantAuth();
|
||||
const tenantId = await multiTenantAuth.determineTenant(req);
|
||||
|
||||
if (!tenantId) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: 'Access denied: No tenant context'
|
||||
});
|
||||
}
|
||||
|
||||
const deviceActivity = await DroneDetection.findAll({
|
||||
where: {
|
||||
server_timestamp: { [Op.gte]: timeWindow },
|
||||
@@ -256,6 +329,7 @@ router.get('/charts/devices', authenticateToken, async (req, res) => {
|
||||
include: [{
|
||||
model: Device,
|
||||
as: 'device',
|
||||
where: { tenant_id: tenantId },
|
||||
attributes: ['name', 'location_description']
|
||||
}],
|
||||
group: ['device_id', 'device.id', 'device.name', 'device.location_description'],
|
||||
|
||||
Reference in New Issue
Block a user