Move inactivity notice validation to separate exported function
This commit is contained in:
parent
c295f3e4df
commit
1ce0476c49
@ -1,59 +1,18 @@
|
|||||||
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);
|
|
||||||
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 =
|
const inactivityId =
|
||||||
context.data.current_user.id +
|
context.data.current_user.id +
|
||||||
(context.request.headers.get("cf-ray") as string).split("-")[0] +
|
(context.request.headers.get("cf-ray") as string).split("-")[0] +
|
||||||
@ -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();
|
||||||
|
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