Move inactivity notice validation to separate exported function
This commit is contained in:
parent
c295f3e4df
commit
1ce0476c49
@ -1,58 +1,17 @@
|
||||
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,
|
||||
});
|
||||
import validateInactivity from "./validate.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
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 validationFailureResponse = validateInactivity(
|
||||
departments,
|
||||
end,
|
||||
reason,
|
||||
start,
|
||||
context.data.departments,
|
||||
);
|
||||
|
||||
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,
|
||||
}
|
||||
);
|
||||
if (validationFailureResponse) return validationFailureResponse;
|
||||
|
||||
const inactivityId =
|
||||
context.data.current_user.id +
|
||||
@ -74,11 +33,11 @@ export async function onRequestPost(context: RequestContext) {
|
||||
}),
|
||||
{
|
||||
expirationTtl: 63072000,
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
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)
|
||||
.run();
|
||||
|
49
functions/api/inactivity/validate.ts
Normal file
49
functions/api/inactivity/validate.ts
Normal 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");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user