feat(dashboard): add seeder page for database seed/reset
- Add /dashboard/seeder page with Seed and Reset buttons - Add Seeder item to sidebar (superadmin only) - Use seeder API endpoints POST /seed and POST /reset - Add confirmation dialogs for destructive actions
This commit is contained in:
parent
0238195723
commit
655f779b11
2 changed files with 198 additions and 3 deletions
185
frontend/src/app/dashboard/seeder/page.tsx
Normal file
185
frontend/src/app/dashboard/seeder/page.tsx
Normal file
|
|
@ -0,0 +1,185 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
import { AlertTriangle, Database, Loader2, RefreshCw, Trash2 } from "lucide-react";
|
||||||
|
import { getSeederApiUrl } from "@/lib/config";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
export default function SeederPage() {
|
||||||
|
const [isSeeding, setIsSeeding] = useState(false);
|
||||||
|
const [isResetting, setIsResetting] = useState(false);
|
||||||
|
const [lastResult, setLastResult] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const handleSeed = async () => {
|
||||||
|
if (!confirm("Tem certeza que deseja popular o banco de dados com dados de teste?")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsSeeding(true);
|
||||||
|
setLastResult(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${getSeederApiUrl()}/seed`, {
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
toast.success("Banco populado com sucesso!");
|
||||||
|
setLastResult(JSON.stringify(data, null, 2));
|
||||||
|
} else {
|
||||||
|
toast.error(data.message || "Erro ao popular banco");
|
||||||
|
setLastResult(JSON.stringify(data, null, 2));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
toast.error("Erro de conexão com o seeder");
|
||||||
|
setLastResult(String(error));
|
||||||
|
} finally {
|
||||||
|
setIsSeeding(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleReset = async () => {
|
||||||
|
if (!confirm("⚠️ ATENÇÃO: Isso vai APAGAR todos os dados exceto o SuperAdmin. Tem certeza?")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!confirm("🚨 ÚLTIMA CONFIRMAÇÃO: Esta ação é IRREVERSÍVEL. Continuar?")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsResetting(true);
|
||||||
|
setLastResult(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${getSeederApiUrl()}/reset`, {
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
toast.success("Banco resetado com sucesso!");
|
||||||
|
setLastResult(JSON.stringify(data, null, 2));
|
||||||
|
} else {
|
||||||
|
toast.error(data.message || "Erro ao resetar banco");
|
||||||
|
setLastResult(JSON.stringify(data, null, 2));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
toast.error("Erro de conexão com o seeder");
|
||||||
|
setLastResult(String(error));
|
||||||
|
} finally {
|
||||||
|
setIsResetting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="container mx-auto py-6 space-y-6">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-3xl font-bold">🌱 Seeder</h1>
|
||||||
|
<p className="text-muted-foreground mt-2">
|
||||||
|
Popular ou resetar o banco de dados de desenvolvimento
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-6 md:grid-cols-2">
|
||||||
|
{/* Seed Card */}
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Database className="h-5 w-5 text-green-500" />
|
||||||
|
Popular Banco
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Adiciona dados de teste: empresas, usuários, vagas, candidaturas
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<ul className="text-sm text-muted-foreground space-y-1">
|
||||||
|
<li>✅ Empresas de exemplo</li>
|
||||||
|
<li>✅ Usuários (admin, recruiter, candidate)</li>
|
||||||
|
<li>✅ Vagas de emprego</li>
|
||||||
|
<li>✅ Candidaturas</li>
|
||||||
|
<li>✅ Tags/Categorias</li>
|
||||||
|
</ul>
|
||||||
|
<Button
|
||||||
|
onClick={handleSeed}
|
||||||
|
disabled={isSeeding || isResetting}
|
||||||
|
className="w-full"
|
||||||
|
variant="default"
|
||||||
|
>
|
||||||
|
{isSeeding ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
|
Populando...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<RefreshCw className="mr-2 h-4 w-4" />
|
||||||
|
Executar Seed
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Reset Card */}
|
||||||
|
<Card className="border-destructive/50">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2 text-destructive">
|
||||||
|
<AlertTriangle className="h-5 w-5" />
|
||||||
|
Resetar Banco
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Remove TODOS os dados exceto o usuário SuperAdmin
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<ul className="text-sm text-muted-foreground space-y-1">
|
||||||
|
<li>❌ Apaga todas as empresas</li>
|
||||||
|
<li>❌ Apaga todos os usuários (exceto superadmin)</li>
|
||||||
|
<li>❌ Apaga todas as vagas</li>
|
||||||
|
<li>❌ Apaga todas as candidaturas</li>
|
||||||
|
<li>✅ Mantém o SuperAdmin</li>
|
||||||
|
</ul>
|
||||||
|
<Button
|
||||||
|
onClick={handleReset}
|
||||||
|
disabled={isSeeding || isResetting}
|
||||||
|
className="w-full"
|
||||||
|
variant="destructive"
|
||||||
|
>
|
||||||
|
{isResetting ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
|
Resetando...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Trash2 className="mr-2 h-4 w-4" />
|
||||||
|
Resetar Banco
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Result Output */}
|
||||||
|
{lastResult && (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="text-sm">Resultado</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<pre className="bg-muted p-4 rounded-lg text-xs overflow-auto max-h-64">
|
||||||
|
{lastResult}
|
||||||
|
</pre>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,7 @@ import Link from "next/link"
|
||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import { usePathname } from "next/navigation"
|
import { usePathname } from "next/navigation"
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from "@/lib/utils"
|
||||||
import { LayoutDashboard, Briefcase, Users, MessageSquare, Building2, FileText, HelpCircle, Ticket, Settings } from "lucide-react"
|
import { LayoutDashboard, Briefcase, Users, MessageSquare, Building2, FileText, HelpCircle, Ticket, Settings, Sprout } from "lucide-react"
|
||||||
import { getCurrentUser, isAdminUser } from "@/lib/auth"
|
import { getCurrentUser, isAdminUser } from "@/lib/auth"
|
||||||
import { useTranslation } from "@/lib/i18n"
|
import { useTranslation } from "@/lib/i18n"
|
||||||
|
|
||||||
|
|
@ -45,6 +45,12 @@ const Sidebar = () => {
|
||||||
href: "/dashboard/backoffice",
|
href: "/dashboard/backoffice",
|
||||||
icon: FileText,
|
icon: FileText,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: "Seeder",
|
||||||
|
href: "/dashboard/seeder",
|
||||||
|
icon: Sprout,
|
||||||
|
superadminOnly: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: t('sidebar.messages'),
|
title: t('sidebar.messages'),
|
||||||
href: "/dashboard/messages",
|
href: "/dashboard/messages",
|
||||||
|
|
@ -56,7 +62,7 @@ const Sidebar = () => {
|
||||||
icon: Ticket,
|
icon: Ticket,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t('sidebar.settings') || "Settings", // Fallback if translation missing
|
title: t('sidebar.settings') || "Settings",
|
||||||
href: "/dashboard/settings",
|
href: "/dashboard/settings",
|
||||||
icon: Settings,
|
icon: Settings,
|
||||||
},
|
},
|
||||||
|
|
@ -117,7 +123,11 @@ const Sidebar = () => {
|
||||||
if (isAdminUser(user)) {
|
if (isAdminUser(user)) {
|
||||||
items = isSuperadmin
|
items = isSuperadmin
|
||||||
? adminItems
|
? adminItems
|
||||||
: adminItems.filter(item => item.href !== "/dashboard/backoffice" && item.href !== "/dashboard/companies")
|
: adminItems.filter(item =>
|
||||||
|
item.href !== "/dashboard/backoffice" &&
|
||||||
|
item.href !== "/dashboard/companies" &&
|
||||||
|
!('superadminOnly' in item && item.superadminOnly)
|
||||||
|
)
|
||||||
} else if (user?.role === "company") {
|
} else if (user?.role === "company") {
|
||||||
items = companyItems
|
items = companyItems
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue