refactor: enhance cache invalidation logic across banking API routes and components for improved data consistency and performance

This commit is contained in:
Julien Froidefond
2025-12-08 14:04:12 +01:00
parent 53bae084c4
commit 8d947ad70f
14 changed files with 412 additions and 200 deletions

View File

@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
import { prisma } from "@/lib/prisma";
import { requireAuth } from "@/lib/auth-utils";
@@ -15,10 +16,23 @@ export async function POST() {
},
});
return NextResponse.json({
success: true,
count: result.count,
});
// Revalider le cache des pages
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/categories", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(
{
success: true,
count: result.count,
},
{
headers: {
"Cache-Control": "no-store",
},
}
);
} catch (error) {
console.error("Error clearing categories:", error);
return NextResponse.json(

View File

@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
import { transactionService } from "@/services/transaction.service";
import { requireAuth } from "@/lib/auth-utils";
@@ -8,7 +9,17 @@ export async function POST() {
try {
const result = await transactionService.deduplicate();
return NextResponse.json(result);
// Revalider le cache des pages
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(result, {
headers: {
"Cache-Control": "no-store",
},
});
} catch (error) {
console.error("Error deduplicating transactions:", error);
return NextResponse.json(

View File

@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
import { transactionService } from "@/services/transaction.service";
import { bankingService } from "@/services/banking.service";
import { requireAuth } from "@/lib/auth-utils";
@@ -28,7 +29,7 @@ export async function GET(request: NextRequest) {
searchParams.get("includeUncategorized") === "true";
const search = searchParams.get("search") || undefined;
const isReconciledParam = searchParams.get("isReconciled");
const isReconciled =
const isReconciled: boolean | "all" =
isReconciledParam === "true"
? true
: isReconciledParam === "false"
@@ -40,7 +41,7 @@ export async function GET(request: NextRequest) {
const sortOrder =
(searchParams.get("sortOrder") as "asc" | "desc") || "desc";
const result = await bankingService.getTransactionsPaginated({
const params = {
limit,
offset,
startDate,
@@ -52,18 +53,18 @@ export async function GET(request: NextRequest) {
isReconciled,
sortField,
sortOrder,
});
};
return NextResponse.json(result, {
headers: {
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=120",
},
});
// Pas de cache serveur pour garantir des données toujours à jour
// Le cache client React Query gère déjà la mise en cache côté client
const result = await bankingService.getTransactionsPaginated(params);
return NextResponse.json(result);
} catch (error) {
console.error("Error fetching transactions:", error);
return NextResponse.json(
{ error: "Failed to fetch transactions" },
{ status: 500 },
{ status: 500 }
);
}
}
@@ -74,12 +75,22 @@ export async function POST(request: Request) {
try {
const transactions: Transaction[] = await request.json();
const result = await transactionService.createMany(transactions);
return NextResponse.json(result);
// Revalider le cache des pages
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(result, {
headers: {
"Cache-Control": "no-store",
},
});
} catch (error) {
console.error("Error creating transactions:", error);
return NextResponse.json(
{ error: "Failed to create transactions" },
{ status: 500 },
{ status: 500 }
);
}
}
@@ -92,14 +103,24 @@ export async function PUT(request: Request) {
const transaction: Transaction = await request.json();
const updated = await transactionService.update(
transaction.id,
transaction,
transaction
);
return NextResponse.json(updated);
// Revalider le cache des pages
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(updated, {
headers: {
"Cache-Control": "no-store",
},
});
} catch (error) {
console.error("Error updating transaction:", error);
return NextResponse.json(
{ error: "Failed to update transaction" },
{ status: 500 },
{ status: 500 }
);
}
}
@@ -115,12 +136,25 @@ export async function DELETE(request: Request) {
if (!id) {
return NextResponse.json(
{ error: "Transaction ID is required" },
{ status: 400 },
{ status: 400 }
);
}
await transactionService.delete(id);
return NextResponse.json({ success: true });
// Revalider le cache des pages
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(
{ success: true },
{
headers: {
"Cache-Control": "no-store",
},
}
);
} catch (error) {
console.error("Error deleting transaction:", error);
const errorMessage =