Skip to content

Commit 0c49ec5

Browse files
committed
feat: update notification usecases
1 parent 7f3e9eb commit 0c49ec5

File tree

13 files changed

+361
-0
lines changed

13 files changed

+361
-0
lines changed

docs/useCases.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ The different use cases currently available in the package are classified below,
8585
- [Get Application Terms of Use](#get-application-terms-of-use)
8686
- [Contact](#Contact)
8787
- [Send Feedback to Object Contacts](#send-feedback-to-object-contacts)
88+
- [Notifications](#Notifications)
89+
- [Get All Notifications by User](#get-all-notifications-by-user)
90+
- [Delete Notification by User](#delete-notification-by-user)
8891

8992
## Collections
9093

@@ -1991,3 +1994,47 @@ In ContactDTO, it takes the following information:
19911994
- **subject**: the email subject line.
19921995
- **body**: the email body to send.
19931996
- **fromEmail**: the email to list in the reply-to field.
1997+
1998+
## Notifications
1999+
2000+
#### Get All Notifications by User
2001+
2002+
Returns a [Notification](../src/notifications/domain/models/Notification.ts) array containing all notifications for the current authenticated user.
2003+
2004+
##### Example call:
2005+
2006+
```typescript
2007+
import { getAllNotificationsByUser } from '@iqss/dataverse-client-javascript'
2008+
2009+
/* ... */
2010+
2011+
getAllNotificationsByUser.execute().then((notifications: Notification[]) => {
2012+
/* ... */
2013+
})
2014+
2015+
/* ... */
2016+
```
2017+
2018+
_See [use case](../src/notifications/domain/useCases/GetAllNotificationsByUser.ts) implementation_.
2019+
2020+
#### Delete Notification by User
2021+
2022+
Deletes a specific notification for the current authenticated user by its ID.
2023+
2024+
##### Example call:
2025+
2026+
```typescript
2027+
import { deleteNotificationByUser } from '@iqss/dataverse-client-javascript'
2028+
2029+
/* ... */
2030+
2031+
const notificationId = 123
2032+
2033+
deleteNotificationByUser.execute(notificationId: number).then(() => {
2034+
/* ... */
2035+
})
2036+
2037+
/* ... */
2038+
```
2039+
2040+
_See [use case](../src/notifications/domain/useCases/DeleteNotificationByUser.ts) implementation_.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './collections'
88
export * from './metadataBlocks'
99
export * from './files'
1010
export * from './contactInfo'
11+
export * from './notifications'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Notification {
2+
id: number
3+
type: string
4+
subjectText: string
5+
messageText: string
6+
sentTimestamp: string
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Notification } from '../models/Notification'
2+
3+
export interface INotificationsRepository {
4+
getAllNotificationsByUser(): Promise<Notification[]>
5+
deleteNotificationByUser(notificationId: number): Promise<void>
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { UseCase } from '../../../core/domain/useCases/UseCase'
2+
import { INotificationsRepository } from '../repositories/INotificationsRepository'
3+
4+
/**
5+
* Use case for deleting a specific notification for the current user.
6+
*
7+
* @param notificationId - The ID of the notification to delete.
8+
* @returns {Promise<void>} - A promise that resolves when the notification is deleted.
9+
*/
10+
export class DeleteNotificationByUser implements UseCase<void> {
11+
constructor(private readonly notificationsRepository: INotificationsRepository) {}
12+
13+
async execute(notificationId: number): Promise<void> {
14+
return this.notificationsRepository.deleteNotificationByUser(notificationId)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { UseCase } from '../../../core/domain/useCases/UseCase'
2+
import { Notification } from '../models/Notification'
3+
import { INotificationsRepository } from '../repositories/INotificationsRepository'
4+
5+
export class GetAllNotificationsByUser implements UseCase<Notification[]> {
6+
constructor(private readonly notificationsRepository: INotificationsRepository) {}
7+
8+
/**
9+
* Use case for retrieving all notifications for the current user.
10+
*
11+
* @returns {Promise<Notification[]>} - A promise that resolves to an array of Notification instances.
12+
*/
13+
async execute(): Promise<Notification[]> {
14+
return (await this.notificationsRepository.getAllNotificationsByUser()) as Notification[]
15+
}
16+
}

src/notifications/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NotificationsRepository } from './infra/repositories/NotificationsRepository'
2+
import { GetAllNotificationsByUser } from './domain/useCases/GetAllNotificationsByUser'
3+
import { DeleteNotificationByUser } from './domain/useCases/DeleteNotificationByUser'
4+
5+
const notificationsRepository = new NotificationsRepository()
6+
7+
const getAllNotificationsByUser = new GetAllNotificationsByUser(notificationsRepository)
8+
const deleteNotificationByUser = new DeleteNotificationByUser(notificationsRepository)
9+
10+
export { getAllNotificationsByUser, deleteNotificationByUser }
11+
12+
export { Notification } from './domain/models/Notification'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ApiRepository } from '../../../core/infra/repositories/ApiRepository'
2+
import { INotificationsRepository } from '../../domain/repositories/INotificationsRepository'
3+
import { Notification } from '../../domain/models/Notification'
4+
5+
export class NotificationsRepository extends ApiRepository implements INotificationsRepository {
6+
private readonly notificationsResourceName: string = 'notifications'
7+
8+
public async getAllNotificationsByUser(): Promise<Notification[]> {
9+
return this.doGet(this.buildApiEndpoint(this.notificationsResourceName, 'all'), true)
10+
.then((response) => response.data.data.notifications as Notification[])
11+
.catch((error) => {
12+
throw error
13+
})
14+
}
15+
16+
public async deleteNotificationByUser(notificationId: number): Promise<void> {
17+
return this.doDelete(
18+
this.buildApiEndpoint(this.notificationsResourceName, notificationId.toString())
19+
)
20+
.then(() => {})
21+
.catch((error) => {
22+
throw error
23+
})
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
ApiConfig,
3+
deleteNotificationByUser,
4+
getAllNotificationsByUser,
5+
WriteError
6+
} from '../../../src'
7+
import { TestConstants } from '../../testHelpers/TestConstants'
8+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
9+
10+
describe('execute', () => {
11+
beforeEach(async () => {
12+
ApiConfig.init(
13+
TestConstants.TEST_API_URL,
14+
DataverseApiAuthMechanism.API_KEY,
15+
process.env.TEST_API_KEY
16+
)
17+
})
18+
19+
test('should successfully delete a notification for authenticated user', async () => {
20+
const notificationId = 1
21+
await deleteNotificationByUser.execute(notificationId)
22+
23+
const notifications = await getAllNotificationsByUser.execute()
24+
expect(notifications.length).toBe(0)
25+
})
26+
27+
test('should throw an error when the notification id does not exist', async () => {
28+
await expect(deleteNotificationByUser.execute(123)).rejects.toThrow(WriteError)
29+
})
30+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ApiConfig, getAllNotificationsByUser, Notification } from '../../../src'
2+
import { TestConstants } from '../../testHelpers/TestConstants'
3+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
4+
5+
describe('execute', () => {
6+
beforeEach(async () => {
7+
ApiConfig.init(
8+
TestConstants.TEST_API_URL,
9+
DataverseApiAuthMechanism.API_KEY,
10+
process.env.TEST_API_KEY
11+
)
12+
})
13+
14+
test('should successfully return notifications for authenticated user', async () => {
15+
const notifications: Notification[] = await getAllNotificationsByUser.execute()
16+
17+
expect(notifications).not.toBeNull()
18+
expect(Array.isArray(notifications)).toBe(true)
19+
})
20+
21+
test('should have correct notification properties if notifications exist', async () => {
22+
const notifications = await getAllNotificationsByUser.execute()
23+
if (notifications.length === 0) {
24+
return
25+
}
26+
const first = notifications[0]
27+
expect(first).toHaveProperty('id')
28+
expect(first).toHaveProperty('type')
29+
expect(first).toHaveProperty('subjectText')
30+
expect(first).toHaveProperty('messageText')
31+
expect(first).toHaveProperty('sentTimestamp')
32+
})
33+
})

0 commit comments

Comments
 (0)