Skip to content

Commit

Permalink
#3044-up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 24, 2024
2 parents 76e94cf + 10a97bb commit 5f16229
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/backend/src/controllers/announcements.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ export default class AnnouncementController {
next(error);
}
}

static async removeUserAnnouncement(req: Request, res: Response, next: NextFunction) {
try {
const { announcementId } = req.params;
const { organization, currentUser } = req;

const unreadAnnouncements = await AnnouncementService.removeUserAnnouncement(
currentUser.userId,
announcementId,
organization.organizationId
);
res.status(200).json(unreadAnnouncements);
} catch (error: unknown) {
next(error);
}
}
}
1 change: 1 addition & 0 deletions src/backend/src/routes/announcements.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import AnnouncementController from '../controllers/announcements.controllers';
const announcementsRouter = express.Router();

announcementsRouter.get('/current-user', AnnouncementController.getUserUnreadAnnouncements);
announcementsRouter.post('/:announcementId/remove', AnnouncementController.removeUserAnnouncement);

export default announcementsRouter;
38 changes: 33 additions & 5 deletions src/backend/src/services/announcement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { Announcement } from 'shared';
import prisma from '../prisma/prisma';
import { getAnnouncementQueryArgs } from '../prisma-query-args/announcements.query.args';
import announcementTransformer from '../transformers/announcements.transformer';
import { NotFoundException } from '../utils/errors.utils';
import { HttpException } from '../utils/errors.utils';
import { HttpException, NotFoundException } from '../utils/errors.utils';

export default class AnnouncementService {
/**
* Creates an announcement that is sent to users
* this data is populated from slack events
* @param text slack message text
* @param usersReceivedIds users to send announcements to
* @param dateCreated date created of slack message
* @param dateMessageSent date created of slack message
* @param senderName name of user who sent slack message
* @param slackEventId id of slack event (provided by slack api)
* @param slackChannelName name of channel message was sent in
Expand Down Expand Up @@ -49,7 +48,7 @@ export default class AnnouncementService {
static async updateAnnouncement(
text: string,
usersReceivedIds: string[],
dateCreated: Date,
dateMessageSent: Date,
senderName: string,
slackEventId: string,
slackChannelName: string,
Expand All @@ -73,7 +72,7 @@ export default class AnnouncementService {
}))
},
slackEventId,
dateCreated,
dateMessageSent,
senderName,
slackChannelName
},
Expand Down Expand Up @@ -126,4 +125,33 @@ export default class AnnouncementService {

return unreadAnnouncements.map(announcementTransformer);
}

/**
* Removes a announcement from the user's unread announcement
* @param userId id of the user to remove announcement from
* @param announcementId id of the announcement to remove
* @param organization the user's organization
* @returns the user's updated unread announcement
*/
static async removeUserAnnouncement(userId: string, announcementId: 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 },
data: {
unreadAnnouncements: {
disconnect: {
announcementId
}
}
},
include: { unreadAnnouncements: getAnnouncementQueryArgs(organizationId) }
});

return updatedUser.unreadAnnouncements.map(announcementTransformer);
}
}
1 change: 1 addition & 0 deletions src/backend/src/services/slack.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export default class slackServices {
}
}
}

return await AnnouncementService.createAnnouncement(
messageText,
userIdsToNotify,
Expand Down
42 changes: 42 additions & 0 deletions src/backend/tests/unmocked/announcements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,46 @@ describe('Announcemnts Tests', () => {
expect(announcements[1].text).toBe('test2');
});
});

describe('Remove Announcement', () => {
it('Succeeds and removes user announcement', async () => {
const testBatman = await createTestUser(batmanAppAdmin, orgId);
await AnnouncementService.createAnnouncement(
'test1',
[testBatman.userId],
new Date(),
'Thomas Emrax',
'1',
'software',
organization.organizationId
);
await AnnouncementService.createAnnouncement(
'test2',
[testBatman.userId],
new Date(),
'Superman',
'50',
'mechanical',
organization.organizationId
);

const announcements = await AnnouncementService.getUserUnreadAnnouncements(
testBatman.userId,
organization.organizationId
);

expect(announcements).toHaveLength(2);
expect(announcements[0].text).toBe('test1');
expect(announcements[1].text).toBe('test2');

const updatedAnnouncements = await AnnouncementService.removeUserAnnouncement(
testBatman.userId,
announcements[0].announcementId,
organization.organizationId
);

expect(updatedAnnouncements).toHaveLength(1);
expect(updatedAnnouncements[0].text).toBe('test2');
});
});
});

0 comments on commit 5f16229

Please sign in to comment.