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 { 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { categoryService } from "@/services/category.service";
|
||||
import { bankingService } from "@/services/banking.service";
|
||||
import { requireAuth } from "@/lib/auth-utils";
|
||||
@@ -14,11 +15,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
if (statsOnly) {
|
||||
const stats = await bankingService.getCategoryStats();
|
||||
return NextResponse.json(stats, {
|
||||
headers: {
|
||||
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=120",
|
||||
},
|
||||
});
|
||||
return NextResponse.json(stats);
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
|
||||
@@ -26,7 +23,7 @@ export async function GET(request: NextRequest) {
|
||||
console.error("Error fetching category stats:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch category stats" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -37,12 +34,23 @@ export async function POST(request: Request) {
|
||||
try {
|
||||
const data: Omit<Category, "id"> = await request.json();
|
||||
const created = await categoryService.create(data);
|
||||
return NextResponse.json(created);
|
||||
|
||||
// Revalider le cache serveur
|
||||
revalidatePath("/categories", "page");
|
||||
revalidatePath("/transactions", "page");
|
||||
revalidatePath("/statistics", "page");
|
||||
revalidatePath("/rules", "page");
|
||||
|
||||
return NextResponse.json(created, {
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error creating category:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create category" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -54,12 +62,23 @@ export async function PUT(request: Request) {
|
||||
try {
|
||||
const category: Category = await request.json();
|
||||
const updated = await categoryService.update(category.id, category);
|
||||
return NextResponse.json(updated);
|
||||
|
||||
// Revalider le cache serveur
|
||||
revalidatePath("/categories", "page");
|
||||
revalidatePath("/transactions", "page");
|
||||
revalidatePath("/statistics", "page");
|
||||
revalidatePath("/rules", "page");
|
||||
|
||||
return NextResponse.json(updated, {
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating category:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update category" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -75,17 +94,31 @@ export async function DELETE(request: Request) {
|
||||
if (!id) {
|
||||
return NextResponse.json(
|
||||
{ error: "Category ID is required" },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await categoryService.delete(id);
|
||||
return NextResponse.json({ success: true });
|
||||
|
||||
// Revalider le cache serveur
|
||||
revalidatePath("/categories", "page");
|
||||
revalidatePath("/transactions", "page");
|
||||
revalidatePath("/statistics", "page");
|
||||
revalidatePath("/rules", "page");
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: true },
|
||||
{
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error deleting category:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to delete category" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { folderService, FolderNotFoundError } from "@/services/folder.service";
|
||||
import { requireAuth } from "@/lib/auth-utils";
|
||||
import type { Folder } from "@/lib/types";
|
||||
@@ -9,12 +10,20 @@ export async function POST(request: Request) {
|
||||
try {
|
||||
const data: Omit<Folder, "id"> = await request.json();
|
||||
const created = await folderService.create(data);
|
||||
return NextResponse.json(created);
|
||||
|
||||
// Revalider le cache des pages
|
||||
revalidatePath("/accounts", "page");
|
||||
|
||||
return NextResponse.json(created, {
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error creating folder:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create folder" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -26,12 +35,20 @@ export async function PUT(request: Request) {
|
||||
try {
|
||||
const folder: Folder = await request.json();
|
||||
const updated = await folderService.update(folder.id, folder);
|
||||
return NextResponse.json(updated);
|
||||
|
||||
// Revalider le cache des pages
|
||||
revalidatePath("/accounts", "page");
|
||||
|
||||
return NextResponse.json(updated, {
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating folder:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update folder" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -47,12 +64,23 @@ export async function DELETE(request: Request) {
|
||||
if (!id) {
|
||||
return NextResponse.json(
|
||||
{ error: "Folder ID is required" },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await folderService.delete(id);
|
||||
return NextResponse.json({ success: true });
|
||||
|
||||
// Revalider le cache des pages
|
||||
revalidatePath("/accounts", "page");
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: true },
|
||||
{
|
||||
headers: {
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof FolderNotFoundError) {
|
||||
return NextResponse.json({ error: "Folder not found" }, { status: 404 });
|
||||
@@ -60,7 +88,7 @@ export async function DELETE(request: Request) {
|
||||
console.error("Error deleting folder:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to delete folder" },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,19 +12,11 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
if (metadataOnly) {
|
||||
const metadata = await bankingService.getMetadata();
|
||||
return NextResponse.json(metadata, {
|
||||
headers: {
|
||||
"Cache-Control": "public, s-maxage=300, stale-while-revalidate=600",
|
||||
},
|
||||
});
|
||||
return NextResponse.json(metadata);
|
||||
}
|
||||
|
||||
const data = await bankingService.getAllData();
|
||||
return NextResponse.json(data, {
|
||||
headers: {
|
||||
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=120",
|
||||
},
|
||||
});
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching banking data:", error);
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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