Skip to content

Commit

Permalink
Fix follow not creating followers (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Feb 15, 2024
1 parent 47532ac commit 63184cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/services/user/user-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,22 @@ describe("PUT /user/follow/", () => {
TESTER_ATTENDEE_FOLLOWING.following.pop();
});

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

const response = await putAsAttendee(`/user/follow/`)
.send({ eventId: TESTER_EVENT_FOLLOWING.eventId })
.expect(StatusCode.ClientErrorNotFound);
.expect(StatusCode.SuccessOK);

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

const updatedEvents = await Models.AttendeeFollowing.findOne({ userId: TESTER_ATTENDEE_FOLLOWING.userId });
expect(updatedEvents?.following).toContain(TESTER_EVENT_FOLLOWING.eventId);

const updatedUsers = await Models.EventFollowers.findOne({ eventId: TESTER_EVENT_FOLLOWING.eventId });
expect(updatedUsers?.followers).toContain(TESTER_ATTENDEE_FOLLOWING.userId);
});

it("works for an attendee user", async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/services/user/user-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ userRouter.put("/follow/", strongJwtVerification, async (req: Request, res: Resp
const payload: JwtPayload = res.locals.payload as JwtPayload;
const eventId: string | undefined = req.body.eventId;

if (!eventId) {
return next(new RouterError(StatusCode.ClientErrorBadRequest, "InvalidEventId"));
}

const eventExists: EventFollowers | null = await Models.EventFollowers.findOneAndUpdate(
{ eventId: eventId },
{ $addToSet: { followers: payload.id } },
{ new: true },
{ new: true, upsert: true },
);

if (!eventExists) {
Expand Down

0 comments on commit 63184cb

Please sign in to comment.