Skip to content

Commit

Permalink
#3074-created get unread announcements endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 18, 2024
1 parent bc93cf1 commit 2f337d9
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/backend/src/controllers/users.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,16 @@ export default class UsersController {
next(error);
}
}

static async getUserUnreadAnnouncements(req: Request, res: Response, next: NextFunction) {
try {
const { userId } = req.params;
const { organization } = req;

const unreadAnnouncements = await UsersService.getUserUnreadAnnouncements(userId, organization);
res.status(200).json(unreadAnnouncements);
} catch (error: unknown) {
next(error);
}
}
}
12 changes: 12 additions & 0 deletions src/backend/src/prisma-query-args/announcements.query.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Prisma } from '@prisma/client';
import { getUserQueryArgs } from './user.query-args';

export type AnnouncementQueryArgs = ReturnType<typeof getAnnouncementQueryArgs>;

export const getAnnouncementQueryArgs = (organizationId: string) =>
Prisma.validator<Prisma.AnnouncementDefaultArgs>()({
include: {
usersReceived: getUserQueryArgs(organizationId),
userCreated: getUserQueryArgs(organizationId)
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ ALTER TABLE "Project" ADD COLUMN "organizationId" TEXT;
CREATE TABLE "Announcement" (
"announcementId" TEXT NOT NULL,
"text" TEXT NOT NULL,
"dateCrated" TIMESTAMP(3) NOT NULL,
"dateCreated" TIMESTAMP(3) NOT NULL,
"userCreatedId" TEXT NOT NULL,
"slackEventId" TEXT NOT NULL,
"slackChannelName" TEXT NOT NULL,

CONSTRAINT "Announcement_pkey" PRIMARY KEY ("announcementId")
);
Expand Down
16 changes: 9 additions & 7 deletions src/backend/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ model User {
deletedFrequentlyAskedQuestions FrequentlyAskedQuestion[] @relation(name: "frequentlyAskedQuestionDeleter")
createdMilestones Milestone[] @relation(name: "milestoneCreator")
deletedMilestones Milestone[] @relation(name: "milestoneDeleter")
receivedAnnouncements Announcement[] @relation(name: "receivedAnnouncements")
unreadAnnouncements Announcement[] @relation(name: "receivedAnnouncements")
createdAnnouncements Announcement[] @relation(name: "createdAnnouncements")
unreadNotifications Notification[] @relation(name: "userNotifications")
}
Expand Down Expand Up @@ -932,12 +932,14 @@ model Milestone {
}

model Announcement {
announcementId String @id @default(uuid())
text String
usersReceived User[] @relation("receivedAnnouncements")
dateCrated DateTime
userCreatedId String
userCreated User @relation("createdAnnouncements", fields: [userCreatedId], references: [userId])
announcementId String @id @default(uuid())
text String
usersReceived User[] @relation("receivedAnnouncements")
dateCreated DateTime
userCreatedId String
userCreated User @relation("createdAnnouncements", fields: [userCreatedId], references: [userId])
slackEventId String
slackChannelName String
}

model Notification {
Expand Down
1 change: 1 addition & 0 deletions src/backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ userRouter.post(
UsersController.getManyUserTasks
);
userRouter.get('/:userId/notifications', UsersController.getUserUnreadNotifications);
userRouter.get('/:userId/announcements', UsersController.getUserUnreadAnnouncements);
userRouter.post(
'/:userId/notifications/remove',
nonEmptyString(body('notificationId')),
Expand Down
18 changes: 18 additions & 0 deletions src/backend/src/services/users.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { getTaskQueryArgs } from '../prisma-query-args/tasks.query-args';
import taskTransformer from '../transformers/tasks.transformer';
import { getNotificationQueryArgs } from '../prisma-query-args/notifications.query-args';
import notificationTransformer from '../transformers/notifications.transformer';
import { getAnnouncementQueryArgs } from '../prisma-query-args/announcements.query.args';
import announcementTransformer from '../transformers/announcements.transformer';

export default class UsersService {
/**
Expand Down Expand Up @@ -585,6 +587,22 @@ export default class UsersService {
return requestedUser.unreadNotifications.map(notificationTransformer);
}

/**
* Gets all of a user's unread announcements
* @param userId id of user to get unread announcements from
* @param organization the user's orgainzation
* @returns the unread announcements of the user
*/
static async getUserUnreadAnnouncements(userId: string, organization: Organization) {
const requestedUser = await prisma.user.findUnique({
where: { userId },
include: { unreadAnnouncements: getAnnouncementQueryArgs(organization.organizationId) }
});
if (!requestedUser) throw new NotFoundException('User', userId);

return requestedUser.unreadAnnouncements.map(announcementTransformer);
}

/**
* Removes a notification from the user's unread notifications
* @param userId id of the user to remove notification from
Expand Down
17 changes: 17 additions & 0 deletions src/backend/src/transformers/announcements.transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Prisma } from '@prisma/client';
import { AnnouncementQueryArgs } from '../prisma-query-args/announcements.query.args';
import { Announcement } from 'shared';
import { userTransformer } from './user.transformer';

const announcementTransformer = (announcement: Prisma.AnnouncementGetPayload<AnnouncementQueryArgs>): Announcement => {
return {
announcementId: announcement.announcementId,
text: announcement.text,
dateCreated: announcement.dateCreated,
userCreated: userTransformer(announcement.userCreated),
slackEventId: announcement.slackEventId,
slackChannelName: announcement.slackChannelName
};
};

export default announcementTransformer;
1 change: 1 addition & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './src/types/task-types';
export * from './src/types/reimbursement-requests-types';
export * from './src/types/design-review-types';
export * from './src/types/notifications.types';
export * from './src/types/announcements.types';
export * from './src/validate-wbs';
export * from './src/date-utils';

Expand Down
10 changes: 10 additions & 0 deletions src/shared/src/types/announcements.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { User } from './user-types';

export interface Announcement {
announcementId: string;
text: string;
userCreated: User;
dateCreated: Date;
slackEventId: string;
slackChannelName: string;
}

0 comments on commit 2f337d9

Please sign in to comment.