From c6c129715dc9a8d6bf6c82e751cc7547455f3840 Mon Sep 17 00:00:00 2001
From: regalijan <r@regalijan.com>
Date: Thu, 19 Oct 2023 16:49:38 -0400
Subject: [PATCH] Create inactivity api middleware

---
 functions/api/inactivity/_middleware.ts | 29 +++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 functions/api/inactivity/_middleware.ts

diff --git a/functions/api/inactivity/_middleware.ts b/functions/api/inactivity/_middleware.ts
new file mode 100644
index 0000000..3a9e704
--- /dev/null
+++ b/functions/api/inactivity/_middleware.ts
@@ -0,0 +1,29 @@
+function makeResponse(body: string, status: number): Response {
+  return new Response(body, {
+    headers: {
+      "content-type": "application/json",
+    },
+    status,
+  });
+}
+
+export async function onRequest(context: RequestContext) {
+  if (!context.data.current_user)
+    return makeResponse('{"error":"You are not logged in"}', 401);
+
+  const { permissions } = context.data.current_user;
+  const departments = {
+    DM: 1 << 2,
+    ET: 1 << 3,
+    FM: 1 << 10,
+    WM: 1 << 9,
+  };
+  const userDepartments = [];
+
+  for (const [dept, permission] of Object.entries(departments))
+    if (permissions & permission) userDepartments.push(dept);
+
+  context.data.departments = userDepartments;
+
+  return await context.next();
+}