Maybe fix inactivity notices?

This commit is contained in:
regalijan 2023-10-19 16:50:52 -04:00
parent d6a99b56cf
commit 2fc73a62cc
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -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);
@ -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}`);
@ -23,7 +23,7 @@ export async function onRequestDelete(context: RequestContext) {
.run();
return new Response(null, {
status: 204,
status: 204
});
}
@ -37,11 +37,11 @@ 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)
@ -49,7 +49,7 @@ export async function onRequestPost(context: RequestContext) {
const requestedNotice: { [k: string]: any } | null =
await context.env.DATA.get(`inactivity_${context.params.id as string}`, {
type: "json",
type: "json"
});
if (!requestedNotice)
@ -57,31 +57,26 @@ export async function onRequestPost(context: RequestContext) {
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);
@ -89,11 +84,11 @@ export async function onRequestPut(context: RequestContext) {
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();
@ -108,7 +103,7 @@ export async function onRequestPut(context: RequestContext) {
end,
reason,
start,
context.data.departments,
context.data.departments
);
if (validationFailureResponse) return validationFailureResponse;
@ -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
});
}