Skip to content

Commit 9ee0152

Browse files
committed
fix(objectql): say a followed parent code field is not served instead of asking for it to be written
A following amount whose parent object no longer serves the code field (the package that contributed it disabled or uninstalled) is still refused ERR_CURRENCY_CODE_REQUIRED / 400, but the message now says the field is not served, naming the disabled package when the registry still holds its contribution. The blank-code text is unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5
1 parent 63e47fe commit 9ee0152

5 files changed

Lines changed: 195 additions & 33 deletions

File tree

‎packages/objectql/src/engine-insert.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { ASSIGNMENT_RULE_TYPE, AssignmentRuleEvaluator, OWNERSHIP_QUEUE_SOURCE_S
5151
import type { AssignmentQueueService } from './assignment-rules.js';
5252
import { redactBoundStatement } from '@objectstack/types';
5353
import { ORGANIZATION_OBJECT } from './tenancy/system-write-organization.js';
54-
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs } from './validation/record-currency-code.js';
54+
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs } from './validation/record-currency-code.js';
5555
import { planHomeAmounts, projectHomeAmounts } from './home-amount-projection.js';
5656
import { stripSearchCompanion } from './search-companion.js';
5757
import type { EvalPermissions } from '@objectstack/formula';
@@ -1337,7 +1337,7 @@ export class InsertFacet {
13371337
// [ADR-0144 D8 rule 4] A `record`-mode amount is stored with its
13381338
// currency code, or the row is refused (`ERR_CURRENCY_CODE_REQUIRED`).
13391339
// [ADR-0144 D2, amended] A child amount's code is its parent's, laid on a copy of the row.
1340-
assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i] && typeof rows[i] === 'object' ? followCodes(rows[i]) : rows[i], 'insert', undefined, undefined, msgCtx);
1340+
assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i] && typeof rows[i] === 'object' ? followCodes(rows[i]) : rows[i], 'insert', undefined, undefined, msgCtx, parentCodeFieldServingOf(this._registry));
13411341
// [#29029] A write that is not a system write has its references
13421342
// checked BEFORE the record's validation rules: a reference the
13431343
// caller cannot read answers `reference_not_found`, whatever a rule

‎packages/objectql/src/engine-update.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { NotNullRefusalError, envelopeDriverWriteRefusal } from './not-null-refu
4949
import { redactBoundStatement } from '@objectstack/types';
5050
import { ORGANIZATION_OBJECT } from './tenancy/system-write-organization.js';
5151
import { rejectCredentialEvaluation } from './credential-evaluation-guard.js';
52-
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs, touchesRecordCurrency } from './validation/record-currency-code.js';
52+
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs, touchesRecordCurrency } from './validation/record-currency-code.js';
5353
import { homeAmountsTouched, planHomeAmounts, projectHomeAmounts, settleBulkHomeAmounts } from './home-amount-projection.js';
5454
import { assertFollowedCurrencyCodesUnchanged, parentCurrencyLockReads, touchesFollowedCurrencyCode } from './record-currency-parent-lock.js';
5555
import { stripSearchCompanion } from './search-companion.js';
@@ -1545,7 +1545,7 @@ export class UpdateFacet {
15451545
: undefined;
15461546
assertRecordCurrencyCodes(
15471547
object, currencyFields, followCodes ? followCodes(currencyImage) : currencyImage,
1548-
'update', currencyPayload, hookContext.input.id, updateMsgCtx,
1548+
'update', currencyPayload, hookContext.input.id, updateMsgCtx, parentCodeFieldServingOf(this._registry),
15491549
);
15501550
}
15511551
// [ADR-0144 D2, amended] …and a record a child row follows the
@@ -1954,7 +1954,7 @@ export class UpdateFacet {
19541954
recordCurrencyPairs(currencyFields), images.map((i) => i.image), this.homeAmountServing(opCtx.context).parentRows,
19551955
);
19561956
for (const { row, image } of images) {
1957-
assertRecordCurrencyCodes(object, currencyFields, followCodes(image), 'update', currencyPayload, row?.id, updateMsgCtx);
1957+
assertRecordCurrencyCodes(object, currencyFields, followCodes(image), 'update', currencyPayload, row?.id, updateMsgCtx, parentCodeFieldServingOf(this._registry));
19581958
}
19591959
}
19601960
// [ADR-0144 D2, amended] The predicate-path twin of the

‎packages/objectql/src/engine-validate.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type { WriteObservabilityOptions } from '@objectstack/spec/contracts';
5050
import { validateRecordInScope, normalizeBlankTypedValues, normalizeNumericStringValues, ValidationError, valueShapeStrictEffective, mediaStrictEffective } from './validation/record-validator.js';
5151
import { evaluateValidationRules, hasParentScopedRequiredWhen, stripReadonlyFields, stripRuntimeOwnedFields } from './validation/rule-validator.js';
5252
import { createBatchSummaryAccumulator } from './summary-batch-accumulation.js';
53-
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs, RecordCurrencyCodeRequiredError, touchesRecordCurrency } from './validation/record-currency-code.js';
53+
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs, RecordCurrencyCodeRequiredError, touchesRecordCurrency } from './validation/record-currency-code.js';
5454
import { assertFollowedCurrencyCodesUnchanged, parentCurrencyLockReads, touchesFollowedCurrencyCode } from './record-currency-parent-lock.js';
5555
import { isHomeAmountRefusal } from './home-amount-projection.js';
5656
import { referencesCheckedBeforeRules } from './engine-referential-integrity.js';
@@ -580,7 +580,7 @@ export async function validate(
580580
assertRecordCurrencyCodes(
581581
object, currencyFields,
582582
followCodes && currencyImage ? followCodes(currencyImage) : currencyImage, mode, mode === 'insert' ? undefined : row, storedIds[i],
583-
messages,
583+
messages, parentCodeFieldServingOf(this._registry),
584584
);
585585
// [ADR-0144 D2, amended] …and the write's parent-code lock, on the
586586
// stored row the by-id update writes: a record a child row follows

‎packages/objectql/src/record-currency-guards.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,67 @@ describe('a single-object save is refused a `dynamic` roll-up over a child field
13421342
expect(() => save(valid.engine, project('ext_home'))).not.toThrow();
13431343
});
13441344
});
1345+
1346+
// ── [ADR-0144 D2, amended] A followed parent code field the parent no longer serves ──
1347+
//
1348+
// The fixture is the package-lifecycle measurement's shape: `billing` owns
1349+
// `suite_invoice` and `suite_line`, `supplier` extends `suite_invoice` with the
1350+
// code field, and `suite_line.amount` follows `invoice.currency_code`. After the
1351+
// supplier leaves, the next child write is still refused ERR_CURRENCY_CODE_REQUIRED
1352+
// / 400, and the message says the field is not served instead of asking for a
1353+
// code to be written into a field that is not there.
1354+
1355+
describe('a following amount whose parent code field is no longer served is refused, and the message says so (ADR-0144 D2, amended)', () => {
1356+
const BILLING = 'com.test.billing';
1357+
const SUPPLIER = 'com.test.supplier';
1358+
const NOT_SERVED = "and 'suite_invoice' does not serve a field 'currency_code' (";
1359+
const NO_CODE_TO_FOLLOW = "; the amount has no code to follow until that field is served again, or leave 'amount' empty. The row was not written.";
1360+
1361+
async function bootSuite() {
1362+
const booted = await bootEngine();
1363+
booted.engine.registerApp({
1364+
id: BILLING, name: 'billing',
1365+
objects: [
1366+
{ name: 'suite_invoice', fields: { title: { type: 'text' } } },
1367+
{ name: 'suite_line', fields: { title: { type: 'text' }, invoice: { type: 'master_detail', reference: 'suite_invoice' }, amount: { type: 'currency', currencyConfig: FOLLOWS } } },
1368+
],
1369+
} as any);
1370+
booted.engine.registerApp({ id: SUPPLIER, name: 'supplier', objectExtensions: [{ extend: 'suite_invoice', fields: { currency_code: CODE_FIELD } }] } as any);
1371+
await booted.engine.insert('suite_invoice', { id: 'inv_usd', title: 'usd', currency_code: 'USD' });
1372+
await booted.engine.insert('suite_invoice', { id: 'inv_blank', title: 'blank' });
1373+
// CONTROL: while the supplier serves the field, a line under a parent holding a code is written.
1374+
await booted.engine.insert('suite_line', { title: 'before', invoice: 'inv_usd', amount: 100 });
1375+
return booted;
1376+
}
1377+
1378+
it('disable: the supplier disabled, a line with an amount is refused naming the field as not served and the disabled package', async () => {
1379+
const { engine, storeFor } = await bootSuite();
1380+
engine.registry.disablePackage(SUPPLIER);
1381+
expect((engine.registry.getObject('suite_invoice')?.fields as any)?.currency_code).toBeUndefined();
1382+
const err = await refusalOf(engine.insert('suite_line', { title: 'after', invoice: 'inv_usd', amount: 100 }));
1383+
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
1384+
expect(err?.field).toBe('invoice.currency_code');
1385+
expect(err?.message).toContain(`${NOT_SERVED}the package '${SUPPLIER}' that contributed it is disabled)${NO_CODE_TO_FOLLOW}`);
1386+
expect(err?.message).not.toContain('has a blank');
1387+
expect(storeFor('suite_line').size).toBe(1);
1388+
// Uninstalled, the registry no longer holds the contribution: the package clause is the generic one.
1389+
engine.registry.uninstallPackage(SUPPLIER);
1390+
const gone = await refusalOf(engine.insert('suite_line', { title: 'gone', invoice: 'inv_usd', amount: 100 }));
1391+
expect(envelopeOf(gone)).toEqual(CODE_REQUIRED);
1392+
expect(gone?.message).toContain(`${NOT_SERVED}a package that contributed it may be disabled or uninstalled)${NO_CODE_TO_FOLLOW}`);
1393+
});
1394+
1395+
it('CONTROL: a served code field that is blank on the parent keeps the blank-code text', async () => {
1396+
const { engine } = await bootSuite();
1397+
const err = await refusalOf(engine.insert('suite_line', { title: 'blank', invoice: 'inv_blank', amount: 100 }));
1398+
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
1399+
expect(err?.message).toBe(
1400+
"Insert on 'suite_line' was REFUSED: 'amount' holds an amount in its parent record's currency (currencyMode: 'record', "
1401+
+ "following 'invoice.currency_code' through the master_detail 'invoice'), and the 'suite_invoice' record it names "
1402+
+ "has a blank 'currency_code', or it names none. The row was not written. Write the parent's ISO 4217 code (for "
1403+
+ "example 'USD') into 'suite_invoice.currency_code' first, and name that parent in 'invoice', or leave 'amount' "
1404+
+ 'empty. A child stores no code of its own: it follows its parent\'s. The server never fills a currency in: a '
1405+
+ 'form pre-fills the organization\'s currency, and the stored code is always the one submitted.',
1406+
);
1407+
});
1408+
});

0 commit comments

Comments
 (0)