Skip to content

Commit d53098d

Browse files
committed
wip(service-analytics): the record-currency door names the home field remedy
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CbmDUh2Sx7fvaGgu7r
1 parent f68b47c commit d53098d

3 files changed

Lines changed: 54 additions & 17 deletions

File tree

‎packages/services/service-analytics/src/analytics-service.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,10 @@ export interface AnalyticsServiceConfig {
12151215
* Such an amount has one currency per ROW, so its column carries the
12161216
* `currencyField` pointer instead of a `currency`, and the cube door
12171217
* refuses an aggregate of it that does not group by that field.
1218+
* [ADR-0144 D6, #27195] `homeAmountField` is that amount's stored
1219+
* home-currency field: answer `currencyConfig.homeAmount.field` ONLY
1220+
* beside a `currencyField`, and `undefined` otherwise. One reader: the
1221+
* cube door's refusal names it as the consolidated-total remedy.
12181222
* - Percent scale (objectui#3136): a measure over a `percent` field inherits
12191223
* that field's storage scale via `percentScaleOf`, so a renderer scales by
12201224
* declared metadata instead of guessing from the value.
@@ -1244,7 +1248,14 @@ export interface AnalyticsServiceConfig {
12441248
sourceFieldMeta?: (
12451249
object: string,
12461250
field: string,
1247-
) => { type?: string; multiple?: boolean; defaultCurrency?: string; currencyField?: string; max?: number } | undefined;
1251+
) => {
1252+
type?: string;
1253+
multiple?: boolean;
1254+
defaultCurrency?: string;
1255+
currencyField?: string;
1256+
homeAmountField?: string;
1257+
max?: number;
1258+
} | undefined;
12481259
/**
12491260
* [#15684] The SQL dialect of the datasource backing `object` — `'sqlite'`,
12501261
* `'postgres'`, `'mysql'`, or `undefined` when the host cannot answer.
@@ -3936,7 +3947,11 @@ export class AnalyticsService implements IAnalyticsService {
39363947
path: sql,
39373948
};
39383949
},
3939-
(o, field) => fieldMeta(o, field)?.currencyField,
3950+
(o, field) => {
3951+
const meta = fieldMeta(o, field);
3952+
if (meta?.currencyField === undefined) return undefined;
3953+
return { codeField: meta.currencyField, homeField: meta.homeAmountField };
3954+
},
39403955
);
39413956
}
39423957

‎packages/services/service-analytics/src/cube-record-currency-door.ts‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@
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

6165
import 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
*/
8187
export 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;

‎packages/services/service-analytics/src/plugin.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,12 @@ export class AnalyticsServicePlugin implements Plugin {
14401440
type?: string;
14411441
multiple?: boolean;
14421442
max?: number;
1443-
currencyConfig?: { currencyMode?: string; defaultCurrency?: string; currencyField?: string };
1443+
currencyConfig?: {
1444+
currencyMode?: string;
1445+
defaultCurrency?: string;
1446+
currencyField?: string;
1447+
homeAmount?: { field?: string };
1448+
};
14441449
}
14451450
| undefined;
14461451
if (!f) return undefined;
@@ -1453,12 +1458,18 @@ export class AnalyticsServicePlugin implements Plugin {
14531458
const recordCurrencyField = f.currencyConfig?.currencyMode === 'record'
14541459
? f.currencyConfig.currencyField
14551460
: undefined;
1461+
// [ADR-0144 D6, #27195] …and the home field beside it, which the cube
1462+
// door's refusal names as the consolidated-total remedy.
1463+
const homeAmountField = recordCurrencyField !== undefined
1464+
? f.currencyConfig?.homeAmount?.field
1465+
: undefined;
14561466
return {
14571467
type: f.type,
14581468
multiple: f.multiple === true,
14591469
max: f.max,
14601470
defaultCurrency: fixedCurrency,
14611471
currencyField: recordCurrencyField,
1472+
homeAmountField,
14621473
};
14631474
},
14641475
// #5033 — the datasource an object is bound to, used ONLY to name the

0 commit comments

Comments
 (0)