52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import validateInactivity from "./validate.js";
|
|
|
|
export async function onRequestPost(context: RequestContext) {
|
|
const { departments, end, reason, senderTokenId, start } = context.data.body;
|
|
|
|
const validationFailureResponse = validateInactivity(
|
|
departments,
|
|
end,
|
|
reason,
|
|
start,
|
|
context.data.departments,
|
|
);
|
|
|
|
if (validationFailureResponse) return validationFailureResponse;
|
|
|
|
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,
|
|
fcm_token: typeof senderTokenId === "string" ? senderTokenId : undefined,
|
|
open: true,
|
|
reason,
|
|
start,
|
|
user: {
|
|
id: context.data.current_user.id,
|
|
email: context.data.current_user.email,
|
|
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,
|
|
});
|
|
}
|