diff --git a/src/core/caching/EncodedModel.ts b/src/core/caching/EncodedModel.ts index 0fba54c77..e8daebf11 100644 --- a/src/core/caching/EncodedModel.ts +++ b/src/core/caching/EncodedModel.ts @@ -8,5 +8,6 @@ export default interface EncodedModel { modelId: string; modelName: ModelName; onesignalId?: string; + externalId?: string; [key: string]: unknown; } diff --git a/src/core/modelRepo/OSModel.ts b/src/core/modelRepo/OSModel.ts index 598361d04..3536324f1 100644 --- a/src/core/modelRepo/OSModel.ts +++ b/src/core/modelRepo/OSModel.ts @@ -17,6 +17,7 @@ export class OSModel extends Subscribable> { onesignalId?: string; awaitOneSignalIdAvailable: Promise; onesignalIdAvailableCallback?: (onesignalId: string) => void; + externalId?: string; constructor( readonly modelName: ModelName, @@ -28,6 +29,7 @@ export class OSModel extends Subscribable> { this.modelName = modelName; this.data = data; this.onesignalId = undefined; + this.externalId = undefined; this.awaitOneSignalIdAvailable = new Promise((resolve) => { this.onesignalIdAvailableCallback = resolve; @@ -48,6 +50,11 @@ export class OSModel extends Subscribable> { } } + public setExternalId(externalId?: string): void { + logMethodCall('setExternalId', { externalId }); + this.externalId = externalId; + } + /** * We use this method to update the model data. * Results in a broadcasted update event. @@ -92,7 +99,8 @@ export class OSModel extends Subscribable> { const modelId = this.modelId as string; const modelName = this.modelName; const onesignalId = this.onesignalId; - return { modelId, modelName, onesignalId, ...this.data }; + const externalId = this.externalId; + return { modelId, modelName, onesignalId, externalId, ...this.data }; } /** @@ -102,7 +110,8 @@ export class OSModel extends Subscribable> { */ static decode(encodedModel: EncodedModel): OSModel { logMethodCall('decode', { encodedModel }); - const { modelId, modelName, onesignalId, ...data } = encodedModel; + const { modelId, modelName, onesignalId, externalId, ...data } = + encodedModel; const decodedModel = new OSModel( modelName as ModelName, @@ -111,6 +120,7 @@ export class OSModel extends Subscribable> { ); decodedModel.setOneSignalId(onesignalId); + decodedModel.setExternalId(externalId); return decodedModel; } } diff --git a/src/page/managers/LoginManager.ts b/src/page/managers/LoginManager.ts index 6c784ae0b..286d844b6 100644 --- a/src/page/managers/LoginManager.ts +++ b/src/page/managers/LoginManager.ts @@ -61,7 +61,7 @@ export default class LoginManager { const isIdentified = LoginManager.isIdentified(identityModel.data); // set the external id on the user locally - LoginManager.setExternalId(identityModel, externalId); + await LoginManager.setExternalId(identityModel, externalId); let userData: Partial; if (!isIdentified) { @@ -152,10 +152,10 @@ export default class LoginManager { await UserDirector.initializeUser(false); } - static setExternalId( + static async setExternalId( identityOSModel: OSModel, externalId: string, - ): void { + ): Promise { logMethodCall('LoginManager.setExternalId', { externalId }); if (!identityOSModel) { @@ -163,6 +163,14 @@ export default class LoginManager { } identityOSModel.set('external_id', externalId, false); + identityOSModel.setExternalId(externalId); + const properties = OneSignal.coreDirector.getPropertiesModel(); + properties?.setExternalId(externalId); + const subscriptions = + await OneSignal.coreDirector.getAllSubscriptionsModels(); + for (let subscription of subscriptions) { + subscription.setExternalId(externalId); + } } static isIdentified(identity: SupportedIdentity): boolean { @@ -263,6 +271,14 @@ export default class LoginManager { // Persist to disk so it is used once we have the opportunity to create a User. const identityModel = OneSignal.coreDirector.getIdentityModel(); identityModel?.set(AliasPair.EXTERNAL_ID, identity.external_id, false); + identityModel?.setExternalId(identity.external_id); + const properties = OneSignal.coreDirector.getPropertiesModel(); + properties?.setExternalId(identity.external_id); + const subscriptions = + await OneSignal.coreDirector.getAllSubscriptionsModels(); + for (let subscription of subscriptions) { + subscription.setExternalId(identity.external_id); + } return userData; }