Create new inactivity endpoint
This commit is contained in:
parent
2337c8c653
commit
a0f507b751
76
functions/api/inactivity/new.ts
Normal file
76
functions/api/inactivity/new.ts
Normal file
@ -0,0 +1,76 @@
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
if (!context.data.departments)
|
||||
return new Response('{"error":"Not in any departments"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
|
||||
const { departments, end, reason, start } = context.data.body;
|
||||
|
||||
if (
|
||||
!Array.isArray(departments) ||
|
||||
!departments.length ||
|
||||
typeof end !== "string" ||
|
||||
typeof reason !== "string" ||
|
||||
typeof start !== "string"
|
||||
)
|
||||
return new Response('{"error":"Invalid notice"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
|
||||
const endDate = new Date(end);
|
||||
const startDate = new Date(start);
|
||||
const now = new Date();
|
||||
|
||||
if (
|
||||
isNaN(endDate.getFullYear()) ||
|
||||
isNaN(startDate.getFullYear()) ||
|
||||
endDate.getFullYear() < now.getFullYear() ||
|
||||
endDate.getFullYear() > now.getFullYear() + 1 ||
|
||||
startDate.getFullYear() < now.getFullYear() ||
|
||||
startDate.getFullYear() > now.getFullYear() + 1 ||
|
||||
endDate.valueOf() < startDate.valueOf()
|
||||
)
|
||||
return new Response('{"error":"Dates are invalid"}', {
|
||||
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({
|
||||
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
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user