69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { revalidatePath } from "next/cache";
|
|
import { transactionService } from "@/services/transaction.service";
|
|
import { requireAuth } from "@/lib/auth-utils";
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const authError = await requireAuth();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const { startDate, endDate, reconciled = true } = body;
|
|
|
|
if (!endDate) {
|
|
return NextResponse.json(
|
|
{ error: "endDate is required" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
// Validate date format (YYYY-MM-DD)
|
|
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
if (startDate && !dateRegex.test(startDate)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid startDate format. Expected YYYY-MM-DD" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
if (!dateRegex.test(endDate)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid endDate format. Expected YYYY-MM-DD" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
if (startDate && startDate > endDate) {
|
|
return NextResponse.json(
|
|
{ error: "startDate must be before or equal to endDate" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const result = await transactionService.reconcileByDateRange(
|
|
startDate,
|
|
endDate,
|
|
reconciled,
|
|
);
|
|
|
|
// Revalider le cache des pages
|
|
revalidatePath("/transactions", "page");
|
|
revalidatePath("/statistics", "page");
|
|
revalidatePath("/dashboard", "page");
|
|
revalidatePath("/settings", "page");
|
|
|
|
return NextResponse.json(result, {
|
|
headers: {
|
|
"Cache-Control": "no-store",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error reconciling transactions by date range:", error);
|
|
const errorMessage =
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to reconcile transactions";
|
|
return NextResponse.json({ error: errorMessage }, { status: 500 });
|
|
}
|
|
}
|