From 42cad11bf6f8b5b6e1cef50dc9c3da97876d764e Mon Sep 17 00:00:00 2001 From: Regalijan Date: Thu, 23 Jan 2025 15:05:39 -0500 Subject: [PATCH] Inactivities page --- app/routes/inactivities.tsx | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 app/routes/inactivities.tsx diff --git a/app/routes/inactivities.tsx b/app/routes/inactivities.tsx new file mode 100644 index 0000000..b77ce0f --- /dev/null +++ b/app/routes/inactivities.tsx @@ -0,0 +1,104 @@ +import { + Button, + Container, + Heading, + Table, + TableCaption, + TableContainer, + Tbody, + Td, + Thead, + Tr, +} from "@chakra-ui/react"; +import { useLoaderData } from "@remix-run/react"; + +export async function loader({ context }: { context: RequestContext }) { + const { current_user: currentUser } = context.data; + + if (!currentUser) + throw new Response(null, { + status: 401, + }); + + const permissionGroups = { + DM: [1 << 11], + ET: [1 << 4, 1 << 12], + FM: [1 << 7], + WM: [1 << 11], + }; + + const searchDepartments = []; + + for (const department of Object.keys(permissionGroups)) { + if (currentUser.permissions & (1 << 0)) searchDepartments.push(department); + else if ( + permissionGroups[department as "DM" | "ET" | "FM" | "WM"].find( + (p) => currentUser.permissions & p, + ) + ) + searchDepartments.push(department); + } + + if (!searchDepartments.length) + throw new Response(null, { + status: 403, + }); + + const today = new Date().toISOString().split("T").at(0); + const { results } = await context.env.D1.prepare( + "SELECT decisions, end, start, user FROM inactivity_notices WHERE start <= ?1 AND date(end, '+30 days') <= ?1; ", + ) + .bind(today) + .all(); + + for (let i = 0; i < results.length; i++) + results[i].user = JSON.parse(results[i].user as string); + + return results.filter((row) => { + const decisionValues = Object.values( + row.decisions as { [k: string]: boolean }, + ); + + return decisionValues.find((d) => d); + }) as unknown as { + decisions: { [k: string]: boolean }; + end: string; + start: string; + user: { email: string; id: string; username: string }; + }[]; +} + +export default function () { + const data = useLoaderData(); + + return ( + + Current Inactivity Notices + + + + All recent inactivity notices (current and last 30 days) + + + Name + User ID + Start Date + End Date + View More + + + {data.map((row) => ( + + + + + + + + ))} + +
{row.user.username}{row.user.id}{row.start}{row.end}
+
+
+ ); +}