Skip to content

Commit 8374620

Browse files
committed
fix(cli): recompute-home-amounts review round 2 — leave the shared OBJECT_NOT_FOUND envelope as it was
Round 1 set httpStatus on refuseUndeclaredObjects' error, which changed the --json envelope of every os migrate --object command and broke the key-set pin in object-scope.integration.test.ts (#21644). The util and its unit test are back to their earlier form; the recompute command's test asserts what the envelope carries (code, exit 1). The changeset drops the line that announced the wider envelope, and the stale 'loaded lazily' comment on the type-only import is corrected. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CXZRTkUQ6UjyMWfN2U
1 parent 35b01a0 commit 8374620

5 files changed

Lines changed: 8 additions & 10 deletions

File tree

‎.changeset/27193-recompute-home-amounts.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ Metadata-protocol: none
1111
- **`@objectstack/objectql`: `recomputeHomeAmounts(engine, logger, { object, field?, since?, apply?, maxRecords?, maxListed?, source? })`**, with `assertHomeAmountRecomputeSince` (the pure `since` check a door can run before it opens a database), `formatHomeAmountRecomputeReport`, `homeAmountRecomputeComplete` and `HOME_AMOUNT_SKIP_REASONS`. It walks every stored row of an object whose `record`-mode amounts declare `currencyConfig.homeAmount` and projects each through the write-time projection itself (the same rate row, division, single rounding, same-currency rule and organization-timezone day), in the row's own organization, so each organization converts into its own home currency at its own rates and is counted apart. A dry run (the default) reports what would change and writes nothing; `apply` writes only the rows that differ, through `engine.update` with the home field in the payload, so the value stored is the one the write itself computes, and only the home and rate fields move (`updated_at` is kept, so a consumer syncing by `updated_at` does not see the recompute). Being a write, it moves what reads the home field as a fresh write would: stored formulas, parent summaries, and the per-row audit rows where audit capture is installed. Rows that cannot be converted are listed by reason and never written: `rate_missing` (with the currency and date), `code_blank` (an amount with no currency code; the remedy is `os migrate record-currency-codes`), `document_date_blank`, `home_currency_unset` and `unconvertible`. `since` filters on the document date (`homeAmount.rateDate`). One run-level `sys_audit_log` entry records each applied run (counts per organization and reason, and the skipped rows), also when an unclassified error stops the run partway (marked `aborted`, then the error is thrown on). A walk cut short by `maxRecords` is `truncated` and not complete. Refused before any row is read: an unknown object (`OBJECT_NOT_FOUND`), an object or `field` declaring no home amount (`INVALID_FIELD`), a `since` that is not `YYYY-MM-DD` (`VALIDATION_FAILED`).
1212
- **`@objectstack/objectql`: `ObjectQL.homeAmountReadsFor(context?)`**, the reads the write-time projection makes (its serving, and the organization a write converts a row under), for a caller that projects a stored row as a write would.
1313
- **`@objectstack/cli`: `os migrate recompute-home-amounts --object <o> [--field <amount>] [--since YYYY-MM-DD] [--apply] [--yes] [--json] [--force] [--max-records] [--database-url]`**, the shell of `os migrate record-currency-codes`, booting the settings service (each organization's `localization.currency` and `.timezone`) and the audit ledger. It exits 1 while any row is listed and skipped, on a dry run as on an apply, because a total over the home field silently leaves such a row out, and when `--max-records` cut the walk short.
14-
- **`@objectstack/cli`: an unknown `--object` on any `os migrate` command now carries `httpStatus: 404` in its `--json` envelope** beside `code: OBJECT_NOT_FOUND` (`refuseUndeclaredObjects` sets ADR-0112 D5's spelling of the status it already carried as `status`).

‎packages/cli/src/commands/migrate/recompute-home-amounts.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ describe('os migrate recompute-home-amounts', () => {
156156
const err = await MigrateRecomputeHomeAmounts.run(['--json', '--object', 'nope'], { root: CLI_ROOT }).catch((e: unknown) => e);
157157
expect(exitOf(err)).toBe(1);
158158
expect(vi.mocked(recomputeHomeAmounts)).not.toHaveBeenCalled();
159-
expect(json()).toMatchObject({ code: 'OBJECT_NOT_FOUND', httpStatus: 404 });
159+
// The shared refusal's envelope carries the code alone (its key set is pinned in
160+
// object-scope.integration.test.ts); the exit code is asserted above.
161+
expect(json()).toMatchObject({ code: 'OBJECT_NOT_FOUND' });
160162
}, RUN_TIMEOUT);
161163

162164
it('a listed and skipped row keeps the exit code at 1, on a dry run and on an apply run (Open item 8)', async () => {

‎packages/cli/src/commands/migrate/recompute-home-amounts.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import { describeOccupancy, JOURNAL_MODE_SWITCH_NOTE } from '../../utils/sqlite-
2121
import { describeDriverSqliteFile } from '../../utils/connection-display.js';
2222
import { buildDataMigrationPlugins } from '../../utils/data-migration-plugins.js';
2323
import { refuseUndeclaredObjects } from '../../utils/migrate-object-scope.js';
24-
// Type-only, so the heavy engine package is still loaded lazily below.
24+
// Type-only: the engine package is imported dynamically inside run(), first for the `--since`
25+
// check (before the probe, the prompt and the boot), never at module load, so `os --help` and
26+
// every other command pay nothing for it.
2527
import type { HomeAmountRecomputeEngine } from '@objectstack/objectql';
2628

2729
async function confirm(question: string): Promise<boolean> {

‎packages/cli/src/utils/migrate-object-scope.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ describe('[#21644] refuseUndeclaredObjects: an unknown --object is refused, neve
2020
expect(error).not.toBeNull();
2121
expect(error?.code).toBe('OBJECT_NOT_FOUND');
2222
expect(error?.status).toBe(404);
23-
// …and under ADR-0112 D5's spelling, the field a `--json` envelope carries.
24-
expect((error as { httpStatus?: number } | null)?.httpStatus).toBe(404);
2523
expect(error?.object).toBe('os_sitee');
2624
expect(error?.message).toContain("'os_sitee'");
2725
// Every declared object, sorted, so the operator can correct the spelling.

‎packages/cli/src/utils/migrate-object-scope.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function narrowedFlagNote(command: string, objects: readonly string[], ap
6767
* Refuse every `--object` name the booted registry does not declare, before
6868
* anything is scanned or written.
6969
*
70-
* Throws the `OBJECT_NOT_FOUND` envelope (`code`, `status` and `httpStatus` 404, `object`
70+
* Throws the `OBJECT_NOT_FOUND` envelope (`code`, `status: 404`, `object`
7171
* naming the first unknown name). Its message names every unknown name and the
7272
* declared objects, sorted.
7373
*/
@@ -80,10 +80,7 @@ export function refuseUndeclaredObjects(
8080
const unknown = [...new Set(requested.filter((name) => !known.has(name)))];
8181
if (unknown.length === 0) return;
8282

83-
const error = objectNotFoundError(unknown[0]) as Error & { httpStatus?: number };
84-
// ADR-0112 D5's spelling of the same 404, the one a command's `--json` envelope reads
85-
// (`errorCodeFields`): without it the envelope carried the code and dropped the status.
86-
error.httpStatus = 404;
83+
const error = objectNotFoundError(unknown[0]);
8784
const names = unknown.map((name) => `'${name}'`).join(', ');
8885
const declaredList = [...known].sort();
8986
error.message =

0 commit comments

Comments
 (0)