Skip to content

Commit

Permalink
Merge pull request #2795 from IDEMSInternational/feat/auth-restore-data
Browse files Browse the repository at this point in the history
Feat!: auth restore data
  • Loading branch information
chrismclarke authored Feb 17, 2025
2 parents dff5cf5 + 42bfd94 commit 9e66680
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/app/shared/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { toObservable } from "@angular/core/rxjs-interop";
import { ServerService } from "../server/server.service";
import { HttpClient } from "@angular/common/http";
import type { IServerUser } from "../server/server.types";
import { DynamicDataService } from "../dynamic-data/dynamic-data.service";

@Injectable({
providedIn: "root",
Expand All @@ -30,7 +31,8 @@ export class AuthService extends AsyncServiceBase {
private injector: Injector,
private templateService: TemplateService,
private serverService: ServerService,
private http: HttpClient
private http: HttpClient,
private dynamicDataService: DynamicDataService
) {
super("Auth");
this.provider = getAuthProvider(this.config.provider);
Expand All @@ -51,6 +53,16 @@ export class AuthService extends AsyncServiceBase {
},
{ allowSignalWrites: true }
);
// expose restore profile data to authoring via `app_auth_profiles` internal collection
effect(async () => {
const profiles = this.restoreProfiles();
if (profiles.length > 0) {
const collectionData = profiles.map((p) => ({ ...p, id: p.app_user_id }));
await this.dynamicDataService.ready();
await this.dynamicDataService.setInternalCollection("auth_profiles", collectionData);
console.log("[Auth] Restore Profiles", profiles);
}
});
}

private get config() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ describe("DynamicDataService", () => {
).toBeRejectedWithError();
});

it("supports internal collections", async () => {
await service.setInternalCollection("mock", [{ id: "1", string: "hello" }]);
const obs = await service.query$<any>("data_list", "_mock");
const data = await firstValueFrom(obs);
expect(data).toEqual([{ id: "1", string: "hello" }]);
});

// QA
it("prevents query of non-existent data lists", async () => {
let errMsg: string;
Expand Down
18 changes: 18 additions & 0 deletions src/app/shared/services/dynamic-data/dynamic-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ export class DynamicDataService extends AsyncServiceBase {
return this.db.getCollection(collectionName)?.count().exec();
}

/**
* Set the data for an internal data collection
* All internal collections are prefixed by `_app_` and are only stored ephemerally (not persisted)
* Data that is set will override any pre-existing data
**/
public async setInternalCollection(name: string, data: any[]) {
const { collectionName } = await this.ensureCollection("data_list", `_${name}`);
const collection = this.db.getCollection(collectionName);
const docs = await collection.find().exec();
await collection.bulkRemove(docs.map((d) => d.id));
await this.db.bulkInsert(collectionName, data);
}

/** Ensure a collection exists, creating if not and populating with corresponding list data */
private async ensureCollection(flow_type: FlowTypes.FlowType, flow_name: string) {
const collectionName = this.normaliseCollectionName(flow_type, flow_name);
Expand Down Expand Up @@ -244,6 +257,11 @@ export class DynamicDataService extends AsyncServiceBase {
* compatible in case of schema changes
* */
private async prepareInitialData(flow_type: FlowTypes.FlowType, flow_name: string) {
// Internal tables, prefixed by `_` are in-memory read-only and do not have preloaded data or schema
if (flow_name.startsWith("_")) {
return { data: [], schema: { ...REACTIVE_SCHEMA_BASE } };
}

const flowData = await this.appDataService.getSheet<FlowTypes.Data_list>(flow_type, flow_name);
if (!flowData || flowData.rows.length === 0) {
throw new Error(`No data exists for collection [${flow_name}], cannot initialise`);
Expand Down
5 changes: 4 additions & 1 deletion src/app/shared/services/userMeta/userMeta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export class UserMetaService extends AsyncServiceBase {

/** Import existing user contact fields and replace current user */
private async importUser(id: string) {
if (!id) {
throw new Error(`[User Import] no id provided`);
}
try {
// TODO - get type-safe return types using openapi http client
const profile = await firstValueFrom(
Expand All @@ -87,7 +90,7 @@ export class UserMetaService extends AsyncServiceBase {
return;
}
const { contact_fields, dynamic_data } = profile as any;
console.log("[User Import]", { contact_fields, dynamic_data });
console.log("[User Import]", profile);
await this.importUserContactFields(contact_fields);
await this.importUserDynamicData(dynamic_data);
} catch (error) {
Expand Down

0 comments on commit 9e66680

Please sign in to comment.