Recategorizing a split transaction moved the parent only: its offset line, which cancels in the parent's category the part sent to other categories, stayed in the former category. Every report adds all rows, so a transaction of -120 with -50 split to B, moved from A to C, left +50 in A and -120 in C instead of -70 in C. The keyword path had the same defect, and its preview also offered the children of a split, which it then recategorized alone. - src/utils/splitOffset.ts: isOffsetChild(parent, child), the rule the split window already used, extracted and freed from the category: the parent's description, the sign opposite to the parent's, a non-zero amount (decision D1: no role column, no migration). - src/services/splitOffsetService.ts: propagateCategoryToOffsets(db, parentId, categoryId) gives the parent's new category to the offset line, whatever its current category (decision D2: an offset left in a former category is repaired at the parent's next change). It runs on the caller's transaction handle: no BEGIN/COMMIT, no getDb(), no notification. Nothing for an unknown id, a transaction that is not split, or a child (children carry is_split = 1 too). Its own module: transactionService.ts already imports categorizationService.ts. - updateTransactionCategory now runs in withTransaction, with its own BEGIN, the parent's UPDATE (unchanged, toSqlFlag), the propagation and COMMIT, or ROLLBACK and rethrow; it notifies after the COMMIT only. - previewKeywordMatches offers rows without a parent only; the keyword loop's UPDATE skips a child (AND parent_transaction_id IS NULL), counts rowsAffected, and propagates for each id before the COMMIT. - SplitAdjustmentModal leaves out the child isOffsetChild recognizes, so an offset left in a former category is no longer shown as a row. Tests: the regression test of transactionService.test.ts fails on main (no UPDATE of the offset); the tests of #390 and #396 now find the parent's UPDATE by its SQL and answer the reads of the propagation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
2.8 KiB
TypeScript
80 lines
2.8 KiB
TypeScript
// splitOffsetService — a split's offset line follows its parent (#454).
|
|
//
|
|
// Shared by the two paths that recategorize a transaction: the category chosen
|
|
// in the Transactions list (`updateTransactionCategory`, transactionService.ts)
|
|
// and a keyword applied with reassignment (`applyKeywordWithReassignment`,
|
|
// categorizationService.ts). It has a module of its own because
|
|
// transactionService.ts already imports categorizationService.ts: exported
|
|
// from either one, it would make an import cycle. It imports neither.
|
|
|
|
import type Database from "@tauri-apps/plugin-sql";
|
|
import { isOffsetChild } from "../utils/splitOffset";
|
|
import { toSqlFlag } from "../utils/sqlFlag";
|
|
|
|
interface ParentRow {
|
|
id: number;
|
|
description: string;
|
|
amount: number;
|
|
is_split: unknown;
|
|
parent_transaction_id: number | null;
|
|
}
|
|
|
|
interface ChildRow {
|
|
id: number;
|
|
description: string;
|
|
amount: number;
|
|
}
|
|
|
|
/**
|
|
* Moves the offset line of the split parent `parentId` to `categoryId`, the
|
|
* category the caller has just given the parent, and returns the number of
|
|
* rows moved (one for a split written by the split window).
|
|
*
|
|
* Runs on `db`, the handle of the caller's transaction, between the caller's
|
|
* BEGIN and COMMIT: it never issues BEGIN, COMMIT or ROLLBACK (it also runs
|
|
* inside the BEGIN of the keyword path), never calls `getDb()` (the DB lock is
|
|
* not reentrant: inside a `withTransaction` callback, a deadlock) and never
|
|
* notifies (the caller does, after its COMMIT).
|
|
*
|
|
* Writes nothing when `parentId` is not a split parent: an unknown id, a
|
|
* transaction that is not split, or a child of a split (children carry
|
|
* `is_split = 1` too, so a parent is the row without `parent_transaction_id`).
|
|
* An offset line follows whatever its current category, so one left in a
|
|
* former category by a change made before #454 is repaired at the parent's
|
|
* next change (decision D2 of #454). Its `is_manually_categorized` flag stays
|
|
* as it is: nothing reads it on a child.
|
|
*/
|
|
export async function propagateCategoryToOffsets(
|
|
db: Database,
|
|
parentId: number,
|
|
categoryId: number | null,
|
|
): Promise<number> {
|
|
const [parent] = await db.select<ParentRow[]>(
|
|
`SELECT id, description, amount, is_split, parent_transaction_id
|
|
FROM transactions
|
|
WHERE id = $1`,
|
|
[parentId],
|
|
);
|
|
if (!parent || toSqlFlag(parent.is_split) === 0 || parent.parent_transaction_id != null) {
|
|
return 0;
|
|
}
|
|
|
|
const children = await db.select<ChildRow[]>(
|
|
`SELECT id, description, amount
|
|
FROM transactions
|
|
WHERE parent_transaction_id = $1
|
|
ORDER BY id`,
|
|
[parentId],
|
|
);
|
|
|
|
let moved = 0;
|
|
for (const child of children) {
|
|
if (!isOffsetChild(parent, child)) continue;
|
|
await db.execute(
|
|
`UPDATE transactions SET category_id = $1, updated_at = CURRENT_TIMESTAMP WHERE id = $2`,
|
|
[categoryId, child.id],
|
|
);
|
|
moved++;
|
|
}
|
|
return moved;
|
|
}
|