Move inactivity notice validation to separate exported function

This commit is contained in:
regalijan 2023-10-19 16:50:16 -04:00
parent c295f3e4df
commit 1ce0476c49
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520
2 changed files with 61 additions and 53 deletions

View File

@ -1,58 +1,17 @@
export async function onRequestPost(context: RequestContext) { import validateInactivity from "./validate.js";
if (!context.data.departments)
return new Response('{"error":"Not in any departments"}', {
headers: {
"content-type": "application/json",
},
status: 403,
});
export async function onRequestPost(context: RequestContext) {
const { departments, end, reason, start } = context.data.body; const { departments, end, reason, start } = context.data.body;
if ( const validationFailureResponse = validateInactivity(
!Array.isArray(departments) || departments,
!departments.length || end,
typeof end !== "string" || reason,
typeof reason !== "string" || start,
typeof start !== "string" context.data.departments,
) );
return new Response('{"error":"Invalid notice"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
const endDate = new Date(end); if (validationFailureResponse) return validationFailureResponse;
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 = const inactivityId =
context.data.current_user.id + context.data.current_user.id +
@ -74,11 +33,11 @@ export async function onRequestPost(context: RequestContext) {
}), }),
{ {
expirationTtl: 63072000, expirationTtl: 63072000,
} },
); );
await context.env.D1.prepare( await context.env.D1.prepare(
"INSERT INTO inactivity_notices (created_at, id, user) VALUES (?, ?, ?);" "INSERT INTO inactivity_notices (created_at, id, user) VALUES (?, ?, ?);",
) )
.bind(Date.now(), inactivityId, context.data.current_user.id) .bind(Date.now(), inactivityId, context.data.current_user.id)
.run(); .run();

View File

@ -0,0 +1,49 @@
function errorResponse(error: string, status = 400): Response {
return new Response(JSON.stringify({ error }), {
headers: {
"content-type": "application/json",
},
status,
});
}
export default function (
selectedDepartments: string[],
end: any,
reason: any,
start: any,
userDepartments?: string[],
): void | Response {
if (!userDepartments)
return errorResponse("Not part of any departments", 403);
if (
!Array.isArray(selectedDepartments) ||
!selectedDepartments.length ||
typeof end !== "string" ||
typeof reason !== "string" ||
typeof start !== "string"
)
return errorResponse("Invalid notice");
if (!selectedDepartments.every((dept) => userDepartments.includes(dept)))
return errorResponse(
"Cannot file an inactivity notice in a department you are not part of",
403,
);
const endDate = new Date(end);
const now = new Date();
const startDate = new Date(start);
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 errorResponse("Dates are invalid");
}