Skip to content

Commit b61f8cf

Browse files
committed
fix: update naming and test
1 parent e087a57 commit b61f8cf

File tree

11 files changed

+129
-43
lines changed

11 files changed

+129
-43
lines changed

docs/useCases.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,18 +2152,18 @@ Returns the number of unread notifications for the current authenticated user.
21522152
##### Example call:
21532153

21542154
```typescript
2155-
import { getUnreadCount } from '@iqss/dataverse-client-javascript'
2155+
import { getUnreadNotificationsCount } from '@iqss/dataverse-client-javascript'
21562156

21572157
/* ... */
21582158

2159-
getUnreadCount.execute().then((count: number) => {
2159+
getUnreadNotificationsCount.execute().then((count: number) => {
21602160
console.log(`You have ${count} unread notifications`)
21612161
})
21622162

21632163
/* ... */
21642164
```
21652165

2166-
_See [use case](../src/notifications/domain/useCases/GetUnreadCount.ts) implementation_.
2166+
_See [use case](../src/notifications/domain/useCases/GetUnreadNotificationsCount.ts) implementation_.
21672167

21682168
#### Mark As Read
21692169

@@ -2172,20 +2172,20 @@ Marks a specific notification as read for the current authenticated user. This o
21722172
##### Example call:
21732173

21742174
```typescript
2175-
import { markAsRead } from '@iqss/dataverse-client-javascript'
2175+
import { markNotificationAsRead } from '@iqss/dataverse-client-javascript'
21762176

21772177
/* ... */
21782178

21792179
const notificationId = 123
21802180

2181-
markAsRead.execute(notificationId).then(() => {
2181+
markNotificationAsRead.execute(notificationId).then(() => {
21822182
console.log('Notification marked as read')
21832183
})
21842184

21852185
/* ... */
21862186
```
21872187

2188-
_See [use case](../src/notifications/domain/useCases/MarkAsRead.ts) implementation_.
2188+
_See [use case](../src/notifications/domain/useCases/MarkNotificationAsRead.ts) implementation_.
21892189

21902190
## Search
21912191

src/notifications/domain/models/Notification.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export interface Notification {
5454
userGuidesVersion?: string
5555
userGuidesSectionPath?: string
5656
roleAssignments?: RoleAssignment[]
57-
dataverseAlias?: string
58-
dataverseDisplayName?: string
57+
collectionAlias?: string
58+
collectionDisplayName?: string
5959
datasetPersistentIdentifier?: string
6060
datasetDisplayName?: string
6161
ownerPersistentIdentifier?: string

src/notifications/domain/repositories/INotificationsRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { Notification } from '../models/Notification'
33
export interface INotificationsRepository {
44
getAllNotificationsByUser(inAppNotificationFormat?: boolean): Promise<Notification[]>
55
deleteNotification(notificationId: number): Promise<void>
6-
getUnreadCount(): Promise<number>
7-
markAsRead(notificationId: number): Promise<void>
6+
getUnreadNotificationsCount(): Promise<number>
7+
markNotificationAsRead(notificationId: number): Promise<void>
88
}

src/notifications/domain/useCases/GetUnreadCount.ts renamed to src/notifications/domain/useCases/GetUnreadNotificationsCount.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { UseCase } from '../../../core/domain/useCases/UseCase'
22
import { INotificationsRepository } from '../repositories/INotificationsRepository'
33

4-
export class GetUnreadCount implements UseCase<number> {
4+
export class GetUnreadNotificationsCount implements UseCase<number> {
55
private notificationsRepository: INotificationsRepository
66

77
constructor(notificationsRepository: INotificationsRepository) {
@@ -14,6 +14,6 @@ export class GetUnreadCount implements UseCase<number> {
1414
* @returns {Promise<number>} - A promise that resolves to the number of unread notifications.
1515
*/
1616
async execute(): Promise<number> {
17-
return await this.notificationsRepository.getUnreadCount()
17+
return await this.notificationsRepository.getUnreadNotificationsCount()
1818
}
1919
}

src/notifications/domain/useCases/MarkAsRead.ts renamed to src/notifications/domain/useCases/MarkNotificationAsRead.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { UseCase } from '../../../core/domain/useCases/UseCase'
22
import { INotificationsRepository } from '../repositories/INotificationsRepository'
33

4-
export class MarkAsRead implements UseCase<void> {
4+
export class MarkNotificationAsRead implements UseCase<void> {
55
private notificationsRepository: INotificationsRepository
66

77
constructor(notificationsRepository: INotificationsRepository) {
@@ -15,6 +15,6 @@ export class MarkAsRead implements UseCase<void> {
1515
* @returns {Promise<void>} - A promise that resolves when the notification is marked as read.
1616
*/
1717
async execute(notificationId: number): Promise<void> {
18-
return await this.notificationsRepository.markAsRead(notificationId)
18+
return await this.notificationsRepository.markNotificationAsRead(notificationId)
1919
}
2020
}

src/notifications/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { NotificationsRepository } from './infra/repositories/NotificationsRepository'
22
import { GetAllNotificationsByUser } from './domain/useCases/GetAllNotificationsByUser'
33
import { DeleteNotification } from './domain/useCases/DeleteNotification'
4-
import { GetUnreadCount } from './domain/useCases/GetUnreadCount'
5-
import { MarkAsRead } from './domain/useCases/MarkAsRead'
4+
import { GetUnreadNotificationsCount } from './domain/useCases/GetUnreadNotificationsCount'
5+
import { MarkNotificationAsRead } from './domain/useCases/MarkNotificationAsRead'
66

77
const notificationsRepository = new NotificationsRepository()
88

99
const getAllNotificationsByUser = new GetAllNotificationsByUser(notificationsRepository)
1010
const deleteNotification = new DeleteNotification(notificationsRepository)
11-
const getUnreadCount = new GetUnreadCount(notificationsRepository)
12-
const markAsRead = new MarkAsRead(notificationsRepository)
11+
const getUnreadNotificationsCount = new GetUnreadNotificationsCount(notificationsRepository)
12+
const markNotificationAsRead = new MarkNotificationAsRead(notificationsRepository)
1313

14-
export { getAllNotificationsByUser, deleteNotification, getUnreadCount, markAsRead }
14+
export {
15+
getAllNotificationsByUser,
16+
deleteNotification,
17+
getUnreadNotificationsCount,
18+
markNotificationAsRead
19+
}
1520

1621
export { Notification, NotificationType, RoleAssignment } from './domain/models/Notification'

src/notifications/infra/repositories/NotificationsRepository.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiRepository } from '../../../core/infra/repositories/ApiRepository'
22
import { INotificationsRepository } from '../../domain/repositories/INotificationsRepository'
33
import { Notification } from '../../domain/models/Notification'
4+
import { NotificationPayload } from '../transformers/NotificationPayload'
45

56
export class NotificationsRepository extends ApiRepository implements INotificationsRepository {
67
private readonly notificationsResourceName: string = 'notifications'
@@ -14,7 +15,17 @@ export class NotificationsRepository extends ApiRepository implements INotificat
1415
true,
1516
queryParams
1617
)
17-
.then((response) => response.data.data.notifications as Notification[])
18+
.then((response) => {
19+
const notifications = response.data.data.notifications
20+
return notifications.map((notification: NotificationPayload) => {
21+
const { dataverseDisplayName, dataverseAlias, ...restNotification } = notification
22+
return {
23+
...restNotification,
24+
...(dataverseDisplayName && { collectionDisplayName: dataverseDisplayName }),
25+
...(dataverseAlias && { collectionAlias: dataverseAlias })
26+
}
27+
}) as Notification[]
28+
})
1829
.catch((error) => {
1930
throw error
2031
})
@@ -30,14 +41,14 @@ export class NotificationsRepository extends ApiRepository implements INotificat
3041
})
3142
}
3243

33-
public async getUnreadCount(): Promise<number> {
44+
public async getUnreadNotificationsCount(): Promise<number> {
3445
return this.doGet(
3546
this.buildApiEndpoint(this.notificationsResourceName, 'unreadCount'),
3647
true
3748
).then((response) => response.data.data.unreadCount as number)
3849
}
3950

40-
public async markAsRead(notificationId: number): Promise<void> {
51+
public async markNotificationAsRead(notificationId: number): Promise<void> {
4152
return this.doPut(
4253
this.buildApiEndpoint(this.notificationsResourceName, 'markAsRead', notificationId),
4354
{}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { RoleAssignment } from '../../domain/models/Notification'
2+
3+
export interface NotificationPayload {
4+
id: number
5+
type: string
6+
subjectText?: string
7+
messageText?: string
8+
sentTimestamp: string
9+
displayAsRead: boolean
10+
installationBrandName?: string
11+
userGuidesBaseUrl?: string
12+
userGuidesVersion?: string
13+
userGuidesSectionPath?: string
14+
roleAssignments?: RoleAssignment[]
15+
dataverseAlias?: string
16+
dataverseDisplayName?: string
17+
datasetPersistentIdentifier?: string
18+
datasetDisplayName?: string
19+
ownerPersistentIdentifier?: string
20+
ownerAlias?: string
21+
ownerDisplayName?: string
22+
requestorFirstName?: string
23+
requestorLastName?: string
24+
requestorEmail?: string
25+
dataFileId?: number
26+
dataFileDisplayName?: string
27+
currentCurationStatus?: string
28+
additionalInfo?: string
29+
objectDeleted?: boolean
30+
}

test/integration/notifications/NotificationsRepository.test.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import {
1111
import { createDataset, CreatedDatasetIdentifiers } from '../../../src/datasets'
1212
import { publishDatasetViaApi, waitForNoLocks } from '../../testHelpers/datasets/datasetHelper'
1313
import { WriteError } from '../../../src'
14+
import { createCollection } from '../../../src/collections'
15+
import {
16+
createCollectionDTO,
17+
deleteCollectionViaApi
18+
} from '../../testHelpers/collections/collectionHelper'
1419

1520
describe('NotificationsRepository', () => {
1621
const sut: NotificationsRepository = new NotificationsRepository()
@@ -90,17 +95,18 @@ describe('NotificationsRepository', () => {
9095
expect(notification).toHaveProperty('displayAsRead')
9196
})
9297

93-
test('should find notification with ASSIGNROLE type', async () => {
98+
test('should find notification with ASSIGNROLE type that has not been deleted', async () => {
9499
const notifications: Notification[] = await sut.getAllNotificationsByUser(true)
95100

96-
// Find a notification with ASSIGNROLE type
97-
const assignRoleNotification = notifications.find((n) => n.type === NotificationType.ASSIGNROLE)
101+
const assignRoleNotification = notifications.find(
102+
(n) => n.type === NotificationType.ASSIGNROLE && !n.objectDeleted
103+
)
98104

99105
expect(assignRoleNotification).toBeDefined()
100106
expect(assignRoleNotification?.type).toBe(NotificationType.ASSIGNROLE)
101107
expect(assignRoleNotification?.sentTimestamp).toBeDefined()
102108
expect(assignRoleNotification?.displayAsRead).toBeDefined()
103-
expect(assignRoleNotification?.dataverseDisplayName).toBeDefined()
109+
expect(assignRoleNotification?.collectionDisplayName).toBeDefined()
104110

105111
expect(assignRoleNotification?.roleAssignments).toBeDefined()
106112
expect(assignRoleNotification?.roleAssignments?.length).toBeGreaterThan(0)
@@ -110,14 +116,42 @@ describe('NotificationsRepository', () => {
110116
expect(assignRoleNotification?.roleAssignments?.[0]).toHaveProperty('definitionPointId')
111117
})
112118

119+
test('should create a collection and find the notification with CREATEDV type', async () => {
120+
const testCollectionAlias = 'test-notification-collection'
121+
const createdCollectionId = await createCollection.execute(
122+
createCollectionDTO(testCollectionAlias)
123+
)
124+
125+
expect(createdCollectionId).toBeDefined()
126+
expect(createdCollectionId).toBeGreaterThan(0)
127+
128+
const notifications: Notification[] = await sut.getAllNotificationsByUser(true)
129+
expect(Array.isArray(notifications)).toBe(true)
130+
expect(notifications.length).toBeGreaterThan(0)
131+
132+
const createdvNotification = notifications.find(
133+
(n) => n.collectionAlias === testCollectionAlias
134+
)
135+
136+
expect(createdvNotification).toBeDefined()
137+
expect(createdvNotification?.type).toBe(NotificationType.CREATEDV)
138+
expect(createdvNotification?.collectionAlias).toBe(testCollectionAlias)
139+
expect(createdvNotification?.sentTimestamp).toBeDefined()
140+
expect(createdvNotification?.displayAsRead).toBe(false)
141+
expect(createdvNotification?.collectionDisplayName).toBe('Test Collection')
142+
expect(createdvNotification?.collectionAlias).toBe(testCollectionAlias)
143+
144+
await deleteCollectionViaApi(testCollectionAlias)
145+
})
146+
113147
test('should return array when inAppNotificationFormat is false', async () => {
114148
const notifications: Notification[] = await sut.getAllNotificationsByUser(false)
115149

116150
expect(Array.isArray(notifications)).toBe(true)
117151
})
118152

119153
test('should return unread count', async () => {
120-
const unreadCount = await sut.getUnreadCount()
154+
const unreadCount = await sut.getUnreadNotificationsCount()
121155

122156
expect(typeof unreadCount).toBe('number')
123157
expect(unreadCount).toBeGreaterThanOrEqual(0)
@@ -130,7 +164,7 @@ describe('NotificationsRepository', () => {
130164

131165
const unreadNotification = notifications[0]
132166

133-
await expect(sut.markAsRead(unreadNotification.id)).resolves.toBeUndefined()
167+
await expect(sut.markNotificationAsRead(unreadNotification.id)).resolves.toBeUndefined()
134168

135169
const updatedNotifications: Notification[] = await sut.getAllNotificationsByUser()
136170
const updatedNotification = updatedNotifications.find((n) => n.id === unreadNotification.id)
@@ -145,6 +179,8 @@ describe('NotificationsRepository', () => {
145179
`[404] Notification ${nonExistentNotificationId} not found.`
146180
)
147181

148-
await expect(sut.markAsRead(nonExistentNotificationId)).rejects.toThrow(expectedError)
182+
await expect(sut.markNotificationAsRead(nonExistentNotificationId)).rejects.toThrow(
183+
expectedError
184+
)
149185
})
150186
})

test/unit/notifications/GetUnreadCount.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import { GetUnreadCount } from '../../../src/notifications/domain/useCases/GetUnreadCount'
1+
import { GetUnreadNotificationsCount } from '../../../src/notifications/domain/useCases/GetUnreadNotificationsCount'
22
import { INotificationsRepository } from '../../../src/notifications/domain/repositories/INotificationsRepository'
33
import { ReadError } from '../../../src'
44

5-
describe('GetUnreadCount', () => {
5+
describe('GetUnreadNotificationsCount', () => {
66
test('should return unread count from repository', async () => {
77
const notificationsRepositoryStub: INotificationsRepository = {} as INotificationsRepository
88

9-
notificationsRepositoryStub.getUnreadCount = jest.fn().mockResolvedValue(5)
10-
const sut = new GetUnreadCount(notificationsRepositoryStub)
9+
notificationsRepositoryStub.getUnreadNotificationsCount = jest.fn().mockResolvedValue(5)
10+
const sut = new GetUnreadNotificationsCount(notificationsRepositoryStub)
1111

1212
const result = await sut.execute()
1313

14-
expect(notificationsRepositoryStub.getUnreadCount).toHaveBeenCalledWith()
14+
expect(notificationsRepositoryStub.getUnreadNotificationsCount).toHaveBeenCalledWith()
1515
expect(result).toBe(5)
1616
})
1717

1818
test('should throw error when repository throws error', async () => {
1919
const notificationsRepositoryStub: INotificationsRepository = {} as INotificationsRepository
20-
notificationsRepositoryStub.getUnreadCount = jest.fn().mockRejectedValue(new ReadError())
21-
const sut = new GetUnreadCount(notificationsRepositoryStub)
20+
notificationsRepositoryStub.getUnreadNotificationsCount = jest
21+
.fn()
22+
.mockRejectedValue(new ReadError())
23+
const sut = new GetUnreadNotificationsCount(notificationsRepositoryStub)
2224

2325
await expect(sut.execute()).rejects.toThrow(ReadError)
2426
})

0 commit comments

Comments
 (0)