|
10 | 10 | // Cells: better-sqlite3 and sqlite-wasm always run; PostgreSQL runs where |
11 | 11 | // `OS_TEST_POSTGRES_URL` is set and is a named skip otherwise. The Postgres |
12 | 12 | // 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, |
14 | 15 | // preview and predicate-update cells) is packages/objectql's |
15 | 16 | // `engine-home-amount-projection.test.ts`. |
16 | 17 |
|
@@ -194,6 +195,38 @@ for (const cell of CELLS) { |
194 | 195 | expect((await engine.find('fx_invoice', { context: SYS.context } as never)).length).toBe(before); |
195 | 196 | }); |
196 | 197 |
|
| 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 | + |
197 | 230 | it('an update of the amount, the code or the date converts again; another edit does not', async () => { |
198 | 231 | home = 'CNY'; |
199 | 232 | const { id } = await engine.insert('fx_invoice', { name: 'u', currency_code: 'USD', invoice_date: '2026-03-15', amount: 10000 }, SYS); |
|
0 commit comments