Spaces:
Paused
Paused
File size: 4,396 Bytes
34367da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import { Router } from 'express';
import { hitlSystem } from '../platform/HumanInTheLoop';
const router = Router();
/**
* Get pending approvals
*/
router.get('/approvals', async (req, res) => {
try {
const { status, approver } = req.query;
let approvals;
if (status === 'pending') {
approvals = hitlSystem.getPendingApprovals(approver as string);
} else {
const filters: any = {};
if (status) filters.status = status;
approvals = hitlSystem.getAuditTrail(filters);
}
res.json({ approvals });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Get approval by ID
*/
router.get('/approvals/:id', async (req, res) => {
try {
const approval = hitlSystem.getApproval(req.params.id);
if (!approval) {
return res.status(404).json({ error: 'Approval not found' });
}
res.json({ approval });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Request approval
*/
router.post('/approvals/request', async (req, res) => {
try {
const { taskId, taskType, description, requestedBy, metadata } = req.body;
if (!taskId || !taskType || !description || !requestedBy) {
return res.status(400).json({ error: 'Missing required fields' });
}
const approval = await hitlSystem.requestApproval(
taskId,
taskType,
description,
requestedBy,
metadata || {}
);
res.json({ approval });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Approve a task
*/
router.post('/approvals/:id/approve', async (req, res) => {
try {
const { approvedBy } = req.body;
if (!approvedBy) {
return res.status(400).json({ error: 'approvedBy is required' });
}
const approval = await hitlSystem.approve(req.params.id, approvedBy);
res.json({ approval });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Reject a task
*/
router.post('/approvals/:id/reject', async (req, res) => {
try {
const { rejectedBy, reason } = req.body;
if (!rejectedBy || !reason) {
return res.status(400).json({ error: 'rejectedBy and reason are required' });
}
const approval = await hitlSystem.reject(req.params.id, rejectedBy, reason);
res.json({ approval });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Get approval statistics
*/
router.get('/approvals/stats', async (req, res) => {
try {
const stats = hitlSystem.getStatistics();
res.json({ stats });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Activate kill switch
*/
router.post('/kill-switch/activate', async (req, res) => {
try {
const { activatedBy, reason } = req.body;
if (!activatedBy || !reason) {
return res.status(400).json({ error: 'activatedBy and reason are required' });
}
hitlSystem.activateKillSwitch(activatedBy, reason);
res.json({ success: true, message: 'Kill switch activated' });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Deactivate kill switch
*/
router.post('/kill-switch/deactivate', async (req, res) => {
try {
const { deactivatedBy } = req.body;
if (!deactivatedBy) {
return res.status(400).json({ error: 'deactivatedBy is required' });
}
hitlSystem.deactivateKillSwitch(deactivatedBy);
res.json({ success: true, message: 'Kill switch deactivated' });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
/**
* Get kill switch status
*/
router.get('/kill-switch/status', async (req, res) => {
try {
const active = hitlSystem.isKillSwitchActive();
res.json({ active });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
export default router;
|