chore: commit previously untracked runtime files (splits, auth, participants, shared)

This commit is contained in:
2026-03-08 18:00:46 +11:00
parent 30a7857d13
commit 5dbeb0cb87
11 changed files with 450 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
return NextResponse.json(user);
}
@@ -0,0 +1,32 @@
import { NextRequest, NextResponse } from "next/server";
import { queryRaw } from "@/lib/db";
interface BalanceRow {
participant_id: number;
name: string;
total_owed: number;
transaction_count: number;
}
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const rows = await queryRaw<BalanceRow>(
`SELECT ts.participant_id, p.name,
SUM(t.amount * ts.share_percent / 100)::numeric(12,2) as total_owed,
COUNT(*)::int as transaction_count
FROM transaction_splits ts
JOIN transactions t ON t.id = ts.transaction_id
JOIN participants p ON p.id = ts.participant_id
WHERE ts.participant_id = $1 AND ts.settled = false
GROUP BY ts.participant_id, p.name`,
[Number(id)]
);
return NextResponse.json(
rows[0] ?? { participant_id: Number(id), total_owed: 0, transaction_count: 0 }
);
}
@@ -0,0 +1,11 @@
import { NextRequest, NextResponse } from "next/server";
import { getParticipantBalances } from "@/lib/queries";
import { getCurrentUser } from "@/lib/auth";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const balances = await getParticipantBalances(user.id);
return NextResponse.json(balances);
}
+21
View File
@@ -0,0 +1,21 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { queryRaw } from "@/lib/db";
export async function GET() {
const participants = await prisma.participants.findMany({
orderBy: { name: "asc" },
});
return NextResponse.json(participants);
}
export async function POST(req: NextRequest) {
const { name, email } = await req.json();
if (!name?.trim()) {
return NextResponse.json({ error: "name required" }, { status: 400 });
}
const participant = await prisma.participants.create({
data: { name: name.trim(), email: email?.trim() || null },
});
return NextResponse.json(participant, { status: 201 });
}
+11
View File
@@ -0,0 +1,11 @@
import { NextRequest, NextResponse } from "next/server";
import { getSharedTransactions } from "@/lib/queries";
import { getCurrentUser } from "@/lib/auth";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const transactions = await getSharedTransactions(user.id);
return NextResponse.json(transactions);
}
+30
View File
@@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
export async function POST(req: NextRequest) {
const body = await req.json();
const { participant_id, split_ids } = body as {
participant_id?: number;
split_ids?: number[];
};
const now = new Date();
if (participant_id) {
const result = await prisma.transaction_splits.updateMany({
where: { participant_id, settled: false },
data: { settled: true, settled_at: now },
});
return NextResponse.json({ settled: result.count });
}
if (split_ids?.length) {
const result = await prisma.transaction_splits.updateMany({
where: { id: { in: split_ids }, settled: false },
data: { settled: true, settled_at: now },
});
return NextResponse.json({ settled: result.count });
}
return NextResponse.json({ error: "participant_id or split_ids required" }, { status: 400 });
}
@@ -0,0 +1,79 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { queryRaw } from "@/lib/db";
interface SplitInput {
participant_id: number;
share_percent: number;
}
interface SplitRow {
id: number;
transaction_id: number;
participant_id: number;
name: string;
share_percent: number;
settled: boolean;
settled_at: string | null;
created_at: string;
}
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const splits = await queryRaw<SplitRow>(
`SELECT ts.*, p.name
FROM transaction_splits ts
JOIN participants p ON p.id = ts.participant_id
WHERE ts.transaction_id = $1
ORDER BY p.name`,
[Number(id)]
);
return NextResponse.json(splits);
}
export async function POST(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const transactionId = Number(id);
const { splits } = (await req.json()) as { splits: SplitInput[] };
if (!splits || !Array.isArray(splits) || splits.length === 0) {
return NextResponse.json({ error: "splits array required" }, { status: 400 });
}
const total = splits.reduce((sum, s) => sum + Number(s.share_percent), 0);
if (Math.abs(total - 100) > 0.01) {
return NextResponse.json(
{ error: `Shares must sum to 100%, got ${total}%` },
{ status: 400 }
);
}
// Replace all splits for this transaction atomically
await prisma.$transaction([
prisma.transaction_splits.deleteMany({ where: { transaction_id: transactionId } }),
...splits.map((s) =>
prisma.transaction_splits.create({
data: {
transaction_id: transactionId,
participant_id: s.participant_id,
share_percent: s.share_percent,
},
})
),
]);
const result = await queryRaw<SplitRow>(
`SELECT ts.*, p.name FROM transaction_splits ts
JOIN participants p ON p.id = ts.participant_id
WHERE ts.transaction_id = $1 ORDER BY p.name`,
[transactionId]
);
return NextResponse.json(result);
}
+195
View File
@@ -0,0 +1,195 @@
"use client";
import { useState, useEffect } from "react";
import { useParticipants, useSetSplits, useTransactionSplits, useBulkAction } from "@/lib/hooks";
interface Split {
participant_id: number;
share_percent: number;
}
interface Props {
transactionId?: number;
transactionIds?: number[];
amount?: number;
description: string;
onClose: () => void;
}
export function SplitModal({ transactionId, transactionIds, amount, description, onClose }: Props) {
const isBulk = !!transactionIds && transactionIds.length > 0;
const singleId = transactionId ?? 0;
const { data: participants } = useParticipants();
const { data: existingSplits } = useTransactionSplits(isBulk ? 0 : singleId);
const setSplits = useSetSplits();
const bulkAction = useBulkAction();
const [splits, setSplitsState] = useState<Split[]>([]);
const [error, setError] = useState("");
// Initialise: bulk always defaults to 100% Me; single loads existing splits
useEffect(() => {
if (!participants || participants.length === 0) return;
const me = participants.find((p) => p.name === "Me");
if (isBulk) {
if (me) setSplitsState([{ participant_id: me.id, share_percent: 100 }]);
} else if (existingSplits && existingSplits.length > 0) {
setSplitsState(
existingSplits.map((s: { participant_id: number; share_percent: number }) => ({
participant_id: s.participant_id,
share_percent: Number(s.share_percent),
}))
);
} else if (me) {
setSplitsState([{ participant_id: me.id, share_percent: 100 }]);
}
}, [existingSplits, participants, isBulk]);
const total = splits.reduce((sum, s) => sum + s.share_percent, 0);
const toggleParticipant = (id: number) => {
setSplitsState((prev) => {
const exists = prev.find((s) => s.participant_id === id);
if (exists) {
return prev.filter((s) => s.participant_id !== id);
}
// Add with equal split
const count = prev.length + 1;
const equal = Math.floor(100 / count);
const remainder = 100 - equal * count;
return [
...prev.map((s, i) => ({ ...s, share_percent: equal + (i === 0 ? remainder : 0) })),
{ participant_id: id, share_percent: equal },
];
});
};
const updateShare = (id: number, value: number) => {
setSplitsState((prev) =>
prev.map((s) => (s.participant_id === id ? { ...s, share_percent: value } : s))
);
};
const splitEvenly = () => {
if (splits.length === 0) return;
const each = Math.floor(100 / splits.length);
const remainder = 100 - each * splits.length;
setSplitsState((prev) =>
prev.map((s, i) => ({ ...s, share_percent: each + (i === 0 ? remainder : 0) }))
);
};
const isPending = isBulk ? bulkAction.isPending : setSplits.isPending;
const handleSave = async () => {
setError("");
if (Math.abs(total - 100) > 0.01) {
setError(`Shares must sum to 100% (currently ${total.toFixed(1)}%)`);
return;
}
try {
if (isBulk) {
await bulkAction.mutateAsync({ action: "split", ids: transactionIds!, splits });
} else {
await setSplits.mutateAsync({ transactionId: singleId, splits });
}
onClose();
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to save splits");
}
};
return (
<div className="fixed inset-0 bg-black/60 flex items-center justify-center z-50" onClick={onClose}>
<div
className="bg-zinc-900 border border-zinc-700 rounded-xl p-6 w-full max-w-md mx-4 shadow-2xl"
onClick={(e) => e.stopPropagation()}
>
<h3 className="text-lg font-semibold mb-1">
{isBulk ? `Split ${transactionIds!.length} Transactions` : "Split Transaction"}
</h3>
<p className="text-sm text-zinc-400 mb-4 truncate">{description}</p>
{!isBulk && amount !== undefined && (
<p className="text-2xl font-mono font-semibold mb-6">
${Number(amount).toFixed(2)}
</p>
)}
{/* Participant toggles */}
<div className="space-y-3 mb-4">
{participants?.map((p) => {
const split = splits.find((s) => s.participant_id === p.id);
const active = !!split;
return (
<div key={p.id} className="flex items-center gap-3">
<button
onClick={() => toggleParticipant(p.id)}
className={`w-8 h-8 rounded-full text-sm font-medium flex-shrink-0 transition-colors ${
active
? "bg-blue-600 text-white"
: "bg-zinc-800 text-zinc-500 hover:bg-zinc-700"
}`}
>
{p.name.charAt(0).toUpperCase()}
</button>
<span className="flex-1 text-sm">{p.name}</span>
{active && (
<div className="flex items-center gap-2">
<input
type="range"
min={1}
max={99}
value={split.share_percent}
onChange={(e) => updateShare(p.id, Number(e.target.value))}
className="w-24 accent-blue-600"
/>
<span className="w-12 text-right text-sm font-mono">
{split.share_percent}%
</span>
{!isBulk && amount !== undefined && (
<span className="w-20 text-right text-sm text-zinc-400 font-mono">
${((amount * split.share_percent) / 100).toFixed(2)}
</span>
)}
</div>
)}
</div>
);
})}
</div>
{/* Total indicator */}
<div className="flex items-center justify-between mb-4 text-sm">
<span className={`font-mono ${Math.abs(total - 100) > 0.01 ? "text-red-400" : "text-green-400"}`}>
Total: {total.toFixed(1)}%
</span>
<button
onClick={splitEvenly}
className="text-blue-400 hover:text-blue-300 text-xs"
>
Split evenly
</button>
</div>
{error && <p className="text-red-400 text-sm mb-3">{error}</p>}
<div className="flex gap-3">
<button
onClick={onClose}
className="flex-1 py-2 bg-zinc-800 hover:bg-zinc-700 rounded-lg text-sm transition-colors"
>
Cancel
</button>
<button
onClick={handleSave}
disabled={isPending || Math.abs(total - 100) > 0.01}
className="flex-1 py-2 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 rounded-lg text-sm font-medium transition-colors"
>
{isPending ? "Saving..." : "Save splits"}
</button>
</div>
</div>
</div>
);
}
+27
View File
@@ -0,0 +1,27 @@
import { NextRequest } from "next/server";
import { queryRaw } from "./db";
export interface CurrentUser {
id: number;
name: string;
email: string;
}
export async function getCurrentUser(req: NextRequest): Promise<CurrentUser | null> {
const email = req.headers.get("x-forwarded-user");
// Dev fallback: no Traefik header → use participant id=1
if (!email) {
if (process.env.NODE_ENV === "development") {
const rows = await queryRaw<CurrentUser>(`SELECT id, name, email FROM participants WHERE id = 1`);
return rows[0] || null;
}
return null;
}
const rows = await queryRaw<CurrentUser>(
`SELECT id, name, COALESCE(email, '') as email FROM participants WHERE email = $1`,
[email]
);
return rows[0] || null;
}