Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Insert inactivity notices into D1
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent 4d6cde7 commit fdc3afd
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions functions/api/inactivity/new.ts
Expand Up @@ -43,33 +43,46 @@ export async function onRequestPost(context: RequestContext) {
status: 400,
});

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"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
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"}',
{
headers: {
"content-type": "application/json",
},
status: 400,
}
);

const inactivityId =
context.data.current_user.id +
(context.request.headers.get("cf-ray") as string).split("-")[0] +
Date.now().toString();

await context.env.DATA.put(`inactivity_${inactivityId}`, JSON.stringify({
created_at: Date.now(),
departments,
end,
reason,
start,
user: {
discriminator: context.data.current_user.discriminator,
id: context.data.current_user.id,
username: context.data.current_user.username,
},
}), {
expirationTtl: 63072000
});
await context.env.DATA.put(
`inactivity_${inactivityId}`,
JSON.stringify({
created_at: Date.now(),
departments,
end,
reason,
start,
user: {
discriminator: context.data.current_user.discriminator,
id: context.data.current_user.id,
username: context.data.current_user.username,
},
}),
{
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, {
status: 204,
Expand Down

0 comments on commit fdc3afd

Please sign in to comment.