-
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.
Browse files
Browse the repository at this point in the history
…-SendNotifications #2997-Send Notifications
- Loading branch information
Showing
9 changed files
with
195 additions
and
32 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/backend/src/prisma-query-args/notifications.query-args.ts
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,11 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import { getUserQueryArgs } from './user.query-args'; | ||
|
||
export type NotificationQueryArgs = ReturnType<typeof getNotificationQueryArgs>; | ||
|
||
export const getNotificationQueryArgs = (organizationId: string) => | ||
Prisma.validator<Prisma.NotificationDefaultArgs>()({ | ||
include: { | ||
users: getUserQueryArgs(organizationId) | ||
} | ||
}); |
24 changes: 0 additions & 24 deletions
24
...ackend/src/prisma/migrations/20241112180715_announcements_and_notifications/migration.sql
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
...ackend/src/prisma/migrations/20241211195435_announcements_and_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,57 @@ | ||
-- CreateTable | ||
CREATE TABLE "Announcement" ( | ||
"announcementId" TEXT NOT NULL, | ||
"text" TEXT NOT NULL, | ||
"dateCrated" TIMESTAMP(3) NOT NULL, | ||
"userCreatedId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Announcement_pkey" PRIMARY KEY ("announcementId") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Notification" ( | ||
"notificationId" TEXT NOT NULL, | ||
"text" TEXT NOT NULL, | ||
"iconName" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Notification_pkey" PRIMARY KEY ("notificationId") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "_receivedAnnouncements" ( | ||
"A" TEXT NOT NULL, | ||
"B" TEXT NOT NULL | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "_userNotifications" ( | ||
"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 "_userNotifications_AB_unique" ON "_userNotifications"("A", "B"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_userNotifications_B_index" ON "_userNotifications"("B"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Announcement" ADD CONSTRAINT "Announcement_userCreatedId_fkey" FOREIGN KEY ("userCreatedId") REFERENCES "User"("userId") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- 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 "_userNotifications" ADD CONSTRAINT "_userNotifications_A_fkey" FOREIGN KEY ("A") REFERENCES "Notification"("notificationId") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_userNotifications" ADD CONSTRAINT "_userNotifications_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import { NotificationQueryArgs } from '../prisma-query-args/notifications.query-args'; | ||
import { Notification } from 'shared'; | ||
|
||
const notificationTransformer = (notification: Prisma.NotificationGetPayload<NotificationQueryArgs>): Notification => { | ||
return { | ||
notificationId: notification.notificationId, | ||
text: notification.text, | ||
iconName: notification.iconName | ||
}; | ||
}; | ||
|
||
export default notificationTransformer; |
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,59 @@ | ||
import { Organization } from '@prisma/client'; | ||
import { createTestOrganization, createTestUser, resetUsers } from '../test-utils'; | ||
import { batmanAppAdmin, supermanAdmin } from '../test-data/users.test-data'; | ||
import { NotFoundException } from '../../src/utils/errors.utils'; | ||
import prisma from '../../src/prisma/prisma'; | ||
import NotificationService from '../../src/services/notifications.services'; | ||
|
||
describe('Notifications 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 NotificationService.sendNotifcationToUsers( | ||
'test notification', | ||
'star', | ||
['1', '2'], | ||
organization.organizationId | ||
) | ||
).rejects.toThrow(new NotFoundException('User', '1')); | ||
}); | ||
|
||
it('Succeeds and sends notification to user', async () => { | ||
const testBatman = await createTestUser(batmanAppAdmin, orgId); | ||
const testSuperman = await createTestUser(supermanAdmin, orgId); | ||
await NotificationService.sendNotifcationToUsers( | ||
'test notification', | ||
'star', | ||
[testBatman.userId, testSuperman.userId], | ||
organization.organizationId | ||
); | ||
|
||
const batmanWithNotifications = await prisma.user.findUnique({ | ||
where: { userId: testBatman.userId }, | ||
include: { unreadNotifications: true } | ||
}); | ||
|
||
const supermanWithNotifications = await prisma.user.findUnique({ | ||
where: { userId: testBatman.userId }, | ||
include: { unreadNotifications: true } | ||
}); | ||
|
||
expect(batmanWithNotifications?.unreadNotifications).toHaveLength(1); | ||
expect(batmanWithNotifications?.unreadNotifications[0].text).toBe('test notification'); | ||
expect(supermanWithNotifications?.unreadNotifications).toHaveLength(1); | ||
expect(supermanWithNotifications?.unreadNotifications[0].text).toBe('test 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Notification { | ||
notificationId: string; | ||
text: string; | ||
iconName: string; | ||
} |