Skip to content

Commit

Permalink
clenaed up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
AydanPirani committed Jul 7, 2024
1 parent 4743128 commit e62a28c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 30 additions & 16 deletions src/services/attendee/attendee-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
});

Expand All @@ -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 }],
});
Expand Down
76 changes: 58 additions & 18 deletions src/services/checkin/checkin-utils.ts
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> {

Check failure on line 12 in src/services/checkin/checkin-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `eventId:·string,·userId:·string` with `⏎····eventId:·string,⏎····userId:·string⏎`
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");
}

Check failure on line 21 in src/services/checkin/checkin-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `····`
return Promise.resolve();
}

async function checkForDuplicateAttendance(eventId: string, userId: string): Promise<void> {

Check failure on line 25 in src/services/checkin/checkin-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `eventId:·string,·userId:·string` with `⏎····eventId:·string,⏎····userId:·string⏎`
const [isRepeatEvent, isRepeatAttendee] = await Promise.all([
Database.EVENTS_ATTENDANCE.exists({ eventId, attendees: userId }),
Database.ATTENDEE_ATTENDANCE.exists({ userId, eventsAttended: eventId }),

Check failure on line 28 in src/services/checkin/checkin-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·userId,·eventsAttended:·eventId` with `⏎············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> {

Check failure on line 45 in src/services/checkin/checkin-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `eventId:·string,·userId:·string` with `⏎····eventId:·string,⏎····userId:·string⏎`
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> {

Check failure on line 60 in src/services/checkin/checkin-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `eventId:·string,·userId:·string,·isCheckin:·boolean·=·false` with `⏎····eventId:·string,⏎····userId:·string,⏎····isCheckin:·boolean·=·false⏎`
try {
await checkEventAndAttendeeExist(eventId, userId);
await checkForDuplicateAttendance(eventId, userId);

if (isCheckin) {
await updateAttendeePriority(userId);
}

await updateAttendanceRecords(eventId, userId);
} catch (error) {
return Promise.reject(error);
}
}
23 changes: 22 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e62a28c

Please sign in to comment.