Skip to content

Commit

Permalink
feat: passing fcmToken when using mobile (#4823)
Browse files Browse the repository at this point in the history
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

This PR adds support to FCM Token to be passed as an optional param when
using mobile platform since mobile handle FCM Token creation natively
through Firebase SDK.

## References


[NOTIFY-1240](https://consensyssoftware.atlassian.net/browse/NOTIFY-1240?atlOrigin=eyJpIjoiNTM2YTJiYzViYWMyNDBlMGExOGRmZjdjYmExY2MxMTIiLCJwIjoiaiJ9)

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Changelog

@notification-services-controller

ADDED: optional FCM token to use for push notifications on
enablePushNotifications

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes


[NOTIFY-1240]:
https://consensyssoftware.atlassian.net/browse/NOTIFY-1240?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
Jonathansoufer authored Oct 21, 2024
1 parent 5c04d50 commit fe28a23
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import type { PushNotificationEnv } from './types';

const MOCK_JWT = 'mockJwt';
const MOCK_FCM_TOKEN = 'mockFcmToken';
const MOCK_MOBILE_FCM_TOKEN = 'mockMobileFcmToken';
const MOCK_TRIGGERS = ['uuid1', 'uuid2'];

describe('NotificationServicesPushController', () => {
const arrangeServicesMocks = () => {
const arrangeServicesMocks = (token?: string) => {
const activatePushNotificationsMock = jest
.spyOn(services, 'activatePushNotifications')
.mockResolvedValue(MOCK_FCM_TOKEN);
.mockResolvedValue(token ?? MOCK_FCM_TOKEN);

const deactivatePushNotificationsMock = jest
.spyOn(services, 'deactivatePushNotifications')
Expand Down Expand Up @@ -58,6 +59,20 @@ describe('NotificationServicesPushController', () => {

expect(services.listenToPushNotifications).toHaveBeenCalled();
});

it('should update the state with provided mobile fcmToken', async () => {
arrangeServicesMocks(MOCK_MOBILE_FCM_TOKEN);
const { controller, messenger } = arrangeMockMessenger();
mockAuthBearerTokenCall(messenger);

await controller.enablePushNotifications(
MOCK_TRIGGERS,
MOCK_MOBILE_FCM_TOKEN,
);
expect(controller.state.fcmToken).toBe(MOCK_MOBILE_FCM_TOKEN);

expect(services.listenToPushNotifications).toHaveBeenCalled();
});
});

describe('disablePushNotifications', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ export default class NotificationServicesPushController extends BaseController<
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
*
* @param UUIDs - An array of UUIDs to enable push notifications for.
* @param fcmToken - The optional FCM token to use for push notifications.
*/
async enablePushNotifications(UUIDs: string[]) {
async enablePushNotifications(UUIDs: string[], fcmToken?: string) {
if (!this.#config.isPushEnabled) {
return;
}
Expand All @@ -267,6 +268,7 @@ export default class NotificationServicesPushController extends BaseController<
bearerToken,
triggers: UUIDs,
env: this.#env,
fcmToken,
createRegToken,
platform: this.#config.platform,
}).catch(() => null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

const MOCK_REG_TOKEN = 'REG_TOKEN';
const MOCK_NEW_REG_TOKEN = 'NEW_REG_TOKEN';
const MOCK_MOBILE_FCM_TOKEN = 'mockMobileFcmToken';
const MOCK_TRIGGERS = ['1', '2', '3'];
const MOCK_JWT = 'MOCK_JWT';

Expand Down Expand Up @@ -78,8 +79,15 @@ describe('NotificationServicesPushController Services', () => {
env: {} as PushNotificationEnv,
};

const mobileParams = {
...params,
fcmToken: MOCK_MOBILE_FCM_TOKEN,
platform: 'mobile' as const,
};

return {
params,
mobileParams,
apis: {
mockGet: mockEndpointGetPushNotificationLinks(override?.mockGet),
mockPut: mockEndpointUpdatePushNotificationLinks(override?.mockPut),
Expand All @@ -98,6 +106,17 @@ describe('NotificationServicesPushController Services', () => {
expect(result).toBe(MOCK_NEW_REG_TOKEN);
});

it('should successfully call APIs and add provided mobile fcmToken', async () => {
const { mobileParams, apis } = arrangeMocks();
const result = await activatePushNotifications(mobileParams);

expect(apis.mockGet.isDone()).toBe(true);
expect(mobileParams.createRegToken).not.toHaveBeenCalled();
expect(apis.mockPut.isDone()).toBe(true);

expect(result).toBe(MOCK_MOBILE_FCM_TOKEN);
});

it('should return null if unable to get links from API', async () => {
const { params, apis } = arrangeMocks({ mockGet: { status: 500 } });
const result = await activatePushNotifications(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type ActivatePushNotificationsParams = {
env: PushNotificationEnv;
createRegToken: CreateRegToken;
platform: 'extension' | 'mobile' | 'portfolio';
fcmToken?: string;
};

/**
Expand All @@ -102,15 +103,16 @@ type ActivatePushNotificationsParams = {
export async function activatePushNotifications(
params: ActivatePushNotificationsParams,
): Promise<string | null> {
const { bearerToken, triggers, env, createRegToken, platform } = params;
const { bearerToken, triggers, env, createRegToken, platform, fcmToken } =
params;

const notificationLinks = await getPushNotificationLinks(bearerToken);

if (!notificationLinks) {
return null;
}

const regToken = await createRegToken(env).catch(() => null);
const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));
if (!regToken) {
return null;
}
Expand Down

0 comments on commit fe28a23

Please sign in to comment.