Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/33888-unserved-parent-code-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/objectql': patch
---

fix(objectql): a following amount whose parent code field is no longer served is refused with a message that says so

Clause-②: no
Metadata-protocol: none

A `record`-mode amount that follows its parent's currency (`currencyField: 'MASTER_DETAIL.CODE'`) is still refused `ERR_CURRENCY_CODE_REQUIRED` / 400 when the parent object, as served at the write, no longer declares the code field (the package that contributed it was disabled or uninstalled). The message used to say the parent's code was blank and asked for a code to be written into that field, which is not there. It now says the parent does not serve the field, names the disabled package when the registry still holds its contribution, and says the amount has no code to follow until the field is served again, or that the amount can be left empty. The code, status, `field` and `amountField` are unchanged, and so is the message for a served code field that is blank.
4 changes: 2 additions & 2 deletions packages/objectql/src/engine-insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { ASSIGNMENT_RULE_TYPE, AssignmentRuleEvaluator, OWNERSHIP_QUEUE_SOURCE_S
import type { AssignmentQueueService } from './assignment-rules.js';
import { redactBoundStatement } from '@objectstack/types';
import { ORGANIZATION_OBJECT } from './tenancy/system-write-organization.js';
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs } from './validation/record-currency-code.js';
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs } from './validation/record-currency-code.js';
import { planHomeAmounts, projectHomeAmounts } from './home-amount-projection.js';
import { stripSearchCompanion } from './search-companion.js';
import type { EvalPermissions } from '@objectstack/formula';
Expand Down Expand Up @@ -1337,7 +1337,7 @@ export class InsertFacet {
// [ADR-0144 D8 rule 4] A `record`-mode amount is stored with its
// currency code, or the row is refused (`ERR_CURRENCY_CODE_REQUIRED`).
// [ADR-0144 D2, amended] A child amount's code is its parent's, laid on a copy of the row.
assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i] && typeof rows[i] === 'object' ? followCodes(rows[i]) : rows[i], 'insert', undefined, undefined, msgCtx);
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));
// [#29029] A write that is not a system write has its references
// checked BEFORE the record's validation rules: a reference the
// caller cannot read answers `reference_not_found`, whatever a rule
Expand Down
6 changes: 3 additions & 3 deletions packages/objectql/src/engine-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { NotNullRefusalError, envelopeDriverWriteRefusal } from './not-null-refu
import { redactBoundStatement } from '@objectstack/types';
import { ORGANIZATION_OBJECT } from './tenancy/system-write-organization.js';
import { rejectCredentialEvaluation } from './credential-evaluation-guard.js';
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs, touchesRecordCurrency } from './validation/record-currency-code.js';
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs, touchesRecordCurrency } from './validation/record-currency-code.js';
import { homeAmountsTouched, planHomeAmounts, projectHomeAmounts, settleBulkHomeAmounts } from './home-amount-projection.js';
import { assertFollowedCurrencyCodesUnchanged, parentCurrencyLockReads, touchesFollowedCurrencyCode } from './record-currency-parent-lock.js';
import { stripSearchCompanion } from './search-companion.js';
Expand Down Expand Up @@ -1545,7 +1545,7 @@ export class UpdateFacet {
: undefined;
assertRecordCurrencyCodes(
object, currencyFields, followCodes ? followCodes(currencyImage) : currencyImage,
'update', currencyPayload, hookContext.input.id, updateMsgCtx,
'update', currencyPayload, hookContext.input.id, updateMsgCtx, parentCodeFieldServingOf(this._registry),
);
}
// [ADR-0144 D2, amended] …and a record a child row follows the
Expand Down Expand Up @@ -1954,7 +1954,7 @@ export class UpdateFacet {
recordCurrencyPairs(currencyFields), images.map((i) => i.image), this.homeAmountServing(opCtx.context).parentRows,
);
for (const { row, image } of images) {
assertRecordCurrencyCodes(object, currencyFields, followCodes(image), 'update', currencyPayload, row?.id, updateMsgCtx);
assertRecordCurrencyCodes(object, currencyFields, followCodes(image), 'update', currencyPayload, row?.id, updateMsgCtx, parentCodeFieldServingOf(this._registry));
}
}
// [ADR-0144 D2, amended] The predicate-path twin of the
Expand Down
4 changes: 2 additions & 2 deletions packages/objectql/src/engine-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import type { WriteObservabilityOptions } from '@objectstack/spec/contracts';
import { validateRecordInScope, normalizeBlankTypedValues, normalizeNumericStringValues, ValidationError, valueShapeStrictEffective, mediaStrictEffective } from './validation/record-validator.js';
import { evaluateValidationRules, hasParentScopedRequiredWhen, stripReadonlyFields, stripRuntimeOwnedFields } from './validation/rule-validator.js';
import { createBatchSummaryAccumulator } from './summary-batch-accumulation.js';
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs, RecordCurrencyCodeRequiredError, touchesRecordCurrency } from './validation/record-currency-code.js';
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs, RecordCurrencyCodeRequiredError, touchesRecordCurrency } from './validation/record-currency-code.js';
import { assertFollowedCurrencyCodesUnchanged, parentCurrencyLockReads, touchesFollowedCurrencyCode } from './record-currency-parent-lock.js';
import { isHomeAmountRefusal } from './home-amount-projection.js';
import { referencesCheckedBeforeRules } from './engine-referential-integrity.js';
Expand Down Expand Up @@ -580,7 +580,7 @@ export async function validate(
assertRecordCurrencyCodes(
object, currencyFields,
followCodes && currencyImage ? followCodes(currencyImage) : currencyImage, mode, mode === 'insert' ? undefined : row, storedIds[i],
messages,
messages, parentCodeFieldServingOf(this._registry),
);
// [ADR-0144 D2, amended] …and the write's parent-code lock, on the
// stored row the by-id update writes: a record a child row follows
Expand Down
64 changes: 64 additions & 0 deletions packages/objectql/src/record-currency-guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,3 +1342,67 @@ describe('a single-object save is refused a `dynamic` roll-up over a child field
expect(() => save(valid.engine, project('ext_home'))).not.toThrow();
});
});

// ── [ADR-0144 D2, amended] A followed parent code field the parent no longer serves ──
//
// The fixture is the package-lifecycle measurement's shape: `billing` owns
// `suite_invoice` and `suite_line`, `supplier` extends `suite_invoice` with the
// code field, and `suite_line.amount` follows `invoice.currency_code`. After the
// supplier leaves, the next child write is still refused ERR_CURRENCY_CODE_REQUIRED
// / 400, and the message says the field is not served instead of asking for a
// code to be written into a field that is not there.

describe('a following amount whose parent code field is no longer served is refused, and the message says so (ADR-0144 D2, amended)', () => {
const BILLING = 'com.test.billing';
const SUPPLIER = 'com.test.supplier';
const NOT_SERVED = "and 'suite_invoice' does not serve a field 'currency_code' (";
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.";

async function bootSuite() {
const booted = await bootEngine();
booted.engine.registerApp({
id: BILLING, name: 'billing',
objects: [
{ name: 'suite_invoice', fields: { title: { type: 'text' } } },
{ name: 'suite_line', fields: { title: { type: 'text' }, invoice: { type: 'master_detail', reference: 'suite_invoice' }, amount: { type: 'currency', currencyConfig: FOLLOWS } } },
],
} as any);
booted.engine.registerApp({ id: SUPPLIER, name: 'supplier', objectExtensions: [{ extend: 'suite_invoice', fields: { currency_code: CODE_FIELD } }] } as any);
await booted.engine.insert('suite_invoice', { id: 'inv_usd', title: 'usd', currency_code: 'USD' });
await booted.engine.insert('suite_invoice', { id: 'inv_blank', title: 'blank' });
// CONTROL: while the supplier serves the field, a line under a parent holding a code is written.
await booted.engine.insert('suite_line', { title: 'before', invoice: 'inv_usd', amount: 100 });
return booted;
}

it('disable: the supplier disabled, a line with an amount is refused naming the field as not served and the disabled package', async () => {
const { engine, storeFor } = await bootSuite();
engine.registry.disablePackage(SUPPLIER);
expect((engine.registry.getObject('suite_invoice')?.fields as any)?.currency_code).toBeUndefined();
const err = await refusalOf(engine.insert('suite_line', { title: 'after', invoice: 'inv_usd', amount: 100 }));
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
expect(err?.field).toBe('invoice.currency_code');
expect(err?.message).toContain(`${NOT_SERVED}the package '${SUPPLIER}' that contributed it is disabled)${NO_CODE_TO_FOLLOW}`);
expect(err?.message).not.toContain('has a blank');
expect(storeFor('suite_line').size).toBe(1);
// Uninstalled, the registry no longer holds the contribution: the package clause is the generic one.
engine.registry.uninstallPackage(SUPPLIER);
const gone = await refusalOf(engine.insert('suite_line', { title: 'gone', invoice: 'inv_usd', amount: 100 }));
expect(envelopeOf(gone)).toEqual(CODE_REQUIRED);
expect(gone?.message).toContain(`${NOT_SERVED}a package that contributed it may be disabled or uninstalled)${NO_CODE_TO_FOLLOW}`);
});

it('CONTROL: a served code field that is blank on the parent keeps the blank-code text', async () => {
const { engine } = await bootSuite();
const err = await refusalOf(engine.insert('suite_line', { title: 'blank', invoice: 'inv_blank', amount: 100 }));
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
expect(err?.message).toBe(
"Insert on 'suite_line' was REFUSED: 'amount' holds an amount in its parent record's currency (currencyMode: 'record', "
+ "following 'invoice.currency_code' through the master_detail 'invoice'), and the 'suite_invoice' record it names "
+ "has a blank 'currency_code', or it names none. The row was not written. Write the parent's ISO 4217 code (for "
+ "example 'USD') into 'suite_invoice.currency_code' first, and name that parent in 'invoice', or leave 'amount' "
+ 'empty. A child stores no code of its own: it follows its parent\'s. The server never fills a currency in: a '
+ 'form pre-fills the organization\'s currency, and the stored code is always the one submitted.',
);
});
});
Loading
Loading