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,
|
||||
|
||||
@@ -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