Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (WIP): handle user id change on user import action #2405

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DataEvaluationService } from "src/app/shared/services/data/data-evaluat
import { AsyncServiceBase } from "src/app/shared/services/asyncService.base";
import { PLH_CALC_FUNCTIONS } from "./template-calc-functions/plh-calc-functions";
import { CORE_CALC_FUNCTIONS } from "./template-calc-functions/core-calc-functions";
import { UserMetaService } from "src/app/shared/services/userMeta/userMeta.service";
import { LocalStorageService } from "src/app/shared/services/local-storage/local-storage.service";

@Injectable({ providedIn: "root" })
Expand All @@ -25,15 +24,14 @@ export class TemplateCalcService extends AsyncServiceBase {
constructor(
private serverService: ServerService,
private dataEvaluationService: DataEvaluationService,
private localStorageService: LocalStorageService,
private userMetaService: UserMetaService
private localStorageService: LocalStorageService
) {
super("TemplateCalc");
this.registerInitFunction(this.initialise);
}
private async initialise() {
this.ensureSyncServicesReady([this.serverService, this.localStorageService]);
await this.ensureAsyncServicesReady([this.dataEvaluationService, this.userMetaService]);
await this.ensureAsyncServicesReady([this.dataEvaluationService]);
await this.setUserMetaData();
this.getCalcContext();
}
Expand All @@ -49,6 +47,10 @@ export class TemplateCalcService extends AsyncServiceBase {
return this.calcContext;
}

public updateThisCtxt<K extends keyof IThisCtxt>(field: K, value: IThisCtxt[K]) {
this.calcContext.thisCtxt[field] = value;
}

/**
* Main export for use in evaluation statements. Includes all functions listed below
* alongside additional a base for variables found at `this.`
Expand All @@ -67,7 +69,7 @@ export class TemplateCalcService extends AsyncServiceBase {
app_first_launch: this.dataEvaluationService.data.first_app_launch,
app_user_id: this.app_user_id,
device_info: this.device_info,
};
} as IThisCtxt;
}

private async setUserMetaData() {
Expand Down Expand Up @@ -119,9 +121,15 @@ export class TemplateCalcService extends AsyncServiceBase {
* `pick_random(this.local.some_list)`
*/
export interface ICalcContext {
thisCtxt: {
[name: string]: any;
};
thisCtxt: IThisCtxt;
globalFunctions: IFunctionHashmap;
globalConstants: IConstantHashmap;
}

interface IThisCtxt {
calc: Function;
app_day: number;
app_first_launch: string;
app_user_id: string;
device_info: DeviceInfo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ export class LocalStorageService extends SyncServiceBase {
}
return key.startsWith("_");
}
/** Check if a field name is protected (starts with underscore prefixed or non-prefixed) */
getProtectedFieldNameWithPrefix(field: IProtectedFieldName) {
const fieldName = getProtectedFieldName(field);
return `${STORAGE_PREFIX}.${fieldName}`;
}
}
40 changes: 26 additions & 14 deletions src/app/shared/services/userMeta/userMeta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TemplateActionRegistry } from "../../components/template/services/insta
import { TemplateFieldService } from "../../components/template/services/template-field.service";
import { LocalStorageService } from "../local-storage/local-storage.service";
import { DynamicDataService } from "../dynamic-data/dynamic-data.service";
import { TemplateCalcService } from "../../components/template/services/template-calc.service";

type IDynamicDataState = ReturnType<DynamicDataService["getState"]>;

Expand All @@ -17,12 +18,13 @@ export class UserMetaService extends AsyncServiceBase {
/** keep an in-memory copy of user to provide synchronously */
public userMeta: IUserMeta;
constructor(
private localStorageService: LocalStorageService,
private dbService: DbService,
private templateActionRegistry: TemplateActionRegistry,
private http: HttpClient,
private dynamicDataService: DynamicDataService,
private fieldService: TemplateFieldService,
private dynamicDataService: DynamicDataService
private http: HttpClient,
private localStorageService: LocalStorageService,
private templateActionRegistry: TemplateActionRegistry,
private templateCalcService: TemplateCalcService
) {
super("UserMetaService");
this.registerInitFunction(this.initialise);
Expand All @@ -35,22 +37,16 @@ export class UserMetaService extends AsyncServiceBase {
this.dbService,
this.fieldService,
this.dynamicDataService,
this.templateCalcService,
]);
this.registerUserActions();
const userMetaValues = await this.dbService.table<IUserMetaEntry>("user_meta").toArray();
const userMeta: IUserMeta = USER_DEFAULTS;
this.userMeta = USER_DEFAULTS;
userMetaValues.forEach((v) => {
userMeta[v.key] = v.value;
this.userMeta[v.key] = v.value;
});
const { identifier: uuid } = await Device.getId();
// fix legacy user IDs - note this can likely be removed after v0.16
if (userMeta.uuid && userMeta.uuid !== uuid) {
await this.setUserMeta({ uuid, uuid_temp: userMeta.uuid });
}
userMeta.uuid = uuid;
this.userMeta = userMeta;
// populate user id contact field
this.localStorageService.setProtected("APP_USER_ID", uuid);
await this.updateUuid(uuid);
}

getUserMeta(key: keyof IUserMeta) {
Expand Down Expand Up @@ -83,12 +79,28 @@ export class UserMetaService extends AsyncServiceBase {
}
}

private async updateUuid(uuid: string) {
// fix legacy user IDs - note this can likely be removed after v0.16
if (this.userMeta.uuid && this.userMeta.uuid !== uuid) {
await this.setUserMeta({ uuid, uuid_temp: this.userMeta.uuid });
}
this.userMeta.uuid = uuid;
// populate user id contact field
this.localStorageService.setProtected("APP_USER_ID", uuid);
// update calc context
this.templateCalcService.updateThisCtxt("app_user_id", uuid);
}

private async importUserContactFields(contact_fields = {}) {
for (const [key, value] of Object.entries(contact_fields)) {
// TODO - handle special contact fields as required (e.g. _app_skin, _app_theme)
if (!this.localStorageService.isProtected(key)) {
this.localStorageService.setString(key, value as string);
}
// handle user ID import
if (key === this.localStorageService.getProtectedFieldNameWithPrefix("APP_USER_ID")) {
await this.updateUuid(String(value));
}
}
}

Expand Down
Loading