From af71cbd5477b00c9bd9c81c4ef4ff629f67c660c Mon Sep 17 00:00:00 2001 From: KathleenX7 Date: Sat, 7 Dec 2024 11:29:26 -0500 Subject: [PATCH] create announcement group --- .../implementations/notificationService.ts | 15 ++++++-- .../pages/announcements/AnnouncementsPage.tsx | 34 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/backend/services/implementations/notificationService.ts b/backend/services/implementations/notificationService.ts index 79a36d1..0770460 100644 --- a/backend/services/implementations/notificationService.ts +++ b/backend/services/implementations/notificationService.ts @@ -24,12 +24,18 @@ class NotificationService implements INotificationService { ): Promise { 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 } }, @@ -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 } + ); } }); } diff --git a/frontend/src/components/pages/announcements/AnnouncementsPage.tsx b/frontend/src/components/pages/announcements/AnnouncementsPage.tsx index 29e396b..04b63a5 100644 --- a/frontend/src/components/pages/announcements/AnnouncementsPage.tsx +++ b/frontend/src/components/pages/announcements/AnnouncementsPage.tsx @@ -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, @@ -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) {