added policy system and self LOA management

This commit is contained in:
2025-12-11 20:28:49 -05:00
parent bcde81093d
commit 710b24e5a7
8 changed files with 170 additions and 23 deletions

View File

@@ -53,6 +53,18 @@ router.get("/me", async (req: Request, res: Response) => {
}
})
//get my LOA history
router.get("/history", async (req: Request, res: Response) => {
const user = req.user.id;
try {
const result = await getUserLOA(user);
res.status(200).json(result)
} catch (error) {
console.error(error);
res.status(500).send(error);
}
})
router.get('/all', async (req, res) => {
try {
const result = await getAllLOA();
@@ -128,7 +140,7 @@ router.get('/policy', async (req: Request, res: Response) => {
if (output.ok) {
const out = await output.json();
res.status(200).json(out.markdown);
res.status(200).json(out.html);
} else {
console.log("BAD");
res.sendStatus(500);

View File

@@ -32,7 +32,23 @@ export async function getAllLOA(page = 1, pageSize = 20): Promise<LOARequest[]>
}
export async function getUserLOA(userId: number): Promise<LOARequest[]> {
const result: LOARequest[] = await pool.query("SELECT * FROM leave_of_absences WHERE member_id = ?", [userId])
const result: LOARequest[] = await pool.query(`
SELECT loa.*, members.name, t.name AS type_name
FROM leave_of_absences AS loa
LEFT JOIN members ON loa.member_id = members.id
LEFT JOIN leave_of_absences_types AS t ON loa.type_id = t.id
WHERE member_id = ?
ORDER BY
CASE
WHEN loa.closed IS NULL
AND NOW() > COALESCE(loa.extended_till, loa.end_date) THEN 1
WHEN loa.closed IS NULL
AND NOW() BETWEEN loa.start_date AND COALESCE(loa.extended_till, loa.end_date) THEN 2
WHEN loa.closed IS NULL AND NOW() < loa.start_date THEN 3
WHEN loa.closed IS NOT NULL THEN 4
END,
loa.start_date DESC
`, [userId])
return result;
}
@@ -67,7 +83,7 @@ export async function closeLOA(id: number, closer: number) {
export async function getLOAbyID(id: number): Promise<LOARequest> {
let res = await pool.query(`SELECT * FROM leave_of_absences WHERE id = ?`, [id]);
console.log(res);
if (res.length == 1)
if (res.length != 1)
throw new Error(`LOA with id ${id} not found`);
return res[0];
}