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:
@@ -1,7 +1,9 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { queryRaw } from "@/lib/db";
|
||||
import { OWNER_SCOPE, STATEMENTS_JOIN, EFFECTIVE_CATEGORY } from "@/lib/analytics-sql";
|
||||
import { OWNER_SCOPE, STATEMENTS_JOIN, EFFECTIVE_CATEGORY, mySplitOf } from "@/lib/analytics-sql";
|
||||
|
||||
const MY_AMOUNT = mySplitOf(`COALESCE(t.amount_aud, t.amount)`);
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
@@ -32,10 +34,8 @@ export async function GET(
|
||||
t.amount,
|
||||
t.amount_aud,
|
||||
CASE
|
||||
WHEN t.transaction_type IN ('refund', 'credit') THEN
|
||||
-(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
|
||||
ELSE
|
||||
(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
|
||||
WHEN t.transaction_type IN ('refund', 'credit') THEN -${MY_AMOUNT}
|
||||
ELSE ${MY_AMOUNT}
|
||||
END::numeric(10,2) as my_amount,
|
||||
t.transaction_type,
|
||||
${EFFECTIVE_CATEGORY} as category,
|
||||
|
||||
Reference in New Issue
Block a user