feat(rules): manual-only rules as one-click quick actions on selected transactions
ci / lint-test (push) Successful in 52s

A rule flagged manual_only never runs in the apply-all pass; instead it shows
as a button in the transactions bulk bar and applies its actions to the current
selection (conditions ignored — the selection is the condition). Recorded as a
rule_apply_run, so it reverts from Rules -> Apply History like any other run.

Motivation: tagging Home + splitting 50/50 with Sonu was two bulk actions every
time. Now it is one click, and any other combo can be defined the same way.

Extracts the action-application and snapshot logic from the apply route into
src/lib/rule-actions.ts so both callers share one implementation — splits upsert
rather than delete+reinsert, so settled flags survive.
This commit is contained in:
2026-07-25 23:02:26 +10:00
parent 856e1a51ab
commit e0b0fc91e0
10 changed files with 324 additions and 118 deletions
+13
View File
@@ -166,6 +166,7 @@ export function useBulkAction() {
merchant_normalized?: string;
splits?: { participant_id: number; share_percent: number }[];
tag_id?: number;
rule_id?: number;
}) => {
const res = await fetch("/api/transactions/bulk", {
method: "POST",
@@ -188,6 +189,16 @@ export function useBulkAction() {
if (variables.action === "tag" || variables.action === "untag") {
qc.invalidateQueries({ queryKey: ["tags"] });
}
// A quick action can touch category, tags and splits at once, and records
// a revertible run — refresh everything it could have changed.
if (variables.action === "apply_rule") {
qc.invalidateQueries({ queryKey: ["tags"] });
qc.invalidateQueries({ queryKey: ["splits"] });
qc.invalidateQueries({ queryKey: ["shared-transactions"] });
qc.invalidateQueries({ queryKey: ["participant-balances"] });
qc.invalidateQueries({ queryKey: ["analytics"] });
qc.invalidateQueries({ queryKey: ["rule-runs"] });
}
},
});
}
@@ -459,6 +470,8 @@ export interface RuleRow {
conditions: { field: string; operator: string; value: string }[];
actions: { set_category?: string; add_tag_ids?: number[]; set_merchant?: string; apply_split?: { participant_id: number; share_percent: number }[] };
enabled: boolean;
/** Excluded from the apply-all run; fired by hand as a quick action instead. */
manual_only?: boolean;
priority: number;
created_at: string;
}
+130
View File
@@ -0,0 +1,130 @@
/**
* Applying a rule's `actions` to transactions, and snapshotting the before-state
* so a run can be reverted.
*
* Shared by the two callers that write rule actions:
* - `POST /api/rules/apply` — condition-matched bulk run
* - `POST /api/transactions/bulk` with `action: "apply_rule"` — quick action
* fired against a hand-picked selection (conditions ignored)
*/
import { queryRaw } from "@/lib/db";
import type { Actions } from "@/lib/rules";
export interface SnapshotEntry {
transaction_id: number;
had_override: boolean;
prev_category_override: string | null;
prev_merchant_normalized: string | null;
prev_tag_ids: number[];
prev_splits: { participant_id: number; share_percent: number; settled: boolean }[];
}
/**
* Apply `actions` to one transaction.
*
* Tags are additive (ON CONFLICT DO NOTHING) — re-running never duplicates and
* never removes tags the rule doesn't mention. Splits replace the participant
* set but upsert shares, so `settled` flags survive a re-run; a plain
* delete+reinsert would silently un-settle reconciled splits.
*/
export async function applyRuleActions(
transactionId: number,
actions: Actions,
opts: { skipSplit?: boolean } = {}
): Promise<void> {
if (actions.set_category || actions.set_merchant) {
await queryRaw(
`INSERT INTO transaction_overrides (transaction_id, category_override, merchant_normalized)
VALUES ($1, $2, $3)
ON CONFLICT (transaction_id) DO UPDATE SET
category_override = COALESCE($2, transaction_overrides.category_override),
merchant_normalized = COALESCE($3, transaction_overrides.merchant_normalized),
updated_at = NOW()`,
[transactionId, actions.set_category || null, actions.set_merchant || null]
);
}
if (actions.add_tag_ids?.length) {
for (const tagId of actions.add_tag_ids) {
await queryRaw(
`INSERT INTO transaction_tags (transaction_id, tag_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
[transactionId, tagId]
);
}
}
if (actions.apply_split?.length && !opts.skipSplit) {
await queryRaw(
`DELETE FROM transaction_splits WHERE transaction_id = $1 AND participant_id != ALL($2::int[])`,
[transactionId, actions.apply_split.map((s) => s.participant_id)]
);
for (const s of actions.apply_split) {
await queryRaw(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent)
VALUES ($1, $2, $3)
ON CONFLICT (transaction_id, participant_id)
DO UPDATE SET share_percent = EXCLUDED.share_percent`,
[transactionId, s.participant_id, s.share_percent]
);
}
}
}
/** Capture the overrides / tags / splits of `ids` before a run mutates them. */
export async function captureSnapshot(ids: number[]): Promise<SnapshotEntry[]> {
if (ids.length === 0) return [];
const overrides = await queryRaw<{
transaction_id: number;
category_override: string | null;
merchant_normalized: string | null;
}>(
`SELECT transaction_id, category_override, merchant_normalized
FROM transaction_overrides WHERE transaction_id = ANY($1::int[])`,
[ids]
);
const overrideMap = new Map(overrides.map((o) => [o.transaction_id, o]));
const tagRows = await queryRaw<{ transaction_id: number; tag_id: number }>(
`SELECT transaction_id, tag_id FROM transaction_tags WHERE transaction_id = ANY($1::int[])`,
[ids]
);
const tagMap = new Map<number, number[]>();
for (const row of tagRows) {
if (!tagMap.has(row.transaction_id)) tagMap.set(row.transaction_id, []);
tagMap.get(row.transaction_id)!.push(row.tag_id);
}
const splitRows = await queryRaw<{
transaction_id: number;
participant_id: number;
share_percent: number;
settled: boolean;
}>(
`SELECT transaction_id, participant_id, share_percent, settled
FROM transaction_splits WHERE transaction_id = ANY($1::int[])`,
[ids]
);
const splitMap = new Map<number, { participant_id: number; share_percent: number; settled: boolean }[]>();
for (const row of splitRows) {
if (!splitMap.has(row.transaction_id)) splitMap.set(row.transaction_id, []);
splitMap.get(row.transaction_id)!.push({
participant_id: row.participant_id,
share_percent: row.share_percent,
settled: row.settled,
});
}
return ids.map((id) => {
const ov = overrideMap.get(id);
return {
transaction_id: id,
had_override: !!ov,
prev_category_override: ov?.category_override ?? null,
prev_merchant_normalized: ov?.merchant_normalized ?? null,
prev_tag_ids: tagMap.get(id) ?? [],
prev_splits: splitMap.get(id) ?? [],
};
});
}