From 402c0049deace16fdb674721238dfd6a4727c102 Mon Sep 17 00:00:00 2001 From: Kieran Klukas <92754843+kcoderhtml@users.noreply.github.com> Date: Wed, 1 May 2024 16:20:43 -0400 Subject: [PATCH] Add new availability update page for users to update their availability for an event --- .../update/[team]/[eventID]/[userID].astro | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/pages/update/[team]/[eventID]/[userID].astro diff --git a/src/pages/update/[team]/[eventID]/[userID].astro b/src/pages/update/[team]/[eventID]/[userID].astro new file mode 100644 index 0000000..4cc0e71 --- /dev/null +++ b/src/pages/update/[team]/[eventID]/[userID].astro @@ -0,0 +1,158 @@ +--- +import Base from "../../../../Layouts/Base.astro"; +import ThreeWayToggle from "../../../../components/ThreeWayToggle.astro"; + +import { LogSnag } from "logsnag"; +import { db, like, and, Event, User } from "astro:db"; + +const logsnag = new LogSnag({ + token: process.env.LOGSNAG_TOKEN || "", + project: "magicsnap", +}); + +const { team, eventID, userID } = Astro.params as { + team: string; + eventID: string; + userID: string; +}; + +const user = ( + await db + .select() + .from(User) + .where(and(like(User.team, team), like(User.userId, userID))) + .all() +)[0]; + +const hash = Astro.url.searchParams.get("hash"); + +if (Astro.request.method === "POST" && user && hash === user.hash) { + try { + const data = await Astro.request.formData(); + + if (data.get("availability") != null) { + const eventID = data.get("eventID") as string; + const status = data.get("selected") as string; + + const event = ( + await db.select().from(Event).where(like(Event.id, eventID)) + )?.[0]; + + if (event) { + let statusGoing = event.statusGoing + .split(",") + .filter((id) => id !== ""); + let statusMaybe = event.statusMaybe + .split(",") + .filter((id) => id !== ""); + let statusNotGoing = event.statusNotGoing + .split(",") + .filter((id) => id !== ""); + + const currentStatus = statusGoing.includes(user.userId) + ? "yes" + : statusMaybe.includes(user.userId) + ? "maybe" + : "no"; + + if (currentStatus !== status) { + if (status === "yes") { + statusGoing.push(user.userId); + statusMaybe = statusMaybe.filter((id) => id !== user.userId); + statusNotGoing = statusNotGoing.filter((id) => id !== user.userId); + } else if (status === "maybe") { + statusMaybe.push(user.userId); + statusGoing = statusGoing.filter((id) => id !== user.userId); + statusNotGoing = statusNotGoing.filter((id) => id !== user.userId); + } else if (status === "no" && currentStatus !== "no") { + statusNotGoing.push(user.userId); + statusGoing = statusGoing.filter((id) => id !== user.userId); + statusMaybe = statusMaybe.filter((id) => id !== user.userId); + } + } + + await db + .update(Event) + .set({ + statusGoing: statusGoing.join(","), + statusMaybe: statusMaybe.join(","), + statusNotGoing: statusNotGoing.join(","), + }) + .where(like(Event.id, eventID)); + + await logsnag.track({ + channel: "actions", + event: "event_status_change", + icon: "📅", + user_id: user.userId, + }); + } + } + } catch (error) { + if (error instanceof Error) { + await logsnag.track({ + channel: "errors", + event: "event_status_change_error", + icon: "📅", + user_id: user.userId, + tags: { + error: error.message, + }, + }); + } + } +} + +const event = ( + await db + .select() + .from(Event) + .where(and(like(Event.team, team), like(Event.id, eventID))) + .all() +)[0]; +--- + + +
+ {user && user.hash === hash && event && ( +

{event.name}

+

+ {event.name} is being held at {event.location} on { + event.date.toLocaleDateString() + + " at " + + event.date.toLocaleTimeString([], { + hour: "numeric", + minute: "2-digit", + }) + } +

+

{event.comments}

+

Are you going?

+
+ +
+ )} + {!user &&

Sorry, we couldn't find your user account.

} + {user && user.hash !== hash &&

Sorry, the link you used is invalid.

} + {!event &&

Sorry, we couldn't find the event you're looking for.

} +
+ + +