diff --git a/src/components/dashboard/EventsSection.astro b/src/components/dashboard/EventsSection.astro index a54f49c..e5f1da4 100644 --- a/src/components/dashboard/EventsSection.astro +++ b/src/components/dashboard/EventsSection.astro @@ -29,6 +29,45 @@ import { Icon } from "astro-icon/components"; + + + + + +
@@ -160,24 +199,30 @@ import { Icon } from "astro-icon/components"; interface Event { id: string; - event_id: string; event_name: string; event_code: string; location: string; - files: string[]; points_to_reward: number; - attendees: string[]; + attendees: AttendeeEntry[]; start_date: string; end_date: string; + has_food: boolean; description: string; + files: string[]; } - async function handleEventCheckIn(eventCode: string) { + interface AttendeeEntry { + user_id: string; + time_checked_in: string; + food: string; + } + + let currentCheckInEvent: Event | null = null; + + async function handleEventCheckIn(eventCode: string): Promise { try { const get = Get.getInstance(); const auth = Authentication.getInstance(); - const update = Update.getInstance(); - const logger = SendLog.getInstance(); const currentUser = auth.getCurrentUser(); if (!currentUser) { @@ -185,18 +230,16 @@ import { Icon } from "astro-icon/components"; } // Find the event with the given code - const events = await get.getFirst( + const event = await get.getFirst( "events", `event_code = "${eventCode}"`, ); - if (!events) { + if (!event) { throw new Error("Invalid event code"); } - const event = events; - // Check if user is already checked in - if (event.attendees.includes(currentUser.id)) { + if (event.attendees.some((entry) => entry.user_id === currentUser.id)) { throw new Error("You have already checked in to this event"); } @@ -206,8 +249,82 @@ import { Icon } from "astro-icon/components"; throw new Error("This event has already ended"); } - // Add user to attendees - const updatedAttendees = [...event.attendees, currentUser.id]; + // If event has food, show food selection modal + if (event.has_food) { + currentCheckInEvent = event; + const modal = document.getElementById( + "foodSelectionModal", + ) as HTMLDialogElement; + modal.showModal(); + } else { + // If no food, complete check-in directly + await completeCheckIn(event, null); + } + } catch (error: any) { + createToast(error?.message || "Failed to check in to event", "error"); + } + } + + // Add food selection form handler + const foodSelectionForm = document.getElementById( + "foodSelectionForm", + ) as HTMLFormElement; + if (foodSelectionForm) { + foodSelectionForm.addEventListener("submit", async (e) => { + e.preventDefault(); + const modal = document.getElementById( + "foodSelectionModal", + ) as HTMLDialogElement; + const foodInput = document.getElementById( + "foodInput", + ) as HTMLInputElement; + + try { + if (currentCheckInEvent) { + await completeCheckIn(currentCheckInEvent, foodInput.value.trim()); + modal.close(); + foodInput.value = ""; // Reset input + currentCheckInEvent = null; + } + } catch (error: any) { + createToast(error?.message || "Failed to check in to event", "error"); + } + }); + } + + async function completeCheckIn( + event: Event, + foodSelection: string | null, + ): Promise { + try { + const auth = Authentication.getInstance(); + const update = Update.getInstance(); + const logger = SendLog.getInstance(); + + const currentUser = auth.getCurrentUser(); + if (!currentUser) { + throw new Error("You must be logged in to check in to events"); + } + + // Create attendee entry with check-in details + const attendeeEntry: AttendeeEntry = { + user_id: currentUser.id, + time_checked_in: new Date().toISOString(), + food: foodSelection || "none", + }; + + // Get existing attendees or initialize empty array + const existingAttendees = event.attendees || []; + + // Check if user is already checked in + if (existingAttendees.some((entry) => entry.user_id === currentUser.id)) { + throw new Error("You have already checked in to this event"); + } + + // Add new attendee entry to the array + const updatedAttendees = [...existingAttendees, attendeeEntry]; + + // Update attendees array with the new entry await update.updateField( "events", event.id, @@ -215,6 +332,15 @@ import { Icon } from "astro-icon/components"; updatedAttendees, ); + // If food selection was made, log it + if (foodSelection) { + await logger.send( + "update", + "event check-in", + `Food selection for ${event.event_name}: ${foodSelection}`, + ); + } + // Award points to user if available if (event.points_to_reward > 0) { const userPoints = currentUser.points || 0; @@ -243,7 +369,6 @@ import { Icon } from "astro-icon/components"; "success", ); } catch (error: any) { - // Show error message createToast(error?.message || "Failed to check in to event", "error"); } } diff --git a/src/components/dashboard/Officer_EventManagement.astro b/src/components/dashboard/Officer_EventManagement.astro index b421f2a..7de8c0b 100644 --- a/src/components/dashboard/Officer_EventManagement.astro +++ b/src/components/dashboard/Officer_EventManagement.astro @@ -25,6 +25,14 @@ interface Event { start_date: string; end_date: string; published: boolean; + has_food: boolean; + attendees: AttendeeEntry[]; +} + +interface AttendeeEntry { + user_id: string; + time_checked_in: string; + food: string; } interface ListResponse { @@ -369,6 +377,24 @@ declare global {
+ +
+ + +
+ `;