More events team nonsense
All checks were successful
Test, Build, Deploy / Test, Build, and Deploy (push) Successful in 55s
Test, Build, Deploy / Create Sentry Release (push) Successful in 6s

This commit is contained in:
2026-04-11 03:30:46 -04:00
parent 465bb30966
commit cfc57c838e
17 changed files with 355 additions and 259 deletions

View File

@@ -2,15 +2,18 @@ import { jsonError } from "../../../common.js";
export async function onRequestDelete(context: RequestContext) {
const eventId = context.params.id as string;
const eventData:
| ({
[k: string]: number;
} & { created_by: string })
| null = await context.env.D1.prepare(
"SELECT created_by, day, month, performed_at, year FROM events WHERE id = ?;",
)
.bind(eventId)
.first();
const eventData = await context.data.prisma.event.findUnique({
select: {
created_by: true,
day: true,
month: true,
performed_at: true,
year: true,
},
where: {
id: eventId,
},
});
if (!eventData) return jsonError("No event exists with that ID", 404);
@@ -41,9 +44,11 @@ export async function onRequestDelete(context: RequestContext) {
400,
);
await context.env.D1.prepare("DELETE FROM events WHERE id = ?;")
.bind(eventId)
.run();
await context.data.prisma.event.delete({
where: {
id: eventId,
},
});
return new Response(null, {
status: 204,
@@ -53,13 +58,18 @@ export async function onRequestDelete(context: RequestContext) {
export async function onRequestPatch(context: RequestContext) {
const eventId = context.params.id as string;
const { body } = context.data;
const eventData:
| ({ [k: string]: number } & { created_by: string; type: string })
| null = await context.env.D1.prepare(
"SELECT created_by, day, month, type, year FROM events WHERE id = ?;",
)
.bind(eventId)
.first();
const eventData = await context.data.prisma.event.findUnique({
select: {
created_by: true,
day: true,
month: true,
type: true,
year: true,
},
where: {
id: eventId,
},
});
if (!eventData) return jsonError("No event exists with that ID", 404);
@@ -83,7 +93,7 @@ export async function onRequestPatch(context: RequestContext) {
typeof body.day !== "number" ||
body.day > date.getUTCDate() ||
body.day < 1 ||
// Check for non-integers
// Check for nonintegers
Math.floor(body.day) !== body.day ||
currentDay >= body.day
)
@@ -107,26 +117,38 @@ export async function onRequestPatch(context: RequestContext) {
4: 35,
};
const weekRange = Math.floor(body.day / 7);
const matchingROTW = await context.env.D1.prepare(
"SELECT id FROM events WHERE (approved = 1 OR pending = 1) AND day BETWEEN ? AND ? AND month = ? AND type = 'rotw' AND year = ?;",
)
.bind(
weekRanges[weekRange] - 7,
weekRanges[weekRange],
eventData.month,
eventData.year,
)
.first();
const matchingROTW = await context.data.prisma.event.findMany({
select: {
id: true,
},
where: {
OR: [{ approved: true }, { pending: true }],
day: {
gte: weekRanges[weekRange] - 7,
lte: weekRanges[weekRange],
},
month: eventData.month,
year: eventData.year,
},
});
if (matchingROTW)
return jsonError("There is already an ROTW scheduled for that week", 400);
} else {
const matchingEvent = await context.env.D1.prepare(
"SELECT id FROM events WHERE (approved = 1 OR pending = 1) AND day = ? AND month = ? AND type = ? AND year = ?;",
)
.bind(body.day, eventData.month, eventData.type, eventData.year)
.first();
const matchingEvent = await context.data.prisma.event.findMany({
select: {
id: true,
},
where: {
OR: [{ approved: true }, { pending: true }],
AND: [
{ day: body.day },
{ month: eventData.month },
{ type: eventData.type },
{ year: eventData.year },
],
},
});
if (matchingEvent)
return jsonError(
@@ -135,9 +157,14 @@ export async function onRequestPatch(context: RequestContext) {
);
}
await context.env.D1.prepare("UPDATE events SET day = ? WHERE id = ?;")
.bind(body.day, eventId)
.run();
await context.data.prisma.event.update({
data: {
day: body.day,
},
where: {
id: eventId,
},
});
await fetch(context.env.EVENTS_WEBHOOK, {
body: JSON.stringify({
@@ -162,22 +189,29 @@ export async function onRequestPatch(context: RequestContext) {
export async function onRequestPost(context: RequestContext) {
const eventId = context.params.id as string;
const eventData = await context.env.D1.prepare(
"SELECT approved, performed_at FROM events WHERE id = ?;",
)
.bind(eventId)
.first();
const eventData = await context.data.prisma.event.findUnique({
select: {
approved: true,
performed_at: true,
},
where: {
id: eventId,
},
});
if (!eventData) return jsonError("No event exists with that ID", 404);
if (!eventData.approved)
return jsonError("Cannot perform unapproved event", 403);
await context.env.D1.prepare(
"UPDATE events SET performed_at = ? WHERE id = ?;",
)
.bind(Date.now(), eventId)
.run();
await context.data.prisma.event.update({
data: {
performed_at: new Date(),
},
where: {
id: eventId,
},
});
return new Response(null, {
status: 204,

View File

@@ -6,12 +6,11 @@ export async function onRequest(context: RequestContext) {
// Skip checks for the by-id endpoint
if (pathSegments.length <= 5) return await context.next();
const eventInfo = await context.env.D1.prepare(
"SELECT * FROM events WHERE id = ?;",
)
.bind(context.params.id)
.first();
const eventInfo = await context.data.prisma.event.findUnique({
where: {
id: context.params.id as string,
},
});
if (!eventInfo) return jsonError("This event does not exist.", 404);
if (![1 << 4, 1 << 12].find((p) => context.data.current_user.permissions & p))

View File

@@ -10,7 +10,7 @@ export async function onRequestPost(context: RequestContext) {
try {
await D1.batch([
D1.prepare(
"UPDATE events SET reached_minimum_player_count = 1 WHERE id = ?",
"UPDATE events SET reached_minimum_player_count = TRUE WHERE id = ?",
).bind(event.id),
D1.prepare(
"UPDATE et_members SET points = points + 10 WHERE id = ?;",

View File

@@ -5,23 +5,25 @@ export async function onRequestPost(context: RequestContext) {
const { event } = context.data;
try {
const completionTimeRow = await D1.prepare(
"SELECT performed_at FROM events WHERE id = ?;",
)
.bind(event.id)
.first();
const completionTimeRow = await context.data.prisma.event.findUnique({
select: {
performed_at: true,
},
where: {
id: event.id,
},
});
if (typeof completionTimeRow?.performed_at === "number")
if (completionTimeRow?.performed_at instanceof Date)
return jsonError(
"The event is already marked as complete or forgotten",
400,
);
await D1.batch([
D1.prepare("UPDATE events SET performed_at = ? WHERE id = ?;").bind(
Date.now(),
event.id,
),
D1.prepare(
"UPDATE events SET performed_at = CURRENT_TIMESTAMP WHERE id = ?;",
).bind(Date.now(), event.id),
D1.prepare(
"UPDATE et_members SET points = points + 10 WHERE id = ?;",
).bind(event.created_by),

View File

@@ -5,12 +5,15 @@ export async function onRequestPost(context: RequestContext) {
if (typeof context.data.body.approved !== "boolean")
return jsonError("Decision type must be a boolean", 400);
const updatedEvent: Record<string, number | string> | null =
await context.env.D1.prepare(
"UPDATE events SET approved = ?, pending = 0 WHERE id = ? RETURNING created_by, day, month, year;",
)
.bind(Number(context.data.body.approved), context.data.event.id)
.first();
const updatedEvent = await context.data.prisma.event.update({
data: {
approved: context.data.body.approved,
pending: false,
},
where: {
id: context.data.event.id,
},
});
if (!updatedEvent) return jsonError("This event does not exist", 404);
@@ -19,10 +22,14 @@ export async function onRequestPost(context: RequestContext) {
type: "json",
});
const usernameData: Record<string, string> | null =
await context.env.D1.prepare("SELECT name FROM et_members WHERE id = ?;")
.bind(updatedEvent.created_by)
.first();
const usernameData = await context.data.prisma.etMember.findUnique({
where: {
id: updatedEvent.created_by,
},
select: {
name: true,
},
});
if (emailData && usernameData) {
await sendEmail(

View File

@@ -2,22 +2,25 @@ import { jsonError } from "../../../../common.js";
export async function onRequestPost(context: RequestContext) {
const { D1 } = context.env;
const { event } = context.data;
const { event, prisma } = context.data;
try {
const row = await D1.prepare(
"SELECT performed_at FROM events WHERE id = ?;",
)
.bind(event.id)
.first();
const row = await prisma.event.findUnique({
select: {
performed_at: true,
},
where: {
id: event.id,
},
});
if (typeof row?.performed_at === "number")
if (row?.performed_at instanceof Date)
return jsonError("Event already marked as completed or forgotten", 400);
await D1.batch([
D1.prepare("UPDATE events SET performed_at = 0 WHERE id = ?;").bind(
event.id,
),
D1.prepare(
"UPDATE events SET performed_at = datetime(0, 'unixepoch') WHERE id = ?;",
).bind(event.id),
D1.prepare(
"UPDATE et_members SET points = points - 5 WHERE id = ?;",
).bind(event.created_by),

View File

@@ -22,11 +22,18 @@ export async function onRequestPost(context: RequestContext) {
return jsonError("Invalid body", 400);
if (
await context.env.D1.prepare(
"SELECT id FROM events WHERE (approved = 1 OR pending = 1) AND day = ? AND month = ? AND type = ? AND year = ?;",
)
.bind(day, currentMonth, type, currentYear)
.first()
await context.data.prisma.event.findFirst({
select: {
id: true,
},
where: {
OR: [{ approved: true }, { pending: true }],
day,
month: currentMonth,
type,
year: currentYear,
},
})
)
return jsonError(
"Event with that type already exists for the specified date",
@@ -44,16 +51,21 @@ export async function onRequestPost(context: RequestContext) {
const weekRange = Math.floor(day / 7);
const existingEventInRange = await context.env.D1.prepare(
"SELECT id FROM events WHERE (approved = 1 OR pending = 1) AND day BETWEEN ? AND ? AND month = ? AND type = 'rotw' AND year = ?;",
)
.bind(
weekRanges[weekRange] - 7,
weekRanges[weekRange],
currentMonth,
currentYear,
)
.first();
const existingEventInRange = await context.data.prisma.event.findFirst({
select: {
id: true,
},
where: {
OR: [{ approved: true }, { pending: true }],
day: {
gte: weekRanges[weekRange] - 7,
lte: weekRanges[weekRange],
},
month: currentMonth,
type: "rotw",
year: currentYear,
},
});
if (existingEventInRange)
return jsonError("There is already an rotw for that week", 400);
@@ -61,23 +73,20 @@ export async function onRequestPost(context: RequestContext) {
const id = `${now.getTime()}${crypto.randomUUID().replaceAll("-", "")}`;
await context.env.D1.prepare(
"INSERT INTO events (answer, approved, created_at, created_by, day, details, id, month, pending, type, year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
)
.bind(
context.data.body.answer || null,
Number(type === "gamenight"),
now.getTime(),
context.data.current_user.id,
await context.data.prisma.event.create({
data: {
answer: context.data.body.answer || null,
approved: type === "gamenight",
created_by: context.data.current_user.id,
day,
details,
id,
currentMonth,
Number(type !== "gamenight"),
month: currentMonth,
pending: type !== "gamenight",
type,
currentYear,
)
.run();
year: currentYear,
},
});
await fetch(context.env.EVENTS_WEBHOOK, {
body: JSON.stringify({