Skip to content

Commit

Permalink
checkIn async func
Browse files Browse the repository at this point in the history
  • Loading branch information
riyap committed Jun 10, 2024
1 parent 6e7fed2 commit bb4d6cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
48 changes: 9 additions & 39 deletions src/services/events/events-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { EventValidator } from "./events-schema";
import { Database } from "../../database";
import { checkInUser } from "./events-utils";

const eventsRouter = Router();

Expand Down Expand Up @@ -99,48 +100,17 @@ eventsRouter.delete("/:EVENTID", async (req, res, next) => {
eventsRouter.post("/check-in", async (req, res, next) => {
try {
const { eventId, userId } = req.body;

// Check if the event and attendee exist
const event = await Database.EVENTS.findOne({ eventId });
const attendee = await Database.ATTENDEES.findOne({ userId });
console.log(event);
console.log(attendee);

if (!event || !attendee) {
const result = await checkInUser(eventId, userId);
if (result.success) {
return res
.status(StatusCodes.OK)
.json({ message: "Check-in successful" });
}

Check failure on line 108 in src/services/events/events-router.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎········`
else {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "Event or Attendee not found" });
}

const eventAttendance = await Database.EVENTS_ATT.findOne({ eventId });
if (!eventAttendance) {
const newEventAttendance = await Database.EVENTS_ATT.create({
eventId: eventId,
attendees: [userId],
});
await newEventAttendance.save();
} else {
eventAttendance.attendees.push(userId);
await eventAttendance.save();
.json({ error: result.message });
}

const attendeeAttendance = await Database.ATTENDEES_ATT.findOne({
userId,
});
if (!attendeeAttendance) {
const newAttendeeAttendance = new Database.ATTENDEES_ATT({
userId: userId,
eventsAttended: [eventId],
});
await newAttendeeAttendance.save();
} else {
attendeeAttendance.eventsAttended.push(eventId);
await attendeeAttendance.save();
}

return res
.status(StatusCodes.OK)
.json({ message: "Check-in successful" });
} catch (error) {
next(error);
}
Expand Down
41 changes: 41 additions & 0 deletions src/services/events/events-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Database } from "../../database";

export async function checkInUser(eventId: string, userId: string) {
// Check if the event and attendee exist
const event = await Database.EVENTS.findOne({ eventId });
const attendee = await Database.ATTENDEES.findOne({ userId });

if (!event || !attendee) {
return { success: false, message: "Event or Attendee not found" };
}

// Check or create event attendance record
let eventAttendance = await Database.EVENTS_ATT.findOne({ eventId });
if (!eventAttendance) {
eventAttendance = await Database.EVENTS_ATT.create({
eventId: eventId,
attendees: [userId],
});
}

Check failure on line 19 in src/services/events/events-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎····`
else {
if (!eventAttendance.attendees.includes(userId)) {
eventAttendance.attendees.push(userId);
}
}
await eventAttendance.save();

// Check or create attendee attendance record
let attendeeAttendance = await Database.ATTENDEES_ATT.findOne({ userId });
if (!attendeeAttendance) {
attendeeAttendance = new Database.ATTENDEES_ATT({
userId: userId,
eventsAttended: [eventId],
});
} else {
if (!attendeeAttendance.eventsAttended.includes(eventId)) {
attendeeAttendance.eventsAttended.push(eventId);
}
}
await attendeeAttendance.save();
return { success: true };
}

0 comments on commit bb4d6cd

Please sign in to comment.