Skip to content

Commit fb37429

Browse files
feat: Update notificationsBulkUpdates api to support category and isRead flag based bulk update
1 parent a06da05 commit fb37429

File tree

6 files changed

+88
-37
lines changed

6 files changed

+88
-37
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ await sirenInstance.markAsReadById("your-notification-id");
275275
### 7. markAsReadByDate
276276
277277
This method marks the notifications as read till the given date.<br />
278-
It accepts an ISO date string as an argument.
278+
It accepts a param object as an argument with keys startDate (ISO date string) and category(string).
279279
280280
```bash
281-
await sirenInstance.markAsReadByDate("2011-10-05T14:48:00.000Z");
281+
await sirenInstance.markAsReadByDate({startDate: "2011-10-05T14:48:00.000Z"});
282282
```
283283
284284
### 8. deleteById
@@ -292,10 +292,10 @@ await sirenInstance.deleteById("your-notification-id");
292292
### 9. deleteByDate
293293
294294
This method deletes the notifications till the given date.<br />
295-
It accepts an ISO date string as an argument
295+
It accepts a param object as an argument with keys startDate (ISO date string), isRead(boolean) and category(string).
296296
297297
```bash
298-
await sirenInstance.deleteByDate("2011-10-05T14:48:00.000Z");
298+
await sirenInstance.deleteByDate({startDate: "2011-10-05T14:48:00.000Z"});
299299
```
300300
301301
### 10. markAllAsViewed

examples/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ <h1>Logs</h1>
124124

125125
async function markAllAsRead() {
126126
if (sirenWeb) {
127-
const res= await sirenWeb.markAsReadByDate(new Date().toISOString());
127+
const res= await sirenWeb.markAsReadByDate({startDate: new Date().toISOString()});
128128
document.getElementById("output").innerHTML = 'Mark All as Read ' + JSON.stringify(res, null, 2);
129129
console.log('markAllNotificationsAsRead', res);
130130
}
@@ -140,7 +140,7 @@ <h1>Logs</h1>
140140

141141
async function clearAll() {
142142
if (sirenWeb) {
143-
const res = await sirenWeb.deleteByDate(new Date().toISOString());
143+
const res = await sirenWeb.deleteByDate({startDate: Date().toISOString(), isRead: true});
144144
document.getElementById("output").innerHTML = 'Delete All Notification ' + JSON.stringify(res, null, 2);
145145
console.log('clearAllNotifications', res);
146146
}

src/Siren.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import type {
3232
ActionResponse,
3333
MarkAsViewedResponse,
3434
UnviewedCountReturnResponse,
35+
BulkUpdateParamsType,
36+
BulkUpdateApiParams,
3537
} from "./types";
3638

3739
export class Siren {
@@ -113,9 +115,9 @@ export class Siren {
113115
res && res.data?.status === VerificationStatus.SUCCESS
114116
? VerificationStatus.SUCCESS
115117
: VerificationStatus.FAILED; // api returns null in failed scenarios
116-
if(this.actionCallbacks?.onStatusChange)
118+
if(this.actionCallbacks?.onStatusChange)
117119
this.actionCallbacks.onStatusChange(this.tokenValidationStatus);
118-
120+
119121
return res;
120122
}
121123

@@ -196,7 +198,7 @@ export class Siren {
196198

197199
/**
198200
* Stops the real-time data fetching , if active.
199-
*
201+
*
200202
* @param {EventType} eventType - For identifying the event type. This can be either `NOTIFICATIONS` or `UNVIEWED_COUNT`.
201203
*/
202204
stopRealTimeFetch(eventType: EventType): void {
@@ -243,20 +245,29 @@ export class Siren {
243245
* - An `ActionResponse` object on success, contains "SUCCESS" as data.
244246
*/
245247

246-
async markAsReadByDate(
247-
startDate: string
248-
): Promise<ActionResponse | null> {
248+
async markAsReadByDate(params: BulkUpdateParamsType): Promise<ActionResponse | null> {
249+
const { startDate, category } = params || {};
249250
const hasPermission = this.authorizeUserAction();
250251

251252
if (!hasPermission) return this.getErrorForUnauthorizedAction();
252253
let response;
253254

254-
if (startDate)
255-
response = await notificationsBulkUpdates(this.token, this.recipientId, {
255+
if (startDate) {
256+
const params: BulkUpdateApiParams = {
256257
until: startDate,
257258
operation: BulkUpdateType.MARK_AS_READ,
258-
});
259-
else response = this.emitMissingParameterError();
259+
};
260+
261+
if(category) params.category = category;
262+
263+
response = await notificationsBulkUpdates(
264+
this.token,
265+
this.recipientId,
266+
params
267+
);
268+
} else {
269+
response = this.emitMissingParameterError();
270+
}
260271

261272
return response;
262273
}
@@ -291,21 +302,31 @@ export class Siren {
291302
* @returns {Promise<ActionResponse>}
292303
* - An `ActionResponse` object on success, containing details about the operation.
293304
*/
294-
async deleteByDate(
295-
startDate: string
296-
): Promise<ActionResponse | null> {
305+
async deleteByDate(params: BulkUpdateParamsType): Promise<ActionResponse | null> {
306+
const { startDate, isRead, category } = params || {};
297307
const hasPermission = this.authorizeUserAction();
298308

299309
if (!hasPermission) return this.getErrorForUnauthorizedAction();
300310
let response;
301311

302-
if (startDate)
303-
response = await notificationsBulkUpdates(this.token, this.recipientId, {
312+
if (startDate) {
313+
const params: BulkUpdateApiParams = {
304314
until: startDate,
305315
operation: BulkUpdateType.MARK_AS_DELETED,
306-
});
307-
else response = this.emitMissingParameterError();
316+
};
308317

318+
if (isRead !== undefined) params.isRead = isRead;
319+
if(category) params.category = category;
320+
response = await notificationsBulkUpdates(
321+
this.token,
322+
this.recipientId,
323+
params
324+
);
325+
} else {
326+
327+
response = this.emitMissingParameterError();
328+
}
329+
309330
return response;
310331
}
311332

src/api/notificationsBulkUpdates.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ const operations = {
1010
const notificationsBulkUpdates = (
1111
token: string,
1212
inAppRecipientId: string,
13-
data: { until: string; operation: BulkUpdateType}
13+
data: {
14+
until: string;
15+
isRead?: boolean;
16+
operation: BulkUpdateType;
17+
}
1418
): Promise<ActionResponse> => {
1519
const path = `api/v2/in-app/recipients/${inAppRecipientId}/notifications/bulk-update`;
1620

src/types.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import "promise-polyfill/src/polyfill";
2-
import type { VerificationStatus, EventType, ApiOperationType } from "./constants/generic";
2+
import type {
3+
VerificationStatus,
4+
EventType,
5+
ApiOperationType,
6+
BulkUpdateType
7+
} from "./constants/generic";
38

49
/**
510
* An interface representing the response structure of the Notifications API.
@@ -266,3 +271,24 @@ export type UnviewedCountReturnResponse = {
266271
data?: { unviewedCount: number } | null;
267272
error?: SirenErrorType | null;
268273
};
274+
275+
/**
276+
* Defining the structure of the input param from user calling bulk update functions.
277+
*/
278+
279+
export type BulkUpdateParamsType = {
280+
startDate: string;
281+
isRead?: boolean;
282+
category?: string;
283+
};
284+
285+
/**
286+
* Defining the structure of input param from user while calling bulk update api.
287+
*/
288+
289+
export type BulkUpdateApiParams = {
290+
until: string;
291+
isRead?: boolean;
292+
operation: BulkUpdateType;
293+
category?: string;
294+
};

tests/siren.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ describe("Siren Class", () => {
338338
.mockImplementation(() =>
339339
Promise.resolve({ data: Response, error: null })
340340
);
341-
const res = await sirenInstance.markAsReadByDate(
342-
"2024-02-24T05:47:38.519+00:00"
343-
);
341+
const res = await sirenInstance.markAsReadByDate({
342+
startDate: "2024-02-24T05:47:38.519+00:00"
343+
});
344344

345345
expect(mockApi).toHaveBeenCalledTimes(1);
346346
expect(mockApi).toHaveBeenCalledWith(
@@ -365,9 +365,9 @@ describe("Siren Class", () => {
365365
error: ApiError,
366366
})
367367
);
368-
const res = await sirenInstance.markAsReadByDate(
369-
"2024-02-24T05:47:38.519+00:00"
370-
);
368+
const res = await sirenInstance.markAsReadByDate({
369+
startDate: "2024-02-24T05:47:38.519+00:00"
370+
});
371371

372372
expect(mockApi).toHaveBeenCalledWith(
373373
constructorProps.token,
@@ -468,9 +468,9 @@ describe("Siren Class", () => {
468468
.mockImplementation(() =>
469469
Promise.resolve({ data: Response, error: null })
470470
);
471-
const res = await sirenInstance.deleteByDate(
472-
"2024-02-24T05:47:38.519+00:00"
473-
);
471+
const res = await sirenInstance.deleteByDate({
472+
startDate: "2024-02-24T05:47:38.519+00:00"
473+
});
474474

475475
expect(mockApi).toHaveBeenCalledTimes(1);
476476
expect(mockApi).toHaveBeenCalledWith(
@@ -495,9 +495,9 @@ describe("Siren Class", () => {
495495
error: ApiError,
496496
})
497497
);
498-
const res = await sirenInstance.deleteByDate(
499-
"2024-02-24T05:47:38.519+00:00"
500-
);
498+
const res = await sirenInstance.deleteByDate({
499+
startDate: "2024-02-24T05:47:38.519+00:00"
500+
});
501501

502502
expect(mockApi).toHaveBeenCalledWith(
503503
constructorProps.token,

0 commit comments

Comments
 (0)