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 11, 2024
1 parent daa66e3 commit a9bc2dc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/core/CoreModuleDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export class CoreModuleDirector {
if (user.hasOneSignalId) {
pushModel.setOneSignalId(user.onesignalId);
}

const identity = this.getIdentityModel();
const externalId = identity?.data.external_id;
if (externalId) {
pushModel.setExternalId(externalId);
}

// don't propagate since we will be including the subscription in the user create call
OneSignal.coreDirector.add(
ModelName.PushSubscriptions,
Expand All @@ -59,7 +66,8 @@ export class CoreModuleDirector {
const identity = this.getIdentityModel();
const properties = this.getPropertiesModel();

const { onesignal_id: onesignalId } = user.identity;
const { onesignal_id: onesignalId, external_id: externalId } =
user.identity;

if (!onesignalId) {
throw new OneSignalError('OneSignal ID is missing from user data');
Expand All @@ -69,6 +77,11 @@ export class CoreModuleDirector {
identity?.setOneSignalId(onesignalId);
properties?.setOneSignalId(onesignalId);

if (externalId) {
identity?.setExternalId(externalId);
properties?.setExternalId(externalId);
}

// identity and properties models are always single, so we hydrate immediately (i.e. replace existing data)
identity?.hydrate(user.identity);
properties?.hydrate(user.properties);
Expand All @@ -78,6 +91,7 @@ export class CoreModuleDirector {
this._hydrateSubscriptions(
user.subscriptions as SubscriptionModel[],
onesignalId,
externalId,
);
EventHelper.checkAndTriggerUserChanged();
} catch (e) {
Expand All @@ -88,6 +102,7 @@ export class CoreModuleDirector {
private _hydrateSubscriptions(
subscriptions: SubscriptionModel[],
onesignalId: string,
externalId?: string,
): void {
logMethodCall('CoreModuleDirector._hydrateSubscriptions', {
subscriptions,
Expand Down Expand Up @@ -122,10 +137,16 @@ export class CoreModuleDirector {
if (existingSubscription) {
// set onesignalId on existing subscription *before* hydrating so that the onesignalId is updated in model cache
existingSubscription.setOneSignalId(onesignalId);
if (externalId) {
existingSubscription?.setExternalId(externalId);
}
existingSubscription.hydrate(subscription);
} else {
const model = new OSModel<SupportedModel>(modelName, subscription);
model.setOneSignalId(onesignalId);
if (externalId) {
model?.setExternalId(externalId);
}
modelStores[modelName].add(model, false); // don't propagate to server
}
});
Expand Down
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;
}
}
16 changes: 16 additions & 0 deletions src/onesignal/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export default class User {
) {
// existing user
newSubscription.setOneSignalId(User.singletonInstance?.onesignalId);
const identityModel = OneSignal.coreDirector.getIdentityModel();
if (identityModel.data.external_id) {
newSubscription.setExternalId(identityModel.data.external_id);
}
OneSignal.coreDirector.add(
ModelName.EmailSubscriptions,
newSubscription,
Expand All @@ -165,6 +169,10 @@ export default class User {
throw e;
},
);
const identityModel = OneSignal.coreDirector.getIdentityModel();
if (identityModel.data.external_id) {
newSubscription.setExternalId(identityModel.data.external_id);
}
}

public async addSms(sms: string): Promise<void> {
Expand Down Expand Up @@ -194,6 +202,10 @@ export default class User {
) {
// existing user
newSubscription.setOneSignalId(User.singletonInstance?.onesignalId);
const identityModel = OneSignal.coreDirector.getIdentityModel();
if (identityModel.data.external_id) {
newSubscription.setExternalId(identityModel.data.external_id);
}
OneSignal.coreDirector.add(
ModelName.SmsSubscriptions,
newSubscription,
Expand All @@ -214,6 +226,10 @@ export default class User {
throw e;
},
);
const identityModel = OneSignal.coreDirector.getIdentityModel();
if (identityModel.data.external_id) {
newSubscription.setExternalId(identityModel.data.external_id);
}
}

public removeEmail(email: string): void {
Expand Down

0 comments on commit a9bc2dc

Please sign in to comment.