Skip to content

Commit

Permalink
notification apis
Browse files Browse the repository at this point in the history
  • Loading branch information
KathleenX7 committed Nov 29, 2024
1 parent a5428eb commit 79f26a3
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 193 deletions.
119 changes: 75 additions & 44 deletions backend/graphql/resolvers/notificationResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import NotificationService from "../../services/implementations/notificationService";
import INotificationService, {
NotificationDTO,
NotificationGroupDTO,
NotificationReceivedDTO,
UpdateNotificationDTO,
CreateNotificationDTO,
} from "../../services/interfaces/notificationService";
import IResidentService from "../../services/interfaces/residentService";
import ResidentService from "../../services/implementations/residentService";
Expand All @@ -14,88 +16,117 @@ const notificationService: INotificationService = new NotificationService(

const notificationResolvers = {
Query: {
getNotificationsByRoomIds: async (
getNotificationsByIds: async (
_parent: undefined,
{ roomIds }: { roomIds: string[] },
{ notificationIds }: { notificationIds: string[] },
): Promise<NotificationReceivedDTO[]> => {
return notificationService.getNotificationsByRoomIds(roomIds.map(Number));
const notificationReceived = await notificationService.getNotificationsByIds(
notificationIds.map(Number),
);
return notificationReceived;
},
getNotificationById: async (
getNotificationByResident: async (
_parent: undefined,
{ id }: { id: string },
): Promise<NotificationReceivedDTO> => {
return notificationService.getNotificationById(Number(id));
{ residentId }: { residentId: string },
): Promise<NotificationReceivedDTO[]> => {
const notificationReceived = await notificationService.getNotificationByResident(
Number(residentId),
);
return notificationReceived;
},
getAllGroupsAndNotifications: async (): Promise<NotificationGroupDTO[]> => {
const notificationGroups = await notificationService.getAllGroupsAndNotifications();
return notificationGroups;
},
},
Mutation: {
sendNotification: async (
createNotificationGroup: async (
_parent: undefined,
{
authorId,
title,
message,
roomIds,
}: {
authorId: number;
title: string;
message: string;
roomIds: number[];
},
): Promise<NotificationDTO> => {
): Promise<NotificationGroupDTO> => {
const ids = roomIds.map((id) => Number(id));
const newNotification = await notificationService.sendNotification(
Number(authorId),
title,
message,
const newNotificationGroup = await notificationService.createNotificationGroup(
ids,
);
return newNotification;
return newNotificationGroup;
},
deleteUserNotification: async (
createAnnouncementGroup: async (): Promise<NotificationGroupDTO> => {
const newNotificationGroup = await notificationService.createAnnouncementGroup();
return newNotificationGroup;
},
sendNotificationToGroup: async (
_parent: undefined,
{ notificationId }: { notificationId: number },
{
groupId,
notification,
}: {
groupId: number;
notification: CreateNotificationDTO;
},
): Promise<NotificationDTO> => {
const deletedNotification = await notificationService.deleteUserNotification(
Number(notificationId),
const newNotification = await notificationService.sendNotificationToGroup(
Number(groupId),
notification,
);
return deletedNotification;
return newNotification;
},
updateSeenNotification: async (
deleteNotificationGroup: async (
_parent: undefined,
{ notificationId }: { notificationId: number },
): Promise<NotificationReceivedDTO> => {
const updatedNotification = await notificationService.updateSeenNotification(
Number(notificationId),
{
groupId,
}: {
groupId: number;
},
): Promise<NotificationGroupDTO> => {
const deletedGroup = await notificationService.deleteNotificationGroup(
Number(groupId),
);
return updatedNotification;
return deletedGroup;
},
updateNotification: async (
updateNotificationById: async (
_parent: undefined,
{
notificationId,
notification,
}: { notificationId: number; notification: UpdateNotificationDTO },
}: {
notificationId: number;
notification: UpdateNotificationDTO;
},
): Promise<NotificationDTO> => {
const updatedNotification = await notificationService.updateNotificationById(
Number(notificationId),
notification,
);
return updatedNotification;
},
sendAnnouncement: async (
deleteNotificationByIds: async (
_parent: undefined,
{
title,
message,
userId,
}: { title: string; message: string; userId: number },
): Promise<NotificationDTO> => {
const newAnnouncement = await notificationService.sendAnnouncement(
title,
message,
Number(userId),
notificationIds,
}: {
notificationIds: number[];
},
): Promise<boolean> => {
const ids = notificationIds.map((id) => Number(id));
await notificationService.deleteNotificationByIds(ids);
return true;
},
updateSeenNotification: async (
_parent: undefined,
{
notificationSeenId,
}: {
notificationSeenId: number;
},
): Promise<NotificationReceivedDTO> => {
const updatedNotificationReceived = await notificationService.updateSeenNotification(
Number(notificationSeenId),
);
return newAnnouncement;
return updatedNotificationReceived;
},
},
};
Expand Down
49 changes: 29 additions & 20 deletions backend/graphql/types/notificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,59 @@ import { gql } from "apollo-server-express";
const notificationType = gql`
type NotificationDTO {
id: ID!
authorId: ID
title: String!
message: String!
createdAt: DateTime!
recipients: [NotificationReceivedDTO!]
createdAt: DateTime
authorId: ID
recipients: [NotificationReceivedDTO]
}
type NotificationGroupDTO {
id: ID!
recipients: [ResidentDTO!]
notifications: [NotificationDTO!]
announcementGroup: Boolean!
}
type NotificationReceivedDTO {
id: ID!
notificationId: ID!
notification: NotificationDTO
recipientId: ID!
seen: Boolean!
}
input UpdateNotificationDTO {
authorId: ID
title: String
message: String
createdAt: DateTime
}
input CreateNotificationDTO {
authorId: ID
message: String!
createdAt: DateTime
}
extend type Query {
getNotificationsByRoomIds(roomIds: [Int!]): [NotificationReceivedDTO!]
getNotificationById(id: ID!): NotificationReceivedDTO!
getNotificationsByIds(notificationIds: [ID!]): [NotificationReceivedDTO!]
getNotificationByResident(residentId: ID!): [NotificationReceivedDTO!]
getAllGroupsAndNotifications: [NotificationGroupDTO!]
}
extend type Mutation {
sendNotification(
authorId: ID!
title: String!
message: String!
roomIds: [Int!]
createNotificationGroup(roomIds: [Int!]): NotificationGroupDTO!
createAnnouncementGroup: NotificationGroupDTO!
sendNotificationToGroup(
groupId: ID!
notification: CreateNotificationDTO!
): NotificationDTO!
deleteUserNotification(notificationId: ID!): NotificationDTO!
updateSeenNotification(notificationId: ID!): NotificationReceivedDTO!
updateNotification(
deleteNotificationGroup(groupId: ID!): NotificationGroupDTO!
updateNotificationById(
notificationId: ID!
notification: UpdateNotificationDTO!
): NotificationDTO!
sendAnnouncement(
title: String
message: String
userId: ID
): NotificationDTO!
deleteNotificationByIds(notificationIds: [ID!]): Boolean!
updateSeenNotification(notificationSeenId: ID!): NotificationReceivedDTO!
}
`;

Expand Down
2 changes: 2 additions & 0 deletions backend/graphql/types/residentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const residentType = gql`
credits: Float!
dateJoined: Date!
dateLeft: Date
notificationGroup: [NotificationGroupDTO!]!
notificationRecieved: [NotificationReceivedDTO]!
}
input CreateResidentDTO {
Expand Down
9 changes: 5 additions & 4 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ model Warning {
}

model NotificationGroup {
id Int @id @default(autoincrement())
recipients Resident[]
notifications Notification[]
id Int @id @default(autoincrement())
recipients Resident[]
notifications Notification[]
announcementGroup Boolean
}

model Notification {
Expand All @@ -158,7 +159,7 @@ model Notification {
createdAt DateTime @default(now()) @map("created_at") @db.Date
author Staff? @relation(fields: [authorId], references: [userId], onDelete: SetNull, onUpdate: Cascade)
authorId Int? @map("author_id")
group NotificationGroup @relation(fields: [groupId], references: [id])
group NotificationGroup @relation(fields: [groupId], references: [id], onDelete: Cascade)
groupId Int @map("group_id")
notificationReceived NotificationReceived[]
Expand Down
Loading

0 comments on commit 79f26a3

Please sign in to comment.