Skip to content

Commit c547387

Browse files
committed
Save new player_id if returned from on_sessionThis happens when the player id deleted via the OneSignal dashboardand the user returns to the site
1 parent a323e2f commit c547387

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/managers/UpdateManager.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ export class UpdateManager {
8181
}
8282

8383
try {
84-
await OneSignalApiShared.updateUserSession(deviceId, deviceRecord);
84+
const newPlayerId = await OneSignalApiShared.updateUserSession(deviceId, deviceRecord);
8585
this.onSessionSent = true;
86+
87+
// If the returned player id is different, save the new id.
88+
if (newPlayerId !== deviceId) {
89+
const subscription = await Database.getSubscription();
90+
subscription.deviceId = newPlayerId;
91+
await Database.setSubscription(subscription);
92+
}
8693
} catch(e) {
8794
Log.error(`Failed to update user session. Error "${e.message}" ${e.stack}`);
8895
}

test/unit/managers/UpdateManager.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from "ava";
2+
import nock from "nock";
23
import sinon, { SinonSandbox } from "sinon";
34
import { TestEnvironment, HttpHttpsEnvironment } from "../../support/sdk/TestEnvironment";
45
import { UpdateManager } from "../../../src/managers/UpdateManager";
@@ -8,6 +9,7 @@ import OneSignalApiShared from "../../../src/OneSignalApiShared";
89
import MainHelper from "../../../src/helpers/MainHelper";
910
import { SubscriptionStateKind } from "../../../src/models/SubscriptionStateKind";
1011
import { PushDeviceRecord } from "../../../src/models/PushDeviceRecord";
12+
import { Subscription } from '../../../src/models/Subscription';
1113

1214
// manually create and restore the sandbox
1315
const sandbox: SinonSandbox = sinon.sandbox.create();
@@ -98,6 +100,29 @@ test("sendOnSessionUpdate triggers on_session for existing subscribed user if ha
98100
t.is(OneSignal.context.updateManager.onSessionAlreadyCalled(), true);
99101
});
100102

103+
test("on_session saves new player id if returned from onesignal.com", async t => {
104+
// 1. Setup existing player id in indexDB
105+
const playerId = Random.getRandomUuid();
106+
const subscription = new Subscription();
107+
subscription.deviceId = playerId;
108+
await Database.setSubscription(subscription);
109+
110+
// 2. Mock on_session endpoint to give a new player_id
111+
const newPlayerId = Random.getRandomUuid();
112+
nock('https://onesignal.com')
113+
.post(`/api/v1/players/${playerId}/on_session`)
114+
.reply(200, { success: true, id: newPlayerId });
115+
116+
// 3. Make on_session call
117+
sandbox.stub(OneSignal.context.sessionManager, "isFirstPageView").returns(true);
118+
sandbox.stub(MainHelper, "getCurrentNotificationType").resolves(SubscriptionStateKind.Subscribed);
119+
await OneSignal.context.updateManager.sendOnSessionUpdate();
120+
121+
// 4. Ensure we have the new playe_id saved in indexDB
122+
const newSubscription = await Database.getSubscription();
123+
t.is(newSubscription.deviceId, newPlayerId);
124+
});
125+
101126
test("sendOnSessionUpdate triggers on_session for existing unsubscribed user if hasn't done so already and if enableOnSession flag is present", async t => {
102127
sandbox.stub(Database, "getSubscription").resolves({ deviceId: Random.getRandomUuid() });
103128
sandbox.stub(OneSignal.context.sessionManager, "isFirstPageView").returns(true);

0 commit comments

Comments
 (0)