feat: CSV import and batch reconciliation UI

- Add reconciled_with_id column to transactions (links manual → statement tx)
- CSV import wizard: 4-step modal (upload → map columns → review → done)
  - Handles any bank format via column mapping with localStorage presets
  - Single signed or separate debit/credit column modes
  - Editable preview table before committing
  - Auto-tags all imported rows with 'csv-import'
- Batch reconcile page: shows all unreconciled manual transactions with
  potential statement matches (date ±3 days, amount ±1%) pre-fetched
  - Select matches across multiple rows, apply all at once
  - Copies overrides/tags/splits from manual → statement tx atomically
  - Manual tx marked reconciled (linked), hidden from main transactions view
  - Transactions with no matches shown separately
- Import CSV button on transactions page
- Reconcile nav item in sidebar
This commit is contained in:
2026-04-13 06:23:08 +10:00
parent 07b8c1ef16
commit 4a49add277
11 changed files with 1263 additions and 7 deletions
+31
View File
@@ -0,0 +1,31 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { ensureTag, batchInsertCSVTransactions } from "@/lib/queries";
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json() as {
bank_name: string;
transactions: {
date: string;
description: string;
amount: number;
transaction_type: string;
merchant_name?: string;
foreign_currency_amount?: number;
foreign_currency_code?: string;
category?: string;
}[];
};
if (!Array.isArray(body.transactions) || body.transactions.length === 0) {
return NextResponse.json({ error: "No transactions provided" }, { status: 400 });
}
const tagId = await ensureTag("csv-import", "#8b5cf6");
const inserted = await batchInsertCSVTransactions(user.id, body.transactions, tagId);
return NextResponse.json({ inserted }, { status: 201 });
}
+11
View File
@@ -0,0 +1,11 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { getPendingReconciliations } from "@/lib/queries";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const data = await getPendingReconciliations(user.id);
return NextResponse.json(data);
}
@@ -0,0 +1,88 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma, queryRaw } from "@/lib/db";
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json() as {
matches: { manual_id: number; statement_tx_id: number }[];
};
if (!Array.isArray(body.matches) || body.matches.length === 0) {
return NextResponse.json({ error: "No matches provided" }, { status: 400 });
}
// Verify all manual_ids belong to this user
const manualIds = body.matches.map((m) => m.manual_id);
const owned = await queryRaw<{ id: number }>(
`SELECT id FROM transactions WHERE id = ANY($1::int[]) AND statement_id IS NULL AND owner_id = $2`,
[manualIds, user.id]
);
if (owned.length !== manualIds.length) {
return NextResponse.json({ error: "One or more transactions not found" }, { status: 404 });
}
let reconciled = 0;
for (const { manual_id, statement_tx_id } of body.matches) {
await prisma.$transaction(async (tx) => {
// Copy overrides: manual → statement tx
const override = await tx.transaction_overrides.findUnique({
where: { transaction_id: manual_id },
});
if (override) {
await tx.transaction_overrides.upsert({
where: { transaction_id: statement_tx_id },
update: {
category_override: override.category_override,
merchant_normalized: override.merchant_normalized,
notes: override.notes,
my_share_percent: override.my_share_percent,
updated_at: new Date(),
},
create: {
transaction_id: statement_tx_id,
category_override: override.category_override,
merchant_normalized: override.merchant_normalized,
notes: override.notes,
my_share_percent: override.my_share_percent,
},
});
}
// Copy tags: manual → statement tx
const tags = await tx.transaction_tags.findMany({ where: { transaction_id: manual_id } });
if (tags.length) {
await tx.transaction_tags.createMany({
data: tags.map((t) => ({ transaction_id: statement_tx_id, tag_id: t.tag_id })),
skipDuplicates: true,
});
}
// Copy splits: manual → statement tx
const splits = await tx.transaction_splits.findMany({ where: { transaction_id: manual_id } });
if (splits.length) {
await tx.transaction_splits.createMany({
data: splits.map((s) => ({
transaction_id: statement_tx_id,
participant_id: s.participant_id,
share_percent: s.share_percent,
})),
skipDuplicates: true,
});
}
// Mark manual tx as reconciled (link to statement tx)
await tx.$executeRawUnsafe(
`UPDATE transactions SET reconciled_with_id = $1 WHERE id = $2`,
statement_tx_id,
manual_id
);
});
reconciled++;
}
return NextResponse.json({ reconciled });
}