(Feat-Fix): Added unit display and completion in work allocation system, and ability to edit completed units. Fixed contractor reporting system not displaying data under corresponding departments.

This commit is contained in:
2025-12-31 08:09:04 +00:00
parent 1ac9d22e2d
commit 4e5555c658
13 changed files with 2361 additions and 29 deletions

View File

@@ -304,6 +304,91 @@ router.put(
},
);
// Update work allocation units (Supervisor, Contractor, or SuperAdmin)
router.put(
"/:id/units",
authenticateToken,
authorize("Supervisor", "Contractor", "SuperAdmin"),
async (ctx) => {
try {
const currentUser = getCurrentUser(ctx);
const allocationId = ctx.params.id;
const body = await ctx.request.body.json() as {
units?: number;
completedUnits?: number;
markComplete?: boolean;
};
const { units, completedUnits, markComplete } = body;
if (units === undefined && completedUnits === undefined) {
ctx.response.status = 400;
ctx.response.body = { error: "Units or completedUnits required" };
return;
}
// Verify allocation exists and user has access
let query = "SELECT * FROM work_allocations WHERE id = ?";
const params: unknown[] = [allocationId];
if (currentUser.role === "Supervisor") {
query += " AND supervisor_id = ?";
params.push(currentUser.id);
} else if (currentUser.role === "Contractor") {
query += " AND contractor_id = ?";
params.push(currentUser.id);
}
const allocations = await db.query<WorkAllocation[]>(query, params);
if (allocations.length === 0) {
ctx.response.status = 403;
ctx.response.body = {
error: "Work allocation not found or access denied",
};
return;
}
const allocation = allocations[0];
const newUnits = units !== undefined ? units : allocation.units;
const newCompletedUnits = completedUnits !== undefined ? completedUnits : (allocation as any).completed_units || 0;
const rate = allocation.rate || 0;
const newTotalAmount = newCompletedUnits * rate;
// Determine status: mark as Completed if markComplete is true
const newStatus = markComplete ? "Completed" : allocation.status;
const completionDate = markComplete ? new Date().toISOString().split("T")[0] : allocation.completion_date;
await db.execute(
"UPDATE work_allocations SET units = ?, completed_units = ?, total_amount = ?, status = ?, completion_date = ? WHERE id = ?",
[newUnits, newCompletedUnits, newTotalAmount, newStatus, completionDate, allocationId],
);
const updatedAllocation = await db.query<WorkAllocation[]>(
`SELECT wa.*,
e.name as employee_name, e.username as employee_username,
s.name as supervisor_name,
c.name as contractor_name,
sd.name as sub_department_name,
d.name as department_name
FROM work_allocations wa
JOIN users e ON wa.employee_id = e.id
JOIN users s ON wa.supervisor_id = s.id
JOIN users c ON wa.contractor_id = c.id
LEFT JOIN sub_departments sd ON wa.sub_department_id = sd.id
LEFT JOIN departments d ON e.department_id = d.id
WHERE wa.id = ?`,
[allocationId],
);
ctx.response.body = updatedAllocation[0];
} catch (error) {
console.error("Update work allocation units error:", error);
ctx.response.status = 500;
ctx.response.body = { error: "Internal server error" };
}
},
);
// Delete work allocation (Supervisor or SuperAdmin)
router.delete(
"/:id",