-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
144 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/backend/src/prisma/migrations/20241122155216_updated_notifications/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `userId` on the `Notification` table. All the data in the column will be lost. | ||
- Added the required column `text` to the `Announcement` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "Notification" DROP CONSTRAINT "Notification_userId_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Announcement" ADD COLUMN "text" TEXT NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Notification" DROP COLUMN "userId"; | ||
|
||
-- CreateTable | ||
CREATE TABLE "_ReceivedAnnouncements" ( | ||
"A" TEXT NOT NULL, | ||
"B" TEXT NOT NULL | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "_NotificationToUser" ( | ||
"A" TEXT NOT NULL, | ||
"B" TEXT NOT NULL | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "_ReceivedAnnouncements_AB_unique" ON "_ReceivedAnnouncements"("A", "B"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_ReceivedAnnouncements_B_index" ON "_ReceivedAnnouncements"("B"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "_NotificationToUser_AB_unique" ON "_NotificationToUser"("A", "B"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_NotificationToUser_B_index" ON "_NotificationToUser"("B"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_ReceivedAnnouncements" ADD CONSTRAINT "_ReceivedAnnouncements_A_fkey" FOREIGN KEY ("A") REFERENCES "Announcement"("announcementId") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_ReceivedAnnouncements" ADD CONSTRAINT "_ReceivedAnnouncements_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("userId") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_NotificationToUser" ADD CONSTRAINT "_NotificationToUser_A_fkey" FOREIGN KEY ("A") REFERENCES "Notification"("notificationId") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_NotificationToUser" ADD CONSTRAINT "_NotificationToUser_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("userId") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getNotificationQueryArgs } from '../prisma-query-args/notifications.query-args'; | ||
import prisma from '../prisma/prisma'; | ||
import notificationTransformer from '../transformers/notification.transformer'; | ||
import { NotFoundException } from './errors.utils'; | ||
|
||
const sendNotificationToUser = async (userId: string, notificationId: string, organizationId: string) => { | ||
const requestedUser = await prisma.user.findUnique({ | ||
where: { userId } | ||
}); | ||
|
||
if (!requestedUser) throw new NotFoundException('User', userId); | ||
|
||
const updatedUser = await prisma.user.update({ | ||
where: { userId: requestedUser.userId }, | ||
data: { unreadNotifications: { connect: { notificationId } } }, | ||
include: { unreadNotifications: getNotificationQueryArgs(organizationId) } | ||
}); | ||
|
||
return updatedUser.unreadNotifications.map(notificationTransformer); | ||
}; | ||
|
||
export const sendNotificationToUsers = async (userIds: string[], text: string, iconName: string, organizationId: string) => { | ||
const createdNotification = await prisma.notification.create({ | ||
data: { | ||
text, | ||
iconName | ||
}, | ||
...getNotificationQueryArgs(organizationId) | ||
}); | ||
|
||
const notificationPromises = userIds.map(async (userId) => { | ||
return sendNotificationToUser(userId, createdNotification.notificationId, organizationId); | ||
}); | ||
|
||
await Promise.all(notificationPromises); | ||
return notificationTransformer(createdNotification); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Organization } from '@prisma/client'; | ||
import { createTestOrganization, createTestUser, resetUsers } from '../test-utils'; | ||
import { batmanAppAdmin, wonderwomanGuest } from '../test-data/users.test-data'; | ||
import { NotFoundException } from '../../src/utils/errors.utils'; | ||
import { sendNotificationToUsers } from '../../src/utils/homepage-notifications.utils'; | ||
import prisma from '../../src/prisma/prisma'; | ||
|
||
describe('Notification Tests', () => { | ||
let orgId: string; | ||
let organization: Organization; | ||
beforeEach(async () => { | ||
organization = await createTestOrganization(); | ||
orgId = organization.organizationId; | ||
}); | ||
|
||
afterEach(async () => { | ||
await resetUsers(); | ||
}); | ||
|
||
describe('Send Notification', () => { | ||
it('fails on invalid user id', async () => { | ||
await expect(async () => await sendNotificationToUsers(['1'], 'test', 'test', orgId)).rejects.toThrow( | ||
new NotFoundException('User', '1') | ||
); | ||
}); | ||
|
||
it('Succeeds and sends notification to user', async () => { | ||
const testBatman = await createTestUser(batmanAppAdmin, orgId); | ||
const testWonderWoman = await createTestUser(wonderwomanGuest, orgId); | ||
|
||
const notification = await sendNotificationToUsers([testBatman.userId, testWonderWoman.userId], 'test', 'icon', orgId); | ||
|
||
const batmanWithNotifications = await prisma.user.findUnique({ | ||
where: { userId: testBatman.userId }, | ||
include: { unreadNotifications: true } | ||
}); | ||
const wonderWomanWithNotifications = await prisma.user.findUnique({ | ||
where: { userId: testWonderWoman.userId }, | ||
include: { unreadNotifications: true } | ||
}); | ||
|
||
expect(batmanWithNotifications?.unreadNotifications).toHaveLength(1); | ||
expect(batmanWithNotifications?.unreadNotifications[0]).toStrictEqual(notification); | ||
|
||
expect(wonderWomanWithNotifications?.unreadNotifications).toHaveLength(1); | ||
expect(wonderWomanWithNotifications?.unreadNotifications[0]).toStrictEqual(notification); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export interface Notification { | ||
text: String; | ||
iconName: String; | ||
notificationId: string; | ||
text: string; | ||
iconName: string; | ||
} |