-
Notifications
You must be signed in to change notification settings - Fork 10
Use Cases for Get Notifications and Delete Notifications #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ofahimIQSS
merged 9 commits into
develop
from
334-create-use-cases-for-get-notifications-and-delete-notifications
Aug 22, 2025
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0c49ec5
feat: update notification usecases
ChengShi-1 3c12fbe
fix: testcases
ChengShi-1 252e08c
fix: naming of delete notification
ChengShi-1 008b3aa
Merge branch 'develop' into 334-create-use-cases-for-get-notification…
ChengShi-1 e93174e
feat: update get notification parameter
ChengShi-1 541e892
feat: display as read
ChengShi-1 de95d37
fix on tests
ChengShi-1 e087a57
update env. variables
ChengShi-1 b61f8cf
fix: update naming and test
ChengShi-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| export interface Notification { | ||
| id: number | ||
| type: string | ||
| subjectText: string | ||
| messageText: string | ||
| sentTimestamp: string | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/notifications/domain/repositories/INotificationsRepository.ts
This file contains hidden or 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,6 @@ | ||
| import { Notification } from '../models/Notification' | ||
|
|
||
| export interface INotificationsRepository { | ||
| getAllNotificationsByUser(): Promise<Notification[]> | ||
|
||
| deleteNotificationByUser(notificationId: number): Promise<void> | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/notifications/domain/useCases/DeleteNotificationByUser.ts
This file contains hidden or 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,16 @@ | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { INotificationsRepository } from '../repositories/INotificationsRepository' | ||
|
|
||
| /** | ||
| * Use case for deleting a specific notification for the current user. | ||
| * | ||
| * @param notificationId - The ID of the notification to delete. | ||
| * @returns {Promise<void>} - A promise that resolves when the notification is deleted. | ||
| */ | ||
| export class DeleteNotificationByUser implements UseCase<void> { | ||
| constructor(private readonly notificationsRepository: INotificationsRepository) {} | ||
|
|
||
| async execute(notificationId: number): Promise<void> { | ||
| return this.notificationsRepository.deleteNotificationByUser(notificationId) | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/notifications/domain/useCases/GetAllNotificationsByUser.ts
This file contains hidden or 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,16 @@ | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { Notification } from '../models/Notification' | ||
| import { INotificationsRepository } from '../repositories/INotificationsRepository' | ||
|
|
||
| export class GetAllNotificationsByUser implements UseCase<Notification[]> { | ||
| constructor(private readonly notificationsRepository: INotificationsRepository) {} | ||
|
|
||
| /** | ||
| * Use case for retrieving all notifications for the current user. | ||
| * | ||
| * @returns {Promise<Notification[]>} - A promise that resolves to an array of Notification instances. | ||
| */ | ||
| async execute(): Promise<Notification[]> { | ||
| return (await this.notificationsRepository.getAllNotificationsByUser()) as Notification[] | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| import { NotificationsRepository } from './infra/repositories/NotificationsRepository' | ||
| import { GetAllNotificationsByUser } from './domain/useCases/GetAllNotificationsByUser' | ||
| import { DeleteNotificationByUser } from './domain/useCases/DeleteNotificationByUser' | ||
|
|
||
| const notificationsRepository = new NotificationsRepository() | ||
|
|
||
| const getAllNotificationsByUser = new GetAllNotificationsByUser(notificationsRepository) | ||
| const deleteNotificationByUser = new DeleteNotificationByUser(notificationsRepository) | ||
|
|
||
| export { getAllNotificationsByUser, deleteNotificationByUser } | ||
|
|
||
| export { Notification } from './domain/models/Notification' |
25 changes: 25 additions & 0 deletions
25
src/notifications/infra/repositories/NotificationsRepository.ts
This file contains hidden or 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,25 @@ | ||
| import { ApiRepository } from '../../../core/infra/repositories/ApiRepository' | ||
| import { INotificationsRepository } from '../../domain/repositories/INotificationsRepository' | ||
| import { Notification } from '../../domain/models/Notification' | ||
|
|
||
| export class NotificationsRepository extends ApiRepository implements INotificationsRepository { | ||
| private readonly notificationsResourceName: string = 'notifications' | ||
|
|
||
| public async getAllNotificationsByUser(): Promise<Notification[]> { | ||
| return this.doGet(this.buildApiEndpoint(this.notificationsResourceName, 'all'), true) | ||
| .then((response) => response.data.data.notifications as Notification[]) | ||
| .catch((error) => { | ||
| throw error | ||
| }) | ||
| } | ||
|
|
||
| public async deleteNotificationByUser(notificationId: number): Promise<void> { | ||
| return this.doDelete( | ||
| this.buildApiEndpoint(this.notificationsResourceName, notificationId.toString()) | ||
| ) | ||
| .then(() => {}) | ||
| .catch((error) => { | ||
| throw error | ||
| }) | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
test/functional/notifications/DeleteNotificationByUser.test.ts
This file contains hidden or 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,32 @@ | ||
| import { | ||
| ApiConfig, | ||
| deleteNotificationByUser, | ||
| getAllNotificationsByUser, | ||
| WriteError | ||
| } from '../../../src' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
|
|
||
| describe('execute', () => { | ||
| beforeEach(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should successfully delete a notification for authenticated user', async () => { | ||
| const notifications = await getAllNotificationsByUser.execute() | ||
| const notificationId = notifications[notifications.length - 1].id | ||
|
|
||
| await deleteNotificationByUser.execute(notificationId) | ||
|
|
||
| const notificationsAfterDelete = await getAllNotificationsByUser.execute() | ||
| expect(notificationsAfterDelete.length).toBe(notifications.length - 1) | ||
| }) | ||
|
|
||
| test('should throw an error when the notification id does not exist', async () => { | ||
| await expect(deleteNotificationByUser.execute(123)).rejects.toThrow(WriteError) | ||
| }) | ||
| }) |
28 changes: 28 additions & 0 deletions
28
test/functional/notifications/GetAllNotificationsByUser.test.ts
This file contains hidden or 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,28 @@ | ||
| import { ApiConfig, getAllNotificationsByUser, Notification } from '../../../src' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
|
|
||
| describe('execute', () => { | ||
| beforeEach(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should successfully return notifications for authenticated user', async () => { | ||
| const notifications: Notification[] = await getAllNotificationsByUser.execute() | ||
|
|
||
| expect(notifications).not.toBeNull() | ||
| expect(Array.isArray(notifications)).toBe(true) | ||
| }) | ||
|
|
||
| test('should have correct notification properties if notifications exist', async () => { | ||
| const notifications = await getAllNotificationsByUser.execute() | ||
|
|
||
| expect(notifications[0]).toHaveProperty('id') | ||
| expect(notifications[0]).toHaveProperty('type') | ||
| expect(notifications[0]).toHaveProperty('sentTimestamp') | ||
| }) | ||
| }) |
78 changes: 78 additions & 0 deletions
78
test/integration/notifications/NotificationsRepository.test.ts
This file contains hidden or 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,78 @@ | ||
| import { | ||
| ApiConfig, | ||
| DataverseApiAuthMechanism | ||
| } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { NotificationsRepository } from '../../../src/notifications/infra/repositories/NotificationsRepository' | ||
| import { Notification } from '../../../src/notifications/domain/models/Notification' | ||
| import { createDataset } from '../../../src/datasets' | ||
| import { publishDatasetViaApi, waitForNoLocks } from '../../testHelpers/datasets/datasetHelper' | ||
| import { WriteError } from '../../../src' | ||
|
|
||
| describe('NotificationsRepository', () => { | ||
| const sut: NotificationsRepository = new NotificationsRepository() | ||
|
|
||
| beforeEach(() => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should return notifications after creating and publishing a dataset', async () => { | ||
| // Create a dataset and publish it so that a notification of Dataset published is created | ||
| const testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO) | ||
|
|
||
| await publishDatasetViaApi(testDatasetIds.numericId) | ||
| await waitForNoLocks(testDatasetIds.numericId, 10) | ||
|
|
||
| const notifications: Notification[] = await sut.getAllNotificationsByUser() | ||
|
|
||
| expect(Array.isArray(notifications)).toBe(true) | ||
| expect(notifications.length).toBeGreaterThan(0) | ||
|
|
||
| const publishedNotification = notifications.find((n) => n.type === 'PUBLISHEDDS') | ||
|
|
||
| expect(publishedNotification).toBeDefined() | ||
|
|
||
| expect(publishedNotification).toHaveProperty('id') | ||
| expect(publishedNotification).toHaveProperty('type') | ||
| expect(publishedNotification).toHaveProperty('subjectText') | ||
| expect(publishedNotification).toHaveProperty('messageText') | ||
| expect(publishedNotification).toHaveProperty('sentTimestamp') | ||
|
|
||
| expect(publishedNotification?.subjectText).toContain( | ||
| 'Dataset created using the createDataset use case' | ||
| ) | ||
| expect(publishedNotification?.messageText).toContain( | ||
| 'Your dataset named Dataset created using the createDataset use case' | ||
| ) | ||
| }) | ||
|
|
||
| test('should delete a notification by ID', async () => { | ||
| const notifications: Notification[] = await sut.getAllNotificationsByUser() | ||
|
|
||
| const notificationToDelete = notifications[0] | ||
|
|
||
| await sut.deleteNotificationByUser(notificationToDelete.id) | ||
|
|
||
| const notificationsAfterDelete: Notification[] = await sut.getAllNotificationsByUser() | ||
| const deletedNotification = notificationsAfterDelete.find( | ||
| (n) => n.id === notificationToDelete.id | ||
| ) | ||
| expect(deletedNotification).toBeUndefined() | ||
| }) | ||
|
|
||
| test('should throw error when trying to delete notification with wrong ID', async () => { | ||
| const nonExistentMetadataBlockName = 99999 | ||
ChengShi-1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const expectedError = new WriteError( | ||
| `[404] Notification ${nonExistentMetadataBlockName} not found.` | ||
| ) | ||
|
|
||
| await expect(sut.deleteNotificationByUser(nonExistentMetadataBlockName)).rejects.toThrow( | ||
| expectedError | ||
| ) | ||
| }) | ||
| }) | ||
This file contains hidden or 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,46 @@ | ||
| import { DeleteNotificationByUser } from '../../../src/notifications/domain/useCases/DeleteNotificationByUser' | ||
| import { INotificationsRepository } from '../../../src/notifications/domain/repositories/INotificationsRepository' | ||
| import { Notification } from '../../../src/notifications/domain/models/Notification' | ||
|
|
||
| const mockNotifications: Notification[] = [ | ||
| { | ||
| id: 1, | ||
| type: 'PUBLISHEDDS', | ||
| subjectText: 'Test notification', | ||
| messageText: 'Test message', | ||
| sentTimestamp: '2025-01-01T00:00:00Z' | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'ASSIGNROLE', | ||
| subjectText: 'Role assignment', | ||
| messageText: 'Role assigned', | ||
| sentTimestamp: '2025-01-01T00:00:00Z' | ||
| } | ||
| ] | ||
|
|
||
| describe('execute', () => { | ||
| test('should delete notification from repository', async () => { | ||
| const notificationsRepositoryStub: INotificationsRepository = {} as INotificationsRepository | ||
| notificationsRepositoryStub.getAllNotificationsByUser = jest.fn().mockResolvedValue([]) | ||
| notificationsRepositoryStub.deleteNotificationByUser = jest | ||
| .fn() | ||
| .mockResolvedValue(mockNotifications) | ||
| const sut = new DeleteNotificationByUser(notificationsRepositoryStub) | ||
|
|
||
| await sut.execute(123) | ||
|
|
||
| expect(notificationsRepositoryStub.deleteNotificationByUser).toHaveBeenCalledWith(123) | ||
| }) | ||
|
|
||
| test('should throw error when repository throws error', async () => { | ||
| const notificationsRepositoryStub: INotificationsRepository = {} as INotificationsRepository | ||
| notificationsRepositoryStub.getAllNotificationsByUser = jest.fn().mockResolvedValue([]) | ||
| notificationsRepositoryStub.deleteNotificationByUser = jest | ||
| .fn() | ||
| .mockRejectedValue(new Error('Repository error')) | ||
| const sut = new DeleteNotificationByUser(notificationsRepositoryStub) | ||
|
|
||
| await expect(sut.execute(123)).rejects.toThrow('Repository error') | ||
| }) | ||
| }) |
This file contains hidden or 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,44 @@ | ||
| import { GetAllNotificationsByUser } from '../../../src/notifications/domain/useCases/GetAllNotificationsByUser' | ||
| import { INotificationsRepository } from '../../../src/notifications/domain/repositories/INotificationsRepository' | ||
| import { Notification } from '../../../src/notifications/domain/models/Notification' | ||
|
|
||
| const mockNotifications: Notification[] = [ | ||
| { | ||
| id: 1, | ||
| type: 'PUBLISHEDDS', | ||
| subjectText: 'Test notification', | ||
| messageText: 'Test message', | ||
| sentTimestamp: '2025-01-01T00:00:00Z' | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'ASSIGNROLE', | ||
| subjectText: 'Role assignment', | ||
| messageText: 'Role assigned', | ||
| sentTimestamp: '2025-01-01T00:00:00Z' | ||
| } | ||
| ] | ||
|
|
||
| describe('execute', () => { | ||
| test('should return notifications from repository', async () => { | ||
| const notificationsRepositoryStub: INotificationsRepository = {} as INotificationsRepository | ||
| notificationsRepositoryStub.getAllNotificationsByUser = jest | ||
| .fn() | ||
| .mockResolvedValue(mockNotifications) | ||
| const sut = new GetAllNotificationsByUser(notificationsRepositoryStub) | ||
|
|
||
| const result = await sut.execute() | ||
|
|
||
| expect(result).toEqual(mockNotifications) | ||
| }) | ||
|
|
||
| test('should throw error when repository throws error', async () => { | ||
| const notificationsRepositoryStub: INotificationsRepository = {} as INotificationsRepository | ||
| notificationsRepositoryStub.getAllNotificationsByUser = jest | ||
| .fn() | ||
| .mockRejectedValue(new Error('Repository error')) | ||
| const sut = new GetAllNotificationsByUser(notificationsRepositoryStub) | ||
|
|
||
| await expect(sut.execute()).rejects.toThrow('Repository error') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.