Greatly reduce repeated code
This commit is contained in:
@ -1,11 +1,4 @@
|
||||
function errorResponse(error: string, status = 400): Response {
|
||||
return new Response(JSON.stringify({ error }), {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status,
|
||||
});
|
||||
}
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export default function (
|
||||
selectedDepartments: string[],
|
||||
@ -14,8 +7,7 @@ export default function (
|
||||
start: any,
|
||||
userDepartments?: string[],
|
||||
): void | Response {
|
||||
if (!userDepartments)
|
||||
return errorResponse("Not part of any departments", 403);
|
||||
if (!userDepartments) return jsonError("Not part of any departments", 403);
|
||||
|
||||
if (
|
||||
!Array.isArray(selectedDepartments) ||
|
||||
@ -24,10 +16,10 @@ export default function (
|
||||
typeof reason !== "string" ||
|
||||
typeof start !== "string"
|
||||
)
|
||||
return errorResponse("Invalid notice");
|
||||
return jsonError("Invalid notice", 400);
|
||||
|
||||
if (!selectedDepartments.every((dept) => userDepartments.includes(dept)))
|
||||
return errorResponse(
|
||||
return jsonError(
|
||||
"Cannot file an inactivity notice in a department you are not part of",
|
||||
403,
|
||||
);
|
||||
@ -45,5 +37,5 @@ export default function (
|
||||
startDate.getFullYear() > now.getFullYear() + 1 ||
|
||||
endDate.valueOf() < startDate.valueOf()
|
||||
)
|
||||
return errorResponse("Dates are invalid");
|
||||
return jsonError("Dates are invalid", 400);
|
||||
}
|
||||
|
Reference in New Issue
Block a user