Fix jwt-token
This commit is contained in:
@@ -315,4 +315,84 @@ router.delete('/:id', authenticateToken, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/devices/pending - List devices pending approval
|
||||
router.get('/pending', async (req, res) => {
|
||||
try {
|
||||
const pendingDevices = await Device.findAll({
|
||||
where: { is_approved: false },
|
||||
attributes: [
|
||||
'id', 'name', 'geo_lat', 'geo_lon', 'last_heartbeat',
|
||||
'created_at', 'firmware_version', 'is_approved'
|
||||
],
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: pendingDevices,
|
||||
count: pendingDevices.length
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching pending devices:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch pending devices',
|
||||
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices/:id/approve - Approve or reject a device
|
||||
router.post('/:id/approve', async (req, res) => {
|
||||
try {
|
||||
const deviceId = parseInt(req.params.id);
|
||||
const { approved } = req.body;
|
||||
|
||||
if (typeof approved !== 'boolean') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'approved field must be a boolean'
|
||||
});
|
||||
}
|
||||
|
||||
const device = await Device.findByPk(deviceId);
|
||||
if (!device) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Device not found'
|
||||
});
|
||||
}
|
||||
|
||||
await device.update({ is_approved: approved });
|
||||
|
||||
// Emit real-time notification
|
||||
const { io } = require('../index');
|
||||
if (io) {
|
||||
io.emit('device_approval_updated', {
|
||||
device_id: deviceId,
|
||||
approved: approved,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: approved ?
|
||||
`Device ${deviceId} has been approved` :
|
||||
`Device ${deviceId} approval has been revoked`
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`${approved ? '✅' : '❌'} Device ${deviceId} approval ${approved ? 'granted' : 'revoked'}`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: device,
|
||||
message: approved ? 'Device approved successfully' : 'Device approval revoked'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating device approval:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to update device approval',
|
||||
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user