Skip to content

Commit

Permalink
create announcement group
Browse files Browse the repository at this point in the history
  • Loading branch information
KathleenX7 committed Dec 7, 2024
1 parent 756bb71 commit af71cbd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
15 changes: 12 additions & 3 deletions backend/services/implementations/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ class NotificationService implements INotificationService {
): Promise<NotificationGroupDTO> {
try {
if (roomIds.length == 0) {
throw 'No rooms specified.';
throw Object.assign(
new Error('No rooms specified.'),
{ code: 400 }
);
}
if (roomIds.length > 1) {
// enforces that a group can only have one member
// remove in the future if the requirements change
throw 'Notification Group can only have one room.';
throw Object.assign(
new Error('Notification Group can only have one room.'),
{ code: 400 }
);
}
const residents = await prisma.resident.findMany({
where: { roomNumber: { in: roomIds } },
Expand All @@ -54,7 +60,10 @@ class NotificationService implements INotificationService {
// throw error if residents match
existingGroup.forEach((group) => {
if (group.recipients.length === residentIds.length) {
throw 'Notification Group already exists with specified roomIds.';
throw Object.assign(
new Error('Notification Group already exists with specified roomIds.'),
{ code: 400 }
);
}
});
}
Expand Down
34 changes: 27 additions & 7 deletions frontend/src/components/pages/announcements/AnnouncementsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const AnnouncementsPage = (): React.ReactElement => {

const [sendNotificationToGroup] = useMutation(SEND_NOTIFICATION_TO_GROUP);
const [createNotificationGroup] = useMutation(CREATE_NOTIFICATION_GROUP);
const [createAnnouncementGroup] = useMutation(CREATE_ANNOUNCEMENT_GROUP);

const sendNotification = async (
message: string,
Expand Down Expand Up @@ -202,13 +203,32 @@ const AnnouncementsPage = (): React.ReactElement => {
message: string,
) => {
try {
const newGroup = (
await createNotificationGroup({
variables: {
roomIds: selectedIds,
},
})
).data.createNotificationGroup;
if (selectedIds.length > 1) {
throw Object.assign(
new Error('Only include one room id.'),
{ code: 400 }
);
} else if (selectedIds.length === 0) {
throw Object.assign(
new Error('No rooms selected.'),
{ code: 400 }
);
}

let newGroup;
if (selectedIds[0] === -1) {
newGroup = (
await createAnnouncementGroup({})
).data.createNotificationGroup;
} else {
newGroup = (
await createNotificationGroup({
variables: {
roomIds: selectedIds,
},
})
).data.createNotificationGroup;
}

await sendNotification(message, newGroup.id, newGroup);
} catch (e) {
Expand Down

0 comments on commit af71cbd

Please sign in to comment.