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 { NextRequest, NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
import { accountService } from "@/services/account.service";
import { bankingService } from "@/services/banking.service";
import { requireAuth } from "@/lib/auth-utils";
@@ -14,11 +15,7 @@ export async function GET(request: NextRequest) {
if (withStats) {
const accountsWithStats = await bankingService.getAccountsWithStats();
return NextResponse.json(accountsWithStats, {
headers: {
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=120",
},
});
return NextResponse.json(accountsWithStats);
}
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
@@ -26,7 +23,7 @@ export async function GET(request: NextRequest) {
console.error("Error fetching accounts:", error);
return NextResponse.json(
{ error: "Failed to fetch accounts" },
{ status: 500 },
{ status: 500 }
);
}
}
@@ -38,12 +35,23 @@ export async function POST(request: Request) {
try {
const data: Omit<Account, "id"> = await request.json();
const created = await accountService.create(data);
return NextResponse.json(created);
// Revalider le cache serveur
revalidatePath("/accounts", "page");
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(created, {
headers: {
"Cache-Control": "no-store",
},
});
} catch (error) {
console.error("Error creating account:", error);
return NextResponse.json(
{ error: "Failed to create account" },
{ status: 500 },
{ status: 500 }
);
}
}
@@ -55,12 +63,23 @@ export async function PUT(request: Request) {
try {
const account: Account = await request.json();
const updated = await accountService.update(account.id, account);
return NextResponse.json(updated);
// Revalider le cache serveur
revalidatePath("/accounts", "page");
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(updated, {
headers: {
"Cache-Control": "no-store",
},
});
} catch (error) {
console.error("Error updating account:", error);
return NextResponse.json(
{ error: "Failed to update account" },
{ status: 500 },
{ status: 500 }
);
}
}
@@ -80,27 +99,55 @@ export async function DELETE(request: Request) {
if (accountIds.length === 0) {
return NextResponse.json(
{ error: "At least one account ID is required" },
{ status: 400 },
{ status: 400 }
);
}
await accountService.deleteMany(accountIds);
return NextResponse.json({ success: true, count: accountIds.length });
// Revalider le cache serveur
revalidatePath("/accounts", "page");
revalidatePath("/transactions", "page");
revalidatePath("/statistics", "page");
revalidatePath("/dashboard", "page");
return NextResponse.json(
{ success: true, count: accountIds.length },
{
headers: {
"Cache-Control": "no-store",
},
}
);
}
if (!id) {
return NextResponse.json(
{ error: "Account ID is required" },
{ status: 400 },
{ status: 400 }
);
}
await accountService.delete(id);
return NextResponse.json({ success: true });
// Revalider le cache serveur
revalidatePath("/accounts", "page");
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 account:", error);
return NextResponse.json(
{ error: "Failed to delete account" },
{ status: 500 },
{ status: 500 }
);
}
}