Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move inactivity notice validation to separate exported function
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent c295f3e commit 1ce0476
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 deletions.
65 changes: 12 additions & 53 deletions functions/api/inactivity/new.ts
@@ -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 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,
});
const validationFailureResponse = validateInactivity(
departments,
end,
reason,
start,
context.data.departments,
);

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 +
Expand All @@ -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();
Expand Down
49 changes: 49 additions & 0 deletions functions/api/inactivity/validate.ts
@@ -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");
}

0 comments on commit 1ce0476

Please sign in to comment.