Check weekly ranges for rotws

This commit is contained in:
Regalijan 2024-02-06 13:15:16 -05:00
parent 4f4e51691c
commit 6aaa96ba57
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -5,12 +5,13 @@ export async function onRequestPost(context: RequestContext) {
const now = new Date(); const now = new Date();
const currentMonth = now.getUTCMonth(); const currentMonth = now.getUTCMonth();
const currentYear = now.getUTCFullYear(); const currentYear = now.getUTCFullYear();
const lastDayOfMonth = new Date(currentYear, currentMonth, 0).getUTCDate();
if ( if (
typeof day !== "number" || typeof day !== "number" ||
day < 1 || day < 1 ||
// Last day of that month // Last day of that month
day > new Date(currentYear, currentMonth, 0).getUTCDate() || day > lastDayOfMonth ||
// Stop people sending weird decimal days // Stop people sending weird decimal days
parseInt(day.toString()) !== day || parseInt(day.toString()) !== day ||
typeof details !== "string" || typeof details !== "string" ||
@ -32,6 +33,32 @@ export async function onRequestPost(context: RequestContext) {
400, 400,
); );
if (type === "rotw") {
const weekRanges: { [k: number]: number } = {
0: 7,
1: 14,
2: 21,
3: 28,
4: 35,
};
const weekRange = Math.floor(day / 7);
const existingEventInRange = await context.env.D1.prepare(
"SELECT id FROM events WHERE day > ? AND day <= ? AND month = ? AND type = 'rotw' AND year = ?;",
)
.bind(
weekRanges[weekRange] - 7,
weekRanges[weekRange],
currentMonth,
currentYear,
)
.first();
if (existingEventInRange)
return jsonError("There is already an rotw for that week", 400);
}
const id = `${now.getTime()}${crypto.randomUUID().replaceAll("-", "")}`; const id = `${now.getTime()}${crypto.randomUUID().replaceAll("-", "")}`;
await context.env.D1.prepare( await context.env.D1.prepare(