Skip to content

Commit 2fe4a2c

Browse files
committed
test: rates are per organization on the real drivers; CLDR fraction digits, not ISO 4217 minor units; the scale pin's header matches its body (#27192)
Claude-Session: https://claude.ai/code/session_01Utp6CYjLJ7sxfUehRrLbT3 Co-authored-by: Claude <noreply@anthropic.com>
1 parent c37e52a commit 2fe4a2c

7 files changed

Lines changed: 62 additions & 18 deletions

File tree

‎.changeset/27192-home-amount-error-codes.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ Metadata-protocol: none
1111

1212
- `ERR_EXCHANGE_RATE_MISSING` (400): a write that must convert an amount into the home currency found no exchange rate in force on the record's document date.
1313
- `ERR_HOME_CURRENCY_UNSET` (400): the same write found no home currency, because the organization's `localization.currency` setting is not set.
14-
- `CURRENCY_FRACTION_DIGITS` (ISO 4217 minor units per code) is the table the engine rounds a home amount with, exported so a consumer rounds with the same table instead of a copy.
14+
- `CURRENCY_FRACTION_DIGITS` (CLDR fraction digits per code, which differ from ISO 4217's minor units for some codes such as IQD, HUF, COP, IDR and PKR) is the table the engine rounds a home amount with, exported so a consumer rounds with the same table instead of a copy.

‎.changeset/27192-home-amount-projection.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Metadata-protocol: none
1212
When a record is inserted, and when an update changes its amount, its currency code or its document date, the engine writes the two fields `homeAmount` names:
1313

1414
- **The rate** is the organization's `sys_exchange_rate` row for the record's currency with the latest `effective_date` on or before the document date (`homeAmount.rateDate`). A `datetime` document date is read as its UTC calendar day. The row's rate is stored in `homeAmount.rateField` as it is, unrounded.
15-
- **The home amount** is the amount divided by the rate (a rate states how many units of the foreign currency one unit of the home currency buys), computed in exact decimal arithmetic and rounded once, half away from zero, to the home currency's minor units (0 for JPY, 2 for USD and CNY, 3 for KWD). USD 10,000 at 0.1389 into CNY stores 71994.24.
15+
- **The home amount** is the amount divided by the rate (a rate states how many units of the foreign currency one unit of the home currency buys), computed in exact decimal arithmetic and rounded once, half away from zero, to the home currency's CLDR fraction digits (0 for JPY, 2 for USD and CNY, 3 for KWD; for a few codes, such as IQD, HUF, COP, IDR and PKR, CLDR's count differs from ISO 4217's minor units, and CLDR's applies). USD 10,000 at 0.1389 into CNY stores 71994.24.
1616
- **Same currency as home:** rate `1`, no rate read. **Blank amount:** blank home amount and rate.
1717
- **The home currency** is the `localization.currency` setting, read for the row's organization through the `settings` service. `ObjectQLPlugin` wires it; an engine booted without the plugin answers it through `setHomeCurrencyResolver`.
1818
- **The engine's alone.** A value a caller sends for either field is dropped (or refused under `strictReadonlyWrites`) as before; a `defaultValue`, a system write or a historical (`preserveAudit`) import of either field is computed over.

‎content/docs/data-modeling/currency.mdx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ document date, the engine computes the home amount:
263263
neither does "the most recent rate" when it is dated later. A `datetime` document date
264264
is read as its calendar day in UTC.
265265
- **The home amount** is the amount **divided by** the rate, computed in exact decimal
266-
arithmetic and rounded once, half away from zero, to the home currency's minor units:
267-
0 decimals for JPY, 2 for USD and CNY, 3 for KWD. USD 10,000 at a rate of 0.1389 into
266+
arithmetic and rounded once, half away from zero, to the home currency's decimal
267+
places as CLDR gives them (its fraction digits): 0 for JPY, 2 for USD and CNY, 3 for
268+
KWD. For a few currencies CLDR's count differs from ISO 4217's minor units (IQD, HUF,
269+
COP, IDR and PKR among them), and CLDR's applies. USD 10,000 at a rate of 0.1389 into
268270
CNY stores ¥71,994.24.
269271
- **The rate field** stores the rate row's own value, unrounded, so the stored home
270272
amount always reproduces from the stored amount and rate.

‎packages/objectql/src/engine-home-amount-projection.test.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
// orderBy, limit, fields), so the rate row the projection picks is chosen by
77
// the same `effective_date <= date ORDER BY effective_date DESC LIMIT 1` a
88
// SQL driver runs. ⛔ No driver-memory arm: its test consumers are a closed
9-
// census (`check:driver-memory-census`). The same pins on SQLite and Postgres
10-
// are packages/qa/dogfood's `test/home-amount-projection.test.ts`.
9+
// census (`check:driver-memory-census`). The double ignores `tenantId`, so
10+
// organization isolation of the rate read is not pinned here: the same pins,
11+
// and the two-organization one, on SQLite and Postgres are packages/qa/dogfood's
12+
// `test/home-amount-projection.test.ts`.
1113
//
1214
// Pinned here, per seam (insert, insertMany, by-id update, predicate update,
1315
// the validate() preview):
1416
//
15-
// - the stored home amount is amount ÷ rate, rounded to the home minor units,
17+
// - the stored home amount is amount ÷ rate, rounded to the home currency's
18+
// CLDR fraction digits,
1619
// stored with the rate used; the same currency as home is rate 1, read-free;
1720
// - a missing rate refuses ERR_EXCHANGE_RATE_MISSING and an unset home
1821
// currency ERR_HOME_CURRENCY_UNSET, and nothing is written;
1922
// - the two projected fields: a caller's value is stripped (`readonly`) and
2023
// recomputed over, a system or `preserveAudit` writer's value is recomputed
21-
// over, a `defaultValue` never stands, a rate field `scale` below the
22-
// rate's decimals refuses `max_scale` (never rounded);
24+
// over, a `defaultValue` never stands; a rate field declaring a `scale`
25+
// below the rate's decimals stores the full rate, never rounded and never
26+
// refused (a `readonly` value is checked for shape only, so the declared
27+
// scale reaches display only);
2328
// - an update projects when it moves the amount, its code or its document
2429
// date, and never otherwise (a corrected rate reaches stored rows only
2530
// through a recompute).

‎packages/objectql/src/home-amount-projection.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* `ERR_HOME_CURRENCY_UNSET` / 400 (ADR-0144 Open item 4): the sources rule
2020
* out a silent default, so the engine never picks one.
2121
* - **The same currency as home** stores rate `1` and the amount rounded to
22-
* the home currency's minor units, with no rate lookup and no rate row
22+
* the home currency's fraction digits, with no rate lookup and no rate row
2323
* needed (Open item 6).
2424
* - **A foreign currency** reads the rate row of that currency with the latest
2525
* `effective_date` on or before the record's document date (Open item 2):
@@ -31,8 +31,10 @@
3131
* foreign currency", so the home amount is the record amount DIVIDED by the
3232
* rate. It is exact decimal arithmetic on the decimal spellings of the two
3333
* numbers (scaled integers, never a floating-point division), rounded once,
34-
* half away from zero, to the home currency's ISO 4217 minor units read from
35-
* `CURRENCY_FRACTION_DIGITS` (Open item 3): JPY 0, USD 2, KWD 3.
34+
* half away from zero, to the home currency's CLDR fraction digits read from
35+
* `CURRENCY_FRACTION_DIGITS` (Open item 3): JPY 0, USD 2, KWD 3. CLDR's
36+
* count differs from ISO 4217's minor units for some codes (IQD, HUF, COP,
37+
* IDR, PKR among them); the table is CLDR's, and so is the rounding.
3638
* - **The rate stored** is the rate row's own value, never rounded (D1: the
3739
* amount is stored "with the rate used"; a rounded rate would not reproduce
3840
* the stored home amount). The rate field is `readonly`, and a readonly

‎packages/qa/dogfood/test/home-amount-projection.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
// Cells: better-sqlite3 and sqlite-wasm always run; PostgreSQL runs where
1111
// `OS_TEST_POSTGRES_URL` is set and is a named skip otherwise. The Postgres
1212
// cell owns one schema, named from this file, created before and dropped
13-
// after. The in-memory face of the same pins (and the strips, defaults,
13+
// after. One pin runs under the `isolated` posture with two organizations: a
14+
// rate row is read in the written row's organization only. The in-memory face of the same pins (and the strips, defaults,
1415
// preview and predicate-update cells) is packages/objectql's
1516
// `engine-home-amount-projection.test.ts`.
1617

@@ -194,6 +195,38 @@ for (const cell of CELLS) {
194195
expect((await engine.find('fx_invoice', { context: SYS.context } as never)).length).toBe(before);
195196
});
196197

198+
it('rates are per organization: one organization\'s rate row never converts another\'s write', async () => {
199+
home = 'CNY';
200+
// Organizations are walled from each other under the `isolated` posture;
201+
// `single` is one organization by definition and reads install-wide.
202+
engine.setTenancyPostureProvider(() => 'isolated');
203+
try {
204+
const driverOf = engine.getDriverForObject('sys_organization')!;
205+
for (const id of ['org_fx_a', 'org_fx_b']) {
206+
if (!(await driverOf.findOne('sys_organization', { where: { id } } as never))) await driverOf.create('sys_organization', { id, name: id });
207+
}
208+
const as = (org: string) => ({ context: { isSystem: true, tenantId: org } });
209+
// SEK has a rate in organization A only; NOK in both, at different rates.
210+
await engine.insert('sys_exchange_rate', { currency: 'SEK', effective_date: '2026-01-01', rate: 1.5, source: 'manual' }, as('org_fx_a'));
211+
await engine.insert('sys_exchange_rate', { currency: 'NOK', effective_date: '2026-01-01', rate: 1.6, source: 'manual' }, as('org_fx_a'));
212+
await engine.insert('sys_exchange_rate', { currency: 'NOK', effective_date: '2026-01-01', rate: 2, source: 'manual' }, as('org_fx_b'));
213+
214+
const inA = await engine.insert('fx_invoice', { name: 'a', currency_code: 'SEK', invoice_date: '2026-03-15', amount: 300 }, as('org_fx_a'));
215+
expect(await read(inA.id)).toEqual({ home: 200, rate: 1.5, org: 'org_fx_a' });
216+
const nokA = await engine.insert('fx_invoice', { name: 'a2', currency_code: 'NOK', invoice_date: '2026-03-15', amount: 320 }, as('org_fx_a'));
217+
expect(await read(nokA.id)).toEqual({ home: 200, rate: 1.6, org: 'org_fx_a' });
218+
const nokB = await engine.insert('fx_invoice', { name: 'b', currency_code: 'NOK', invoice_date: '2026-03-15', amount: 320 }, as('org_fx_b'));
219+
expect(await read(nokB.id)).toEqual({ home: 160, rate: 2, org: 'org_fx_b' });
220+
221+
// Organization B has no SEK rate: A's row is not read for it, so the write is refused.
222+
const err = await engine.insert('fx_invoice', { name: 'b2', currency_code: 'SEK', invoice_date: '2026-03-15', amount: 300 }, as('org_fx_b'))
223+
.then(() => null, (e: { code?: string }) => e);
224+
expect(err?.code).toBe('ERR_EXCHANGE_RATE_MISSING');
225+
} finally {
226+
engine.setTenancyPostureProvider(() => undefined);
227+
}
228+
});
229+
197230
it('an update of the amount, the code or the date converts again; another edit does not', async () => {
198231
home = 'CNY';
199232
const { id } = await engine.insert('fx_invoice', { name: 'u', currency_code: 'USD', invoice_date: '2026-03-15', amount: 10000 }, SYS);

‎packages/spec/src/data/index.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,13 @@ export * from './api-derivation';
180180
// so the two can never drift apart (#7521).
181181
export * from './managed-api-affordance';
182182
export * from './field.zod';
183-
// [ADR-0144 Open item 3] The ISO 4217 minor units per currency — the checked-in
184-
// CLDR snapshot whose key set is the `iso_4217_currency` value domain. Exported
185-
// for its first digit reader: objectql's write-time home projection rounds a
186-
// home amount to the home currency's minor units with it, from this one table
187-
// rather than a second copy or a runtime `Intl` probe.
183+
// [ADR-0144 Open item 3] The CLDR fraction digits per currency — the checked-in
184+
// CLDR snapshot whose key set is the `iso_4217_currency` value domain. Its
185+
// digit counts are CLDR's, which differ from ISO 4217's minor units for some
186+
// codes (IQD 0 vs 3; HUF, COP, IDR, PKR 0 vs 2). Exported for its first digit
187+
// reader: objectql's write-time home projection rounds a home amount to the
188+
// home currency's CLDR fraction digits with it, from this one table rather
189+
// than a second copy or a runtime `Intl` probe.
188190
export * from './currency-fraction-digits';
189191
// The credential read mask (ADR-0100) — the ONE string a masked read serves, in
190192
// place of the two byte-identical literals objectql and service-settings each

0 commit comments

Comments
 (0)