Permalink
Newer
100644
26 lines (19 sloc)
1.01 KB
1
import { jsonError, jsonResponse } from "../../../common.js";
2
3
export async function onRequestGet(context: RequestContext) {
4
const url = new URL(context.request.url);
5
const month = parseInt(url.searchParams.get("month") ?? "");
6
const year = parseInt(url.searchParams.get("year") ?? "");
7
8
if (isNaN(month) || isNaN(year))
9
return jsonError("Invalid month or year", 400);
10
11
const currentYear = new Date().getUTCFullYear();
12
const currentMonth = new Date().getUTCMonth() + 1;
13
14
if (currentYear < year || (currentYear === year && currentMonth < month))
15
return jsonError("Cannot get events for a time in the future", 400);
16
17
const eventRecords = await context.env.D1.prepare(
18
"SELECT answer, approved, created_by, day, details, month, pending, performed_at, type, year FROM events WHERE month = ? AND year = ? ORDER BY day ASC;",
19
)
20
.bind(month, year)
21
.all();
22
23
if (!eventRecords.success) return jsonError("Failed to retrieve events", 400);
24
25
return jsonResponse(JSON.stringify(eventRecords.results));
26
}