|
4 | 4 | * [#29986 codemod] The |
5 | 5 | * "Realtime, i18n, home amounts, validation messages, references, master-detail" |
6 | 6 | * section of `ObjectQL`: the bodies of `setRealtimeService`, `setI18nService`, |
7 | | - * `setHomeLocalizationResolver`, `homeAmountOrganizationOf`, |
8 | | - * `previewHomeAmounts`, `projectHomeAmountsOntoPatch`, `referenceExists`, |
| 7 | + * `setHomeLocalizationResolver`, `homeAmountServing`, |
| 8 | + * `homeAmountOrganizationOf`, `previewHomeAmounts`, |
| 9 | + * `projectHomeAmountsOntoPatch`, `referenceExists`, |
9 | 10 | * `callerReadableReferenceExists`, `resolveMasterDetailParent`, |
10 | 11 | * `resolveMasterDetailParents`, `materializeParentHeader`, |
11 | 12 | * `masterDeclaredFields`, `inspectDanglingReferences`, `inspectCascadeDelete`, |
@@ -46,8 +47,8 @@ import type { IRealtimeService } from '@objectstack/spec/contracts'; |
46 | 47 | import { isStoredMetadataBodyObject } from '@objectstack/spec/kernel'; |
47 | 48 | import type { ExecutionContext } from '@objectstack/spec/kernel'; |
48 | 49 | 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'; |
51 | 52 | import { resolveMasterDetailRelation } from './master-detail.js'; |
52 | 53 | import { materializeDeclaredFields } from './declared-fields.js'; |
53 | 54 | import { collectCascadeDeleteObjects } from './engine-delete-cascade.js'; |
@@ -109,6 +110,61 @@ export class ReferencesAndMasterDetailFacet { |
109 | 110 | this.homeLocalizationResolver = resolver; |
110 | 111 | } |
111 | 112 |
|
| 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 | + |
112 | 168 | /** |
113 | 169 | * [ADR-0144 D5] The organization a written row belongs to, for its home |
114 | 170 | * projection: the row's own `organization_id`, else the writer's active |
|
0 commit comments