Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put events-router #69

Merged
merged 19 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ app.use("/", morgan("dev"));
app.use("/", bodyParser.json());

// API routes
app.use("/attendee", attendeeRouter);
riyap marked this conversation as resolved.
Show resolved Hide resolved
app.use("/attendees", attendeeRouter);
app.use("/auth", authRouter);
app.use("/events", eventsRouter);
app.use("/notifications", notificationsRouter);
Expand Down
22 changes: 20 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import {
AttendeeSchema,
AttendeeValidator,
} from "./services/attendees/attendee-schema";
import { RoleValidator, RoleSchema } from "./services/auth/auth-schema";
import {
AttendeesAttendanceSchema,
AttendeesAttendanceValidator,
} from "./services/attendees/attendee-schema";
import {
EventSchema,
privateEventValidator,
} from "./services/events/events-schema";
import {
EventAttendanceSchema,
EventAttendanceValidator,
} from "./services/events/events-schema";
import { RoleValidator, RoleSchema } from "./services/auth/auth-schema";
import {
RegistrationSchema,
RegistrationValidator,
Expand Down Expand Up @@ -55,12 +63,22 @@ function initializeModel(
export const Database = {
ROLES: initializeModel("roles", RoleSchema, RoleValidator),
EVENTS: initializeModel("events", EventSchema, privateEventValidator),
EVENTS_ATT: initializeModel(
riyap marked this conversation as resolved.
Show resolved Hide resolved
"events_att",
EventAttendanceSchema,
EventAttendanceValidator
),
ATTENDEES: initializeModel("attendees", AttendeeSchema, AttendeeValidator),
ATTENDEES_ATT: initializeModel(
"attendees_att",
AttendeesAttendanceSchema,
AttendeesAttendanceValidator
),
SUBSCRIPTIONS: initializeModel(
"subscriptions",
SubscriptionSchema,
SubscriptionSchemaValidator
),
ATTENDEES: initializeModel("attendees", AttendeeSchema, AttendeeValidator),
REGISTRATION: initializeModel(
"registration",
RegistrationSchema,
Expand Down
24 changes: 18 additions & 6 deletions src/services/attendees/attendee-schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import mongoose from "mongoose";
import { Schema } from "mongoose";
import { z } from "zod";

// Zod schema for attendee
const AttendeeValidator = z.object({
export const AttendeeValidator = z.object({
userId: z.string(),
name: z.string(),
email: z.string().email(),
Expand All @@ -21,17 +21,17 @@ const AttendeeValidator = z.object({
});

// Mongoose schema for attendee
const AttendeeSchema = new mongoose.Schema({
export const AttendeeSchema = new Schema({
userId: { type: String, required: true, unique: true },
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
events: [{ type: mongoose.Schema.Types.ObjectId, ref: "Event" }],
events: [{ type: Schema.Types.ObjectId, ref: "Event" }],
dietaryRestrictions: { type: [String], required: true },
allergies: { type: [String], required: true },
hasCheckedIn: { type: Boolean, default: false },
points: { type: Number, default: 0 },
hasPriority: {
type: new mongoose.Schema(
type: new Schema(
{
dayOne: { type: Boolean, default: false },
dayTwo: { type: Boolean, default: false },
Expand All @@ -45,4 +45,16 @@ const AttendeeSchema = new mongoose.Schema({
},
});

export { AttendeeSchema, AttendeeValidator };
export const AttendeesAttendanceSchema = new Schema({
riyap marked this conversation as resolved.
Show resolved Hide resolved
userId: {
type: String,
ref: "Attendee",
required: true,
},
eventsAttended: [{ type: String, ref: "Event", required: true }],
});

export const AttendeesAttendanceValidator = z.object({
userId: z.string(),
eventsAttended: z.array(z.string()),
});
51 changes: 43 additions & 8 deletions src/services/events/events-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { publicEventValidator } from "./events-schema";
import { Database } from "../../database";
import { checkInUser } from "./events-utils";
AydanPirani marked this conversation as resolved.
Show resolved Hide resolved
// import {mongoose} from "mongoose";

const eventsRouter = Router();

Expand All @@ -26,6 +28,21 @@ eventsRouter.get("/currentOrNext", async (req, res, next) => {
}
});

eventsRouter.get("/:EVENTID", async (req, res, next) => {
riyap marked this conversation as resolved.
Show resolved Hide resolved
const eventId = req.params.EVENTID;
try {
const event = await Database.EVENTS.findOne({ eventId: eventId });
if (!event) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "DoesNotExist" });
}
return res.status(StatusCodes.OK).json(event.toObject());
riyap marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
next(error);
}
});

eventsRouter.post("/", async (req, res, next) => {
try {
const validatedData = publicEventValidator.parse(req.body);
Expand All @@ -37,22 +54,21 @@ eventsRouter.post("/", async (req, res, next) => {
}
});

eventsRouter.get("/:EVENTID", async (req, res, next) => {
eventsRouter.put("/:EVENTID", async (req, res, next) => {
AydanPirani marked this conversation as resolved.
Show resolved Hide resolved
const eventId = req.params.EVENTID;
try {
const unfiltered_event = await Database.EVENTS.findOne({
eventId: eventId,
});
const validatedData = publicEventValidator.parse(req.body);
riyap marked this conversation as resolved.
Show resolved Hide resolved
const event = await Database.EVENTS.findOne({ eventId: eventId });

if (!unfiltered_event) {
if (!event) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "DoesNotExist" });
}

const filtered_event = publicEventValidator.parse(unfiltered_event);

return res.status(StatusCodes.OK).json(filtered_event);
Object.assign(event, validatedData);
await event.save();
riyap marked this conversation as resolved.
Show resolved Hide resolved
return res.sendStatus(StatusCodes.OK);
} catch (error) {
next(error);
}
Expand All @@ -75,6 +91,7 @@ eventsRouter.get("/", async (req, res, next) => {
eventsRouter.delete("/:EVENTID", async (req, res, next) => {
const eventId = req.params.EVENTID;
try {
// const objectId = mongoose.Types.ObjectId(eventId)
await Database.EVENTS.findOneAndDelete({ eventId: eventId });

return res.sendStatus(StatusCodes.NO_CONTENT);
Expand All @@ -83,4 +100,22 @@ eventsRouter.delete("/:EVENTID", async (req, res, next) => {
}
});

eventsRouter.post("/check-in", async (req, res, next) => {
try {
const { eventId, userId } = req.body;
const result = await checkInUser(eventId, userId);
if (result.success) {
return res
.status(StatusCodes.OK)
.json({ message: "Check-in successful" });
} else {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: result.message });
}
} catch (error) {
next(error);
}
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i left this for testing, like the qr function; can get rid of later

export default eventsRouter;
22 changes: 21 additions & 1 deletion src/services/events/events-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { v4 as uuidv4 } from "uuid";
export const EventType = z.enum(["A", "B", "C"]);

export const publicEventValidator = z.object({
eventId: z.coerce.string(),
eventId: z.coerce.string().optional(),
name: z.string(),
startTime: z.coerce.date(),
endTime: z.coerce.date(),
Expand Down Expand Up @@ -70,3 +70,23 @@ export const EventSchema = new Schema({
enum: EventType.Values,
},
});

export const EventAttendanceSchema = new Schema({
eventId: {
type: String,
ref: "Event",
required: true,
},
attendees: [
{
type: String,
ref: "Attendee",
required: true,
},
],
});

export const EventAttendanceValidator = z.object({
eventId: z.string(),
attendees: z.array(z.string()),
});
40 changes: 40 additions & 0 deletions src/services/events/events-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 });
riyap marked this conversation as resolved.
Show resolved Hide resolved

if (!event || !attendee) {
return { success: false, message: "Event or Attendee not found" };
riyap marked this conversation as resolved.
Show resolved Hide resolved
}

// 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],
});
riyap marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (!eventAttendance.attendees.includes(userId)) {
eventAttendance.attendees.push(userId);
}
}
await eventAttendance.save();
riyap marked this conversation as resolved.
Show resolved Hide resolved

// 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);
}
}
riyap marked this conversation as resolved.
Show resolved Hide resolved
await attendeeAttendance.save();
return { success: true };
}