Skip to content

Commit

Permalink
add updateInAppNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasadi committed May 15, 2024
1 parent 30a5e10 commit 7e1521d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notificationapi-node-server-sdk",
"version": "2.0.1",
"version": "2.1.0",
"description": "NotificationAPI server-side library for Node.js",
"keywords": [
"notificationapi",
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/notificationapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Channels,
CreateSubNotificationRequest,
DeleteSubNotificationRequest,
InAppNotificationPatchRequest,
PushProviders,
SendRequest,
SetUserPreferencesRequest,
Expand Down Expand Up @@ -671,6 +672,39 @@ describe('deleteUserPreferences with subNotificationId', () => {
);
});
});
describe('updateInAppNotification without subNotificationId', () => {
const retractEndPointRegex = /.*\/users\/.*/;
const clientId = 'testClientId';
const clientSecret = 'testClientSecret';
const userId = 'testUserId';
const hashedUserId = `${createHmac('sha256', clientSecret)
.update(userId)
.digest('base64')}`;
const params: Required<InAppNotificationPatchRequest> = {
trackingIds: ['testTrackingId'],
opened: '1970-01-01T00:00:00.000Z',
clicked: '1970-01-01T00:00:00.000Z',
archived: '1970-01-01T00:00:00.000Z',
actioned1: '1970-01-01T00:00:00.000Z',
actioned2: '1970-01-01T00:00:00.000Z',
reply: { date: '1970-01-01T00:00:00.000Z', message: 'nice!' }
};

test('makes API calls to the correct end-point', async () => {
axiosMock.onPatch(retractEndPointRegex).reply(200);
await notificationapi.init(clientId, clientSecret);
await notificationapi.updateInAppNotification(userId, params);
expect(axiosMock.history.patch).toHaveLength(1);
expect(axiosMock.history.patch[0].url).toEqual(
`https://api.notificationapi.com/${clientId}/users/${userId}/notifications/INAPP_WEB`
);
expect(axiosMock.history.patch[0].params).toEqual(undefined);
expect(axiosMock.history.patch[0].headers.Authorization).toEqual(
'Basic ' +
Buffer.from(`${clientId}:${userId}:${hashedUserId}`).toString('base64')
);
});
});
describe('Identify user', () => {
const userEndPointRegex = /.*\/users\/.*/;
const clientId = 'testClientId_identify_user';
Expand Down
12 changes: 12 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,15 @@ export interface PushSubscription {
export interface WebPushToken {
sub: PushSubscription;
}
export interface InAppNotificationPatchRequest {
trackingIds: string[];
opened?: string;
clicked?: string;
archived?: string;
actioned1?: string;
actioned2?: string;
reply?: {
date: string;
message: string;
};
}
28 changes: 26 additions & 2 deletions src/notificationapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CreateSubNotificationRequest,
DeleteSubNotificationRequest,
InAppNotificationPatchRequest,
InitConfiguration,
RetractRequest,
SendRequest,
Expand All @@ -14,7 +15,7 @@ const DEFAULT_BASE_URL = 'https://api.notificationapi.com';

class NotificationAPIService {
private USER_AGENT = 'notificationapi-node-server-sdk';
private VERSION = '2.0.1';
private VERSION = '2.1.0';

clientId: null | string = null;
clientSecret: null | string = null;
Expand Down Expand Up @@ -123,7 +124,30 @@ class NotificationAPIService {
: { notificationId }
);
};
/** Used to to update a scheduled notification. */

/** Used to update the opened, archived, ... of an inapp notification. */
updateInAppNotification = async (
/** The ID of the user in your system. Required.*/
userId: string,
params: InAppNotificationPatchRequest
) => {
const hashedUserId = `${createHmac('sha256', this.clientSecret as string)
.update(userId)
.digest('base64')}`;
const customAuthorization =
'Basic ' +
Buffer.from(`${this.clientId}:${userId}:${hashedUserId}`).toString(
'base64'
);
return this.request(
'PATCH',
`users/${userId}/notifications/INAPP_WEB`,
params,
customAuthorization
);
};

/** Used to update a scheduled notification. */
updateSchedule = async (
trackingId: string,
sendRequest: Partial<SendRequest>
Expand Down

0 comments on commit 7e1521d

Please sign in to comment.