@@ -305,6 +305,8 @@ import { count, rejectCredentialAggregation } from './engine-count-aggregate.js'
305305import { assertGroupByNamesNoJsonStoredField } from './group-by-structured-json-door.js';
306306import { assertProjectionNamesAField } from './empty-projection-door.js';
307307import { assertAggregationFieldTypesAccepted } from './aggregate-field-type-door.js';
308+ import { assertRecordCurrencyAggregatesGrouped } from './record-currency-aggregate-door.js';
309+ import { assertRecordCurrencyCodes, touchesRecordCurrency } from './validation/record-currency-code.js';
308310import { assertAggregatedColumnsAreDistinct, assertDateBucketTargetsAreTemporal } from './aggregate-query-shape-door.js';
309311import { applyProtection, pluralToSingular, ExternalWriteForbiddenError } from '@objectstack/spec/shared';
310312import { SchemaRegistry, type ArtifactInstallScope } from './registry.js';
@@ -15382,6 +15384,9 @@ export class ObjectQL implements IObjectQLEngine {
1538215384 normalizeMultiValueFields(schemaForValidation, rows[i], 'include');
1538315385 // [#22183] `keptOptionValues`: the option values an import kept for this write.
1538415386 validateRecordInScope(schemaForValidation, rows[i], 'insert', 'include', { mediaValueShapeStrict, valueShapeStrict, messages: msgCtx, onAdmittedValueShapeViolation, keptOptionValues: opCtx.context?.keptOptionValues });
15387+ // [ADR-0144 D8 rule 4] A `record`-mode amount is stored with its
15388+ // currency code, or the row is refused (`ERR_CURRENCY_CODE_REQUIRED`).
15389+ assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i], 'insert');
1538515390 emitValidationAdvisories(options?.onValidationAdvisory, evaluateValidationRules(schemaForValidation as any, rows[i], 'insert', { logger: this.logger, currentUser: this.buildEvalUser(opCtx.context), skipStateMachine: shouldSkipStateMachine(opCtx.context), messages: msgCtx, parent: insertParentForRow?.(rows[i]), related: batchSummaries ? batchSummaries.overlay(insertRelatedForRow(rows[i])) : insertRelatedForRow(rows[i]), permissions: insertPermissionsFor(rows[i]) }), this.logger, object);
1538615391 await this.assertReferencesResolve(
1538715392 schemaForValidation, rows[i], suppliedPerRow[i], opCtx.context, msgCtx,
@@ -17099,6 +17104,14 @@ export class ObjectQL implements IObjectQLEngine {
1709917104 // because those are already judged. See `ReadonlyValueScope`.
1710017105 normalizeMultiValueFields(updateSchema, hookContext.input.data as Record<string, unknown>, 'only');
1710117106 validateRecordInScope(updateSchema, hookContext.input.data as Record<string, unknown>, 'update', 'only', { mediaValueShapeStrict, valueShapeStrict, messages: updateMsgCtx, onAdmittedValueShapeViolation });
17107+ // [ADR-0144 D8 rule 4] The stored row with the FINAL payload laid
17108+ // over it keeps a currency code beside a record-mode amount the
17109+ // payload touches (`ERR_CURRENCY_CODE_REQUIRED`).
17110+ assertRecordCurrencyCodes(
17111+ object, (updateSchema as { fields?: unknown } | undefined)?.fields,
17112+ { ...(priorRecord ?? {}), ...(hookContext.input.data as Record<string, unknown>) },
17113+ 'update', hookContext.input.data as Record<string, unknown>, hookContext.input.id,
17114+ );
1710217115 // ── [#19989] The post-image seam on the BY-ID path ─────────────
1710317116 //
1710417117 // The by-id twin of the predicate-path call below, placed at the
@@ -17418,6 +17431,20 @@ export class ObjectQL implements IObjectQLEngine {
1741817431 // N rows are written.
1741917432 normalizeMultiValueFields(updateSchema, hookContext.input.data as Record<string, unknown>, 'only');
1742017433 validateRecordInScope(updateSchema, hookContext.input.data as Record<string, unknown>, 'update', 'only', { mediaValueShapeStrict, valueShapeStrict, messages: updateMsgCtx, onAdmittedValueShapeViolation });
17434+ // [ADR-0144 D8 rule 4] The predicate-path twin: every matched row,
17435+ // with the FINAL payload laid over it, keeps a currency code beside
17436+ // a record-mode amount the payload touches. The matched rows are
17437+ // read only for such a payload (`readPriorRows` is the memo the
17438+ // seams below share).
17439+ {
17440+ const currencyPayload = hookContext.input.data as Record<string, unknown>;
17441+ const currencyFields = (updateSchema as { fields?: unknown } | undefined)?.fields;
17442+ if (touchesRecordCurrency(currencyFields, currencyPayload)) {
17443+ for (const row of (await readPriorRows()) ?? []) {
17444+ assertRecordCurrencyCodes(object, currencyFields, { ...row, ...currencyPayload }, 'update', currencyPayload, row?.id);
17445+ }
17446+ }
17447+ }
1742117448 // ── [#19950] The post-image seam on the PREDICATE path ─────────
1742217449 //
1742317450 // An enforcement layer's write `check` must hold for EVERY row a
@@ -20343,6 +20370,11 @@ export class ObjectQL implements IObjectQLEngine {
2034320370 // string on SQLite and a 500 on PostgreSQL; `sum` over one answered `0`,
2034420371 // `0` and a 500. Every row of the table is asked (see the door's header).
2034520372 assertAggregationFieldTypesAccepted(object, this._registry.getObject(object), query.aggregations);
20373+ // [ADR-0144 D6] …and a `sum` / `avg` / `min` / `max` over a `record`-mode
20374+ // amount that does not group by its code field: the rows hold amounts in
20375+ // different currencies, which add into one number under one label.
20376+ // After the type door, so a type it refuses keeps that door's words.
20377+ assertRecordCurrencyAggregatesGrouped(object, this._registry.getObject(object), query.groupBy, query.aggregations);
2034620378 // [#25818] …and the two shapes that answered a plausible wrong table: a
2034720379 // `dateGranularity` over a field declared neither a date nor a datetime
2034820380 // (every row in one null bucket), and two columns of the aggregated row
0 commit comments