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

event-favorites #125

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
13 changes: 13 additions & 0 deletions src/database/attendee-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ export class AttendeeProfile {
@prop({ required: true })
public points: number;
}

export class AttendeeFollowing {
@prop({ required: true })
public userId: string;

@prop({
required: true,
type: () => {
return String;
},
})
public events: string[];
}
13 changes: 13 additions & 0 deletions src/database/event-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,16 @@ export class EventAttendance {
})
public attendees: string[];
}

export class EventFollowing {
npunati27 marked this conversation as resolved.
Show resolved Hide resolved
@prop({ required: true })
public eventId: string;

@prop({
required: true,
type: () => {
return String;
},
})
public followers: string[];
}
10 changes: 8 additions & 2 deletions src/database/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { getModelForClass } from "@typegoose/typegoose";
import { Database, generateConfig } from "../database.js";

import { AuthInfo } from "./auth-db.js";
import { AttendeeMetadata, AttendeeProfile } from "./attendee-db.js";
import { AttendeeFollowing, AttendeeMetadata, AttendeeProfile } from "./attendee-db.js";
import { AdmissionDecision } from "./admission-db.js";
import { EventAttendance, EventMetadata, PublicEvent, StaffEvent } from "./event-db.js";
import { EventAttendance, EventMetadata, PublicEvent, StaffEvent, EventFollowing } from "./event-db.js";
import { NewsletterSubscription } from "./newsletter-db.js";
import { RegistrationApplication, RegistrationInfo } from "./registration-db.js";
import { UserAttendance, UserInfo } from "./user-db.js";
Expand All @@ -16,6 +16,7 @@ import { AnyParamConstructor } from "@typegoose/typegoose/lib/types.js";
enum AttendeeCollection {
METADATA = "metadata",
PROFILE = "profile",
FOLLOWING = "following",
}

enum AuthCollection {
Expand All @@ -31,6 +32,7 @@ enum EventCollection {
ATTENDANCE = "attendance",
STAFF_EVENTS = "staffevents",
PUBLIC_EVENTS = "publicevents",
FOLLOWERS = "followers",
}

enum NewsletterCollection {
Expand Down Expand Up @@ -58,6 +60,7 @@ export default class Models {
// Attendee
static AttendeeMetadata: mongoose.Model<AttendeeMetadata> = undefined!;
static AttendeeProfile: mongoose.Model<AttendeeProfile> = undefined!;
static AttendeeFollowing: mongoose.Model<AttendeeFollowing> = undefined!;
// Auth
static AuthInfo: mongoose.Model<AuthInfo> = undefined!;
// Admission
Expand All @@ -67,6 +70,7 @@ export default class Models {
static PublicEvent: mongoose.Model<PublicEvent> = undefined!;
static EventMetadata: mongoose.Model<EventMetadata> = undefined!;
static EventAttendance: mongoose.Model<EventAttendance> = undefined!;
static EventFollowing: mongoose.Model<EventFollowing> = undefined!;
npunati27 marked this conversation as resolved.
Show resolved Hide resolved
// Newsletter
static NewsletterSubscription: mongoose.Model<NewsletterSubscription> = undefined!;
// Registration
Expand All @@ -79,12 +83,14 @@ export default class Models {
static initialize(): void {
this.AttendeeMetadata = getModel(AttendeeMetadata, Database.ATTENDEE, AttendeeCollection.METADATA);
this.AttendeeProfile = getModel(AttendeeProfile, Database.ATTENDEE, AttendeeCollection.PROFILE);
this.AttendeeFollowing = getModel(AttendeeFollowing, Database.ATTENDEE, AttendeeCollection.FOLLOWING);
this.AuthInfo = getModel(AuthInfo, Database.AUTH, AuthCollection.INFO);
this.AdmissionDecision = getModel(AdmissionDecision, Database.ADMISSION, AdmissionCollection.DECISION);
this.StaffEvent = getModel(StaffEvent, Database.EVENT, EventCollection.STAFF_EVENTS);
this.PublicEvent = getModel(PublicEvent, Database.EVENT, EventCollection.PUBLIC_EVENTS);
this.EventMetadata = getModel(EventMetadata, Database.EVENT, EventCollection.METADATA);
this.EventAttendance = getModel(EventAttendance, Database.EVENT, EventCollection.ATTENDANCE);
this.EventFollowing = getModel(EventFollowing, Database.EVENT, EventCollection.FOLLOWERS);
this.NewsletterSubscription = getModel(NewsletterSubscription, Database.NEWSLETTER, NewsletterCollection.SUBSCRIPTIONS);
this.RegistrationInfo = getModel(RegistrationInfo, Database.REGISTRATION, RegistrationCollection.INFO);
this.RegistrationApplication = getModel(
Expand Down
51 changes: 51 additions & 0 deletions src/services/event/event-router.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it, beforeEach } from "@jest/globals";
import { EventFollowing } from "database/event-db.js";
import { AttendeeFollowing } from "database/attendee-db.js";
import Models from "../../database/models.js";
import { StatusCode } from "status-code-enum";
import { TESTER, getAsAttendee, getAsStaff } from "../../testTools.js";

const TESTER_EVENT_FOLLOWING = {
eventId: "other-event",
followers: ["user5", "user8"],
} satisfies EventFollowing;

const TESTER_ATTENDEE_FOLLOWING = {
userId: TESTER.id,
events: ["event3", "event9"],
} satisfies AttendeeFollowing;

// Before each test, initialize database with tester & other users
beforeEach(async () => {
Models.initialize();
await Models.EventFollowing.create(TESTER_EVENT_FOLLOWING);
await Models.AttendeeFollowing.create(TESTER_ATTENDEE_FOLLOWING);
});

describe("GET /event/followers/:EVENTID", () => {
it("gives an forbidden error for a non-staff user", async () => {
const response = await getAsAttendee(`/event/followers/${TESTER_EVENT_FOLLOWING.eventId}/`).expect(
StatusCode.ClientErrorForbidden,
);

expect(JSON.parse(response.text)).toHaveProperty("error", "Forbidden");
});

it("gives an not found error for a non-existent event", async () => {
await Models.EventFollowing.deleteOne({
eventId: TESTER_EVENT_FOLLOWING.eventId,
});

const response = await getAsStaff(`/event/followers/${TESTER_EVENT_FOLLOWING.eventId}/`).expect(
StatusCode.ClientErrorNotFound,
);

expect(JSON.parse(response.text)).toHaveProperty("error", "EventNotFound");
});

it("works for a staff user", async () => {
const response = await getAsStaff(`/event/followers/${TESTER_EVENT_FOLLOWING.eventId}/`).expect(StatusCode.SuccessOK);

expect(JSON.parse(response.text)).toEqual(TESTER_EVENT_FOLLOWING.followers);
});
});
36 changes: 35 additions & 1 deletion src/services/event/event-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from "./event-formats.js";
import { FilteredEventView } from "./event-models.js";

import { EventMetadata, PublicEvent, StaffEvent } from "../../database/event-db.js";
import { EventFollowing, EventMetadata, PublicEvent, StaffEvent } from "../../database/event-db.js";
import Models from "../../database/models.js";
import { ObjectId } from "mongodb";
import { StatusCode } from "status-code-enum";
Expand Down Expand Up @@ -635,6 +635,40 @@ eventsRouter.put("/", strongJwtVerification, async (req: Request, res: Response,
}
});

/**
* @api {get} /event/favorites/:EVENTID/ GET /event/favorites/:EVENTID/
* @apiGroup Event
* @apiDescription Get users that favorite an event for a specific event by its unique ID.
*
* @apiHeader {String} Authorization User's JWT Token with staff permissions.
*
* @apiParam {String} EVENTID The unique identifier of the event.
*
* @apiSuccess (200: Success) {JSON} followers The followers of an event.
* @apiSuccessExample Example Success Response
* HTTP/1.1 200 OK
* [
* "user1",
* "user2",
* "user3"
* ]
* @apiUse strongVerifyErrors
* @apiError (400: Bad Request) {String} EventNotFound Event with the given ID not found.
* @apiError (403: Forbidden) {String} InvalidPermission User does not have staff permissions.
*/
eventsRouter.get("/followers/:EVENTID", strongJwtVerification, async (req: Request, res: Response, next: NextFunction) => {
const payload: JwtPayload = res.locals.payload as JwtPayload;
if (!hasStaffPerms(payload)) {
return next(new RouterError(StatusCode.ClientErrorForbidden, "Forbidden"));
}
const eventId: string | undefined = req.params.EVENTID;
const followers: EventFollowing | null = await Models.EventFollowing.findOne({ eventId: eventId });
if (!followers) {
return next(new RouterError(StatusCode.ClientErrorNotFound, "EventNotFound"));
}
return res.status(StatusCode.SuccessOK).send(followers.followers);
});

// Prototype error handler
eventsRouter.use((err: Error, req: Request, res: Response) => {
if (!err) {
Expand Down
Loading
Loading