Skip to content

Commit

Permalink
Add jwtToken property to OSModel
Browse files Browse the repository at this point in the history
Adds jwtToken as a property of OSModel. Removes unused jwtTokenAvailable property from Operation. Removes storing jwtToken in the Database. Since jwtToken is on OSModel, operations can access it from Operation.model.jwtToken and it doesn't need a separate property on Operation.
  • Loading branch information
shepherd-l committed Oct 11, 2024
1 parent a604d91 commit 5797e88
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import { ModelName } from '../../../../src/core/models/SupportedModels';
import { UserPropertiesModel } from '../../../../src/core/models/UserPropertiesModel';
import { OSModel } from '../../../../src/core/modelRepo/OSModel';
import { ExecutorResultFailNotRetriable } from '../../../../src/core/executors/ExecutorResult';
import Database from '../../../../src/shared/services/Database';

describe('User Property Request tests', () => {
beforeAll(() => {
// Required for Operation class
test.stub(Database.prototype, 'getJWTToken', Promise.resolve([]));
});

test('updateUserProperties returns no retry failure result', async () => {
const delta = {
changeType: CoreChangeType.Update,
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 @@ -9,5 +9,6 @@ export default interface EncodedModel {
modelName: ModelName;
onesignalId?: string;
externalId?: string;
jwtToken?: string;
[key: string]: unknown;
}
3 changes: 1 addition & 2 deletions src/core/executors/ExecutorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export default abstract class ExecutorBase {

// TO DO: fix optional model object. should always be defined on operation
await operation.model?.awaitOneSignalIdAvailable;
await operation.jwtTokenAvailable;

let res: ExecutorResult<SupportedModel> = {
success: false,
Expand All @@ -169,7 +168,7 @@ export default abstract class ExecutorBase {
if (res.result) {
// since we took a snapshot of the operation, we get a new instance with the correct model reference
const operationInstance =
await Operation.getInstanceWithModelReference(operation);
Operation.getInstanceWithModelReference(operation);
operationInstance?.model?.hydrate(res.result as SupportedModel);
}
OperationCache.delete(operation?.operationId);
Expand Down
20 changes: 18 additions & 2 deletions src/core/modelRepo/OSModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
onesignalIdAvailableCallback?: (onesignalId: string) => void;
externalId?: string;
applyToRecordId?: string;
jwtToken?: string;

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

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

public setJwtToken(jwtToken?: string): void {
logMethodCall('setJwtToken', { jwtToken });
this.jwtToken = jwtToken;
}

public setApplyToRecordId(applyToRecordId: string): void {
logMethodCall('setapplyToRecordId', { applyToRecordId });
this.applyToRecordId = applyToRecordId;
Expand Down Expand Up @@ -105,7 +112,15 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {
const modelName = this.modelName;
const onesignalId = this.onesignalId;
const externalId = this.externalId;
return { modelId, modelName, onesignalId, externalId, ...this.data };
const jwtToken = this.jwtToken;
return {
modelId,
modelName,
onesignalId,
externalId,
jwtToken,
...this.data,
};
}

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

const decodedModel = new OSModel<SupportedModel>(
Expand All @@ -126,6 +141,7 @@ export class OSModel<Model> extends Subscribable<ModelStoreChange<Model>> {

decodedModel.setOneSignalId(onesignalId);
decodedModel.setExternalId(externalId);
decodedModel.setJwtToken(jwtToken);
return decodedModel;
}
}
9 changes: 0 additions & 9 deletions src/core/operationRepo/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export class Operation<Model> {
payload?: Partial<SupportedModel>;
model?: OSModel<Model>;
applyToRecordId?: string;
jwtTokenAvailable: Promise<void>;
jwtToken?: string | null;

constructor(
readonly changeType: CoreChangeType,
Expand All @@ -25,11 +23,6 @@ export class Operation<Model> {
this.model = deltas ? deltas[deltas.length - 1].model : undefined;
this.applyToRecordId = deltas?.[deltas.length - 1]?.applyToRecordId;
this.timestamp = Date.now();
// eslint-disable-next-line no-async-promise-executor
this.jwtTokenAvailable = new Promise<void>(async (resolve) => {
this.jwtToken = await Database.getJWTToken();
resolve();
});
}

private getPayload(deltas: CoreDelta<Model>[]): any {
Expand Down Expand Up @@ -99,8 +92,6 @@ export class Operation<Model> {
operation.operationId = operationId;
operation.timestamp = timestamp;
operation.payload = payload;
operation.jwtToken = rawOperation.jwtToken;
operation.jwtTokenAvailable = Promise.resolve();
return operation;
} else {
throw new Error(
Expand Down
5 changes: 0 additions & 5 deletions src/core/requestService/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,3 @@ export function processIdentityOperation<Model>(operation: Operation<Model>): {
aliasPair: new AliasPair(AliasPair.ONESIGNAL_ID, onesignalId),
};
}

export async function getJWTHeader(): Promise<APIHeaders | undefined> {
const jwtToken = await Database.getJWTToken();
return !!jwtToken ? { Authorization: `Bearer ${jwtToken}` } : undefined;
}
17 changes: 16 additions & 1 deletion src/page/managers/LoginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export default class LoginManager {
// before, logging in, process anything waiting in the delta queue so it's not lost
OneSignal.coreDirector.forceDeltaQueueProcessingOnAllExecutors();

// set Jwt and send Jwt header regardless if jwtRequired
if (token) {
await Database.setJWTToken(token);
this._setJwtTokenOnAllModels(token);
}

const identityModel = OneSignal.coreDirector.getIdentityModel();
Expand Down Expand Up @@ -428,4 +429,18 @@ export default class LoginManager {
);
}
}

/* Helper methods */
private static async _setJwtTokenOnAllModels(token?: string): Promise<void> {
const identityModel = OneSignal.coreDirector.getIdentityModel();
const propertiesModel = OneSignal.coreDirector.getPropertiesModel();
const subscriptionModels =
await OneSignal.coreDirector.getAllSubscriptionsModels();

identityModel?.setJwtToken(token);
propertiesModel?.setJwtToken(token);
subscriptionModels.forEach((sub) => {
sub.setJwtToken(token);
});
}
}
16 changes: 0 additions & 16 deletions src/shared/services/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,6 @@ export default class Database {
}
}

async setJWTToken(token: string): Promise<void> {
await this.put('Ids', { type: 'jwtToken', id: token });
}

async getJWTToken(): Promise<string | null> {
return await this.get<string>('Ids', 'jwtToken');
}

async setProvideUserConsent(consent: boolean): Promise<void> {
await this.put('Options', { key: 'userConsent', value: consent });
}
Expand Down Expand Up @@ -529,14 +521,6 @@ export default class Database {
return await Database.singletonInstance.getSubscription();
}

static async setJWTToken(token: string) {
return await Database.singletonInstance.setJWTToken(token);
}

static async getJWTToken(): Promise<string | null> {
return await Database.singletonInstance.getJWTToken();
}

static async setConsentGiven(consent: boolean): Promise<void> {
return await Database.singletonInstance.setProvideUserConsent(consent);
}
Expand Down

0 comments on commit 5797e88

Please sign in to comment.