diff --git a/src/services/checkin/checkin-router.ts b/src/services/checkin/checkin-router.ts index 7d9c19f..7a4bbd9 100644 --- a/src/services/checkin/checkin-router.ts +++ b/src/services/checkin/checkin-router.ts @@ -51,12 +51,20 @@ checkinRouter.post( RoleChecker([Role.Enum.ADMIN, Role.Enum.STAFF]), async (req, res, next) => { try { - const { eventId, userId, isCheckin } = EventValidator.parse( + const { eventId, userId } = EventValidator.parse( req.body ); - await checkInUserToEvent(eventId, userId, isCheckin); - + try { + await checkInUserToEvent(eventId, userId); + } catch (error: unknown) { + if (error instanceof Error && error.message == "IsDuplicate") { + return res + .status(StatusCodes.FORBIDDEN) + .json({ error: "IsDuplicate" }); + } + return res.sendStatus(StatusCodes.INTERNAL_SERVER_ERROR); + } return res.status(StatusCodes.OK).json(userId); } catch (error) { next(error); diff --git a/src/services/checkin/checkin-schema.ts b/src/services/checkin/checkin-schema.ts index 3e0e8c0..c3307c0 100644 --- a/src/services/checkin/checkin-schema.ts +++ b/src/services/checkin/checkin-schema.ts @@ -12,7 +12,6 @@ const MerchScanValidator = z.object({ const EventValidator = z.object({ eventId: z.string(), userId: z.string(), - isCheckin: z.boolean(), }); export { ScanValidator, MerchScanValidator, EventValidator }; diff --git a/src/services/checkin/checkin-utils.ts b/src/services/checkin/checkin-utils.ts index 85e4130..acefb1d 100644 --- a/src/services/checkin/checkin-utils.ts +++ b/src/services/checkin/checkin-utils.ts @@ -1,6 +1,7 @@ import { Database } from "../../database"; import crypto from "crypto"; import { Config } from "../../config"; +import { EventType } from "../events/events-schema"; export function getCurrentDay() { const currDate = new Date(); @@ -47,6 +48,7 @@ async function updateAttendeePriority(userId: string) { ); } + async function updateAttendanceRecords(eventId: string, userId: string) { await Promise.all([ Database.EVENTS_ATTENDANCE.findOneAndUpdate( @@ -83,24 +85,35 @@ async function assignPixelsToUser(userId: string, pixels: number) { ); } +async function markUserAsCheckedIn(userId: string) { + await Database.ATTENDEE.findOneAndUpdate( + { userId }, + { $set: { hasCheckedIn: true } } + ); +} + export async function checkInUserToEvent( eventId: string, userId: string, - isCheckin: boolean = false ) { await checkEventAndAttendeeExist(eventId, userId); await checkForDuplicateAttendance(eventId, userId); + + const event = await Database.EVENTS.findOne({ eventId }); + if (!event) { + throw new Error("Event not found"); + } - if (!isCheckin) { + // check for checkin event, and for meals + if (event.eventType == EventType.Enum.CHECKIN) { + await markUserAsCheckedIn(userId); + } else if (event.eventType != EventType.Enum.MEALS){ await updateAttendeePriority(userId); } await updateAttendanceRecords(eventId, userId); - const event = await Database.EVENTS.findOne({ eventId }); - if (!event) { - throw new Error("Event not found"); - } + await assignPixelsToUser(userId, event.points); } diff --git a/src/services/events/events-schema.ts b/src/services/events/events-schema.ts index c7de781..9b8cc40 100644 --- a/src/services/events/events-schema.ts +++ b/src/services/events/events-schema.ts @@ -8,6 +8,7 @@ export const EventType = z.enum([ "SPECIAL", "PARTNERS", "MEALS", + "CHECKIN", ]); export const externalEventView = z.object({