From 5fb2f4cabb602d251147a146158795cdbaf79e2d Mon Sep 17 00:00:00 2001 From: KathleenX7 Date: Sat, 7 Dec 2024 10:39:55 -0500 Subject: [PATCH] group creation restrictions --- .../implementations/notificationService.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/backend/services/implementations/notificationService.ts b/backend/services/implementations/notificationService.ts index c8c2143..79a36d1 100644 --- a/backend/services/implementations/notificationService.ts +++ b/backend/services/implementations/notificationService.ts @@ -23,11 +23,42 @@ class NotificationService implements INotificationService { roomIds: number[], ): Promise { try { + if (roomIds.length == 0) { + throw 'No rooms specified.'; + } + 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.'; + } const residents = await prisma.resident.findMany({ where: { roomNumber: { in: roomIds } }, }); const residentIds = residents.map((resident) => resident.userId); + const existingGroup = await prisma.notificationGroup.findMany({ + where: { + announcementGroup: false, + recipients: { + every: { + userId: { in: residentIds }, + }, + }, + }, + include: { + recipients: true, + } + }); + + if (existingGroup && existingGroup.length > 0) { + // throw error if residents match + existingGroup.forEach((group) => { + if (group.recipients.length === residentIds.length) { + throw 'Notification Group already exists with specified roomIds.'; + } + }); + } + const newNotificationGroup = await prisma.notificationGroup.create({ data: { recipients: { @@ -53,6 +84,15 @@ class NotificationService implements INotificationService { async createAnnouncementGroup(): Promise { try { + const existingGroup = await prisma.notificationGroup.findMany({ + where: { + announcementGroup: true, + }, + }); + if (existingGroup && existingGroup.length > 0) { + throw 'Announcement Group already exists.'; + } + const residents = await prisma.resident.findMany({ where: { dateLeft: null }, });