Add RUT registration & Validation, bulk aprove
This commit is contained in:
@@ -147,6 +147,76 @@ app.get('/api/recent', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Helper: RUT Validation
|
||||
function validateRut(rut) {
|
||||
if (!rut || !/^[0-9]+-[0-9kK]{1}$/.test(rut)) return false;
|
||||
let [num, dv] = rut.split('-');
|
||||
let total = 0;
|
||||
let multiple = 2;
|
||||
for (let i = num.length - 1; i >= 0; i--) {
|
||||
total += parseInt(num.charAt(i)) * multiple;
|
||||
multiple = (multiple + 1) % 8 || 2;
|
||||
}
|
||||
let res = 11 - (total % 11);
|
||||
let finalDv = res === 11 ? '0' : res === 10 ? 'K' : res.toString();
|
||||
return finalDv.toUpperCase() === dv.toUpperCase();
|
||||
}
|
||||
|
||||
// People CRUD
|
||||
app.get('/api/people', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const where = req.user.role === 'ADMIN' ? {} : { addedById: req.user.id };
|
||||
const people = await prisma.person.findMany({
|
||||
where,
|
||||
include: { addedBy: { select: { username: true } } }
|
||||
});
|
||||
res.json(people);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/people', authenticateToken, async (req, res) => {
|
||||
const { rut, name, durationDays } = req.body;
|
||||
if (!validateRut(rut)) {
|
||||
return res.status(400).json({ error: 'Invalid RUT format (12345678-K)' });
|
||||
}
|
||||
|
||||
const startDate = new Date();
|
||||
const endDate = new Date();
|
||||
endDate.setDate(endDate.getDate() + parseInt(durationDays || 1));
|
||||
|
||||
try {
|
||||
const person = await prisma.person.create({
|
||||
data: {
|
||||
rut,
|
||||
name,
|
||||
startDate,
|
||||
endDate,
|
||||
status: 'PENDING',
|
||||
addedById: req.user.id
|
||||
}
|
||||
});
|
||||
res.json(person);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Admin: Bulk Approve People by User
|
||||
app.post('/api/people/bulk-approve', authenticateToken, isAdmin, async (req, res) => {
|
||||
const { userId } = req.body;
|
||||
try {
|
||||
await prisma.person.updateMany({
|
||||
where: { addedById: parseInt(userId), status: 'PENDING' },
|
||||
data: { status: 'APPROVED' }
|
||||
});
|
||||
res.json({ message: 'Bulk approval successful' });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Detection Endpoint (from Python)
|
||||
app.post('/api/detect', async (req, res) => {
|
||||
const { plate_number } = req.body;
|
||||
|
||||
Reference in New Issue
Block a user