feat: add functionality to clear category assignments from transactions and display categorized count in settings

This commit is contained in:
Julien Froidefond
2025-11-27 10:31:24 +01:00
parent 7314cb6716
commit e516ed9efd
2 changed files with 72 additions and 3 deletions

View File

@@ -0,0 +1,24 @@
import { NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
export async function POST() {
try {
const result = await prisma.transaction.updateMany({
where: {
categoryId: { not: null },
},
data: {
categoryId: null,
},
})
return NextResponse.json({
success: true,
count: result.count,
})
} catch (error) {
console.error("Error clearing categories:", error)
return NextResponse.json({ error: "Failed to clear categories" }, { status: 500 })
}
}

View File

@@ -16,7 +16,7 @@ import {
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Download, Trash2, Upload, RefreshCw, Database, FileJson } from "lucide-react"
import { Download, Trash2, Upload, RefreshCw, Database, FileJson, Tags } from "lucide-react"
import type { BankingData } from "@/lib/types"
export default function SettingsPage() {
@@ -82,6 +82,22 @@ export default function SettingsPage() {
window.location.reload()
}
const clearAllCategories = async () => {
try {
const response = await fetch("/api/banking/transactions/clear-categories", {
method: "POST",
})
if (!response.ok) throw new Error("Erreur")
refresh()
alert("Catégories supprimées de toutes les opérations")
} catch (error) {
console.error(error)
alert("Erreur lors de la suppression des catégories")
}
}
const categorizedCount = data.transactions.filter((t) => t.categoryId).length
return (
<div className="flex h-screen bg-background">
<Sidebar />
@@ -132,10 +148,39 @@ export default function SettingsPage() {
</CardTitle>
<CardDescription>Actions irréversibles - procédez avec prudence</CardDescription>
</CardHeader>
<CardContent>
<CardContent className="space-y-3">
{/* Supprimer catégories des opérations */}
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructive">
<Button variant="outline" className="w-full justify-start border-orange-300 text-orange-700 hover:bg-orange-50">
<Tags className="w-4 h-4 mr-2" />
Supprimer les catégories des opérations
<span className="ml-auto text-xs text-muted-foreground">
{categorizedCount} opération{categorizedCount > 1 ? "s" : ""} catégorisée{categorizedCount > 1 ? "s" : ""}
</span>
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Supprimer toutes les catégories ?</AlertDialogTitle>
<AlertDialogDescription>
Cette action va retirer la catégorie de {categorizedCount} opération{categorizedCount > 1 ? "s" : ""}.
Les catégories elles-mêmes ne seront pas supprimées, seulement leur affectation aux opérations.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Annuler</AlertDialogCancel>
<AlertDialogAction onClick={clearAllCategories} className="bg-orange-600 hover:bg-orange-700">
Supprimer les affectations
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Réinitialiser toutes les données */}
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructive" className="w-full justify-start">
<Trash2 className="w-4 h-4 mr-2" />
Réinitialiser toutes les données
</Button>