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 all 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
10,996 changes: 10,996 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "jest --config=testing/jest.config.ts"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.21",
"@types/express-rate-limit": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import morgan from "morgan";
import bodyParser from "body-parser";
import errorHandler from "./middleware/error-handler";

import attendeeRouter from "./services/attendees/attendee-router";
import attendeeRouter from "./services/attendee/attendee-router";
import authRouter from "./services/auth/auth-router";
import eventsRouter from "./services/events/events-router";
import notificationsRouter from "./services/notifications/notifications-router";
Expand Down
24 changes: 21 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import mongoose, { Schema, Document } from "mongoose";
import {
AttendeeSchema,
AttendeeValidator,
} from "./services/attendees/attendee-schema";
import { RoleValidator, RoleSchema } from "./services/auth/auth-schema";
} from "./services/attendee/attendee-schema";
import {
AttendeeAttendanceSchema,
AttendeeAttendanceValidator,
} from "./services/attendee/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_ATTENDANCE: initializeModel(
"events_attendance",
EventAttendanceSchema,
EventAttendanceValidator
),
ATTENDEE: initializeModel("attendee", AttendeeSchema, AttendeeValidator),
ATTENDEE_ATTENDANCE: initializeModel(
"attendee_attendance",
AttendeeAttendanceSchema,
AttendeeAttendanceValidator
),
SUBSCRIPTIONS: initializeModel(
"subscriptions",
SubscriptionSchema,
SubscriptionSchemaValidator
),
ATTENDEES: initializeModel("attendees", AttendeeSchema, AttendeeValidator),
REGISTRATION: initializeModel(
"registration",
RegistrationSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Database } from "../../database";
import RoleChecker from "../../middleware/role-checker";
import { Role } from "../auth/auth-models";
import dotenv from "dotenv";
import { generateQrHash } from "./attendees-utils";
import { generateQrHash } from "./attendee-utils";

dotenv.config();

Expand All @@ -21,15 +21,15 @@ attendeeRouter.post(
const { eventId } = EventIdValidator.parse(req.params);

try {
const attendee = await Database.ATTENDEES.findOne({ userId });
const attendee = await Database.ATTENDEE.findOne({ userId });

if (!attendee) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

await Database.ATTENDEES.updateOne(
await Database.ATTENDEE.updateOne(
{ userId: userId },
{ $addToSet: { favorites: eventId } }
);
Expand All @@ -51,15 +51,15 @@ attendeeRouter.delete(
const { eventId } = EventIdValidator.parse(req.params);

try {
const attendee = await Database.ATTENDEES.findOne({ userId });
const attendee = await Database.ATTENDEE.findOne({ userId });

if (!attendee) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

await Database.ATTENDEES.updateOne(
await Database.ATTENDEE.updateOne(
{ userId: userId },
{ $pull: { favorites: eventId } }
);
Expand All @@ -80,7 +80,7 @@ attendeeRouter.get(
const userId = payload.userId;

try {
const attendee = await Database.ATTENDEES.findOne({ userId });
const attendee = await Database.ATTENDEE.findOne({ userId });

if (!attendee) {
return res
Expand All @@ -99,7 +99,7 @@ attendeeRouter.get(
attendeeRouter.post("/", async (req, res, next) => {
try {
const attendeeData = AttendeeValidator.parse(req.body);
const attendee = new Database.ATTENDEES(attendeeData);
const attendee = new Database.ATTENDEE(attendeeData);
await attendee.save();

return res.status(StatusCodes.CREATED).json(attendeeData);
Expand Down Expand Up @@ -135,7 +135,7 @@ attendeeRouter.get(
const userId = payload.userId;

// Check if the user exists in the database
const user = await Database.ATTENDEES.findOne({ userId });
const user = await Database.ATTENDEE.findOne({ userId });

if (!user) {
return res
Expand Down
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 Down Expand Up @@ -30,20 +30,18 @@ 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", default: [] },
],
events: [{ type: String, ref: "Event", default: [] }],
dietaryRestrictions: { type: [String], required: true },
allergies: { type: [String], required: true },
hasCheckedIn: { type: Boolean, default: false },
points: { type: Number, default: 0 },
foodWave: { type: Number, default: 0 },
hasPriority: {
type: new mongoose.Schema(
type: new Schema(
{
dayOne: { type: Boolean, default: false },
dayTwo: { type: Boolean, default: false },
Expand All @@ -58,8 +56,19 @@ const AttendeeSchema = new mongoose.Schema({
favorites: [{ type: String }],
});

const EventIdValidator = z.object({
eventId: z.string().uuid(),
export const AttendeeAttendanceSchema = new Schema({
userId: {
type: String,
ref: "Attendee",
required: true,
},
eventsAttended: [{ type: String, ref: "Event", required: true }],
});

export { AttendeeSchema, AttendeeValidator, EventIdValidator };
export const AttendeeAttendanceValidator = z.object({
userId: z.string(),
eventsAttended: z.array(z.string()),
});
export const EventIdValidator = z.object({
eventId: z.string().uuid(),
});
2 changes: 2 additions & 0 deletions src/services/auth/auth-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export const JwtPayloadValidator = z.object({
displayName: z.string(),
roles: Role.array(),
});

export type JwtPayloadType = z.infer<typeof JwtPayloadValidator>;
14 changes: 13 additions & 1 deletion src/services/auth/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import { Config } from "../../config";
import { Database } from "../../database";
import { Role } from "./auth-models";
import { JwtPayloadType, Role } from "./auth-models";

export function createGoogleStrategy(device: string) {
return new GoogleStrategy(
Expand Down Expand Up @@ -47,3 +47,15 @@ export async function getJwtPayloadFromDatabase(userId: string) {

return payload;
}

export function isUser(payload?: JwtPayloadType) {
return payload?.roles.includes(Role.Enum.USER);
}

export function isStaff(payload?: JwtPayloadType) {
return payload?.roles.includes(Role.Enum.STAFF);
}

export function isAdmin(payload?: JwtPayloadType) {
return payload?.roles.includes(Role.Enum.ADMIN);
}
Loading