Skip to content

Commit

Permalink
BACKEND-15: Add user to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnie4k committed Nov 8, 2024
1 parent e04a4da commit 18775be
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/announcements/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Types } from "mongoose";
import { Announcement } from "./models";
import { AnnouncementCreationParams, AnnouncementUpdateParams } from "./types";
import {
AnnouncementCreationParams,
AnnouncementCreationParamsCreator,
AnnouncementUpdateParams,
} from "./types";
import { AnnouncementService } from "./services";
import {
Body,
Expand All @@ -17,16 +21,19 @@ import {
} from "tsoa";
import { exampleAnnouncement, exampleAnnouncements } from "./examples";
import { AppService } from "../apps/services";
import { UserService } from "../users/services";

@Route("announcements")
export class AnnouncementController extends Controller {
private announcementService;
private appService;
private userService;

constructor() {
super();
this.announcementService = new AnnouncementService();
this.appService = new AppService();
this.userService = new UserService();
}

/**
Expand All @@ -52,13 +59,16 @@ export class AnnouncementController extends Controller {
@Example(exampleAnnouncement)
@SuccessResponse(201, "Created")
public async createAnnouncement(
@Body() req: AnnouncementCreationParams
@Body() req: AnnouncementCreationParamsCreator
): Promise<Announcement> {
this.setStatus(201);

// Validate app slugs
await this.appService.validateAppSlugs(req.apps);
return this.announcementService.insertAnnouncement(req);

// Fetch email
const user = await this.userService.getUserByEmail(req.creator);
return this.announcementService.insertAnnouncement(req, user._id);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/announcements/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,37 @@ export class AnnouncementService {
*/
public getAnnouncements = async (isDebug: boolean) => {
if (isDebug) {
return await AnnouncementModel.find({ isDebug: true });
return await AnnouncementModel.find({ isDebug: true }).populate(
"creator"
);
}
return await AnnouncementModel.find({ isDebug: false });
return await AnnouncementModel.find({ isDebug: false }).populate("creator");
};

/**
* Insert an announcement into the database.
*
* @param announcementData The data for the new announcement.
* @param creatorId The ID of the user creating this announcement.
* @throws InvalidArgumentError when invalid inputs are supplied.
* @returns A promise resolving to the new announcement document.
*/
public insertAnnouncement = async (
announcementData: AnnouncementCreationParams
announcementData: AnnouncementCreationParams,
creatorId: Types.ObjectId
) => {
// Validate dates
if (!isDateBefore(announcementData.startDate, announcementData.endDate)) {
throw new InvalidArgumentError("Start date must be before end date");
}

return await AnnouncementModel.create(announcementData);
const announcement = await AnnouncementModel.create({
...announcementData,
creator: creatorId,
});
await announcement.populate("creator");

return announcement;
};

/**
Expand Down Expand Up @@ -87,6 +97,7 @@ export class AnnouncementService {
if (!updatedAnnouncement) {
throw new InvalidArgumentError("Invalid announcementId supplied");
}
await updatedAnnouncement.populate("creator");
return updatedAnnouncement;
};

Expand All @@ -104,6 +115,7 @@ export class AnnouncementService {
if (!deletedAnnouncement) {
throw new InvalidArgumentError("Invalid announcementId supplied");
}
await deletedAnnouncement.populate("creator");
return deletedAnnouncement;
};
}
13 changes: 13 additions & 0 deletions src/announcements/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ export type AnnouncementCreationParams = {
title: string;
};

export type AnnouncementCreationParamsCreator = {
apps: string[];
body: string;
creator: string;
endDate: Date;
imageUrl: string;
isDebug: boolean;
link: string;
startDate: Date;
title: string;
};

export type AnnouncementUpdateParams = {
apps?: string[];
body?: string;
creator?: string;
endDate?: Date;
imageUrl?: string;
isDebug?: boolean;
Expand Down
10 changes: 10 additions & 0 deletions src/middleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export const authMiddleware = async (
name: payload.name,
};
}

// Add user information to request body for creating announcement
if (req.path === "/announcements" && req.method === "POST") {
req.body.creator = payload.email;
}

// Add user information to request body for creating a user
if (req.path === "/users" && req.method === "POST") {
req.body.imageUrl = payload.picture;
}
} catch (err) {
console.error(err);
if (err instanceof Error) {
Expand Down
1 change: 1 addition & 0 deletions src/users/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type UserCreationParams = {
email: string;
imageUrl?: string;
isAdmin: boolean;
};

Expand Down
21 changes: 18 additions & 3 deletions tests/announcements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { faker } from "@faker-js/faker";
import { AnnouncementModel } from "../src/announcements/models";
import { AnnouncementService } from "../src/announcements/services";
import AnnouncementFactory from "./mocks/AnnouncementFactory";
import UserFactory from "./mocks/UserFactory";
import { UserModel } from "../src/users/models";

describe("getAnnouncements", () => {
let announcementService: AnnouncementService;
Expand Down Expand Up @@ -95,15 +97,19 @@ describe("insertAnnouncements", () => {
// given
const mocks = await AnnouncementFactory.create(1);
const mockAnnouncement = mocks[0];
const mockUser = (await UserFactory.create(1))[0];
const mockUserId = (await UserModel.create(mockUser))._id;

// when
const insertResponse = await announcementService.insertAnnouncement(
mockAnnouncement
mockAnnouncement,
mockUserId
);

// then
expect(insertResponse.apps).toStrictEqual(mockAnnouncement.apps);
expect(insertResponse.body).toStrictEqual(mockAnnouncement.body);
expect(insertResponse.creator?.id).toStrictEqual(mockUserId.toString());
expect(insertResponse.endDate).toStrictEqual(mockAnnouncement.endDate);
expect(insertResponse.imageUrl).toStrictEqual(mockAnnouncement.imageUrl);
expect(insertResponse.isDebug).toStrictEqual(mockAnnouncement.isDebug);
Expand All @@ -117,15 +123,19 @@ describe("insertAnnouncements", () => {
const mocks = await AnnouncementFactory.create(1);
const mockAnnouncement = mocks[0];
mockAnnouncement.isDebug = true;
const mockUser = (await UserFactory.create(1))[0];
const mockUserId = (await UserModel.create(mockUser))._id;

// when
const insertResponse = await announcementService.insertAnnouncement(
mockAnnouncement
mockAnnouncement,
mockUserId
);

// then
expect(insertResponse.apps).toStrictEqual(mockAnnouncement.apps);
expect(insertResponse.body).toStrictEqual(mockAnnouncement.body);
expect(insertResponse.creator?.id).toStrictEqual(mockUserId.toString());
expect(insertResponse.endDate).toStrictEqual(mockAnnouncement.endDate);
expect(insertResponse.imageUrl).toStrictEqual(mockAnnouncement.imageUrl);
expect(insertResponse.isDebug).toStrictEqual(mockAnnouncement.isDebug);
Expand All @@ -142,10 +152,15 @@ describe("insertAnnouncements", () => {
from: mockAnnouncement.endDate,
to: new Date().getTime() + 1000 * 60 * 60 * 24 * 365 * 1000,
});
const mockUser = (await UserFactory.create(1))[0];
const mockUserId = (await UserModel.create(mockUser))._id;

// when
const insertRequest = async () => {
await announcementService.insertAnnouncement(mockAnnouncement);
await announcementService.insertAnnouncement(
mockAnnouncement,
mockUserId
);
};

// then
Expand Down

0 comments on commit 18775be

Please sign in to comment.