Skip to content

Commit aba639d

Browse files
committed
refactor(objectql): move homeAmountServing's body into the references facet
The codemod's --members pass moved it, with its field-call stay reason lifted for homeLocalizationResolver alone in a one-off local run (the script is unchanged). Its undo proof rebuilds the base engine.ts and the facet module byte for byte. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5kF4yD4vMatZJ2chQM
1 parent 604a2b3 commit aba639d

2 files changed

Lines changed: 62 additions & 59 deletions

File tree

‎packages/objectql/src/engine-references-and-master-detail.ts‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* [#29986 codemod] The
55
* "Realtime, i18n, home amounts, validation messages, references, master-detail"
66
* section of `ObjectQL`: the bodies of `setRealtimeService`, `setI18nService`,
7-
* `setHomeLocalizationResolver`, `homeAmountOrganizationOf`,
8-
* `previewHomeAmounts`, `projectHomeAmountsOntoPatch`, `referenceExists`,
7+
* `setHomeLocalizationResolver`, `homeAmountServing`,
8+
* `homeAmountOrganizationOf`, `previewHomeAmounts`,
9+
* `projectHomeAmountsOntoPatch`, `referenceExists`,
910
* `callerReadableReferenceExists`, `resolveMasterDetailParent`,
1011
* `resolveMasterDetailParents`, `materializeParentHeader`,
1112
* `masterDeclaredFields`, `inspectDanglingReferences`, `inspectCascadeDelete`,
@@ -46,8 +47,8 @@ import type { IRealtimeService } from '@objectstack/spec/contracts';
4647
import { isStoredMetadataBodyObject } from '@objectstack/spec/kernel';
4748
import type { ExecutionContext } from '@objectstack/spec/kernel';
4849
import { postureUsesUnionScope } from '@objectstack/spec/security';
49-
import { homeAmountsTouched, planHomeAmounts, projectHomeAmounts } from './home-amount-projection.js';
50-
import type { HomeAmountPlanEntry, HomeAmountReads, HomeAmountServing, HomeLocalizationResolver } from './home-amount-projection.js';
50+
import { EXCHANGE_RATE_OBJECT, homeAmountsTouched, memoizeHomeAmountServing, planHomeAmounts, projectHomeAmounts } from './home-amount-projection.js';
51+
import type { HomeAmountPlanEntry, HomeAmountReads, HomeAmountServing, HomeLocalization, HomeLocalizationResolver } from './home-amount-projection.js';
5152
import { resolveMasterDetailRelation } from './master-detail.js';
5253
import { materializeDeclaredFields } from './declared-fields.js';
5354
import { collectCascadeDeleteObjects } from './engine-delete-cascade.js';
@@ -109,6 +110,61 @@ export class ReferencesAndMasterDetailFacet {
109110
this.homeLocalizationResolver = resolver;
110111
}
111112

113+
/**
114+
* [ADR-0144 D5, Open item 2] What one write's home projection reads, memoized
115+
* for that write: each organization's home currency once, and each
116+
* (organization, currency, document date) rate once, however many rows ask.
117+
*
118+
* The rate is the `sys_exchange_rate` row of that currency with the latest
119+
* `effective_date` on or before the document date, in the organization the
120+
* row lands in. Read `sudo()`-shaped (`{ ...context, isSystem: true }`): a
121+
* rank-and-file writer cannot read the rate table (parameter 2 makes it an
122+
* administrator's object), yet every write of theirs is converted through
123+
* it; the caller's transaction rides along, and `tenantId` is the row's own
124+
* organization, so one organization's rate never converts another's
125+
* amount. An install without the rate object registered has no rate for any
126+
* currency.
127+
*/
128+
static homeAmountServing(this: Pick<ReferencesAndMasterDetailHost, '_registry' | 'find' | 'homeLocalizationResolver'>, context: ExecutionContext | undefined): HomeAmountServing {
129+
// One settings read per organization per write answers both the home
130+
// currency and the timezone.
131+
const localizations = new Map<string, Promise<HomeLocalization | undefined>>();
132+
const localizationOf = (organizationId: string | undefined) => {
133+
const key = organizationId ?? '';
134+
let hit = localizations.get(key);
135+
if (!hit) {
136+
hit = Promise.resolve(this.homeLocalizationResolver?.(organizationId));
137+
localizations.set(key, hit);
138+
}
139+
return hit;
140+
};
141+
return memoizeHomeAmountServing({
142+
homeCurrency: async (organizationId) => (await localizationOf(organizationId))?.currency,
143+
timezone: async (organizationId) => (await localizationOf(organizationId))?.timezone,
144+
rateOn: async (organizationId, currency, onOrBefore) => {
145+
if (!this._registry.getObject(EXCHANGE_RATE_OBJECT)) return undefined;
146+
const rows = await this.find(EXCHANGE_RATE_OBJECT, {
147+
where: { currency, effective_date: { $lte: onOrBefore } },
148+
orderBy: [{ field: 'effective_date', order: 'desc' }],
149+
limit: 1,
150+
fields: ['rate', 'effective_date'],
151+
context: {
152+
...(context ?? {}),
153+
isSystem: true,
154+
...(organizationId !== undefined ? { tenantId: organizationId } : {}),
155+
} as ExecutionContext,
156+
});
157+
const row = rows[0] as { rate?: unknown; effective_date?: unknown } | undefined;
158+
if (!row) return undefined;
159+
const effective = row.effective_date;
160+
return {
161+
rate: row.rate,
162+
effectiveDate: effective instanceof Date ? effective.toISOString().slice(0, 10) : (typeof effective === 'string' ? effective : undefined),
163+
};
164+
},
165+
});
166+
}
167+
112168
/**
113169
* [ADR-0144 D5] The organization a written row belongs to, for its home
114170
* projection: the row's own `organization_id`, else the writer's active

‎packages/objectql/src/engine.ts‎

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,13 @@ import { count, rejectCredentialAggregation } from './engine-count-aggregate.js'
237237
import { rejectCredentialEvaluation } from './credential-evaluation-guard.js';
238238
import { assertRecordCurrencyCodes, touchesRecordCurrency } from './validation/record-currency-code.js';
239239
import {
240-
EXCHANGE_RATE_OBJECT,
241240
homeAmountsTouched,
242-
memoizeHomeAmountServing,
243241
planHomeAmounts,
244242
projectHomeAmounts,
245243
settleBulkHomeAmounts,
246244
type HomeAmountPlanEntry,
247245
type HomeAmountReads,
248246
type HomeAmountServing,
249-
type HomeLocalization,
250247
type HomeLocalizationResolver,
251248
} from './home-amount-projection.js';
252249
import { SchemaRegistry, type ArtifactInstallScope } from './registry.js';
@@ -2545,59 +2542,9 @@ export class ObjectQL implements IObjectQLEngine {
25452542
return ReferencesAndMasterDetailFacet.homeAmountReadsFor.call(referencesAndMasterDetailHost.of(this), context);
25462543
}
25472544

2548-
/**
2549-
* [ADR-0144 D5, Open item 2] What one write's home projection reads, memoized
2550-
* for that write: each organization's home currency once, and each
2551-
* (organization, currency, document date) rate once, however many rows ask.
2552-
*
2553-
* The rate is the `sys_exchange_rate` row of that currency with the latest
2554-
* `effective_date` on or before the document date, in the organization the
2555-
* row lands in. Read `sudo()`-shaped (`{ ...context, isSystem: true }`): a
2556-
* rank-and-file writer cannot read the rate table (parameter 2 makes it an
2557-
* administrator's object), yet every write of theirs is converted through
2558-
* it; the caller's transaction rides along, and `tenantId` is the row's own
2559-
* organization, so one organization's rate never converts another's
2560-
* amount. An install without the rate object registered has no rate for any
2561-
* currency.
2562-
*/
25632545
private homeAmountServing(context: ExecutionContext | undefined): HomeAmountServing {
2564-
// One settings read per organization per write answers both the home
2565-
// currency and the timezone.
2566-
const localizations = new Map<string, Promise<HomeLocalization | undefined>>();
2567-
const localizationOf = (organizationId: string | undefined) => {
2568-
const key = organizationId ?? '';
2569-
let hit = localizations.get(key);
2570-
if (!hit) {
2571-
hit = Promise.resolve(this.homeLocalizationResolver?.(organizationId));
2572-
localizations.set(key, hit);
2573-
}
2574-
return hit;
2575-
};
2576-
return memoizeHomeAmountServing({
2577-
homeCurrency: async (organizationId) => (await localizationOf(organizationId))?.currency,
2578-
timezone: async (organizationId) => (await localizationOf(organizationId))?.timezone,
2579-
rateOn: async (organizationId, currency, onOrBefore) => {
2580-
if (!this._registry.getObject(EXCHANGE_RATE_OBJECT)) return undefined;
2581-
const rows = await this.find(EXCHANGE_RATE_OBJECT, {
2582-
where: { currency, effective_date: { $lte: onOrBefore } },
2583-
orderBy: [{ field: 'effective_date', order: 'desc' }],
2584-
limit: 1,
2585-
fields: ['rate', 'effective_date'],
2586-
context: {
2587-
...(context ?? {}),
2588-
isSystem: true,
2589-
...(organizationId !== undefined ? { tenantId: organizationId } : {}),
2590-
} as ExecutionContext,
2591-
});
2592-
const row = rows[0] as { rate?: unknown; effective_date?: unknown } | undefined;
2593-
if (!row) return undefined;
2594-
const effective = row.effective_date;
2595-
return {
2596-
rate: row.rate,
2597-
effectiveDate: effective instanceof Date ? effective.toISOString().slice(0, 10) : (typeof effective === 'string' ? effective : undefined),
2598-
};
2599-
},
2600-
});
2546+
// [#29986 codemod] The body moved to `engine-references-and-master-detail.ts`, its docblock with it (see `ReferencesAndMasterDetailHost`).
2547+
return ReferencesAndMasterDetailFacet.homeAmountServing.call(referencesAndMasterDetailHost.of(this), context);
26012548
}
26022549

26032550
private static homeAmountOrganizationOf(

0 commit comments

Comments
 (0)