fix(analytics): my share is what is left over, not 100%
ci / lint-test (push) Successful in 41s

The split-adjusted spend expression assumed a transaction with no split row for
me was entirely mine. That is wrong when a transaction is allocated fully to
someone else: I paid, they owe all of it, and there is no row for me to match.
Both branches of the CASE missed and the ELSE charged me the full amount.

24 transactions were affected, all travel bookings between 2026-01-09 and
2026-06-26, overstating my spend by $8,579.07 across every analytics view.

Adds myShare/mySplitOf to analytics-sql.ts, which fall back to
100 - (sum of everyone else's shares) instead of 100, and applies them to all
five analytics routes. Centralised for the same reason as EXCLUDE_NON_SPEND:
the expression was duplicated five times and had already drifted.
This commit is contained in:
2026-07-26 10:05:09 +10:00
parent 02495e4173
commit ab00f8c592
6 changed files with 42 additions and 26 deletions
+2 -7
View File
@@ -8,6 +8,7 @@ import {
EXCLUDE_NON_SPEND,
SPEND_ROWS,
SPEND_BASE,
mySplitOf,
} from "@/lib/analytics-sql";
export async function GET(req: NextRequest) {
@@ -34,13 +35,7 @@ export async function GET(req: NextRequest) {
`SELECT
TO_CHAR(DATE_TRUNC('month', t.transaction_date::date), 'YYYY-MM') as month,
${EFFECTIVE_CATEGORY} as category,
SUM(
CASE
WHEN ts.share_percent IS NOT NULL THEN (${SPEND_BASE}) * ts.share_percent / 100
WHEN o.my_share_percent IS NOT NULL THEN (${SPEND_BASE}) * o.my_share_percent / 100
ELSE (${SPEND_BASE})
END
)::numeric(12,2) as total_spent,
SUM(${mySplitOf(SPEND_BASE)})::numeric(12,2) as total_spent,
COUNT(*)::int as transaction_count
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id