Skip to content

Commit

Permalink
Add external id property to operations
Browse files Browse the repository at this point in the history
Will be used to retry operations for users with a valid Jwt token. Accessed through Operation.model.externalId
  • Loading branch information
shepherd-l committed Jul 9, 2024
1 parent daa66e3 commit 5424208
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/core/caching/EncodedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default interface EncodedModel {
modelId: string;
modelName: ModelName;
onesignalId?: string;
externalId?: string;
[key: string]: unknown;
}
14 changes: 12 additions & 2 deletions src/core/modelRepo/OSModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
onesignalId?: string;
awaitOneSignalIdAvailable: Promise<string>;
onesignalIdAvailableCallback?: (onesignalId: string) => void;
externalId?: string;

constructor(
readonly modelName: ModelName,
Expand All @@ -28,6 +29,7 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
this.modelName = modelName;
this.data = data;
this.onesignalId = undefined;
this.externalId = undefined;

this.awaitOneSignalIdAvailable = new Promise<string>((resolve) => {
this.onesignalIdAvailableCallback = resolve;
Expand All @@ -48,6 +50,11 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
}
}

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.
Expand Down Expand Up @@ -92,7 +99,8 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
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 };
}

/**
Expand All @@ -102,7 +110,8 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
*/
static decode(encodedModel: EncodedModel): OSModel<SupportedModel> {
logMethodCall('decode', { encodedModel });
const { modelId, modelName, onesignalId, ...data } = encodedModel;
const { modelId, modelName, onesignalId, externalId, ...data } =
encodedModel;

const decodedModel = new OSModel<SupportedModel>(
modelName as ModelName,
Expand All @@ -111,6 +120,7 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
);

decodedModel.setOneSignalId(onesignalId);
decodedModel.setExternalId(externalId);
return decodedModel;
}
}
22 changes: 19 additions & 3 deletions src/page/managers/LoginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserData>;
if (!isIdentified) {
Expand Down Expand Up @@ -152,17 +152,25 @@ export default class LoginManager {
await UserDirector.initializeUser(false);
}

static setExternalId(
static async setExternalId(
identityOSModel: OSModel<SupportedIdentity>,
externalId: string,
): void {
): Promise<void> {
logMethodCall('LoginManager.setExternalId', { externalId });

if (!identityOSModel) {
throw new OneSignalError('login: no identity model found');
}

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) {

Check failure on line 171 in src/page/managers/LoginManager.ts

View workflow job for this annotation

GitHub Actions / test

'subscription' is never reassigned. Use 'const' instead
subscription.setExternalId(externalId);
}
}

static isIdentified(identity: SupportedIdentity): boolean {
Expand Down Expand Up @@ -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) {

Check failure on line 279 in src/page/managers/LoginManager.ts

View workflow job for this annotation

GitHub Actions / test

'subscription' is never reassigned. Use 'const' instead
subscription.setExternalId(identity.external_id);
}
return userData;
}

Expand Down

0 comments on commit 5424208

Please sign in to comment.