diff --git a/package.json b/package.json index 5dee606..ac327ec 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/notificationapi.test.ts b/src/__tests__/notificationapi.test.ts index bbae46a..d6c9873 100644 --- a/src/__tests__/notificationapi.test.ts +++ b/src/__tests__/notificationapi.test.ts @@ -7,6 +7,7 @@ import { Channels, CreateSubNotificationRequest, DeleteSubNotificationRequest, + InAppNotificationPatchRequest, PushProviders, SendRequest, SetUserPreferencesRequest, @@ -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 = { + 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'; diff --git a/src/interfaces.ts b/src/interfaces.ts index f173a4b..ae4e97a 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -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; + }; +} diff --git a/src/notificationapi.ts b/src/notificationapi.ts index d96aa93..3612d6f 100644 --- a/src/notificationapi.ts +++ b/src/notificationapi.ts @@ -1,6 +1,7 @@ import { CreateSubNotificationRequest, DeleteSubNotificationRequest, + InAppNotificationPatchRequest, InitConfiguration, RetractRequest, SendRequest, @@ -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; @@ -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