132 lines
3.8 KiB
TypeScript
132 lines
3.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { transactionService } from "@/services/transaction.service";
|
|
import { bankingService } from "@/services/banking.service";
|
|
import { requireAuth } from "@/lib/auth-utils";
|
|
import type { Transaction } from "@/lib/types";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const authError = await requireAuth();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
// Parse pagination params
|
|
const limit = parseInt(searchParams.get("limit") || "50", 10);
|
|
const offset = parseInt(searchParams.get("offset") || "0", 10);
|
|
|
|
// Parse filter params
|
|
const startDate = searchParams.get("startDate") || undefined;
|
|
const endDate = searchParams.get("endDate") || undefined;
|
|
const accountIds = searchParams.get("accountIds")
|
|
? searchParams.get("accountIds")!.split(",")
|
|
: undefined;
|
|
const categoryIds = searchParams.get("categoryIds")
|
|
? searchParams.get("categoryIds")!.split(",")
|
|
: undefined;
|
|
const includeUncategorized =
|
|
searchParams.get("includeUncategorized") === "true";
|
|
const search = searchParams.get("search") || undefined;
|
|
const isReconciledParam = searchParams.get("isReconciled");
|
|
const isReconciled =
|
|
isReconciledParam === "true"
|
|
? true
|
|
: isReconciledParam === "false"
|
|
? false
|
|
: "all";
|
|
const sortField =
|
|
(searchParams.get("sortField") as "date" | "amount" | "description") ||
|
|
"date";
|
|
const sortOrder =
|
|
(searchParams.get("sortOrder") as "asc" | "desc") || "desc";
|
|
|
|
const result = await bankingService.getTransactionsPaginated({
|
|
limit,
|
|
offset,
|
|
startDate,
|
|
endDate,
|
|
accountIds,
|
|
categoryIds,
|
|
includeUncategorized,
|
|
search,
|
|
isReconciled,
|
|
sortField,
|
|
sortOrder,
|
|
});
|
|
|
|
return NextResponse.json(result, {
|
|
headers: {
|
|
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=120",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching transactions:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch transactions" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const authError = await requireAuth();
|
|
if (authError) return authError;
|
|
try {
|
|
const transactions: Transaction[] = await request.json();
|
|
const result = await transactionService.createMany(transactions);
|
|
return NextResponse.json(result);
|
|
} catch (error) {
|
|
console.error("Error creating transactions:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to create transactions" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
const authError = await requireAuth();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const transaction: Transaction = await request.json();
|
|
const updated = await transactionService.update(
|
|
transaction.id,
|
|
transaction,
|
|
);
|
|
return NextResponse.json(updated);
|
|
} catch (error) {
|
|
console.error("Error updating transaction:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to update transaction" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: Request) {
|
|
const authError = await requireAuth();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const id = searchParams.get("id");
|
|
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: "Transaction ID is required" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
await transactionService.delete(id);
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Error deleting transaction:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to delete transaction" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|