5353 *
5454 * `INVALID_FIELD` / 400 through `invalidMemberError` (ADR-0112), with the
5555 * column and its object attached, as the #21044 door attaches them: a verdict
56- * about one member the request named. The words carry the prescription:
57- * group by the code field. A consolidated total aggregates the stored home
58- * field instead (ADR-0144 D6), which the write-time projection (D5) fills.
56+ * about one member the request named. The words carry both prescriptions
57+ * (#27195): for one consolidated total, aggregate the stored home field
58+ * instead (ADR-0144 D6), which the write-time projection (D5) fills — named
59+ * when the amount declares `currencyConfig.homeAmount`, and the declaration
60+ * to add when it does not — or group by the code field for one row per
61+ * currency. A measure over the home field is no record-mode amount (the home
62+ * field is `dynamic`), so this door never judges it: it is served unchanged.
5963 */
6064
6165import type { AnalyticsQuery } from '@objectstack/spec/contracts' ;
@@ -75,15 +79,17 @@ export const AMOUNT_AGGREGATES: ReadonlySet<string> = new Set(['sum', 'avg', 'mi
7579 * reference) — or `undefined` when it resolves to no declared measure.
7680 * @param dimensionColumnOf - The column a `dimensions` entry groups by, located
7781 * the same way, or `null` when this door cannot pin one.
78- * @param recordCurrencyFieldOf - The `currencyConfig.currencyField` of a column
79- * declared `currencyMode: 'record'`, or `undefined` for any other column.
82+ * @param recordCurrencyOf - For a column declared `currencyMode: 'record'`, its
83+ * `currencyConfig.currencyField` and, when it declares `homeAmount`, the
84+ * home field (`homeAmount.field`) on the same object; `undefined` for any
85+ * other column.
8086 */
8187export function assertRecordCurrencyAggregatesGrouped (
8288 query : AnalyticsQuery ,
8389 cubeName : string ,
8490 measureOf : ( member : string ) => { type : unknown ; column : MeasureColumn | null } | undefined ,
8591 dimensionColumnOf : ( member : string ) => MeasureColumn | null ,
86- recordCurrencyFieldOf : ( object : string , field : string ) => string | undefined ,
92+ recordCurrencyOf : ( object : string , field : string ) => { codeField : string ; homeField ?: string } | undefined ,
8793) : void {
8894 const measures = query . measures ?? [ ] ;
8995 if ( measures . length === 0 ) return ;
@@ -92,20 +98,25 @@ export function assertRecordCurrencyAggregatesGrouped(
9298 const measure = measureOf ( member ) ;
9399 if ( ! measure ?. column || typeof measure . type !== 'string' || ! AMOUNT_AGGREGATES . has ( measure . type ) ) continue ;
94100 const target = measure . column ;
95- const codeField = recordCurrencyFieldOf ( target . object , target . column ) ;
96- if ( codeField === undefined ) continue ;
101+ const declared = recordCurrencyOf ( target . object , target . column ) ;
102+ if ( declared === undefined ) continue ;
103+ const { codeField, homeField } = declared ;
97104 grouped ??= ( query . dimensions ?? [ ] ) . map ( dimensionColumnOf ) ;
98105 if ( grouped . some ( ( d ) => d ?. object === target . object && d . column === codeField ) ) continue ;
99106
100- const codePath = target . path === target . column
101- ? codeField
102- : `${ target . path . slice ( 0 , target . path . lastIndexOf ( '.' ) + 1 ) } ${ codeField } ` ;
107+ const prefix = target . path === target . column ? '' : target . path . slice ( 0 , target . path . lastIndexOf ( '.' ) + 1 ) ;
108+ const codePath = `${ prefix } ${ codeField } ` ;
109+ const homeRemedy = homeField !== undefined
110+ ? `point the measure at '${ prefix } ${ homeField } ', the home-currency amount object '${ target . object } ' `
111+ + `declares for '${ target . column } ' (\`homeAmount.field\`), for one total in the home currency`
112+ : `for one total in the home currency, declare \`currencyConfig.homeAmount\` on '${ target . column } ' `
113+ + `of object '${ target . object } ' and point the measure at its home field` ;
103114 const err = invalidMemberError (
104115 `Measure '${ member } ' on cube '${ cubeName } ' takes the ${ measure . type } of field '${ target . path } ', `
105116 + `a record-currency amount whose currency is each record's '${ codeField } ', but the query does not `
106- + `group by '${ codePath } ', so the query was NOT run. Add the dimension over ' ${ codePath } ' to the `
107- + `query, which answers one row per currency. Without the grouping the ${ measure . type } would `
108- + 'combine amounts in different currencies into one number.' ,
117+ + `group by '${ codePath } ', so the query was NOT run. Without the grouping the ${ measure . type } would `
118+ + `combine amounts in different currencies into one number. Either ${ homeRemedy } ; or add the `
119+ + `dimension over ' ${ codePath } ' to the query, which answers one row per currency.` ,
109120 { member, param : 'measures' , cube : cubeName } ,
110121 ) as Error & { field ?: string ; object ?: string } ;
111122 err . field = target . path ;
0 commit comments