Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fcm): do not mutate overrides object #4866

Merged
merged 4 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions providers/fcm/src/lib/fcm.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,37 @@ test('should clean the payload for the FCM data message', async () => {
data: cleanPayload,
});
});

test('should trigger fcm multiple times with the same overrides', async () => {
const tokens = ['tester1', 'tester2'];
const overrides: IPushOptions['overrides'] = {
type: 'data',
data: { foo: 'bar' },
};

tokens.forEach(async (token) => {
await provider.sendMessage({
title: 'Test',
content: 'Test push',
target: [token],
payload: {
sound: 'test_sound',
},
overrides,
subscriber,
step,
});
expect(app.initializeApp).toHaveBeenCalledTimes(1);
expect(app.cert).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
tokens: [token],
data: {
title: 'Test',
body: 'Test push',
message: 'Test push',
sound: 'test_sound',
},
});
});
});
47 changes: 23 additions & 24 deletions providers/fcm/src/lib/fcm.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,25 @@ export class FcmPushProvider implements IPushProvider {
async sendMessage(
options: IPushOptions
): Promise<ISendMessageSuccessResponse> {
delete (options.overrides as { deviceTokens?: string[] })?.deviceTokens;
const {
deviceTokens: _,
type,
android,
apns,
fcmOptions,
webPush: webpush,
data,
...overridesData
} = (options.overrides as IPushOptions['overrides'] & {
deviceTokens?: string[];
webPush: { [key: string]: { [key: string]: string } | string };
}) || {};

const overridesData = options.overrides || ({} as any);
const payload = this.cleanPayload(options.payload);

const androidData: AndroidConfig = overridesData.android;
const apnsData: ApnsConfig = overridesData.apns;
const fcmOptionsData: FcmOptions = overridesData.fcmOptions;
const webPushData: WebpushConfig = overridesData.webPush;
delete overridesData.android;
delete overridesData.apns;
delete overridesData.fcmOptions;
delete overridesData.webPush;

let res;

if (overridesData?.type === 'data') {
delete (options.overrides as { type?: string })?.type;
if (type === 'data') {
res = await this.messaging.sendMulticast({
tokens: options.target,
data: {
Expand All @@ -72,26 +73,24 @@ export class FcmPushProvider implements IPushProvider {
body: options.content,
message: options.content,
},
...(androidData ? { android: androidData } : {}),
...(apnsData ? { apns: apnsData } : {}),
...(fcmOptionsData ? { fcmOptions: fcmOptionsData } : {}),
...(webPushData ? { webpush: webPushData } : {}),
android,
apns,
fcmOptions,
webpush,
});
} else {
const { data, ...overrides } = overridesData;

res = await this.messaging.sendMulticast({
tokens: options.target,
notification: {
title: options.title,
body: options.content,
...overrides,
...overridesData,
},
data,
...(androidData ? { android: androidData } : {}),
...(apnsData ? { apns: apnsData } : {}),
...(fcmOptionsData ? { fcmOptions: fcmOptionsData } : {}),
...(webPushData ? { webpush: webPushData } : {}),
android,
apns,
fcmOptions,
webpush,
});
}

Expand Down
Loading