-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4743128
commit e62a28c
Showing
4 changed files
with
111 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> { | ||
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<void> { | ||
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<void> { | ||
const day = getCurrentDay(); | ||
await Database.ATTENDEE.findOneAndUpdate( | ||
{ userId }, | ||
{ $set: { [`hasPriority.${day}`]: true } } | ||
); | ||
} | ||
|
||
const attendeeAttendancePromise = | ||
async function updateAttendanceRecords(eventId: string, userId: string): Promise<void> { | ||
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<void> { | ||
try { | ||
await checkEventAndAttendeeExist(eventId, userId); | ||
await checkForDuplicateAttendance(eventId, userId); | ||
|
||
if (isCheckin) { | ||
await updateAttendeePriority(userId); | ||
} | ||
|
||
await updateAttendanceRecords(eventId, userId); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
[email protected], 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" | ||
|