feat(insights): committed/discretionary chart, recurring charge detection, fees & interest audit

This commit is contained in:
2026-03-09 23:04:52 +11:00
parent 7379437cc3
commit c1d031511a
5 changed files with 535 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
// Statement-level fees and interest (aggregated by Gemini from the PDF)
const stmtRows = await queryRaw<{
bank_name: string;
fees: string;
interest: string;
}>(
`SELECT
bank_name,
SUM(COALESCE(fees_charged, 0))::numeric(12,2) AS fees,
SUM(COALESCE(interest_charged, 0))::numeric(12,2) AS interest
FROM statements
WHERE owner_id = $1
GROUP BY bank_name
HAVING SUM(COALESCE(fees_charged, 0)) + SUM(COALESCE(interest_charged, 0)) > 0
ORDER BY (SUM(COALESCE(fees_charged, 0)) + SUM(COALESCE(interest_charged, 0))) DESC`,
[user.id]
);
// Transaction-level fee and interest line items (split-adjusted)
const txnRows = await queryRaw<{
id: number;
transaction_date: string;
description: string;
merchant_name: string | null;
transaction_type: string;
my_amount: string;
bank_name: string;
}>(
`SELECT
t.id,
t.transaction_date,
t.description,
t.merchant_name,
t.transaction_type,
CASE
WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100
ELSE COALESCE(t.amount_aud, t.amount)
END::numeric(12,2) AS my_amount,
s.bank_name
FROM transactions t
LEFT JOIN transaction_splits ts ON ts.transaction_id = t.id AND ts.participant_id = $1
JOIN statements s ON s.id = t.statement_id
WHERE s.owner_id = $1
AND t.transaction_type IN ('fee', 'interest')
ORDER BY t.transaction_date DESC`,
[user.id]
);
const by_bank = stmtRows.map((r) => ({
bank_name: r.bank_name,
fees: Number(r.fees),
interest: Number(r.interest),
total: Number(r.fees) + Number(r.interest),
}));
const transactions = txnRows.map((r) => ({
...r,
my_amount: Number(r.my_amount),
}));
// Totals from statement-level data (more complete — Gemini reads the statement summary)
const total_fees = by_bank.reduce((s, r) => s + r.fees, 0);
const total_interest = by_bank.reduce((s, r) => s + r.interest, 0);
return NextResponse.json({ by_bank, transactions, total_fees, total_interest });
}
@@ -0,0 +1,133 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const rows = await queryRaw<{
merchant: string;
category: string;
occurrences: number;
avg_amount: string;
first_seen: string;
last_seen: string;
total_paid: string;
median_interval: string;
frequency: string | null;
}>(
`WITH merchant_txns AS (
SELECT
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) AS merchant,
COALESCE(o.category_override, t.category) AS category,
t.transaction_date,
CASE
WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100
ELSE COALESCE(t.amount_aud, t.amount)
END AS my_amount
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
LEFT JOIN transaction_splits ts ON ts.transaction_id = t.id AND ts.participant_id = $1
JOIN statements s ON s.id = t.statement_id
WHERE s.owner_id = $1
AND t.transaction_type IN ('debit', 'fee')
AND COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) IS NOT NULL
),
merchant_with_lag AS (
SELECT
merchant,
category,
transaction_date,
my_amount,
LAG(transaction_date) OVER (PARTITION BY merchant ORDER BY transaction_date) AS prev_date
FROM merchant_txns
),
merchant_stats AS (
SELECT
merchant,
MODE() WITHIN GROUP (ORDER BY category) AS category,
COUNT(*) + 1 AS occurrences,
AVG(my_amount)::numeric(12,2) AS avg_amount,
MIN(transaction_date) AS first_seen,
MAX(transaction_date) AS last_seen,
SUM(my_amount)::numeric(12,2) AS total_paid,
PERCENTILE_CONT(0.5) WITHIN GROUP (
ORDER BY (transaction_date - prev_date)::int
) AS median_interval,
STDDEV((transaction_date - prev_date)::int) AS stddev_interval
FROM merchant_with_lag
WHERE prev_date IS NOT NULL
GROUP BY merchant
HAVING COUNT(*) >= 2
),
classified AS (
SELECT *,
CASE
WHEN median_interval BETWEEN 6 AND 8 THEN 'weekly'
WHEN median_interval BETWEEN 13 AND 16 THEN 'fortnightly'
WHEN median_interval BETWEEN 27 AND 35 THEN 'monthly'
WHEN median_interval BETWEEN 85 AND 95 THEN 'quarterly'
WHEN median_interval BETWEEN 350 AND 380 THEN 'annual'
ELSE NULL
END AS frequency
FROM merchant_stats
WHERE stddev_interval < median_interval * 0.4
AND (
median_interval BETWEEN 6 AND 8 OR
median_interval BETWEEN 13 AND 16 OR
median_interval BETWEEN 27 AND 35 OR
median_interval BETWEEN 85 AND 95 OR
median_interval BETWEEN 350 AND 380
)
)
SELECT merchant, category, occurrences, avg_amount, first_seen, last_seen, total_paid,
median_interval::numeric(8,1), frequency
FROM classified
WHERE frequency IS NOT NULL
ORDER BY
CASE frequency
WHEN 'weekly' THEN avg_amount * 4.33
WHEN 'fortnightly' THEN avg_amount * 2.17
WHEN 'monthly' THEN avg_amount
WHEN 'quarterly' THEN avg_amount / 3
WHEN 'annual' THEN avg_amount / 12
END DESC NULLS LAST`,
[user.id]
);
const today = new Date();
const subscriptions = rows.map((r) => {
const lastSeen = new Date(r.last_seen);
const daysSinceLast = Math.floor((today.getTime() - lastSeen.getTime()) / 86400000);
const medianInterval = Number(r.median_interval);
const is_active = daysSinceLast < medianInterval * 1.5;
const avg = Number(r.avg_amount);
const monthly_equiv =
r.frequency === "weekly" ? avg * 4.33 :
r.frequency === "fortnightly" ? avg * 2.17 :
r.frequency === "quarterly" ? avg / 3 :
r.frequency === "annual" ? avg / 12 :
avg;
return {
merchant: r.merchant,
category: r.category,
frequency: r.frequency,
avg_amount: avg,
monthly_equiv: Math.round(monthly_equiv * 100) / 100,
first_seen: r.first_seen,
last_seen: r.last_seen,
occurrences: r.occurrences,
total_paid: Number(r.total_paid),
is_active,
};
});
const total_monthly_equiv = subscriptions
.filter((s) => s.is_active)
.reduce((sum, s) => sum + s.monthly_equiv, 0);
return NextResponse.json({ subscriptions, total_monthly_equiv: Math.round(total_monthly_equiv * 100) / 100 });
}