Skip to content

Commit

Permalink
handle duplicate group
Browse files Browse the repository at this point in the history
  • Loading branch information
KathleenX7 committed Dec 14, 2024
1 parent 252fc1d commit ab230e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
5 changes: 5 additions & 0 deletions backend/services/implementations/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class NotificationService implements INotificationService {
const residents = await prisma.resident.findMany({
where: { roomNumber: { in: roomIds } },
});
if (residents.length !== roomIds.length) {
throw Object.assign(new Error("Room id does not exist."), {
code: 400,
});
}
const residentIds = residents.map((resident) => resident.userId);

const existingGroup = await prisma.notificationGroup.findMany({
Expand Down
55 changes: 42 additions & 13 deletions frontend/src/components/pages/announcements/AnnouncementsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,17 @@ const AnnouncementsPage = (): React.ReactElement => {
) => {
try {
if (selectedIds.length > 1) {
throw Object.assign(
new Error('Only include one room id.'),
{ code: 400 }
);
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 }
);
throw Object.assign(new Error("No rooms selected."), { code: 400 });
}

let newGroup;
if (selectedIds[0] === -1) {
newGroup = (
await createAnnouncementGroup({})
).data.createNotificationGroup;
newGroup = (await createAnnouncementGroup({})).data
.createNotificationGroup;
} else {
newGroup = (
await createNotificationGroup({
Expand All @@ -231,8 +226,42 @@ const AnnouncementsPage = (): React.ReactElement => {
}

await sendNotification(message, newGroup.id, newGroup);
} catch (e) {
console.log(e);
} catch (e: any) {
if (e.message === "Announcement Group already exists.") {
const announcementId = announcements.find(
(group) => group.announcementGroup === true,
)?.id;

if (announcementId) {
await sendNotification(message, announcementId);
setSelectedGroup(announcementId);
}
} else if (
e.message ===
"Notification Group already exists with specified roomIds."
) {
const groupId = announcements.find((group) => {
if (
group.recipients &&
group.recipients.length === selectedIds.length
) {
for (let i = 0; i < group.recipients.length; i += 1) {
if (!selectedIds.includes(group.recipients[i].roomNumber)) {
return false;
}
}
return true;
}
return false;
})?.id;

if (groupId) {
await sendNotification(message, groupId);
setSelectedGroup(groupId);
}
} else {
console.log(e);
}
}
};

Expand Down

0 comments on commit ab230e1

Please sign in to comment.