Implemented self extension
This commit is contained in:
@@ -189,7 +189,50 @@ router.post('/adminCancel/:id', [requireRole(['17th Administrator', '17th HQ', '
|
||||
})
|
||||
|
||||
// extend LOA
|
||||
router.post('/extend/:id', [requireRole(['17th Administrator', '17th HQ', '17th Command'])], async (req: Request, res: Response) => {
|
||||
router.post('/extend/:id', async (req: Request, res: Response) => {
|
||||
const to: Date = req.body.to;
|
||||
|
||||
const member = req.user.id;
|
||||
|
||||
let LOA = await getLOAbyID(Number(req.params.id));
|
||||
if (!LOA) {
|
||||
return res.status(404).send("LOA not found");
|
||||
}
|
||||
|
||||
if (LOA.member_id !== member) {
|
||||
return res.status(403).send("You do not have permission to extend this LOA");
|
||||
}
|
||||
|
||||
if (LOA.extended_till !== null) {
|
||||
return res.status(409).send("You must contact the administration team to extend your LOA again");
|
||||
}
|
||||
|
||||
if (!to) {
|
||||
return res.status(400).send("Extension length is required");
|
||||
}
|
||||
|
||||
try {
|
||||
await setLOAExtension(Number(req.params.id), to);
|
||||
|
||||
audit.leaveOfAbsence('extended', { actorId: req.user.id, targetId: Number(req.params.id) });
|
||||
logger.info('app', 'LOA Extended', { extended_by: req.user.id, LOA: req.params.id })
|
||||
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
'app',
|
||||
'Failed to extend LOA',
|
||||
{
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
}
|
||||
);
|
||||
res.status(500).json(error);
|
||||
}
|
||||
})
|
||||
|
||||
// admin extend LOA
|
||||
router.post('/extendAdmin/:id', [requireRole(['17th Administrator', '17th HQ', '17th Command'])], async (req: Request, res: Response) => {
|
||||
const to: Date = req.body.to;
|
||||
|
||||
if (!to) {
|
||||
|
||||
@@ -175,3 +175,20 @@ export async function extendLOA(id: number, to: Date) {
|
||||
throw new Error("Could not extend LOA");
|
||||
}
|
||||
}
|
||||
|
||||
export async function adminExtendLOA(id: number, to: Date) {
|
||||
const res = await fetch(`${addr}/loa/extendAdmin/${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");
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import { ChevronDown, ChevronUp, Ellipsis, X } from "lucide-vue-next";
|
||||
import { cancelLOA, extendLOA, getAllLOAs, getMyLOAs } from "@/api/loa";
|
||||
import { adminExtendLOA, cancelLOA, extendLOA, getAllLOAs, getMyLOAs } from "@/api/loa";
|
||||
import { onMounted, ref, computed } from "vue";
|
||||
import { LOARequest } from "@shared/types/loa";
|
||||
import Dialog from "../ui/dialog/Dialog.vue";
|
||||
@@ -101,7 +101,6 @@ 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);
|
||||
@@ -109,7 +108,11 @@ function toCalendarDate(date: Date): CalendarDate {
|
||||
}
|
||||
|
||||
async function commitExtend() {
|
||||
if (props.adminMode) {
|
||||
await adminExtendLOA(targetLOA.value.id, extendTo.value.toDate(getLocalTimeZone()));
|
||||
} else {
|
||||
await extendLOA(targetLOA.value.id, extendTo.value.toDate(getLocalTimeZone()));
|
||||
}
|
||||
isExtending.value = false;
|
||||
await loadLOAs();
|
||||
}
|
||||
@@ -145,7 +148,7 @@ function setPage(pagenum: number) {
|
||||
<div class="flex gap-5">
|
||||
<Calendar v-model="extendTo" class="rounded-md border shadow-sm w-min" layout="month-and-year"
|
||||
:min-value="toCalendarDate(targetEnd)"
|
||||
:max-value="toCalendarDate(targetEnd).add({ years: 1 })" />
|
||||
:max-value="props.adminMode ? toCalendarDate(targetEnd).add({ years: 1 }) : toCalendarDate(targetEnd).add({ months: 1 })" />
|
||||
<div class="flex flex-col w-full gap-3 px-2">
|
||||
<p>Quick Options</p>
|
||||
<Button variant="outline" @click="extendTo = toCalendarDate(targetEnd).add({ days: 7 })">1
|
||||
@@ -205,7 +208,8 @@ function setPage(pagenum: number) {
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuItem v-if="!post.closed && props.adminMode"
|
||||
<DropdownMenuItem v-if="!post.closed"
|
||||
:disabled="post.extended_till !== null && !props.adminMode"
|
||||
@click="isExtending = true; targetLOA = post">
|
||||
Extend
|
||||
</DropdownMenuItem>
|
||||
|
||||
Reference in New Issue
Block a user