implemented LOA cancelling and extensioning

This commit is contained in:
2025-12-11 19:08:24 -05:00
parent a3216ba5ab
commit bcde81093d
7 changed files with 219 additions and 82 deletions

View File

@@ -3,7 +3,7 @@ const router = express.Router();
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import pool from '../db'; import pool from '../db';
import { closeLOA, createNewLOA, getAllLOA, getLOAbyID, getLoaTypes, getUserLOA } from '../services/loaService'; import { closeLOA, createNewLOA, getAllLOA, getLOAbyID, getLoaTypes, getUserLOA, setLOAExtension } from '../services/loaService';
import { LOARequest } from '@app/shared/types/loa'; import { LOARequest } from '@app/shared/types/loa';
//member posts LOA //member posts LOA
@@ -104,10 +104,15 @@ router.post('/adminCancel/:id', async (req: Request, res: Response) => {
// TODO: Enforce admin only // TODO: Enforce admin only
router.post('/extend/:id', async (req: Request, res: Response) => { router.post('/extend/:id', async (req: Request, res: Response) => {
const extendTo = req.body; const to: Date = req.body.to;
console.log(extendTo);
try {
if (!to) {
res.status(400).send("Extension length is required");
}
try {
await setLOAExtension(Number(req.params.id), to);
res.sendStatus(200);
} catch (error) { } catch (error) {
console.error(error) console.error(error)
res.status(500).json(error); res.status(500).json(error);
@@ -123,8 +128,7 @@ router.get('/policy', async (req: Request, res: Response) => {
if (output.ok) { if (output.ok) {
const out = await output.json(); const out = await output.json();
console.log(out); res.status(200).json(out.markdown);
res.status(200).json(out);
} else { } else {
console.log("BAD"); console.log("BAD");
res.sendStatus(500); res.sendStatus(500);

View File

@@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
import pool from '../db'; import pool from '../db';
import { getUserActiveLOA } from '../services/loaService';
import { getUserData } from '../services/memberService'; import { getUserData } from '../services/memberService';
import { getUserRoles } from '../services/rolesService'; import { getUserRoles } from '../services/rolesService';
@@ -40,12 +41,13 @@ router.get('/me', async (req, res) => {
try { try {
const { id, name, state } = await getUserData(req.user.id); const { id, name, state } = await getUserData(req.user.id);
const LOAData = await pool.query( // const LOAData = await pool.query(
`SELECT * // `SELECT *
FROM leave_of_absences // FROM leave_of_absences
WHERE member_id = ? // WHERE member_id = ?
AND deleted = 0 // AND deleted = 0
AND UTC_TIMESTAMP() BETWEEN start_date AND end_date;`, req.user.id); // AND UTC_TIMESTAMP() BETWEEN start_date AND end_date;`, req.user.id);
const LOAData = await getUserActiveLOA(req.user.id);
const roleData = await getUserRoles(req.user.id); const roleData = await getUserRoles(req.user.id);

View File

@@ -6,13 +6,28 @@ export async function getLoaTypes(): Promise<LOAType[]> {
return await pool.query('SELECT * FROM leave_of_absences_types;'); return await pool.query('SELECT * FROM leave_of_absences_types;');
} }
export async function getAllLOA(): Promise<LOARequest[]> { export async function getAllLOA(page = 1, pageSize = 20): Promise<LOARequest[]> {
let res: LOARequest[] = await pool.query( const offset = (page - 1) * pageSize;
`SELECT loa.*, members.name, t.name AS type_name
FROM leave_of_absences AS loa const sql = `
LEFT JOIN members ON loa.member_id = members.id SELECT loa.*, members.name, t.name AS type_name
LEFT JOIN leave_of_absences_types AS t ON loa.type_id = t.id; FROM leave_of_absences AS loa
`) as LOARequest[]; LEFT JOIN members ON loa.member_id = members.id
LEFT JOIN leave_of_absences_types AS t ON loa.type_id = t.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
LIMIT ? OFFSET ?;
`;
let res: LOARequest[] = await pool.query(sql, [pageSize, offset]) as LOARequest[];
return res; return res;
} }
@@ -21,6 +36,16 @@ export async function getUserLOA(userId: number): Promise<LOARequest[]> {
return result; return result;
} }
export async function getUserActiveLOA(userId: number): Promise<LOARequest[]> {
const sql = `SELECT *
FROM leave_of_absences
WHERE member_id = ?
AND closed IS NULL
AND UTC_TIMESTAMP() BETWEEN start_date AND end_date;`
const LOAData = await pool.query(sql, [userId]);
return LOAData;
}
export async function createNewLOA(data: LOARequest) { export async function createNewLOA(data: LOARequest) {
const sql = `INSERT INTO leave_of_absences const sql = `INSERT INTO leave_of_absences
(member_id, filed_date, start_date, end_date, type_id, reason) (member_id, filed_date, start_date, end_date, type_id, reason)
@@ -41,12 +66,17 @@ export async function closeLOA(id: number, closer: number) {
export async function getLOAbyID(id: number): Promise<LOARequest> { export async function getLOAbyID(id: number): Promise<LOARequest> {
let res = await pool.query(`SELECT * FROM leave_of_absences WHERE id = ?`, [id]); let res = await pool.query(`SELECT * FROM leave_of_absences WHERE id = ?`, [id]);
if (res.length != 1) console.log(res);
if (res.length == 1)
throw new Error(`LOA with id ${id} not found`); throw new Error(`LOA with id ${id} not found`);
return res[0]; return res[0];
} }
export async function setLOAExtension(id: number, extendTo: Date) { export async function setLOAExtension(id: number, extendTo: Date) {
let res = await pool.query(`UPDATE leave_of_absences
SET extended_till = ?
WHERE leave_of_absences.id = ? `, [toDateTime(extendTo), id]);
if (res.affectedRows != 1)
throw new Error(`Could not extend LOA`);
return res[0];
} }

View File

@@ -5,6 +5,7 @@ import { useUserStore } from './stores/user';
import Alert from './components/ui/alert/Alert.vue'; import Alert from './components/ui/alert/Alert.vue';
import AlertDescription from './components/ui/alert/AlertDescription.vue'; import AlertDescription from './components/ui/alert/AlertDescription.vue';
import Navbar from './components/Navigation/Navbar.vue'; import Navbar from './components/Navigation/Navbar.vue';
import { cancelLOA } from './api/loa';
const userStore = useUserStore(); const userStore = useUserStore();
@@ -29,10 +30,11 @@ const environment = import.meta.env.VITE_ENVIRONMENT;
<p>This is a development build of the application. Some features will be unavailable or unstable.</p> <p>This is a development build of the application. Some features will be unavailable or unstable.</p>
</AlertDescription> </AlertDescription>
</Alert> </Alert>
<Alert v-if="userStore.user?.loa?.[0]" class="m-2 mx-auto w-5xl" variant="info"> <Alert v-if="userStore.user?.LOAData?.[0]" class="m-2 mx-auto w-5xl" variant="info">
<AlertDescription class="flex flex-row items-center text-nowrap gap-5 mx-auto"> <AlertDescription class="flex flex-row items-center text-nowrap gap-5 mx-auto">
<p>You are on LOA until <strong>{{ formatDate(userStore.user?.loa?.[0].end_date) }}</strong></p> <p>You are on LOA until <strong>{{ formatDate(userStore.user?.LOAData?.[0].end_date) }}</strong></p>
<Button variant="secondary">End LOA</Button> <Button variant="secondary" @click="async () => { await cancelLOA(userStore.user?.LOAData?.[0].id); userStore.loadUser(); }">End
LOA</Button>
</AlertDescription> </AlertDescription>
</Alert> </Alert>
</div> </div>

View File

@@ -90,7 +90,6 @@ export async function getLoaTypes(): Promise<LOAType[]> {
}; };
export async function getLoaPolicy(): Promise<string> { export async function getLoaPolicy(): Promise<string> {
//@ts-ignore
const res = await fetch(`${addr}/loa/policy`, { const res = await fetch(`${addr}/loa/policy`, {
method: "GET", method: "GET",
credentials: 'include', credentials: 'include',
@@ -106,3 +105,34 @@ export async function getLoaPolicy(): Promise<string> {
return null; return null;
} }
} }
export async function cancelLOA(id: number, admin: boolean = false) {
let route = admin ? 'adminCancel' : 'cancel';
const res = await fetch(`${addr}/loa/${route}/${id}`, {
method: "POST",
credentials: 'include',
});
if (res.ok) {
return
} else {
throw new Error("Could not cancel LOA");
}
}
export async function extendLOA(id: number, to: Date) {
const res = await fetch(`${addr}/loa/extend/${id}`, {
method: "POST",
credentials: 'include',
body: JSON.stringify({ to }),
headers: {
"Content-Type": "application/json",
}
});
if (res.ok) {
return
} else {
throw new Error("Could not extend LOA");
}
}

View File

@@ -177,7 +177,7 @@ const maxEndDate = computed(() => {
<FieldLabel>Type</FieldLabel> <FieldLabel>Type</FieldLabel>
<Select :model-value="field.value" @update:model-value="field.onChange"> <Select :model-value="field.value" @update:model-value="field.onChange">
<SelectTrigger class="w-full"> <SelectTrigger class="w-full">
<SelectValue></SelectValue> <SelectValue placeholder="Select type"></SelectValue>
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem v-for="type in loaTypes" :value="type"> <SelectItem v-for="type in loaTypes" :value="type">

View File

@@ -16,30 +16,47 @@ import {
DropdownMenuTrigger, DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu" } from "@/components/ui/dropdown-menu"
import { Ellipsis } from "lucide-vue-next"; import { Ellipsis } from "lucide-vue-next";
import { getAllLOAs, LOARequest } from "@/api/loa"; import { cancelLOA, extendLOA, getAllLOAs } from "@/api/loa";
import { onMounted, ref, computed } from "vue"; import { onMounted, ref, computed } from "vue";
import { LOARequest } from "@shared/types/loa";
import Dialog from "../ui/dialog/Dialog.vue";
import DialogTrigger from "../ui/dialog/DialogTrigger.vue";
import DialogContent from "../ui/dialog/DialogContent.vue";
import DialogHeader from "../ui/dialog/DialogHeader.vue";
import DialogTitle from "../ui/dialog/DialogTitle.vue";
import DialogDescription from "../ui/dialog/DialogDescription.vue";
import Button from "../ui/button/Button.vue";
import Calendar from "../ui/calendar/Calendar.vue";
import {
CalendarDate,
getLocalTimeZone,
} from "@internationalized/date"
const LOAList = ref<LOARequest[]>([]); const LOAList = ref<LOARequest[]>([]);
onMounted(async () => { onMounted(async () => {
LOAList.value = await getAllLOAs(); await loadLOAs();
}); });
function formatDate(dateStr: string): string { async function loadLOAs() {
if (!dateStr) return ""; // const unsort = await getAllLOAs();
return new Date(dateStr).toLocaleDateString("en-US", { // LOAList.value = sortByStartDate(unsort);
LOAList.value = await getAllLOAs();
}
function formatDate(date: Date): string {
if (!date) return "";
date = typeof date === 'string' ? new Date(date) : date;
return date.toLocaleDateString("en-US", {
year: "numeric", year: "numeric",
month: "short", month: "short",
day: "numeric", day: "numeric",
}); });
} }
function loaStatus(loa: { function loaStatus(loa: LOARequest): "Upcoming" | "Active" | "Overdue" | "Closed" {
start_date: string; if (loa.closed) return "Closed";
end_date: string;
deleted?: number;
}): "Upcoming" | "Active" | "Expired" | "Cancelled" {
if (loa.deleted) return "Cancelled";
const now = new Date(); const now = new Date();
const start = new Date(loa.start_date); const start = new Date(loa.start_date);
@@ -47,9 +64,9 @@ function loaStatus(loa: {
if (now < start) return "Upcoming"; if (now < start) return "Upcoming";
if (now >= start && now <= end) return "Active"; if (now >= start && now <= end) return "Active";
if (now > end) return "Expired"; if (now > end) return "Overdue";
return "Expired"; // fallback return "Overdue"; // fallback
} }
function sortByStartDate(loas: LOARequest[]): LOARequest[] { function sortByStartDate(loas: LOARequest[]): LOARequest[] {
@@ -58,50 +75,102 @@ function sortByStartDate(loas: LOARequest[]): LOARequest[] {
); );
} }
const sortedLoas = computed(() => sortByStartDate(LOAList.value)); async function cancelAndReload(id: number) {
await cancelLOA(id, true);
await loadLOAs();
}
const isExtending = ref(false);
const targetLOA = ref<LOARequest | null>(null);
const extendTo = ref<CalendarDate | null>(null);
const targetEnd = computed(() => { return targetLOA.value.extended_till ? targetLOA.value.extended_till : targetLOA.value.end_date })
function toCalendarDate(date: Date): CalendarDate {
if (typeof date === 'string')
date = new Date(date);
return new CalendarDate(date.getFullYear(), date.getMonth() + 1, date.getDate())
}
async function commitExtend() {
await extendLOA(targetLOA.value.id, extendTo.value.toDate(getLocalTimeZone()));
isExtending.value = false;
await loadLOAs();
}
</script> </script>
<template> <template>
<div class="w-5xl mx-auto"> <div>
<Table> <Dialog :open="isExtending" @update:open="(val) => isExtending = val">
<TableHeader> <DialogContent>
<TableRow> <DialogHeader>
<TableHead class="w-[100px]">Member</TableHead> <DialogTitle>Extend {{ targetLOA.name }}'s Leave of Absence </DialogTitle>
<TableHead>Start</TableHead> </DialogHeader>
<TableHead>End</TableHead> <div class="flex gap-5">
<TableHead>Reason</TableHead> <Calendar v-model="extendTo" class="rounded-md border shadow-sm w-min" layout="month-and-year"
<TableHead>Posted on</TableHead> :min-value="toCalendarDate(targetEnd)"
<TableHead>Status</TableHead> :max-value="toCalendarDate(targetEnd).add({ years: 1 })" />
</TableRow> <div class="flex flex-col w-full gap-3 px-2">
</TableHeader> <p>Quick Options</p>
<TableBody> <Button variant="outline" @click="extendTo = toCalendarDate(targetEnd).add({ days: 7 })">1
<TableRow v-for="post in sortedLoas" :key="post.id" class="hover:bg-muted/50"> Week</Button>
<TableCell class="font-medium"> <Button variant="outline" @click="extendTo = toCalendarDate(targetEnd).add({ months: 1 })">1
{{ post.name }} Month</Button>
</TableCell> </div>
<TableCell>{{ formatDate(post.start_date) }}</TableCell> </div>
<TableCell>{{ formatDate(post.end_date) }}</TableCell> <div class="flex justify-end gap-4">
<TableCell>{{ post.reason }}</TableCell> <Button variant="outline" @click="isExtending = false">Cancel</Button>
<TableCell>{{ formatDate(post.filed_date) }}</TableCell> <Button @click="commitExtend">Extend</Button>
<TableCell> </div>
<Badge v-if="loaStatus(post) === 'Upcoming'" class="bg-blue-500">Upcoming</Badge> </DialogContent>
<Badge v-else-if="loaStatus(post) === 'Active'" class="bg-green-500">Active</Badge> </Dialog>
<Badge v-else-if="loaStatus(post) === 'Expired'" class="bg-gray-400">Expired</Badge> <div class="max-w-7xl w-full mx-auto">
<Badge v-else class="bg-red-500">Cancelled</Badge> <Table>
</TableCell> <TableHeader>
<TableCell @click.stop="console.log('hi')" class="text-right"> <TableRow>
<DropdownMenu> <TableHead>Member</TableHead>
<DropdownMenuTrigger class="cursor-pointer"> <TableHead>Type</TableHead>
<Ellipsis></Ellipsis> <TableHead>Start</TableHead>
</DropdownMenuTrigger> <TableHead>End</TableHead>
<DropdownMenuContent> <TableHead class="w-[500px]">Reason</TableHead>
<DropdownMenuItem :variant="'destructive'">Cancel</DropdownMenuItem> <TableHead>Posted on</TableHead>
</DropdownMenuContent> <TableHead>Status</TableHead>
</DropdownMenu> </TableRow>
</TableCell> </TableHeader>
<TableBody>
</TableRow> <TableRow v-for="post in LOAList" :key="post.id" class="hover:bg-muted/50">
</TableBody> <TableCell class="font-medium">
</Table> {{ post.name }}
</TableCell>
<TableCell>{{ post.type_name }}</TableCell>
<TableCell>{{ formatDate(post.start_date) }}</TableCell>
<TableCell>{{ post.extended_till ? formatDate(post.extended_till) : formatDate(post.end_date) }}
</TableCell>
<TableCell>{{ post.reason }}</TableCell>
<TableCell>{{ formatDate(post.filed_date) }}</TableCell>
<TableCell>
<Badge v-if="loaStatus(post) === 'Upcoming'" class="bg-blue-400">Upcoming</Badge>
<Badge v-else-if="loaStatus(post) === 'Active'" class="bg-green-500">Active</Badge>
<Badge v-else-if="loaStatus(post) === 'Overdue'" class="bg-yellow-400">Overdue</Badge>
<Badge v-else class="bg-gray-400">Ended</Badge>
</TableCell>
<TableCell @click.stop="" class="text-right">
<DropdownMenu>
<DropdownMenuTrigger class="cursor-pointer">
<Ellipsis></Ellipsis>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem v-if="!post.closed" @click="isExtending = true; targetLOA = post">
Extend
</DropdownMenuItem>
<DropdownMenuItem :variant="'destructive'" @click="cancelAndReload(post.id)">End
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</div> </div>
</template> </template>