feat(merchants): scatter plot, merchant profiles, and per-merchant transaction history

- /merchants page with spend-vs-frequency scatter chart (click to open profile)
- Merchant profile drawer: stats, monthly trend line, full transaction history
- /api/analytics/merchants: split-adjusted merchant aggregates + monthly trends
- /api/analytics/merchants/[merchant]: per-merchant transaction list
- Add Merchants nav item to sidebar
This commit is contained in:
2026-03-10 00:05:48 +11:00
parent 2a10450c3e
commit 714c5a9b25
5 changed files with 647 additions and 0 deletions
@@ -0,0 +1,53 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ merchant: string }> }
) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const { merchant } = await params;
const decoded = decodeURIComponent(merchant);
const transactions = await queryRaw<{
id: number;
transaction_date: string;
description: string;
amount: number;
amount_aud: number | null;
my_amount: number;
transaction_type: string;
category: string;
bank_name: string;
statement_id: number;
}>(`
SELECT
t.id,
t.transaction_date::text,
t.description,
t.amount,
t.amount_aud,
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(10,2) as my_amount,
t.transaction_type,
COALESCE(o.category_override, t.category) as category,
s.bank_name,
t.statement_id
FROM transactions t
JOIN statements s ON s.id = t.statement_id
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
WHERE s.owner_id = $1
AND t.transaction_type IN ('debit', 'fee')
AND COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name, t.description) = $2
ORDER BY t.transaction_date DESC
LIMIT 500
`, [user.id, decoded]);
return NextResponse.json({ transactions });
}
+112
View File
@@ -0,0 +1,112 @@
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: 401 });
const { searchParams } = new URL(req.url);
const months = Math.min(24, Math.max(1, Number(searchParams.get("months") || "12")));
const cutoff = new Date();
cutoff.setMonth(cutoff.getMonth() - months);
const fromDate = cutoff.toISOString().slice(0, 10);
// Merchant aggregates — split-adjusted, debit transactions only
const rows = await queryRaw<{
merchant: string;
category: string;
transaction_count: number;
total_spend: number;
avg_transaction: number;
first_seen: string;
last_seen: string;
months_active: number;
}>(`
SELECT
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name, t.description) as merchant,
MODE() WITHIN GROUP (ORDER BY COALESCE(o.category_override, t.category)) as category,
COUNT(*)::int as transaction_count,
SUM(
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 total_spend,
AVG(
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(10,2) as avg_transaction,
MIN(t.transaction_date)::text as first_seen,
MAX(t.transaction_date)::text as last_seen,
COUNT(DISTINCT TO_CHAR(DATE_TRUNC('month', t.transaction_date::date), 'YYYY-MM'))::int as months_active
FROM transactions t
JOIN statements s ON s.id = t.statement_id
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
WHERE s.owner_id = $1
AND t.transaction_type IN ('debit', 'fee')
AND t.transaction_date >= $2
AND COALESCE(o.category_override, t.category) NOT IN ('transfers', 'investment')
GROUP BY 1
HAVING SUM(
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
) > 0
ORDER BY total_spend DESC
LIMIT 200
`, [user.id, fromDate]);
// Monthly trend per merchant (top 50 by spend)
const topMerchants = rows.slice(0, 50).map((r) => r.merchant);
interface TrendRow { merchant: string; month: string; total: number }
let trendRows: TrendRow[] = [];
if (topMerchants.length > 0) {
trendRows = await queryRaw<TrendRow>(`
SELECT
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name, t.description) as merchant,
TO_CHAR(DATE_TRUNC('month', t.transaction_date::date), 'YYYY-MM') as month,
SUM(
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(10,2) as total
FROM transactions t
JOIN statements s ON s.id = t.statement_id
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
WHERE s.owner_id = $1
AND t.transaction_type IN ('debit', 'fee')
AND t.transaction_date >= $2
AND COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name, t.description) = ANY($3)
AND COALESCE(o.category_override, t.category) NOT IN ('transfers', 'investment')
GROUP BY 1, 2
ORDER BY 1, 2
`, [user.id, fromDate, topMerchants]);
}
// Group trend data by merchant
const trendByMerchant: Record<string, Record<string, number>> = {};
for (const tr of trendRows) {
if (!trendByMerchant[tr.merchant]) trendByMerchant[tr.merchant] = {};
trendByMerchant[tr.merchant][tr.month] = Number(tr.total);
}
const merchants = rows.map((r) => ({
...r,
transaction_count: Number(r.transaction_count),
total_spend: Number(r.total_spend),
avg_transaction: Number(r.avg_transaction),
months_active: Number(r.months_active),
monthly_trend: trendByMerchant[r.merchant] || {},
}));
return NextResponse.json({ merchants, months });
}