From e62a28c16822435519a54547baf5c9563283628f Mon Sep 17 00:00:00 2001 From: Aydan Pirani Date: Sat, 6 Jul 2024 23:23:42 -0700 Subject: [PATCH] clenaed up the code --- package.json | 1 + src/services/attendee/attendee-schema.ts | 46 +++++++++----- src/services/checkin/checkin-utils.ts | 76 ++++++++++++++++++------ yarn.lock | 23 ++++++- 4 files changed, 111 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index b135e76..77e56f5 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "body-parser": "^1.20.2", "cors": "^2.8.5", "crypto": "^1.0.1", + "datetime": "^0.0.3", "dotenv": "^16.4.5", "express": "^4.19.1", "express-rate-limit": "^6.0.0", diff --git a/src/services/attendee/attendee-schema.ts b/src/services/attendee/attendee-schema.ts index a9324d6..177a73d 100644 --- a/src/services/attendee/attendee-schema.ts +++ b/src/services/attendee/attendee-schema.ts @@ -14,18 +14,22 @@ export const AttendeeValidator = z.object({ foodWave: z.number().int().min(0).default(0), hasPriority: z .object({ - dayOne: z.boolean().default(false), - dayTwo: z.boolean().default(false), - dayThree: z.boolean().default(false), - dayFour: z.boolean().default(false), - dayFive: z.boolean().default(false), + Mon: z.boolean().default(false), + Tue: z.boolean().default(false), + Wed: z.boolean().default(false), + Thu: z.boolean().default(false), + Fri: z.boolean().default(false), + Sat: z.boolean().default(false), + Sun: z.boolean().default(false), }) .default({ - dayOne: false, - dayTwo: false, - dayThree: false, - dayFour: false, - dayFive: false, + Mon: false, + Tue: false, + Wed: false, + Thu: false, + Fri: false, + Sat: false, + Sun: false, }), }); @@ -43,15 +47,25 @@ export const AttendeeSchema = new Schema({ hasPriority: { type: new Schema( { - dayOne: { type: Boolean, default: false }, - dayTwo: { type: Boolean, default: false }, - dayThree: { type: Boolean, default: false }, - dayFour: { type: Boolean, default: false }, - dayFive: { type: Boolean, default: false }, + Mon: { type: Boolean, default: false }, + Tue: { type: Boolean, default: false }, + Wed: { type: Boolean, default: false }, + Thu: { type: Boolean, default: false }, + Fri: { type: Boolean, default: false }, + Sat: { type: Boolean, default: false }, + Sun: { type: Boolean, default: false }, }, { _id: false } ), - default: () => ({}), + default: { + Mon: false, + Tue: false, + Wed: false, + Thu: false, + Fri: false, + Sat: false, + Sun: false, + }, }, favorites: [{ type: String }], }); diff --git a/src/services/checkin/checkin-utils.ts b/src/services/checkin/checkin-utils.ts index ac9d313..a7b5937 100644 --- a/src/services/checkin/checkin-utils.ts +++ b/src/services/checkin/checkin-utils.ts @@ -1,33 +1,73 @@ import { Database } from "../../database"; -export async function checkInUserToEvent(eventId: string, userId: string) { - // Check if the event and attendee exist +function getCurrentDay() { + const currDate = new Date(); + const dayString = new Intl.DateTimeFormat("en-US", { + timeZone: "America/Chicago", + weekday: "short", + }).format(currDate); + return dayString; +} + +async function checkEventAndAttendeeExist(eventId: string, userId: string): Promise { const [event, attendee] = await Promise.all([ - Database.EVENTS.findOne({ eventId }), - Database.ATTENDEE.findOne({ userId }), + Database.EVENTS.exists({ eventId }), + Database.ATTENDEE.exists({ userId }), ]); if (!event || !attendee) { - return Promise.reject("Event or Attendee not found"); + throw new Error("Event or Attendee not found"); + } + + return Promise.resolve(); +} + +async function checkForDuplicateAttendance(eventId: string, userId: string): Promise { + const [isRepeatEvent, isRepeatAttendee] = await Promise.all([ + Database.EVENTS_ATTENDANCE.exists({ eventId, attendees: userId }), + Database.ATTENDEE_ATTENDANCE.exists({ userId, eventsAttended: eventId }), + ]); + + if (isRepeatEvent || isRepeatAttendee) { + throw new Error("Is Duplicate"); } +} - const eventAttendancePromise = Database.EVENTS_ATTENDANCE.findOneAndUpdate( - { eventId }, - { $addToSet: { attendees: userId } }, - { new: true, upsert: true } +// Update attendee priority for the current day +async function updateAttendeePriority(userId: string): Promise { + const day = getCurrentDay(); + await Database.ATTENDEE.findOneAndUpdate( + { userId }, + { $set: { [`hasPriority.${day}`]: true } } ); +} - const attendeeAttendancePromise = +async function updateAttendanceRecords(eventId: string, userId: string): Promise { + await Promise.all([ + Database.EVENTS_ATTENDANCE.findOneAndUpdate( + { eventId }, + { $addToSet: { attendees: userId } }, + { new: true, upsert: true } + ), Database.ATTENDEE_ATTENDANCE.findOneAndUpdate( { userId }, { $addToSet: { eventsAttended: eventId } }, { new: true, upsert: true } - ); - - return Promise.all([eventAttendancePromise, attendeeAttendancePromise]) - .then(() => ({ success: true })) - .catch(() => ({ - success: false, - message: "Couldn't upsert event or attendee", - })); + ), + ]); +} + +export async function checkInUserToEvent(eventId: string, userId: string, isCheckin: boolean = false): Promise { + try { + await checkEventAndAttendeeExist(eventId, userId); + await checkForDuplicateAttendance(eventId, userId); + + if (isCheckin) { + await updateAttendeePriority(userId); + } + + await updateAttendanceRecords(eventId, userId); + } catch (error) { + return Promise.reject(error); + } } diff --git a/yarn.lock b/yarn.lock index e69da6b..e949d7d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3026,6 +3026,13 @@ data-view-byte-offset@^1.0.0: es-errors "^1.3.0" is-data-view "^1.0.1" +datetime@^0.0.3: + version "0.0.3" + resolved "https://registry.yarnpkg.com/datetime/-/datetime-0.0.3.tgz#fa9086d20f81d54e7776a8180ee7320ae367ed78" + integrity sha512-7Y3rDInvMV5V/ZiIUFJb12t8wO+lNpcLyE0Za5FEZOtXFQmr/NK/E8EtXkhKH82N6Shi30VuyaAqUHS45ilsQA== + dependencies: + vows ">=0.5.4" + debug@2.6.9, debug@^2.6.9: version "2.6.9" resolved "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz" @@ -3702,6 +3709,11 @@ express@^4.19.1: utils-merge "1.0.1" vary "~1.1.2" +eyes@~0.1.6: + version "0.1.8" + resolved "https://registry.yarnpkg.com/eyes/-/eyes-0.1.8.tgz#62cf120234c683785d902348a800ef3e0cc20bc0" + integrity sha512-GipyPsXO1anza0AOZdy69Im7hGFCNB7Y/NGjDlZGJ3GJJLtwNSb2vrzYrTYJRrRloVx7pl+bhUaTB8yiccPvFQ== + fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: version "3.1.3" resolved "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz" @@ -3958,7 +3970,7 @@ glob-parent@^6.0.1: dependencies: is-glob "^4.0.3" -glob@^7.1.3, glob@^7.1.4: +glob@^7.1.2, glob@^7.1.3, glob@^7.1.4: version "7.2.3" resolved "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz" integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q== @@ -6475,6 +6487,15 @@ vary@^1, vary@~1.1.2: resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz" integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== +vows@>=0.5.4: + version "0.8.3" + resolved "https://registry.yarnpkg.com/vows/-/vows-0.8.3.tgz#36e353c2bca3a93902fc32eb8c5baab2e3a93f10" + integrity sha512-PVIxa/ovXhrw5gA3mz6M+ZF3PHlqX4tutR2p/y9NWPAaFVKcWBE8b2ktfr0opQM/qFmcOVWKjSCJVjnYOvjXhw== + dependencies: + diff "^4.0.1" + eyes "~0.1.6" + glob "^7.1.2" + walker@^1.0.8: version "1.0.8" resolved "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz"