Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Maybe fix inactivity notices?
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent d6a99b5 commit 2fc73a6
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions functions/api/inactivity/[id].ts
Expand Up @@ -3,7 +3,7 @@ import validateInactivityNotice from "./validate.js";

export async function onRequestDelete(context: RequestContext) {
const kvResult = await context.env.DATA.get(
`inactivity_${context.params.id}`,
`inactivity_${context.params.id}`
);

if (!kvResult) return jsonError("No inactivity notice with that ID", 404);
Expand All @@ -14,7 +14,7 @@ export async function onRequestDelete(context: RequestContext) {
)
return jsonError(
"You do not have permission to delete this inactivity notice",
403,
403
);

await context.env.DATA.delete(`inactivity_${context.params.id}`);
Expand All @@ -23,7 +23,7 @@ export async function onRequestDelete(context: RequestContext) {
.run();

return new Response(null, {
status: 204,
status: 204
});
}

Expand All @@ -37,63 +37,58 @@ export async function onRequestPost(context: RequestContext) {
DM: 1 << 11,
ET: 1 << 4,
FM: 1 << 7,
WM: 1 << 6,
WM: 1 << 6
};

const userAdminDepartments = Object.keys(adminDepartments).filter(
(dept) => context.data.current_user.permissions & adminDepartments[dept],
(dept) => context.data.current_user.permissions & adminDepartments[dept]
);

if (!userAdminDepartments.length)
return jsonError("You are not a manager of any departments", 403);

const requestedNotice: { [k: string]: any } | null =
await context.env.DATA.get(`inactivity_${context.params.id as string}`, {
type: "json",
type: "json"
});

if (!requestedNotice)
return jsonError("Inactivity notices does not exist", 404);

const decisions: { [dept: string]: boolean } = {};

userAdminDepartments.forEach((dept) =>
Object.defineProperty(decisions, dept, {
value: accepted,
}),
);
for (const department of userAdminDepartments)
decisions[department] = accepted;

Object.defineProperty(requestedNotice, "decisions", {
value: decisions,
});
requestedNotice.decisions = decisions;

await context.env.DATA.put(
`inactivity_${context.params.id as string}`,
JSON.stringify(requestedNotice),
{ expirationTtl: 63072000 },
{ expirationTtl: 63072000 }
);

return new Response(null, {
status: 204,
status: 204
});
}

export async function onRequestPut(context: RequestContext) {
const kvResult: InactivityNoticeProps | null = await context.env.DATA.get(
`inactivity_${context.params.id}`,
{ type: "json" },
{ type: "json" }
);

if (!kvResult) return jsonError("No inactivity notice with that ID", 404);

if (kvResult.user.id !== context.data.current_user.id)
return jsonError(
"You do not have permission to modify this inactivity notice",
403,
403
);

const d1entry = await context.env.D1.prepare(
"SELECT open FROM inactivity_notices WHERE id = ?;",
"SELECT open FROM inactivity_notices WHERE id = ?;"
)
.bind(context.params.id)
.run();
Expand All @@ -108,7 +103,7 @@ export async function onRequestPut(context: RequestContext) {
end,
reason,
start,
context.data.departments,
context.data.departments
);

if (validationFailureResponse) return validationFailureResponse;
Expand All @@ -122,11 +117,11 @@ export async function onRequestPut(context: RequestContext) {
`inactivity_${context.params.id}`,
JSON.stringify(kvResult),
{
expirationTtl: 63072000,
},
expirationTtl: 63072000
}
);

return new Response(null, {
status: 204,
status: 204
});
}

0 comments on commit 2fc73a6

Please sign in to comment.