@memberjunction/ng-explorer-core 5.38.0 → 5.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app-routing.module.d.ts.map +1 -1
- package/dist/app-routing.module.js +13 -13
- package/dist/app-routing.module.js.map +1 -1
- package/dist/generated/lazy-feature-config.d.ts +1 -1
- package/dist/generated/lazy-feature-config.d.ts.map +1 -1
- package/dist/generated/lazy-feature-config.js +3 -2
- package/dist/generated/lazy-feature-config.js.map +1 -1
- package/dist/lib/guards/app-lock-guard.service.d.ts +26 -0
- package/dist/lib/guards/app-lock-guard.service.d.ts.map +1 -0
- package/dist/lib/guards/app-lock-guard.service.js +55 -0
- package/dist/lib/guards/app-lock-guard.service.js.map +1 -0
- package/dist/lib/resource-wrappers/chat-conversations-resource.component.d.ts.map +1 -1
- package/dist/lib/resource-wrappers/chat-conversations-resource.component.js +40 -27
- package/dist/lib/resource-wrappers/chat-conversations-resource.component.js.map +1 -1
- package/dist/lib/resource-wrappers/view-resource.component.d.ts +6 -5
- package/dist/lib/resource-wrappers/view-resource.component.d.ts.map +1 -1
- package/dist/lib/resource-wrappers/view-resource.component.js +19 -24
- package/dist/lib/resource-wrappers/view-resource.component.js.map +1 -1
- package/dist/lib/shell/components/tabs/tab-container.component.d.ts.map +1 -1
- package/dist/lib/shell/components/tabs/tab-container.component.js +9 -0
- package/dist/lib/shell/components/tabs/tab-container.component.js.map +1 -1
- package/dist/lib/shell/shell.component.d.ts +24 -6
- package/dist/lib/shell/shell.component.d.ts.map +1 -1
- package/dist/lib/shell/shell.component.js +360 -191
- package/dist/lib/shell/shell.component.js.map +1 -1
- package/dist/lib/single-record/single-record.component.d.ts +31 -75
- package/dist/lib/single-record/single-record.component.d.ts.map +1 -1
- package/dist/lib/single-record/single-record.component.js +60 -471
- package/dist/lib/single-record/single-record.component.js.map +1 -1
- package/dist/lib/single-search-result/single-search-result.component.d.ts +3 -8
- package/dist/lib/single-search-result/single-search-result.component.d.ts.map +1 -1
- package/dist/lib/single-search-result/single-search-result.component.js +19 -68
- package/dist/lib/single-search-result/single-search-result.component.js.map +1 -1
- package/dist/public-api.d.ts +1 -0
- package/dist/public-api.d.ts.map +1 -1
- package/dist/public-api.js +1 -0
- package/dist/public-api.js.map +1 -1
- package/package.json +46 -46
- package/dist/lib/__tests__/form-resolver.service.test.d.ts +0 -2
- package/dist/lib/__tests__/form-resolver.service.test.d.ts.map +0 -1
- package/dist/lib/__tests__/form-resolver.service.test.js +0 -258
- package/dist/lib/__tests__/form-resolver.service.test.js.map +0 -1
- package/dist/lib/services/form-resolver.service.d.ts +0 -139
- package/dist/lib/services/form-resolver.service.d.ts.map +0 -1
- package/dist/lib/services/form-resolver.service.js +0 -235
- package/dist/lib/services/form-resolver.service.js.map +0 -1
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import { LogError } from '@memberjunction/core';
|
|
3
|
-
import { InteractiveFormsEngine } from '@memberjunction/core-entities';
|
|
4
|
-
import { MJGlobal, UUIDsEqual } from '@memberjunction/global';
|
|
5
|
-
import { BaseFormComponent } from '@memberjunction/ng-base-forms';
|
|
6
|
-
import { UserInfoEngine } from '@memberjunction/core-entities';
|
|
7
|
-
import * as i0 from "@angular/core";
|
|
8
|
-
/**
|
|
9
|
-
* UserInfoEngine setting-key prefix for per-user, per-entity form-variant
|
|
10
|
-
* preferences. Persisted to `MJ: User Settings` so the choice follows the
|
|
11
|
-
* user across browsers and devices — localStorage is intentionally NOT used.
|
|
12
|
-
*
|
|
13
|
-
* Key format: `mj.formVariant.<entityname-lowercased>`.
|
|
14
|
-
*
|
|
15
|
-
* Value is one of:
|
|
16
|
-
* - an `EntityFormOverride.ID` (UUID) → use that specific override
|
|
17
|
-
* - {@link FormResolverService.EXPLICIT_DEFAULT_SENTINEL} → user picked
|
|
18
|
-
* the CodeGen Angular fallback explicitly; resolver skips all overrides
|
|
19
|
-
* - (key absent) → no preference, apply auto-pick rules
|
|
20
|
-
*/
|
|
21
|
-
const VARIANT_SETTING_PREFIX = 'mj.formVariant.';
|
|
22
|
-
/**
|
|
23
|
-
* Picks the form to render for an entity record and exposes the full list of
|
|
24
|
-
* applicable variants so the toolbar's variant switcher can offer alternates.
|
|
25
|
-
*
|
|
26
|
-
* Tier order for the **default** pick:
|
|
27
|
-
* 1. `EntityFormOverride` row matching the caller's User/Role/Global scope,
|
|
28
|
-
* Status='Active', ordered by scope tier (User > Role > Global), then
|
|
29
|
-
* Priority DESC, then `__mj_CreatedAt DESC`. First row wins.
|
|
30
|
-
* 2. Existing `ClassFactory.GetRegistration(BaseFormComponent, entityName)` —
|
|
31
|
-
* the @RegisterClass + CodeGen-generated path used today.
|
|
32
|
-
* 3. None — caller surfaces the "no form registered" error.
|
|
33
|
-
*
|
|
34
|
-
* **Session selection.** If the user previously chose a non-default variant
|
|
35
|
-
* via the variant switcher and that choice is still applicable + Active, that
|
|
36
|
-
* choice wins over the default. Choice is keyed by entity name in
|
|
37
|
-
* `localStorage`. Honoring this is part of the variant-switcher contract;
|
|
38
|
-
* without it, switching variants would only last for the lifetime of the
|
|
39
|
-
* record-form view.
|
|
40
|
-
*
|
|
41
|
-
* **Performance.** Backed by `InteractiveFormsEngine` (in MJCoreEntities)
|
|
42
|
-
* — overrides are cached in memory with BaseEntity event-driven
|
|
43
|
-
* invalidation, so resolution is an O(N) JS filter against a small
|
|
44
|
-
* set instead of a per-LoadForm RunView round-trip. Cold latency
|
|
45
|
-
* dropped from ~50ms to <1ms after the first load. The engine is
|
|
46
|
-
* lazy-Config'd here on first resolution; users who never open a
|
|
47
|
-
* record pay nothing.
|
|
48
|
-
*/
|
|
49
|
-
export class FormResolverService {
|
|
50
|
-
async ResolveFormForEntity(entity, user, provider) {
|
|
51
|
-
const variants = await this.listApplicableVariants(entity, user, provider);
|
|
52
|
-
const active = this.pickActive(entity, variants);
|
|
53
|
-
if (active) {
|
|
54
|
-
return { kind: 'interactive', override: active, variants };
|
|
55
|
-
}
|
|
56
|
-
const reg = MJGlobal.Instance.ClassFactory.GetRegistration(BaseFormComponent, entity.Name);
|
|
57
|
-
return reg
|
|
58
|
-
? { kind: 'class', subClass: reg.SubClass, variants }
|
|
59
|
-
: { kind: 'none', variants };
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Public list-API for the variant switcher UI. Returns all variants
|
|
63
|
-
* applicable to (entity, user). Includes Active, Pending, and Inactive
|
|
64
|
-
* rows so the picker can offer "switch back to v1.0.0" alongside the
|
|
65
|
-
* current active variant.
|
|
66
|
-
*/
|
|
67
|
-
async ListVariantsForEntity(entity, user, provider) {
|
|
68
|
-
return this.listApplicableVariants(entity, user, provider);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Sentinel stored in localStorage when the user **explicitly picks the
|
|
72
|
-
* "Default form"** option from the variant picker. Distinct from a
|
|
73
|
-
* missing localStorage key (= "no preference, use auto-pick rules")
|
|
74
|
-
* and from an override UUID. Without this sentinel, picking "Default"
|
|
75
|
-
* cleared the preference and `pickActive` re-applied the auto-pick —
|
|
76
|
-
* which always selects the first Active override, making the
|
|
77
|
-
* CodeGen/Angular fallback unreachable from the UI.
|
|
78
|
-
*
|
|
79
|
-
* Format: a leading `__` makes it visually distinct from a UUID and
|
|
80
|
-
* impossible to collide with one (UUIDs don't contain underscores).
|
|
81
|
-
*/
|
|
82
|
-
static EXPLICIT_DEFAULT_SENTINEL = '__codegen-default__';
|
|
83
|
-
/**
|
|
84
|
-
* Build the per-entity setting key. Lowercased so case variants of
|
|
85
|
-
* the entity name collapse onto a single record in `MJ: User Settings`.
|
|
86
|
-
*/
|
|
87
|
-
settingKey(entityName) {
|
|
88
|
-
return VARIANT_SETTING_PREFIX + entityName.toLowerCase();
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Persist a specific override choice. Pass the override UUID. Writes
|
|
92
|
-
* via `UserInfoEngine.SetSettingDebounced` so rapid successive picks
|
|
93
|
-
* don't hammer the DB. Use {@link SetExplicitDefault} to record "user
|
|
94
|
-
* wants the CodeGen Angular fallback" and {@link ClearSelectedVariant}
|
|
95
|
-
* to wipe the preference entirely (revert to auto-pick).
|
|
96
|
-
*/
|
|
97
|
-
SetSelectedVariant(entityName, overrideID) {
|
|
98
|
-
UserInfoEngine.Instance.SetSettingDebounced(this.settingKey(entityName), overrideID);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Record that the user explicitly picked the "Default form" row in
|
|
102
|
-
* the picker. `pickActive` reads this sentinel and returns null even
|
|
103
|
-
* when Active overrides exist for the entity, so the form-loading
|
|
104
|
-
* path falls back to CodeGen's `@RegisterClass` lookup.
|
|
105
|
-
*/
|
|
106
|
-
SetExplicitDefault(entityName) {
|
|
107
|
-
UserInfoEngine.Instance.SetSettingDebounced(this.settingKey(entityName), FormResolverService.EXPLICIT_DEFAULT_SENTINEL);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Wipe the user's stored preference for this entity. Next load
|
|
111
|
-
* applies the auto-pick rules (first Active override in tier order).
|
|
112
|
-
* Called internally when a saved override ID is no longer valid.
|
|
113
|
-
* Fire-and-forget — the resolver doesn't need to await the delete.
|
|
114
|
-
*/
|
|
115
|
-
ClearSelectedVariant(entityName) {
|
|
116
|
-
void UserInfoEngine.Instance.DeleteSetting(this.settingKey(entityName))
|
|
117
|
-
.catch(err => LogError(`FormResolverService.ClearSelectedVariant: ${err instanceof Error ? err.message : String(err)}`));
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Read the user's previously-saved variant choice for the entity.
|
|
121
|
-
* Synchronous because `UserInfoEngine` keeps the user-settings table
|
|
122
|
-
* in memory after bootstrap. Returns the stored UUID, the explicit-
|
|
123
|
-
* default sentinel, or null when no preference exists.
|
|
124
|
-
*/
|
|
125
|
-
GetSelectedVariant(entityName) {
|
|
126
|
-
return UserInfoEngine.Instance.GetSetting(this.settingKey(entityName)) ?? null;
|
|
127
|
-
}
|
|
128
|
-
// ── internals ────────────────────────────────────────────────────────
|
|
129
|
-
async listApplicableVariants(entity, user, provider) {
|
|
130
|
-
// Lazy-load the form-metadata engine on first call. No-op when
|
|
131
|
-
// already loaded. BaseEngine's event subscription keeps the
|
|
132
|
-
// cache fresh through save / delete / remote-invalidate — we
|
|
133
|
-
// don't need `BypassCache: true` (the previous RunView path's
|
|
134
|
-
// escape hatch) because the engine's invalidation IS that
|
|
135
|
-
// freshness guarantee. See `InteractiveFormsEngine` doc-block.
|
|
136
|
-
try {
|
|
137
|
-
await InteractiveFormsEngine.Instance.Config(false, user, provider);
|
|
138
|
-
}
|
|
139
|
-
catch (err) {
|
|
140
|
-
LogError(`FormResolverService: InteractiveFormsEngine.Config failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
141
|
-
return [];
|
|
142
|
-
}
|
|
143
|
-
const userRoleIds = new Set((user.UserRoles ?? []).map(r => r.RoleID).filter((id) => !!id));
|
|
144
|
-
// Filter cached overrides for this (entity, user, roles) tuple.
|
|
145
|
-
// Includes Active + Pending + Inactive (the variant picker shows
|
|
146
|
-
// all three; pickActive() filters to Active separately). Replaces
|
|
147
|
-
// a per-LoadForm RunView with an in-memory predicate — sub-ms.
|
|
148
|
-
const applicable = InteractiveFormsEngine.Instance.Overrides.filter(o => {
|
|
149
|
-
if (!o.EntityID || !UUIDsEqual(o.EntityID, entity.ID))
|
|
150
|
-
return false;
|
|
151
|
-
if (o.Scope === 'User')
|
|
152
|
-
return !!o.UserID && o.UserID.toLowerCase() === user.ID.toLowerCase();
|
|
153
|
-
if (o.Scope === 'Role')
|
|
154
|
-
return !!o.RoleID && userRoleIds.has(o.RoleID);
|
|
155
|
-
if (o.Scope === 'Global')
|
|
156
|
-
return true;
|
|
157
|
-
return false;
|
|
158
|
-
});
|
|
159
|
-
// Sort: User > Role > Global, then Priority DESC (higher beats),
|
|
160
|
-
// then newest first. Matches the prior SQL ORDER BY semantics so
|
|
161
|
-
// pickActive() / the variant switcher see the same order.
|
|
162
|
-
const scopeRank = (s) => {
|
|
163
|
-
if (s === 'User')
|
|
164
|
-
return 1;
|
|
165
|
-
if (s === 'Role')
|
|
166
|
-
return 2;
|
|
167
|
-
if (s === 'Global')
|
|
168
|
-
return 3;
|
|
169
|
-
return 4;
|
|
170
|
-
};
|
|
171
|
-
applicable.sort((a, b) => {
|
|
172
|
-
const sa = scopeRank(a.Scope);
|
|
173
|
-
const sb = scopeRank(b.Scope);
|
|
174
|
-
if (sa !== sb)
|
|
175
|
-
return sa - sb;
|
|
176
|
-
const pa = a.Priority ?? 0;
|
|
177
|
-
const pb = b.Priority ?? 0;
|
|
178
|
-
if (pa !== pb)
|
|
179
|
-
return pb - pa; // Priority DESC
|
|
180
|
-
const ca = a.__mj_CreatedAt instanceof Date ? a.__mj_CreatedAt.getTime() : 0;
|
|
181
|
-
const cb = b.__mj_CreatedAt instanceof Date ? b.__mj_CreatedAt.getTime() : 0;
|
|
182
|
-
return cb - ca; // newest first
|
|
183
|
-
});
|
|
184
|
-
// Project to the slim row shape the resolver returns. Keeps the
|
|
185
|
-
// contract identical to the old RunView return so callers don't
|
|
186
|
-
// change shape.
|
|
187
|
-
return applicable.map(o => ({
|
|
188
|
-
ID: o.ID,
|
|
189
|
-
EntityID: o.EntityID,
|
|
190
|
-
ComponentID: o.ComponentID,
|
|
191
|
-
Scope: o.Scope,
|
|
192
|
-
UserID: o.UserID,
|
|
193
|
-
RoleID: o.RoleID,
|
|
194
|
-
Priority: o.Priority,
|
|
195
|
-
Status: o.Status,
|
|
196
|
-
Name: o.Name,
|
|
197
|
-
Description: o.Description,
|
|
198
|
-
}));
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Pick the override that should actually render given the variant list
|
|
202
|
-
* and the user's stored selection (if any).
|
|
203
|
-
*
|
|
204
|
-
* Selection rules:
|
|
205
|
-
* - If the user explicitly picked the "Default form" row in the
|
|
206
|
-
* variant picker (sentinel in localStorage) → return null so the
|
|
207
|
-
* form-loading path falls back to CodeGen's `@RegisterClass` lookup.
|
|
208
|
-
* This is what makes the Angular fallback reachable from the UI.
|
|
209
|
-
* - Else if the user has a saved variant ID AND that variant is in
|
|
210
|
-
* the applicable list AND it's Active → use it.
|
|
211
|
-
* - Else → first Active row in tier+priority order (auto-pick).
|
|
212
|
-
* - Else → null (fall back to CodeGen/@RegisterClass path).
|
|
213
|
-
*/
|
|
214
|
-
pickActive(entity, variants) {
|
|
215
|
-
const selectedID = this.GetSelectedVariant(entity.Name);
|
|
216
|
-
if (selectedID === FormResolverService.EXPLICIT_DEFAULT_SENTINEL) {
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
if (selectedID) {
|
|
220
|
-
const sel = variants.find(v => v.Status === 'Active' && UUIDsEqual(v.ID, selectedID));
|
|
221
|
-
if (sel)
|
|
222
|
-
return sel;
|
|
223
|
-
// Selection no longer valid — wipe it so future loads auto-pick.
|
|
224
|
-
this.ClearSelectedVariant(entity.Name);
|
|
225
|
-
}
|
|
226
|
-
return variants.find(v => v.Status === 'Active') ?? null;
|
|
227
|
-
}
|
|
228
|
-
static ɵfac = function FormResolverService_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || FormResolverService)(); };
|
|
229
|
-
static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: FormResolverService, factory: FormResolverService.ɵfac, providedIn: 'root' });
|
|
230
|
-
}
|
|
231
|
-
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(FormResolverService, [{
|
|
232
|
-
type: Injectable,
|
|
233
|
-
args: [{ providedIn: 'root' }]
|
|
234
|
-
}], null, null); })();
|
|
235
|
-
//# sourceMappingURL=form-resolver.service.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"form-resolver.service.js","sourceRoot":"","sources":["../../../src/lib/services/form-resolver.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAQ,MAAM,eAAe,CAAC;AACjD,OAAO,EAA2C,QAAQ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ACnB,mBAAmB;cAD/B,UAAU;eAAC,EAAE,UAAU,EAAE,MAAM,EAAE","sourcesContent":["import { Injectable, Type } from '@angular/core';\nimport { IMetadataProvider, UserInfo, EntityInfo, LogError } from '@memberjunction/core';\nimport { InteractiveFormsEngine } from '@memberjunction/core-entities';\nimport { MJGlobal, UUIDsEqual } from '@memberjunction/global';\nimport { BaseFormComponent } from '@memberjunction/ng-base-forms';\nimport { UserInfoEngine } from '@memberjunction/core-entities';\n\n/**\n * Slim row shape for an `EntityFormOverride` lookup. Resolution doesn't need\n * the full BaseEntity object — `simple` ResultType is faster and avoids a\n * compile-time dependency on the generated entity class. The row is only ever\n * read by the resolver; mutation goes through the generated entity in other\n * code paths (Studio, AI authoring).\n */\nexport interface EntityFormOverrideRow {\n ID: string;\n EntityID: string;\n ComponentID: string;\n Scope: 'User' | 'Role' | 'Global';\n UserID: string | null;\n RoleID: string | null;\n Priority: number;\n Status: 'Active' | 'Inactive' | 'Pending';\n Name?: string;\n Description?: string | null;\n}\n\n/** Resolved form kind for a given (entity, user, roles) tuple. */\nexport type FormResolution =\n | { kind: 'interactive'; override: EntityFormOverrideRow; variants: EntityFormOverrideRow[] }\n | { kind: 'class'; subClass: Type<BaseFormComponent>; variants: EntityFormOverrideRow[] }\n | { kind: 'none'; variants: EntityFormOverrideRow[] };\n\n/**\n * UserInfoEngine setting-key prefix for per-user, per-entity form-variant\n * preferences. Persisted to `MJ: User Settings` so the choice follows the\n * user across browsers and devices — localStorage is intentionally NOT used.\n *\n * Key format: `mj.formVariant.<entityname-lowercased>`.\n *\n * Value is one of:\n * - an `EntityFormOverride.ID` (UUID) → use that specific override\n * - {@link FormResolverService.EXPLICIT_DEFAULT_SENTINEL} → user picked\n * the CodeGen Angular fallback explicitly; resolver skips all overrides\n * - (key absent) → no preference, apply auto-pick rules\n */\nconst VARIANT_SETTING_PREFIX = 'mj.formVariant.';\n\n/**\n * Picks the form to render for an entity record and exposes the full list of\n * applicable variants so the toolbar's variant switcher can offer alternates.\n *\n * Tier order for the **default** pick:\n * 1. `EntityFormOverride` row matching the caller's User/Role/Global scope,\n * Status='Active', ordered by scope tier (User > Role > Global), then\n * Priority DESC, then `__mj_CreatedAt DESC`. First row wins.\n * 2. Existing `ClassFactory.GetRegistration(BaseFormComponent, entityName)` —\n * the @RegisterClass + CodeGen-generated path used today.\n * 3. None — caller surfaces the \"no form registered\" error.\n *\n * **Session selection.** If the user previously chose a non-default variant\n * via the variant switcher and that choice is still applicable + Active, that\n * choice wins over the default. Choice is keyed by entity name in\n * `localStorage`. Honoring this is part of the variant-switcher contract;\n * without it, switching variants would only last for the lifetime of the\n * record-form view.\n *\n * **Performance.** Backed by `InteractiveFormsEngine` (in MJCoreEntities)\n * — overrides are cached in memory with BaseEntity event-driven\n * invalidation, so resolution is an O(N) JS filter against a small\n * set instead of a per-LoadForm RunView round-trip. Cold latency\n * dropped from ~50ms to <1ms after the first load. The engine is\n * lazy-Config'd here on first resolution; users who never open a\n * record pay nothing.\n */\n@Injectable({ providedIn: 'root' })\nexport class FormResolverService {\n\n async ResolveFormForEntity(\n entity: EntityInfo,\n user: UserInfo,\n provider: IMetadataProvider,\n ): Promise<FormResolution> {\n const variants = await this.listApplicableVariants(entity, user, provider);\n const active = this.pickActive(entity, variants);\n\n if (active) {\n return { kind: 'interactive', override: active, variants };\n }\n\n const reg = MJGlobal.Instance.ClassFactory.GetRegistration(BaseFormComponent, entity.Name);\n return reg\n ? { kind: 'class', subClass: reg.SubClass as Type<BaseFormComponent>, variants }\n : { kind: 'none', variants };\n }\n\n /**\n * Public list-API for the variant switcher UI. Returns all variants\n * applicable to (entity, user). Includes Active, Pending, and Inactive\n * rows so the picker can offer \"switch back to v1.0.0\" alongside the\n * current active variant.\n */\n async ListVariantsForEntity(\n entity: EntityInfo,\n user: UserInfo,\n provider: IMetadataProvider,\n ): Promise<EntityFormOverrideRow[]> {\n return this.listApplicableVariants(entity, user, provider);\n }\n\n /**\n * Sentinel stored in localStorage when the user **explicitly picks the\n * \"Default form\"** option from the variant picker. Distinct from a\n * missing localStorage key (= \"no preference, use auto-pick rules\")\n * and from an override UUID. Without this sentinel, picking \"Default\"\n * cleared the preference and `pickActive` re-applied the auto-pick —\n * which always selects the first Active override, making the\n * CodeGen/Angular fallback unreachable from the UI.\n *\n * Format: a leading `__` makes it visually distinct from a UUID and\n * impossible to collide with one (UUIDs don't contain underscores).\n */\n public static readonly EXPLICIT_DEFAULT_SENTINEL = '__codegen-default__';\n\n /**\n * Build the per-entity setting key. Lowercased so case variants of\n * the entity name collapse onto a single record in `MJ: User Settings`.\n */\n private settingKey(entityName: string): string {\n return VARIANT_SETTING_PREFIX + entityName.toLowerCase();\n }\n\n /**\n * Persist a specific override choice. Pass the override UUID. Writes\n * via `UserInfoEngine.SetSettingDebounced` so rapid successive picks\n * don't hammer the DB. Use {@link SetExplicitDefault} to record \"user\n * wants the CodeGen Angular fallback\" and {@link ClearSelectedVariant}\n * to wipe the preference entirely (revert to auto-pick).\n */\n SetSelectedVariant(entityName: string, overrideID: string): void {\n UserInfoEngine.Instance.SetSettingDebounced(this.settingKey(entityName), overrideID);\n }\n\n /**\n * Record that the user explicitly picked the \"Default form\" row in\n * the picker. `pickActive` reads this sentinel and returns null even\n * when Active overrides exist for the entity, so the form-loading\n * path falls back to CodeGen's `@RegisterClass` lookup.\n */\n SetExplicitDefault(entityName: string): void {\n UserInfoEngine.Instance.SetSettingDebounced(\n this.settingKey(entityName),\n FormResolverService.EXPLICIT_DEFAULT_SENTINEL,\n );\n }\n\n /**\n * Wipe the user's stored preference for this entity. Next load\n * applies the auto-pick rules (first Active override in tier order).\n * Called internally when a saved override ID is no longer valid.\n * Fire-and-forget — the resolver doesn't need to await the delete.\n */\n ClearSelectedVariant(entityName: string): void {\n void UserInfoEngine.Instance.DeleteSetting(this.settingKey(entityName))\n .catch(err => LogError(`FormResolverService.ClearSelectedVariant: ${err instanceof Error ? err.message : String(err)}`));\n }\n\n /**\n * Read the user's previously-saved variant choice for the entity.\n * Synchronous because `UserInfoEngine` keeps the user-settings table\n * in memory after bootstrap. Returns the stored UUID, the explicit-\n * default sentinel, or null when no preference exists.\n */\n GetSelectedVariant(entityName: string): string | null {\n return UserInfoEngine.Instance.GetSetting(this.settingKey(entityName)) ?? null;\n }\n\n // ── internals ────────────────────────────────────────────────────────\n\n private async listApplicableVariants(\n entity: EntityInfo,\n user: UserInfo,\n provider: IMetadataProvider,\n ): Promise<EntityFormOverrideRow[]> {\n // Lazy-load the form-metadata engine on first call. No-op when\n // already loaded. BaseEngine's event subscription keeps the\n // cache fresh through save / delete / remote-invalidate — we\n // don't need `BypassCache: true` (the previous RunView path's\n // escape hatch) because the engine's invalidation IS that\n // freshness guarantee. See `InteractiveFormsEngine` doc-block.\n try {\n await InteractiveFormsEngine.Instance.Config(false, user, provider);\n } catch (err) {\n LogError(`FormResolverService: InteractiveFormsEngine.Config failed: ${err instanceof Error ? err.message : String(err)}`);\n return [];\n }\n\n const userRoleIds = new Set(\n (user.UserRoles ?? []).map(r => r.RoleID).filter((id): id is string => !!id),\n );\n\n // Filter cached overrides for this (entity, user, roles) tuple.\n // Includes Active + Pending + Inactive (the variant picker shows\n // all three; pickActive() filters to Active separately). Replaces\n // a per-LoadForm RunView with an in-memory predicate — sub-ms.\n const applicable = InteractiveFormsEngine.Instance.Overrides.filter(o => {\n if (!o.EntityID || !UUIDsEqual(o.EntityID, entity.ID)) return false;\n if (o.Scope === 'User') return !!o.UserID && o.UserID.toLowerCase() === user.ID.toLowerCase();\n if (o.Scope === 'Role') return !!o.RoleID && userRoleIds.has(o.RoleID);\n if (o.Scope === 'Global') return true;\n return false;\n });\n\n // Sort: User > Role > Global, then Priority DESC (higher beats),\n // then newest first. Matches the prior SQL ORDER BY semantics so\n // pickActive() / the variant switcher see the same order.\n const scopeRank = (s: string | null | undefined): number => {\n if (s === 'User') return 1;\n if (s === 'Role') return 2;\n if (s === 'Global') return 3;\n return 4;\n };\n applicable.sort((a, b) => {\n const sa = scopeRank(a.Scope);\n const sb = scopeRank(b.Scope);\n if (sa !== sb) return sa - sb;\n const pa = a.Priority ?? 0;\n const pb = b.Priority ?? 0;\n if (pa !== pb) return pb - pa; // Priority DESC\n const ca = a.__mj_CreatedAt instanceof Date ? a.__mj_CreatedAt.getTime() : 0;\n const cb = b.__mj_CreatedAt instanceof Date ? b.__mj_CreatedAt.getTime() : 0;\n return cb - ca; // newest first\n });\n\n // Project to the slim row shape the resolver returns. Keeps the\n // contract identical to the old RunView return so callers don't\n // change shape.\n return applicable.map(o => ({\n ID: o.ID,\n EntityID: o.EntityID,\n ComponentID: o.ComponentID,\n Scope: o.Scope as 'User' | 'Role' | 'Global',\n UserID: o.UserID,\n RoleID: o.RoleID,\n Priority: o.Priority,\n Status: o.Status as 'Active' | 'Inactive' | 'Pending',\n Name: o.Name,\n Description: o.Description,\n }));\n }\n\n /**\n * Pick the override that should actually render given the variant list\n * and the user's stored selection (if any).\n *\n * Selection rules:\n * - If the user explicitly picked the \"Default form\" row in the\n * variant picker (sentinel in localStorage) → return null so the\n * form-loading path falls back to CodeGen's `@RegisterClass` lookup.\n * This is what makes the Angular fallback reachable from the UI.\n * - Else if the user has a saved variant ID AND that variant is in\n * the applicable list AND it's Active → use it.\n * - Else → first Active row in tier+priority order (auto-pick).\n * - Else → null (fall back to CodeGen/@RegisterClass path).\n */\n private pickActive(\n entity: EntityInfo,\n variants: EntityFormOverrideRow[],\n ): EntityFormOverrideRow | null {\n const selectedID = this.GetSelectedVariant(entity.Name);\n if (selectedID === FormResolverService.EXPLICIT_DEFAULT_SENTINEL) {\n return null;\n }\n if (selectedID) {\n const sel = variants.find(v => v.Status === 'Active' && UUIDsEqual(v.ID, selectedID));\n if (sel) return sel;\n // Selection no longer valid — wipe it so future loads auto-pick.\n this.ClearSelectedVariant(entity.Name);\n }\n return variants.find(v => v.Status === 'Active') ?? null;\n }\n}\n"]}
|