From 147b5ddca972b079d746bc53bf2f18f0a6c3efa4 Mon Sep 17 00:00:00 2001 From: Chris Pyle Date: Fri, 20 Dec 2024 16:55:52 -0500 Subject: [PATCH] #2823 announcement service function tests --- .../src/services/announcement.service.ts | 17 +- .../tests/unmocked/announcements.test.ts | 158 ++++++++++++++++++ 2 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 src/backend/tests/unmocked/announcements.test.ts diff --git a/src/backend/src/services/announcement.service.ts b/src/backend/src/services/announcement.service.ts index 342d5de296..9ff1e7007e 100644 --- a/src/backend/src/services/announcement.service.ts +++ b/src/backend/src/services/announcement.service.ts @@ -67,7 +67,7 @@ export default class AnnouncementService { data: { text, usersReceived: { - connect: usersReceivedIds.map((id) => ({ + set: usersReceivedIds.map((id) => ({ userId: id })) }, @@ -83,16 +83,25 @@ export default class AnnouncementService { } static async deleteAnnouncement(slackEventId: string, organizationId: string): Promise { + const originalAnnouncement = await prisma.announcement.findUnique({ + where: { + slackEventId + } + }); + + if (!originalAnnouncement) throw new NotFoundException('Announcement', slackEventId); + const announcement = await prisma.announcement.update({ where: { slackEventId }, data: { - dateDeleted: new Date() + dateDeleted: new Date(), + usersReceived: { + set: [] + } }, ...getAnnouncementQueryArgs(organizationId) }); - if (!announcement) throw new NotFoundException('Announcement', slackEventId); - return announcementTransformer(announcement); } } diff --git a/src/backend/tests/unmocked/announcements.test.ts b/src/backend/tests/unmocked/announcements.test.ts new file mode 100644 index 0000000000..7bd6781a1d --- /dev/null +++ b/src/backend/tests/unmocked/announcements.test.ts @@ -0,0 +1,158 @@ +import { Organization, User } from '@prisma/client'; +import { createTestOrganization, createTestUser, resetUsers } from '../test-utils'; +import { + batmanAppAdmin, + batmanSettings, + supermanAdmin, + supermanSettings, + wonderwomanGuest, + wonderwomanSettings +} from '../test-data/users.test-data'; +import AnnouncementService from '../../src/services/announcement.service'; +import UsersService from '../../src/services/users.services'; +import { NotFoundException } from '../../src/utils/errors.utils'; + +describe('announcement tests', () => { + let orgId: string; + let organization: Organization; + let batman: User; + let superman: User; + let wonderwoman: User; + + beforeEach(async () => { + organization = await createTestOrganization(); + orgId = organization.organizationId; + batman = await createTestUser(batmanAppAdmin, orgId, batmanSettings); + superman = await createTestUser(supermanAdmin, orgId, supermanSettings); + wonderwoman = await createTestUser(wonderwomanGuest, orgId, wonderwomanSettings); + }); + + afterEach(async () => { + await resetUsers(); + }); + + it('creates announcements which can be recieved via users', async () => { + const announcement = await AnnouncementService.createAnnouncement( + 'text', + [superman.userId, batman.userId], + new Date(1000000000000), + 'sender name', + 'slack id', + 'channel name', + orgId + ); + expect(announcement?.text).toBe('text'); + expect(announcement?.usersReceived).toHaveLength(2); + expect(announcement?.senderName).toBe('sender name'); + expect(announcement?.dateCreated).toStrictEqual(new Date(1000000000000)); + expect(announcement?.slackEventId).toBe('slack id'); + expect(announcement?.slackChannelName).toBe('channel name'); + expect(announcement?.dateDeleted).toBeUndefined(); + + const smAnnouncements = await UsersService.getUserUnreadAnnouncements(superman.userId, organization); + const bmAnnouncements = await UsersService.getUserUnreadAnnouncements(batman.userId, organization); + const wwAnnouncements = await UsersService.getUserUnreadAnnouncements(wonderwoman.userId, organization); + + expect(smAnnouncements).toHaveLength(1); + expect(smAnnouncements[0]?.text).toBe('text'); + expect(smAnnouncements[0]?.usersReceived).toHaveLength(2); + expect(smAnnouncements[0]?.senderName).toBe('sender name'); + expect(smAnnouncements[0]?.dateCreated).toStrictEqual(new Date(1000000000000)); + expect(smAnnouncements[0]?.slackEventId).toBe('slack id'); + expect(smAnnouncements[0]?.slackChannelName).toBe('channel name'); + expect(smAnnouncements[0]?.dateDeleted).toBeUndefined(); + + expect(bmAnnouncements).toHaveLength(1); + expect(wwAnnouncements).toHaveLength(0); + }); + + it('updates an announcement', async () => { + await AnnouncementService.createAnnouncement( + 'text', + [superman.userId, batman.userId], + new Date(1000000000000), + 'sender name', + 'slack id', + 'channel name', + orgId + ); + + let smAnnouncements = await UsersService.getUserUnreadAnnouncements(superman.userId, organization); + let bmAnnouncements = await UsersService.getUserUnreadAnnouncements(batman.userId, organization); + let wwAnnouncements = await UsersService.getUserUnreadAnnouncements(wonderwoman.userId, organization); + + expect(smAnnouncements).toHaveLength(1); + expect(bmAnnouncements).toHaveLength(1); + expect(wwAnnouncements).toHaveLength(0); + + const updatedAnnouncement = await AnnouncementService.updateAnnouncement( + 'new text', + [batman.userId, wonderwoman.userId], + new Date(1000000000000), + 'sender name', + 'slack id', + 'channel name', + orgId + ); + + smAnnouncements = await UsersService.getUserUnreadAnnouncements(superman.userId, organization); + bmAnnouncements = await UsersService.getUserUnreadAnnouncements(batman.userId, organization); + wwAnnouncements = await UsersService.getUserUnreadAnnouncements(wonderwoman.userId, organization); + + expect(smAnnouncements).toHaveLength(0); + expect(bmAnnouncements).toHaveLength(1); + expect(wwAnnouncements).toHaveLength(1); + expect(bmAnnouncements[0]?.text).toBe('new text'); + expect(wwAnnouncements[0]?.text).toBe('new text'); + expect(updatedAnnouncement?.text).toBe('new text'); + }); + + it('fails to update if there is no slack id', async () => { + await expect( + async () => + await AnnouncementService.updateAnnouncement( + 'new text', + [batman.userId, wonderwoman.userId], + new Date(1000000000000), + 'sender name', + 'slack id', + 'channel name', + orgId + ) + ).rejects.toThrow(new NotFoundException('Announcement', 'slack id')); + }); + + it('deletes an announcement', async () => { + await AnnouncementService.createAnnouncement( + 'text', + [superman.userId, batman.userId], + new Date(1000000000000), + 'sender name', + 'slack id', + 'channel name', + orgId + ); + + let smAnnouncements = await UsersService.getUserUnreadAnnouncements(superman.userId, organization); + let bmAnnouncements = await UsersService.getUserUnreadAnnouncements(batman.userId, organization); + + expect(smAnnouncements).toHaveLength(1); + expect(bmAnnouncements).toHaveLength(1); + + const deletedAnnouncement = await AnnouncementService.deleteAnnouncement('slack id', orgId); + + smAnnouncements = await UsersService.getUserUnreadAnnouncements(superman.userId, organization); + bmAnnouncements = await UsersService.getUserUnreadAnnouncements(batman.userId, organization); + + expect(smAnnouncements).toHaveLength(0); + expect(bmAnnouncements).toHaveLength(0); + expect(deletedAnnouncement?.text).toBe('text'); + expect(deletedAnnouncement?.dateDeleted).toBeDefined(); + }); + + it('throws if it cannot find the announcement to delete', async () => { + await expect(async () => await AnnouncementService.deleteAnnouncement('non-existent id', orgId)).rejects.toThrow( + new NotFoundException('Announcement', 'non-existent id') + ); + }); +});