Fix jwt-token

This commit is contained in:
2025-09-23 13:55:10 +02:00
parent ee4d3503e5
commit 8fbe2cb354
13 changed files with 1626 additions and 2 deletions

View File

@@ -616,6 +616,135 @@ router.put('/security', authenticateToken, requirePermissions(['security.edit'])
}
});
/**
* GET /tenant/limits
* Get current tenant limits and usage status
*/
router.get('/limits', 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 { getTenantLimitsStatus } = require('../middleware/tenant-limits');
const limitsStatus = await getTenantLimitsStatus(tenant.id);
res.json({
success: true,
data: limitsStatus
});
} catch (error) {
console.error('Error fetching tenant limits:', error);
res.status(500).json({
success: false,
message: 'Failed to fetch tenant limits'
});
}
});
/**
* GET /tenant/data-retention/preview
* Preview what data would be deleted by retention cleanup
* Note: Actual cleanup is handled by separate data-retention-service container
*/
router.get('/data-retention/preview', authenticateToken, requirePermissions(['settings.view']), 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'
});
}
// Calculate what would be deleted (preview only)
const retentionDays = tenant.features?.data_retention_days || 90;
if (retentionDays === -1) {
return res.json({
success: true,
data: {
tenantSlug: tenant.slug,
retentionDays: 'unlimited',
cutoffDate: null,
toDelete: {
detections: 0,
heartbeats: 0,
logs: 0
},
note: 'This tenant has unlimited data retention'
}
});
}
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
const { DroneDetection, Heartbeat } = require('../models');
const { Op } = require('sequelize');
const [detectionsCount, heartbeatsCount] = await Promise.all([
DroneDetection.count({
where: {
tenant_id: tenant.id,
timestamp: { [Op.lt]: cutoffDate }
}
}),
Heartbeat.count({
where: {
tenant_id: tenant.id,
timestamp: { [Op.lt]: cutoffDate }
}
})
]);
res.json({
success: true,
data: {
tenantSlug: tenant.slug,
retentionDays,
cutoffDate: cutoffDate.toISOString(),
toDelete: {
detections: detectionsCount,
heartbeats: heartbeatsCount,
logs: 0 // Security logs are cleaned up by the data retention service
},
note: 'Actual cleanup is performed daily at 2:00 AM UTC by the data-retention-service container'
}
});
} catch (error) {
console.error('Error previewing data retention cleanup:', error);
res.status(500).json({
success: false,
message: 'Failed to preview data retention cleanup'
});
}
});
/**
* GET /tenant/users
* Get users in current tenant (user admin or higher)