Insert inactivity notices into D1

This commit is contained in:
2023-10-19 16:50:07 -04:00
parent 4d6cde7a0b
commit fdc3afdc69

View File

@ -43,33 +43,46 @@ export async function onRequestPost(context: RequestContext) {
status: 400, status: 400,
}); });
if (!departments.every(d => context.data.departments.includes(d))) if (!departments.every((d) => context.data.departments.includes(d)))
return new Response('{"error":"Cannot file a notice in a department you are not part of"}', { return new Response(
headers: { '{"error":"Cannot file a notice in a department you are not part of"}',
"content-type": "application/json", {
}, headers: {
status: 400, "content-type": "application/json",
}); },
status: 400,
}
);
const inactivityId = const inactivityId =
context.data.current_user.id + context.data.current_user.id +
(context.request.headers.get("cf-ray") as string).split("-")[0] + (context.request.headers.get("cf-ray") as string).split("-")[0] +
Date.now().toString(); Date.now().toString();
await context.env.DATA.put(`inactivity_${inactivityId}`, JSON.stringify({ await context.env.DATA.put(
created_at: Date.now(), `inactivity_${inactivityId}`,
departments, JSON.stringify({
end, created_at: Date.now(),
reason, departments,
start, end,
user: { reason,
discriminator: context.data.current_user.discriminator, start,
id: context.data.current_user.id, user: {
username: context.data.current_user.username, discriminator: context.data.current_user.discriminator,
}, id: context.data.current_user.id,
}), { username: context.data.current_user.username,
expirationTtl: 63072000 },
}); }),
{
expirationTtl: 63072000,
}
);
await context.env.D1.prepare(
"INSERT INTO inactivity_notices (created_at, id, user) VALUES (?, ?, ?);"
)
.bind(Date.now(), inactivityId, context.data.current_user.id)
.run();
return new Response(null, { return new Response(null, {
status: 204, status: 204,