More events team nonsense
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user