278e57354c
- Monthly spend chart with category breakdown drill-down - Merchant frequency and spend analytics with per-merchant history - Subscription detection and recurring charge tracking - Fee and interest analytics endpoint - Expanded category list with formatCategory display helper
56 lines
2.1 KiB
TypeScript
56 lines
2.1 KiB
TypeScript
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 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)
|
|
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', 'interest', 'refund', 'credit')
|
|
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 });
|
|
}
|