More events team nonsense
This commit is contained in:
@@ -31,6 +31,7 @@ import {
|
||||
import { useLoaderData } from "@remix-run/react";
|
||||
import { useState } from "react";
|
||||
import utc from "dayjs/plugin/utc.js";
|
||||
import { EtMember } from "../../generated/prisma/client.js";
|
||||
|
||||
export const links: LinksFunction = () => {
|
||||
return [
|
||||
@@ -56,18 +57,31 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
});
|
||||
|
||||
const now = new Date();
|
||||
const eventsData = await context.env.D1.prepare(
|
||||
"SELECT answer, approved, created_by, day, details, id, month, pending, performed_at, reached_minimum_player_count, type, year FROM events WHERE month = ? AND year = ? ORDER BY day;",
|
||||
)
|
||||
.bind(now.getUTCMonth() + 1, now.getUTCFullYear())
|
||||
.all();
|
||||
|
||||
if (eventsData.error)
|
||||
throw new Response(null, {
|
||||
status: 500,
|
||||
const eventsData = await context.data.prisma.event.findMany({
|
||||
orderBy: {
|
||||
day: "asc",
|
||||
},
|
||||
select: {
|
||||
answer: true,
|
||||
approved: true,
|
||||
created_by: true,
|
||||
day: true,
|
||||
details: true,
|
||||
id: true,
|
||||
month: true,
|
||||
pending: true,
|
||||
performed_at: true,
|
||||
reached_minimum_player_count: true,
|
||||
type: true,
|
||||
year: true,
|
||||
},
|
||||
where: {
|
||||
month: now.getUTCMonth() + 1,
|
||||
year: now.getUTCFullYear(),
|
||||
},
|
||||
});
|
||||
|
||||
const calendarData = eventsData.results.map((e) => {
|
||||
const calendarData = eventsData.map((e) => {
|
||||
return {
|
||||
id: e.id,
|
||||
title: (e.type as string).toUpperCase(),
|
||||
@@ -78,19 +92,17 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
};
|
||||
});
|
||||
|
||||
const memberData = await context.env.D1.prepare(
|
||||
"SELECT id, name FROM et_members WHERE id IN (SELECT created_by FROM events WHERE month = ? AND year = ?);",
|
||||
)
|
||||
.bind(now.getUTCMonth() + 1, now.getUTCFullYear())
|
||||
.all();
|
||||
const memberData = await context.data.prisma.$queryRaw<
|
||||
EtMember[]
|
||||
>`SELECT id, name FROM et_members WHERE id IN (SELECT created_by FROM events WHERE month = ${now.getUTCMonth() + 1} AND year = ${now.getUTCFullYear()});`;
|
||||
|
||||
return {
|
||||
calendarData,
|
||||
canManage: Boolean(
|
||||
[1 << 4, 1 << 12].find((p) => context.data.current_user.permissions & p),
|
||||
),
|
||||
eventList: eventsData.results,
|
||||
memberData: memberData.results,
|
||||
eventList: eventsData,
|
||||
memberData,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -111,15 +123,7 @@ export default function () {
|
||||
<ModalBody>
|
||||
<Heading size="md">Host</Heading>
|
||||
<Text>
|
||||
{
|
||||
(
|
||||
data.memberData.find(
|
||||
(m) => m.id === eventData.created_by,
|
||||
) as {
|
||||
[k: string]: any;
|
||||
}
|
||||
)?.name
|
||||
}
|
||||
{data.memberData.find((m) => m.id === eventData.created_by)?.name}
|
||||
</Text>
|
||||
<br />
|
||||
<Heading size="md">Event Type</Heading>
|
||||
|
||||
@@ -25,13 +25,12 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
status: 403,
|
||||
});
|
||||
|
||||
const memberResults = await context.env.D1.prepare(
|
||||
"SELECT id, name FROM et_members;",
|
||||
).all();
|
||||
|
||||
if (!memberResults.success)
|
||||
throw new Response(null, {
|
||||
status: 500,
|
||||
const { prisma } = context.data;
|
||||
const memberResults = await prisma.etMember.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
|
||||
const data: {
|
||||
@@ -45,7 +44,7 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
};
|
||||
} = {};
|
||||
|
||||
for (const row of memberResults.results as Record<string, string>[]) {
|
||||
for (const row of memberResults) {
|
||||
data[row.id].fotd = 0;
|
||||
data[row.id].gamenight = 0;
|
||||
data[row.id].name = row.name;
|
||||
@@ -53,22 +52,26 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
data[row.id].qotd = 0;
|
||||
}
|
||||
|
||||
const eventsResult = await context.env.D1.prepare(
|
||||
"SELECT answered_at, created_by, day, month, performed_at, reached_minimum_player_count, type, year FROM events;",
|
||||
).all();
|
||||
|
||||
if (!eventsResult.success)
|
||||
throw new Response(null, {
|
||||
status: 500,
|
||||
const eventsResult = await prisma.event.findMany({
|
||||
select: {
|
||||
answered_at: true,
|
||||
created_by: true,
|
||||
day: true,
|
||||
month: true,
|
||||
performed_at: true,
|
||||
reached_minimum_player_count: true,
|
||||
type: true,
|
||||
year: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const row of eventsResult.results) {
|
||||
const creator = row.created_by as string;
|
||||
const type = row.type as string;
|
||||
for (const row of eventsResult) {
|
||||
const creator = row.created_by;
|
||||
const type = row.type;
|
||||
|
||||
if (!data[creator]) continue;
|
||||
|
||||
if (row.performed_at) data[creator][type as string] += 10;
|
||||
if (row.performed_at) data[creator][type] += 10;
|
||||
else {
|
||||
const now = new Date();
|
||||
const currentYear = now.getUTCFullYear();
|
||||
@@ -76,33 +79,17 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
const currentDay = now.getUTCDate();
|
||||
|
||||
if (
|
||||
(row.year as number) < currentYear ||
|
||||
(currentYear === row.year && currentMonth > (row.month as number)) ||
|
||||
row.year < currentYear ||
|
||||
(currentYear === row.year && currentMonth > row.month) ||
|
||||
(currentMonth === row.month &&
|
||||
currentYear === row.year &&
|
||||
(row.day as number) < currentDay)
|
||||
row.day < currentDay)
|
||||
)
|
||||
data[creator][type] -= 5;
|
||||
}
|
||||
|
||||
switch (row.type) {
|
||||
case "gamenight":
|
||||
if (row.reached_minimum_player_count) data[creator].gamenight += 10;
|
||||
|
||||
break;
|
||||
|
||||
case "rotw":
|
||||
if (
|
||||
(row.answered_at as number) - (row.performed_at as number) >=
|
||||
86400000
|
||||
)
|
||||
data[creator].rotw += 10;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (row.type === "gamenight" && row.reached_minimum_player_count)
|
||||
data[creator].gamenight += 10;
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
@@ -44,9 +44,12 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
status: 403,
|
||||
});
|
||||
|
||||
return (
|
||||
await context.env.D1.prepare("SELECT id, name FROM et_members;").all()
|
||||
).results;
|
||||
return await context.data.prisma.etMember.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export default function () {
|
||||
|
||||
@@ -37,14 +37,40 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
year--;
|
||||
}
|
||||
|
||||
const data = await context.env.D1.prepare(
|
||||
"SELECT day, details, id FROM events WHERE approved = 1 AND month = ? AND year = ? AND (performed_at IS NULL OR (reached_minimum_player_count = 0 AND type = 'gamenight')) ORDER BY day;",
|
||||
)
|
||||
.bind(month, year)
|
||||
.all();
|
||||
const data = await context.data.prisma.event.findMany({
|
||||
orderBy: {
|
||||
day: "asc",
|
||||
},
|
||||
select: {
|
||||
answered_at: true,
|
||||
day: true,
|
||||
details: true,
|
||||
id: true,
|
||||
performed_at: true,
|
||||
reached_minimum_player_count: true,
|
||||
type: true,
|
||||
},
|
||||
where: {
|
||||
AND: {
|
||||
approved: true,
|
||||
month,
|
||||
year,
|
||||
OR: [
|
||||
{
|
||||
AND: [
|
||||
{
|
||||
reached_minimum_player_count: false,
|
||||
type: "gamenight",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
events: data.results as Record<string, string | number>[],
|
||||
events: data,
|
||||
past_cutoff: now.getUTCDate() > 7,
|
||||
};
|
||||
}
|
||||
@@ -52,7 +78,7 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
export default function () {
|
||||
const { events, past_cutoff } = useLoaderData<typeof loader>();
|
||||
const { isOpen, onClose, onOpen } = useDisclosure();
|
||||
const [eventData, setEventData] = useState({} as { [k: string]: any });
|
||||
const [eventData, setEventData] = useState({} as (typeof events)[number]);
|
||||
const [isBrowserSupported, setIsBrowserSupported] = useState(true);
|
||||
const toast = useToast();
|
||||
|
||||
@@ -74,10 +100,10 @@ export default function () {
|
||||
});
|
||||
}
|
||||
|
||||
function getStatus(event: { [k: string]: string | number }) {
|
||||
function getStatus(event: (typeof events)[number]) {
|
||||
if (!event.performed_at) return "Approved";
|
||||
if (event.type === "rotw" && event.answered_at) return "Solved";
|
||||
if (event.type === "gamenight" && event.areached_minimum_player_count)
|
||||
if (event.type === "gamenight" && event.reached_minimum_player_count)
|
||||
return "Certified";
|
||||
|
||||
return "Completed";
|
||||
@@ -106,7 +132,7 @@ export default function () {
|
||||
});
|
||||
|
||||
const newData = structuredClone(eventData);
|
||||
newData.reached_minimum_player_count = 1;
|
||||
newData.reached_minimum_player_count = true;
|
||||
|
||||
setEventData(newData);
|
||||
}
|
||||
@@ -134,9 +160,12 @@ export default function () {
|
||||
});
|
||||
|
||||
const newData = structuredClone(eventData);
|
||||
newData.performed_at = Date.now();
|
||||
|
||||
setEventData(newData);
|
||||
setEventData(
|
||||
Object.defineProperty(newData, "performed_at", {
|
||||
value: new Date().toISOString(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function forgotten() {
|
||||
@@ -162,9 +191,12 @@ export default function () {
|
||||
});
|
||||
|
||||
const newData = structuredClone(eventData);
|
||||
newData.performed_at = 0;
|
||||
|
||||
setEventData(newData);
|
||||
setEventData(
|
||||
Object.defineProperty(newData, "performed_at", {
|
||||
value: new Date().toISOString(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function solve() {
|
||||
@@ -190,9 +222,12 @@ export default function () {
|
||||
});
|
||||
|
||||
const newData = structuredClone(eventData);
|
||||
newData.answered_at = Date.now();
|
||||
|
||||
setEventData(newData);
|
||||
setEventData(
|
||||
Object.defineProperty(newData, "performed_at", {
|
||||
value: new Date().toISOString(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -216,13 +251,13 @@ export default function () {
|
||||
<Box gap="8px">
|
||||
<Heading size="xs">Completion</Heading>
|
||||
<Button
|
||||
disabled={typeof eventData.completed_at === "number"}
|
||||
disabled={Boolean(eventData.performed_at)}
|
||||
onClick={async () => await completed()}
|
||||
>
|
||||
Mark as Complete
|
||||
</Button>
|
||||
<Button
|
||||
disabled={typeof eventData.completed_at === "number"}
|
||||
disabled={typeof eventData.performed_at === "number"}
|
||||
onClick={async () => await forgotten()}
|
||||
>
|
||||
Mark as Forgotten
|
||||
@@ -243,7 +278,7 @@ export default function () {
|
||||
<Box gap="8px">
|
||||
<Heading size="xs">Certified Status</Heading>
|
||||
<Button
|
||||
disabled={Boolean(eventData.reached_minimum_player_count)}
|
||||
disabled={eventData.reached_minimum_player_count}
|
||||
onClick={async () => await certify()}
|
||||
>
|
||||
{eventData.reached_minimum_player_count
|
||||
@@ -277,8 +312,8 @@ export default function () {
|
||||
<Tr>
|
||||
<Td>{event.day}</Td>
|
||||
<Td>
|
||||
{(event.details as string).length > 100
|
||||
? `${(event.details as string).substring(0, 97)}...`
|
||||
{event.details.length > 100
|
||||
? `${event.details.substring(0, 97)}...`
|
||||
: event.details}
|
||||
</Td>
|
||||
<Td>{getStatus(event)}</Td>
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 = ?;",
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -12,9 +12,14 @@ export async function onRequestPost(context: RequestContext) {
|
||||
|
||||
if (typeof points !== "number") return jsonError("Invalid point count", 400);
|
||||
|
||||
await context.env.D1.prepare("UPDATE et_members SET points = ? WHERE id = ?;")
|
||||
.bind(points, context.params.id)
|
||||
.run();
|
||||
await context.data.prisma.etMember.update({
|
||||
data: {
|
||||
points,
|
||||
},
|
||||
where: {
|
||||
id: context.params.id as string,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export async function onRequestDelete(context: RequestContext) {
|
||||
await context.env.D1.prepare("DELETE FROM et_strikes WHERE id = ?;")
|
||||
.bind(context.params.id)
|
||||
.run();
|
||||
await context.data.prisma.etStrike.delete({
|
||||
where: {
|
||||
id: context.params.id as string,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
|
||||
@@ -11,9 +11,11 @@ export async function onRequestPost(context: RequestContext) {
|
||||
user.length > 20 ||
|
||||
user.length < 17 ||
|
||||
user.match(/\D/) ||
|
||||
!(await D1.prepare("SELECT id FROM et_members WHERE id = ?;")
|
||||
.bind(user)
|
||||
.first())
|
||||
!(await context.data.prisma.etMember.findUnique({
|
||||
where: {
|
||||
id: user,
|
||||
},
|
||||
}))
|
||||
)
|
||||
return jsonError("Invalid user id", 400);
|
||||
|
||||
|
||||
@@ -19,9 +19,9 @@ export async function onRequestDelete(context: RequestContext) {
|
||||
)
|
||||
return jsonError("Invalid ID", 400);
|
||||
|
||||
await context.env.D1.prepare("DELETE FROM et_members WHERE id = ?;")
|
||||
.bind(id)
|
||||
.run();
|
||||
await context.data.prisma.etMember.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
@@ -40,9 +40,10 @@ export async function onRequestPatch(context: RequestContext) {
|
||||
if (typeof body.name !== "string" && typeof body.roblox_username !== "string")
|
||||
return jsonError("At least one property must be provided", 400);
|
||||
|
||||
const updates = [];
|
||||
let queryData: { name?: string; roblox_id?: number } = {};
|
||||
|
||||
if (body.name?.length) updates.push({ query: "name = ?", value: body.name });
|
||||
if (body.name?.length)
|
||||
queryData = Object.defineProperty(queryData, "name", { value: body.name });
|
||||
|
||||
if (typeof body.roblox_username === "string" && body.roblox_username) {
|
||||
const robloxResolveResp = await fetch(
|
||||
@@ -66,21 +67,22 @@ export async function onRequestPatch(context: RequestContext) {
|
||||
if (!data.length)
|
||||
return jsonError("No Roblox user exists with that name", 400);
|
||||
|
||||
updates.push({ query: "roblox_id = ?", value: data[0].id });
|
||||
queryData = Object.defineProperty(queryData, "roblox_id", {
|
||||
value: data[0].id,
|
||||
});
|
||||
}
|
||||
|
||||
await context.env.D1.prepare(
|
||||
`UPDATE et_members
|
||||
SET ${updates.map((u) => u.query).join(", ")}
|
||||
WHERE id = ?;`,
|
||||
)
|
||||
.bind(...updates.map((u) => u.value), body.id)
|
||||
.run();
|
||||
await context.data.prisma.etMember.update({
|
||||
data: queryData,
|
||||
where: {
|
||||
id: body.id,
|
||||
},
|
||||
});
|
||||
|
||||
return jsonResponse(
|
||||
JSON.stringify({
|
||||
name: body.name,
|
||||
roblox_id: updates.find((u) => typeof u.value === "number")?.value,
|
||||
roblox_id: queryData.roblox_id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -100,9 +102,11 @@ export async function onRequestPost(context: RequestContext) {
|
||||
return jsonError("Invalid name", 400);
|
||||
|
||||
if (
|
||||
await context.env.D1.prepare("SELECT * FROM et_members WHERE id = ?;")
|
||||
.bind(id)
|
||||
.first()
|
||||
await context.data.prisma.etMember.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
return jsonError("User is already a member", 400);
|
||||
|
||||
@@ -151,14 +155,16 @@ export async function onRequestPost(context: RequestContext) {
|
||||
roblox_id = data[0].id;
|
||||
}
|
||||
|
||||
const createdAt = Date.now();
|
||||
const addingUser = context.data.current_user.id;
|
||||
|
||||
await context.env.D1.prepare(
|
||||
"INSERT INTO et_members (created_at, created_by, id, name, roblox_id) VALUES (?, ?, ?, ?, ?);",
|
||||
)
|
||||
.bind(createdAt, addingUser, id, name, roblox_id || null)
|
||||
.run();
|
||||
await context.data.prisma.etMember.create({
|
||||
data: {
|
||||
created_by: addingUser,
|
||||
id,
|
||||
name,
|
||||
roblox_id,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
|
||||
@@ -3,13 +3,12 @@ import { jsonError } from "../../common.js";
|
||||
export async function onRequestDelete(context: RequestContext) {
|
||||
const path = decodeURIComponent(context.params.id as string);
|
||||
|
||||
if (typeof path !== "string") return jsonError("Invalid path", 400);
|
||||
|
||||
await context.env.D1.prepare(
|
||||
"DELETE FROM short_links WHERE path = ? AND user = ?;",
|
||||
)
|
||||
.bind(path, context.data.current_user.id)
|
||||
.run();
|
||||
await context.data.prisma.shortLink.delete({
|
||||
where: {
|
||||
path,
|
||||
user: context.data.current_user.id,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
@@ -39,15 +38,15 @@ export async function onRequestPatch(context: RequestContext) {
|
||||
|
||||
if (path.length > 256) return jsonError("Path is too long", 400);
|
||||
|
||||
await context.env.D1.prepare(
|
||||
"UPDATE short_links SET path = ? WHERE path = ? AND user = ?;",
|
||||
)
|
||||
.bind(
|
||||
await context.data.prisma.shortLink.update({
|
||||
data: {
|
||||
path,
|
||||
decodeURIComponent(context.params.id as string),
|
||||
context.data.current_user.id,
|
||||
)
|
||||
.run();
|
||||
},
|
||||
where: {
|
||||
path: decodeURIComponent(context.params.id as string),
|
||||
user: context.data.current_user.id,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
|
||||
@@ -9,11 +9,10 @@ export default async function (
|
||||
if (!roles) permissions |= 1 << 1;
|
||||
if (roles?.includes("593209890949038082")) permissions |= 1 << 2; // Discord Moderator
|
||||
if (
|
||||
Boolean(
|
||||
await context.env.D1.prepare("SELECT * FROM et_members WHERE id = ?;")
|
||||
.bind(userid)
|
||||
.first(),
|
||||
)
|
||||
await context.data.prisma.etMember.findUnique({
|
||||
select: { id: true },
|
||||
where: { id: userid },
|
||||
})
|
||||
)
|
||||
permissions |= 1 << 3; // Events Team
|
||||
if (roles?.includes("607594065952899072")) permissions |= 1 << 4; // Events Manager
|
||||
|
||||
Reference in New Issue
Block a user