refactor: enhance cache invalidation logic across banking API routes and components for improved data consistency and performance
This commit is contained in:
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user