feat: username login support - recherche par username OU email
This commit is contained in:
@@ -12,6 +12,7 @@ import AAPDashboard from "./pages/AAPDashboard";
|
|||||||
import Settings from "./pages/Settings";
|
import Settings from "./pages/Settings";
|
||||||
import UsersAdmin from "./pages/UsersAdmin";
|
import UsersAdmin from "./pages/UsersAdmin";
|
||||||
import ImportLogs from "./pages/ImportLogs";
|
import ImportLogs from "./pages/ImportLogs";
|
||||||
|
import BoiteAIdees from "./pages/BoiteAIdees";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
|
|
||||||
// ─── Guard d'authentification ─────────────────────────────────────────────────
|
// ─── Guard d'authentification ─────────────────────────────────────────────────
|
||||||
@@ -97,6 +98,16 @@ function LogsPage() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function BoiteAIdeesPage() {
|
||||||
|
return (
|
||||||
|
<AuthGuard>
|
||||||
|
<DashboardWrapper>
|
||||||
|
<BoiteAIdees />
|
||||||
|
</DashboardWrapper>
|
||||||
|
</AuthGuard>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Routeur principal ────────────────────────────────────────────────────────
|
// ─── Routeur principal ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function Router() {
|
function Router() {
|
||||||
@@ -111,6 +122,7 @@ function Router() {
|
|||||||
<Route path="/admin/settings" component={SettingsPage} />
|
<Route path="/admin/settings" component={SettingsPage} />
|
||||||
<Route path="/admin/users" component={UsersPage} />
|
<Route path="/admin/users" component={UsersPage} />
|
||||||
<Route path="/admin/logs" component={LogsPage} />
|
<Route path="/admin/logs" component={LogsPage} />
|
||||||
|
<Route path="/boite-a-idees" component={BoiteAIdeesPage} />
|
||||||
<Route path="/404" component={NotFound} />
|
<Route path="/404" component={NotFound} />
|
||||||
<Route component={NotFound} />
|
<Route component={NotFound} />
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
RefreshCw,
|
RefreshCw,
|
||||||
Menu,
|
Menu,
|
||||||
X,
|
X,
|
||||||
|
Lightbulb,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
@@ -48,6 +49,14 @@ const NAV_GROUPS: NavGroup[] = [
|
|||||||
{ label: "Appels à Projets", href: "/aap", icon: <Target size={16} /> },
|
{ label: "Appels à Projets", href: "/aap", icon: <Target size={16} /> },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: "Boîte à idées",
|
||||||
|
icon: <Lightbulb size={18} />,
|
||||||
|
defaultOpen: true,
|
||||||
|
items: [
|
||||||
|
{ label: "Boîte à idées", href: "/boite-a-idees", icon: <Lightbulb size={16} /> },
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: "Administration",
|
label: "Administration",
|
||||||
icon: <Settings size={18} />,
|
icon: <Settings size={18} />,
|
||||||
@@ -71,6 +80,7 @@ export function AppLayout({ children, user, onLogout }: AppLayoutProps) {
|
|||||||
const [mobileOpen, setMobileOpen] = useState(false);
|
const [mobileOpen, setMobileOpen] = useState(false);
|
||||||
const [openGroups, setOpenGroups] = useState<Record<string, boolean>>({
|
const [openGroups, setOpenGroups] = useState<Record<string, boolean>>({
|
||||||
"Tableaux de bord": true,
|
"Tableaux de bord": true,
|
||||||
|
"Boîte à idées": true,
|
||||||
"Administration": false,
|
"Administration": false,
|
||||||
});
|
});
|
||||||
const [location] = useLocation();
|
const [location] = useLocation();
|
||||||
|
|||||||
465
client/src/pages/BoiteAIdees.tsx
Normal file
465
client/src/pages/BoiteAIdees.tsx
Normal file
@@ -0,0 +1,465 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { trpc } from "@/lib/trpc";
|
||||||
|
import { useAuth } from "@/_core/hooks/useAuth";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogFooter,
|
||||||
|
} from "@/components/ui/dialog";
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from "@/components/ui/select";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import {
|
||||||
|
Lightbulb,
|
||||||
|
Plus,
|
||||||
|
MessageSquare,
|
||||||
|
Calendar,
|
||||||
|
User,
|
||||||
|
ChevronDown,
|
||||||
|
ChevronUp,
|
||||||
|
Send,
|
||||||
|
Clock,
|
||||||
|
CheckCircle2,
|
||||||
|
XCircle,
|
||||||
|
AlertCircle,
|
||||||
|
} from "lucide-react";
|
||||||
|
import { format } from "date-fns";
|
||||||
|
import { fr } from "date-fns/locale";
|
||||||
|
|
||||||
|
type Statut = "ouvert" | "en_cours" | "resolu" | "ferme";
|
||||||
|
|
||||||
|
const STATUT_CONFIG: Record<Statut, { label: string; color: string; icon: React.ReactNode }> = {
|
||||||
|
ouvert: {
|
||||||
|
label: "Ouvert",
|
||||||
|
color: "bg-blue-100 text-blue-700 border-blue-200",
|
||||||
|
icon: <AlertCircle className="h-3 w-3" />,
|
||||||
|
},
|
||||||
|
en_cours: {
|
||||||
|
label: "En cours",
|
||||||
|
color: "bg-amber-100 text-amber-700 border-amber-200",
|
||||||
|
icon: <Clock className="h-3 w-3" />,
|
||||||
|
},
|
||||||
|
resolu: {
|
||||||
|
label: "Résolu",
|
||||||
|
color: "bg-emerald-100 text-emerald-700 border-emerald-200",
|
||||||
|
icon: <CheckCircle2 className="h-3 w-3" />,
|
||||||
|
},
|
||||||
|
ferme: {
|
||||||
|
label: "Fermé",
|
||||||
|
color: "bg-slate-100 text-slate-600 border-slate-200",
|
||||||
|
icon: <XCircle className="h-3 w-3" />,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
type Idea = {
|
||||||
|
id: number;
|
||||||
|
userId: number;
|
||||||
|
userName: string;
|
||||||
|
titre: string;
|
||||||
|
message: string;
|
||||||
|
statut: Statut;
|
||||||
|
reponseAdmin: string | null;
|
||||||
|
reponduPar: string | null;
|
||||||
|
reponduAt: Date | null;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function BoiteAIdees() {
|
||||||
|
const { user } = useAuth();
|
||||||
|
const isAdmin = user?.role === "admin";
|
||||||
|
|
||||||
|
const { data: ideas = [], refetch } = trpc.ideas.list.useQuery();
|
||||||
|
const createMutation = trpc.ideas.create.useMutation({
|
||||||
|
onSuccess: () => {
|
||||||
|
toast.success("Votre demande a été envoyée avec succès !");
|
||||||
|
setNewDialogOpen(false);
|
||||||
|
setNewTitre("");
|
||||||
|
setNewMessage("");
|
||||||
|
refetch();
|
||||||
|
},
|
||||||
|
onError: (e) => toast.error(e.message),
|
||||||
|
});
|
||||||
|
const reponseMutation = trpc.ideas.repondre.useMutation({
|
||||||
|
onSuccess: () => {
|
||||||
|
toast.success("Réponse enregistrée !");
|
||||||
|
setReponseDialogOpen(false);
|
||||||
|
setReponseText("");
|
||||||
|
setSelectedIdea(null);
|
||||||
|
refetch();
|
||||||
|
},
|
||||||
|
onError: (e) => toast.error(e.message),
|
||||||
|
});
|
||||||
|
|
||||||
|
// État nouvelle demande
|
||||||
|
const [newDialogOpen, setNewDialogOpen] = useState(false);
|
||||||
|
const [newTitre, setNewTitre] = useState("");
|
||||||
|
const [newMessage, setNewMessage] = useState("");
|
||||||
|
|
||||||
|
// État réponse admin
|
||||||
|
const [reponseDialogOpen, setReponseDialogOpen] = useState(false);
|
||||||
|
const [selectedIdea, setSelectedIdea] = useState<Idea | null>(null);
|
||||||
|
const [reponseText, setReponseText] = useState("");
|
||||||
|
const [reponseStatut, setReponseStatut] = useState<Statut>("resolu");
|
||||||
|
|
||||||
|
// État expansion des cartes
|
||||||
|
const [expandedIds, setExpandedIds] = useState<Set<number>>(new Set());
|
||||||
|
|
||||||
|
// Filtre statut
|
||||||
|
const [filtreStatut, setFiltreStatut] = useState<string>("tous");
|
||||||
|
const [filtreRecherche, setFiltreRecherche] = useState("");
|
||||||
|
|
||||||
|
const toggleExpand = (id: number) => {
|
||||||
|
setExpandedIds((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
if (next.has(id)) next.delete(id);
|
||||||
|
else next.add(id);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleRepondre = (idea: Idea) => {
|
||||||
|
setSelectedIdea(idea);
|
||||||
|
setReponseText(idea.reponseAdmin ?? "");
|
||||||
|
setReponseStatut(idea.statut === "ouvert" ? "en_cours" : idea.statut);
|
||||||
|
setReponseDialogOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const filteredIdeas = (ideas as Idea[]).filter((idea) => {
|
||||||
|
const matchStatut = filtreStatut === "tous" || idea.statut === filtreStatut;
|
||||||
|
const matchRecherche =
|
||||||
|
!filtreRecherche ||
|
||||||
|
idea.titre.toLowerCase().includes(filtreRecherche.toLowerCase()) ||
|
||||||
|
idea.message.toLowerCase().includes(filtreRecherche.toLowerCase()) ||
|
||||||
|
idea.userName.toLowerCase().includes(filtreRecherche.toLowerCase());
|
||||||
|
return matchStatut && matchRecherche;
|
||||||
|
});
|
||||||
|
|
||||||
|
const counts = {
|
||||||
|
tous: (ideas as Idea[]).length,
|
||||||
|
ouvert: (ideas as Idea[]).filter((i) => i.statut === "ouvert").length,
|
||||||
|
en_cours: (ideas as Idea[]).filter((i) => i.statut === "en_cours").length,
|
||||||
|
resolu: (ideas as Idea[]).filter((i) => i.statut === "resolu").length,
|
||||||
|
ferme: (ideas as Idea[]).filter((i) => i.statut === "ferme").length,
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="p-6 max-w-5xl mx-auto">
|
||||||
|
{/* En-tête */}
|
||||||
|
<div className="flex items-center justify-between mb-6">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className="p-2 bg-amber-100 rounded-xl">
|
||||||
|
<Lightbulb className="h-6 w-6 text-amber-600" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h1 className="text-2xl font-bold text-slate-800">Boîte à idées</h1>
|
||||||
|
<p className="text-sm text-slate-500">
|
||||||
|
{isAdmin
|
||||||
|
? "Gérez les questions et suggestions des utilisateurs"
|
||||||
|
: "Posez vos questions et partagez vos suggestions"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
onClick={() => setNewDialogOpen(true)}
|
||||||
|
className="bg-[#0a2463] hover:bg-[#0a2463]/90 text-white gap-2 shadow-md"
|
||||||
|
>
|
||||||
|
<Plus className="h-4 w-4" />
|
||||||
|
Nouvelle demande
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Filtres */}
|
||||||
|
<div className="flex flex-wrap gap-3 mb-6">
|
||||||
|
<div className="flex-1 min-w-[200px]">
|
||||||
|
<Input
|
||||||
|
placeholder="Rechercher par titre, message ou demandeur..."
|
||||||
|
value={filtreRecherche}
|
||||||
|
onChange={(e) => setFiltreRecherche(e.target.value)}
|
||||||
|
className="bg-white border-slate-200"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2 flex-wrap">
|
||||||
|
{(["tous", "ouvert", "en_cours", "resolu", "ferme"] as const).map((s) => (
|
||||||
|
<button
|
||||||
|
key={s}
|
||||||
|
onClick={() => setFiltreStatut(s)}
|
||||||
|
className={`px-3 py-1.5 rounded-full text-xs font-medium border transition-all ${
|
||||||
|
filtreStatut === s
|
||||||
|
? "bg-[#0a2463] text-white border-[#0a2463]"
|
||||||
|
: "bg-white text-slate-600 border-slate-200 hover:border-[#0a2463]/40"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{s === "tous" ? "Tous" : STATUT_CONFIG[s].label}{" "}
|
||||||
|
<span className="opacity-70">({counts[s]})</span>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Liste des demandes */}
|
||||||
|
{filteredIdeas.length === 0 ? (
|
||||||
|
<div className="text-center py-16 text-slate-400">
|
||||||
|
<MessageSquare className="h-12 w-12 mx-auto mb-3 opacity-30" />
|
||||||
|
<p className="text-lg font-medium">Aucune demande pour le moment</p>
|
||||||
|
<p className="text-sm mt-1">Soyez le premier à soumettre une question ou une suggestion.</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-3">
|
||||||
|
{filteredIdeas.map((idea) => {
|
||||||
|
const isExpanded = expandedIds.has(idea.id);
|
||||||
|
const cfg = STATUT_CONFIG[idea.statut];
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={idea.id}
|
||||||
|
className="bg-white rounded-xl border border-slate-100 shadow-sm hover:shadow-md transition-shadow overflow-hidden"
|
||||||
|
>
|
||||||
|
{/* En-tête de la carte */}
|
||||||
|
<div
|
||||||
|
className="flex items-start gap-4 p-4 cursor-pointer"
|
||||||
|
onClick={() => toggleExpand(idea.id)}
|
||||||
|
>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="flex items-center gap-2 flex-wrap mb-1">
|
||||||
|
<span
|
||||||
|
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium border ${cfg.color}`}
|
||||||
|
>
|
||||||
|
{cfg.icon}
|
||||||
|
{cfg.label}
|
||||||
|
</span>
|
||||||
|
<h3 className="font-semibold text-slate-800 text-sm truncate">
|
||||||
|
{idea.titre}
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-4 text-xs text-slate-400 mt-1">
|
||||||
|
<span className="flex items-center gap-1">
|
||||||
|
<User className="h-3 w-3" />
|
||||||
|
{idea.userName}
|
||||||
|
</span>
|
||||||
|
<span className="flex items-center gap-1">
|
||||||
|
<Calendar className="h-3 w-3" />
|
||||||
|
{format(new Date(idea.createdAt), "d MMM yyyy à HH:mm", { locale: fr })}
|
||||||
|
</span>
|
||||||
|
{idea.reponseAdmin && (
|
||||||
|
<span className="flex items-center gap-1 text-emerald-600">
|
||||||
|
<MessageSquare className="h-3 w-3" />
|
||||||
|
Réponse disponible
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2 shrink-0">
|
||||||
|
{isAdmin && (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="text-xs h-7 px-2 border-[#0a2463]/30 text-[#0a2463] hover:bg-[#0a2463]/5"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
handleRepondre(idea);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Send className="h-3 w-3 mr-1" />
|
||||||
|
Répondre
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{isExpanded ? (
|
||||||
|
<ChevronUp className="h-4 w-4 text-slate-400" />
|
||||||
|
) : (
|
||||||
|
<ChevronDown className="h-4 w-4 text-slate-400" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Contenu développé */}
|
||||||
|
{isExpanded && (
|
||||||
|
<div className="border-t border-slate-100 px-4 pb-4 pt-3 space-y-3">
|
||||||
|
{/* Message */}
|
||||||
|
<div className="bg-slate-50 rounded-lg p-3">
|
||||||
|
<p className="text-xs font-medium text-slate-500 mb-1.5 uppercase tracking-wide">
|
||||||
|
Message
|
||||||
|
</p>
|
||||||
|
<p className="text-sm text-slate-700 whitespace-pre-wrap">{idea.message}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Réponse admin */}
|
||||||
|
{idea.reponseAdmin && (
|
||||||
|
<div className="bg-emerald-50 rounded-lg p-3 border border-emerald-100">
|
||||||
|
<p className="text-xs font-medium text-emerald-600 mb-1.5 uppercase tracking-wide flex items-center gap-1">
|
||||||
|
<MessageSquare className="h-3 w-3" />
|
||||||
|
Réponse de l'équipe
|
||||||
|
{idea.reponduPar && (
|
||||||
|
<span className="text-emerald-500 normal-case ml-1">
|
||||||
|
— {idea.reponduPar}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{idea.reponduAt && (
|
||||||
|
<span className="text-emerald-400 normal-case ml-1">
|
||||||
|
({format(new Date(idea.reponduAt), "d MMM yyyy", { locale: fr })})
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
<p className="text-sm text-emerald-800 whitespace-pre-wrap">
|
||||||
|
{idea.reponseAdmin}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Dialog : Nouvelle demande */}
|
||||||
|
<Dialog open={newDialogOpen} onOpenChange={setNewDialogOpen}>
|
||||||
|
<DialogContent className="max-w-lg">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle className="flex items-center gap-2">
|
||||||
|
<Lightbulb className="h-5 w-5 text-amber-500" />
|
||||||
|
Nouvelle demande
|
||||||
|
</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4 py-2">
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="titre" className="text-sm font-medium">
|
||||||
|
Titre <span className="text-red-500">*</span>
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="titre"
|
||||||
|
placeholder="Résumez votre demande en quelques mots..."
|
||||||
|
value={newTitre}
|
||||||
|
onChange={(e) => setNewTitre(e.target.value)}
|
||||||
|
className="mt-1"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="message" className="text-sm font-medium">
|
||||||
|
Message <span className="text-red-500">*</span>
|
||||||
|
</Label>
|
||||||
|
<Textarea
|
||||||
|
id="message"
|
||||||
|
placeholder="Décrivez votre question, suggestion ou remarque en détail..."
|
||||||
|
value={newMessage}
|
||||||
|
onChange={(e) => setNewMessage(e.target.value)}
|
||||||
|
rows={5}
|
||||||
|
className="mt-1 resize-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button variant="outline" onClick={() => setNewDialogOpen(false)}>
|
||||||
|
Annuler
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
if (!newTitre.trim() || !newMessage.trim()) {
|
||||||
|
toast.error("Veuillez remplir tous les champs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
createMutation.mutate({ titre: newTitre.trim(), message: newMessage.trim() });
|
||||||
|
}}
|
||||||
|
disabled={createMutation.isPending}
|
||||||
|
className="bg-[#0a2463] hover:bg-[#0a2463]/90 text-white gap-2"
|
||||||
|
>
|
||||||
|
<Send className="h-4 w-4" />
|
||||||
|
{createMutation.isPending ? "Envoi..." : "Envoyer"}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
{/* Dialog : Réponse admin */}
|
||||||
|
<Dialog open={reponseDialogOpen} onOpenChange={setReponseDialogOpen}>
|
||||||
|
<DialogContent className="max-w-lg">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle className="flex items-center gap-2">
|
||||||
|
<MessageSquare className="h-5 w-5 text-[#0a2463]" />
|
||||||
|
Répondre à la demande
|
||||||
|
</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
{selectedIdea && (
|
||||||
|
<div className="space-y-4 py-2">
|
||||||
|
{/* Rappel de la demande */}
|
||||||
|
<div className="bg-slate-50 rounded-lg p-3">
|
||||||
|
<p className="text-xs font-medium text-slate-500 mb-1">Demande de {selectedIdea.userName}</p>
|
||||||
|
<p className="text-sm font-semibold text-slate-800">{selectedIdea.titre}</p>
|
||||||
|
<p className="text-xs text-slate-600 mt-1 line-clamp-2">{selectedIdea.message}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Statut */}
|
||||||
|
<div>
|
||||||
|
<Label className="text-sm font-medium">Statut</Label>
|
||||||
|
<Select
|
||||||
|
value={reponseStatut}
|
||||||
|
onValueChange={(v) => setReponseStatut(v as Statut)}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="mt-1">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="ouvert">Ouvert</SelectItem>
|
||||||
|
<SelectItem value="en_cours">En cours</SelectItem>
|
||||||
|
<SelectItem value="resolu">Résolu</SelectItem>
|
||||||
|
<SelectItem value="ferme">Fermé</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Réponse */}
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="reponse" className="text-sm font-medium">
|
||||||
|
Réponse <span className="text-red-500">*</span>
|
||||||
|
</Label>
|
||||||
|
<Textarea
|
||||||
|
id="reponse"
|
||||||
|
placeholder="Rédigez votre réponse..."
|
||||||
|
value={reponseText}
|
||||||
|
onChange={(e) => setReponseText(e.target.value)}
|
||||||
|
rows={5}
|
||||||
|
className="mt-1 resize-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<DialogFooter>
|
||||||
|
<Button variant="outline" onClick={() => setReponseDialogOpen(false)}>
|
||||||
|
Annuler
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
if (!reponseText.trim()) {
|
||||||
|
toast.error("Veuillez rédiger une réponse");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reponseMutation.mutate({
|
||||||
|
id: selectedIdea!.id,
|
||||||
|
reponseAdmin: reponseText.trim(),
|
||||||
|
statut: reponseStatut,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
disabled={reponseMutation.isPending}
|
||||||
|
className="bg-[#0a2463] hover:bg-[#0a2463]/90 text-white gap-2"
|
||||||
|
>
|
||||||
|
<Send className="h-4 w-4" />
|
||||||
|
{reponseMutation.isPending ? "Envoi..." : "Enregistrer la réponse"}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import { Button } from "@/components/ui/button";
|
|||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
@@ -26,6 +26,7 @@ import {
|
|||||||
Eye,
|
Eye,
|
||||||
CheckCircle,
|
CheckCircle,
|
||||||
XCircle,
|
XCircle,
|
||||||
|
AtSign,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
@@ -37,7 +38,8 @@ type Role = "admin" | "user" | "readonly";
|
|||||||
interface LocalUser {
|
interface LocalUser {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
email: string;
|
username: string | null;
|
||||||
|
email: string | null;
|
||||||
role: Role;
|
role: Role;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
@@ -64,13 +66,21 @@ const ROLE_ICONS: Record<Role, React.ReactNode> = {
|
|||||||
|
|
||||||
interface UserFormData {
|
interface UserFormData {
|
||||||
name: string;
|
name: string;
|
||||||
|
username: string;
|
||||||
email: string;
|
email: string;
|
||||||
password: string;
|
password: string;
|
||||||
role: Role;
|
role: Role;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_FORM: UserFormData = { name: "", email: "", password: "", role: "user", isActive: true };
|
const DEFAULT_FORM: UserFormData = {
|
||||||
|
name: "",
|
||||||
|
username: "",
|
||||||
|
email: "",
|
||||||
|
password: "",
|
||||||
|
role: "user",
|
||||||
|
isActive: true,
|
||||||
|
};
|
||||||
|
|
||||||
export default function UsersAdmin() {
|
export default function UsersAdmin() {
|
||||||
const [showDialog, setShowDialog] = useState(false);
|
const [showDialog, setShowDialog] = useState(false);
|
||||||
@@ -118,7 +128,14 @@ export default function UsersAdmin() {
|
|||||||
|
|
||||||
const openEdit = (user: LocalUser) => {
|
const openEdit = (user: LocalUser) => {
|
||||||
setEditingUser(user);
|
setEditingUser(user);
|
||||||
setForm({ name: user.name, email: user.email, password: "", role: user.role, isActive: user.isActive });
|
setForm({
|
||||||
|
name: user.name,
|
||||||
|
username: user.username ?? "",
|
||||||
|
email: user.email ?? "",
|
||||||
|
password: "",
|
||||||
|
role: user.role,
|
||||||
|
isActive: user.isActive,
|
||||||
|
});
|
||||||
setShowDialog(true);
|
setShowDialog(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -127,14 +144,21 @@ export default function UsersAdmin() {
|
|||||||
const data: Parameters<typeof updateMutation.mutate>[0] = {
|
const data: Parameters<typeof updateMutation.mutate>[0] = {
|
||||||
id: editingUser.id,
|
id: editingUser.id,
|
||||||
name: form.name,
|
name: form.name,
|
||||||
email: form.email,
|
username: form.username || undefined,
|
||||||
|
email: form.email || undefined,
|
||||||
role: form.role,
|
role: form.role,
|
||||||
isActive: form.isActive,
|
isActive: form.isActive,
|
||||||
};
|
};
|
||||||
if (form.password) data.password = form.password;
|
if (form.password) data.password = form.password;
|
||||||
updateMutation.mutate(data);
|
updateMutation.mutate(data);
|
||||||
} else {
|
} else {
|
||||||
createMutation.mutate({ name: form.name, email: form.email, password: form.password, role: form.role });
|
createMutation.mutate({
|
||||||
|
name: form.name,
|
||||||
|
username: form.username || undefined,
|
||||||
|
email: form.email || undefined,
|
||||||
|
password: form.password,
|
||||||
|
role: form.role,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -149,7 +173,9 @@ export default function UsersAdmin() {
|
|||||||
<Users size={22} className="text-primary" />
|
<Users size={22} className="text-primary" />
|
||||||
<h1 className="text-2xl font-bold text-foreground">Gestion des utilisateurs</h1>
|
<h1 className="text-2xl font-bold text-foreground">Gestion des utilisateurs</h1>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-muted-foreground text-sm">{users.length} utilisateur{users.length !== 1 ? "s" : ""} enregistré{users.length !== 1 ? "s" : ""}</p>
|
<p className="text-muted-foreground text-sm">
|
||||||
|
{users.length} utilisateur{users.length !== 1 ? "s" : ""} enregistré{users.length !== 1 ? "s" : ""}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<Button onClick={openCreate} className="gap-2">
|
<Button onClick={openCreate} className="gap-2">
|
||||||
<Plus size={15} />
|
<Plus size={15} />
|
||||||
@@ -175,7 +201,7 @@ export default function UsersAdmin() {
|
|||||||
<thead>
|
<thead>
|
||||||
<tr className="border-b border-border bg-muted/30">
|
<tr className="border-b border-border bg-muted/30">
|
||||||
<th className="text-left px-4 py-3 font-semibold text-muted-foreground">Nom</th>
|
<th className="text-left px-4 py-3 font-semibold text-muted-foreground">Nom</th>
|
||||||
<th className="text-left px-4 py-3 font-semibold text-muted-foreground">Email</th>
|
<th className="text-left px-4 py-3 font-semibold text-muted-foreground">Identifiant / Email</th>
|
||||||
<th className="text-left px-4 py-3 font-semibold text-muted-foreground w-36">Rôle</th>
|
<th className="text-left px-4 py-3 font-semibold text-muted-foreground w-36">Rôle</th>
|
||||||
<th className="text-left px-4 py-3 font-semibold text-muted-foreground w-24">Statut</th>
|
<th className="text-left px-4 py-3 font-semibold text-muted-foreground w-24">Statut</th>
|
||||||
<th className="text-left px-4 py-3 font-semibold text-muted-foreground w-32">Dernière connexion</th>
|
<th className="text-left px-4 py-3 font-semibold text-muted-foreground w-32">Dernière connexion</th>
|
||||||
@@ -193,7 +219,25 @@ export default function UsersAdmin() {
|
|||||||
<span className="font-medium text-foreground">{user.name}</span>
|
<span className="font-medium text-foreground">{user.name}</span>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-3 text-muted-foreground">{user.email}</td>
|
<td className="px-4 py-3">
|
||||||
|
<div className="flex flex-col gap-0.5">
|
||||||
|
{user.username && (
|
||||||
|
<span className="inline-flex items-center gap-1 text-xs font-mono bg-violet-50 text-violet-700 border border-violet-200 rounded px-1.5 py-0.5 w-fit">
|
||||||
|
<User size={10} />
|
||||||
|
{user.username}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{user.email && (
|
||||||
|
<span className="inline-flex items-center gap-1 text-xs text-muted-foreground">
|
||||||
|
<AtSign size={10} />
|
||||||
|
{user.email}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{!user.username && !user.email && (
|
||||||
|
<span className="text-xs text-muted-foreground italic">—</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
<Badge variant="outline" className={cn("text-xs gap-1", ROLE_COLORS[user.role])}>
|
<Badge variant="outline" className={cn("text-xs gap-1", ROLE_COLORS[user.role])}>
|
||||||
{ROLE_ICONS[user.role]}
|
{ROLE_ICONS[user.role]}
|
||||||
@@ -218,10 +262,20 @@ export default function UsersAdmin() {
|
|||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
<div className="flex items-center gap-1">
|
<div className="flex items-center gap-1">
|
||||||
<Button variant="ghost" size="icon" className="w-7 h-7 text-muted-foreground hover:text-foreground" onClick={() => openEdit(user)}>
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="w-7 h-7 text-muted-foreground hover:text-foreground"
|
||||||
|
onClick={() => openEdit(user)}
|
||||||
|
>
|
||||||
<Pencil size={13} />
|
<Pencil size={13} />
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="ghost" size="icon" className="w-7 h-7 text-muted-foreground hover:text-destructive" onClick={() => setDeleteId(user.id)}>
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="w-7 h-7 text-muted-foreground hover:text-destructive"
|
||||||
|
onClick={() => setDeleteId(user.id)}
|
||||||
|
>
|
||||||
<Trash2 size={13} />
|
<Trash2 size={13} />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
@@ -241,23 +295,65 @@ export default function UsersAdmin() {
|
|||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>{editingUser ? "Modifier l'utilisateur" : "Nouvel utilisateur"}</DialogTitle>
|
<DialogTitle>{editingUser ? "Modifier l'utilisateur" : "Nouvel utilisateur"}</DialogTitle>
|
||||||
<DialogDescription>
|
<DialogDescription>
|
||||||
{editingUser ? "Modifiez les informations de l'utilisateur" : "Créez un nouveau compte utilisateur"}
|
{editingUser
|
||||||
|
? "Modifiez les informations de l'utilisateur"
|
||||||
|
: "Créez un nouveau compte. L'identifiant ou l'e-mail servira à la connexion."}
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<div className="space-y-4 py-2">
|
<div className="space-y-4 py-2">
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
{/* Nom complet */}
|
||||||
<div className="space-y-2 col-span-2">
|
<div className="space-y-2 col-span-2">
|
||||||
<Label>Nom complet</Label>
|
<Label>Nom complet <span className="text-destructive">*</span></Label>
|
||||||
<Input placeholder="Jean Dupont" value={form.name} onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))} />
|
<Input
|
||||||
|
placeholder="Jean Dupont"
|
||||||
|
value={form.name}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Identifiant */}
|
||||||
<div className="space-y-2 col-span-2">
|
<div className="space-y-2 col-span-2">
|
||||||
<Label>Adresse e-mail</Label>
|
<Label>
|
||||||
<Input type="email" placeholder="jean@itinova.fr" value={form.email} onChange={(e) => setForm((f) => ({ ...f, email: e.target.value }))} />
|
Identifiant (username)
|
||||||
|
<span className="ml-1 text-xs text-muted-foreground">(optionnel)</span>
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
placeholder="jean.dupont"
|
||||||
|
value={form.username}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, username: e.target.value }))}
|
||||||
|
/>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
Permet la connexion sans e-mail. Ex : <code>adminItinova</code>
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Email */}
|
||||||
<div className="space-y-2 col-span-2">
|
<div className="space-y-2 col-span-2">
|
||||||
<Label>{editingUser ? "Nouveau mot de passe (laisser vide pour ne pas changer)" : "Mot de passe"}</Label>
|
<Label>
|
||||||
<Input type="password" placeholder="••••••••" value={form.password} onChange={(e) => setForm((f) => ({ ...f, password: e.target.value }))} />
|
Adresse e-mail
|
||||||
|
<span className="ml-1 text-xs text-muted-foreground">(optionnel)</span>
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
type="email"
|
||||||
|
placeholder="jean@itinova.fr"
|
||||||
|
value={form.email}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, email: e.target.value }))}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Mot de passe */}
|
||||||
|
<div className="space-y-2 col-span-2">
|
||||||
|
<Label>
|
||||||
|
{editingUser
|
||||||
|
? "Nouveau mot de passe (laisser vide pour ne pas changer)"
|
||||||
|
: <>Mot de passe <span className="text-destructive">*</span></>}
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder="••••••••"
|
||||||
|
value={form.password}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, password: e.target.value }))}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/* Rôle */}
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Rôle</Label>
|
<Label>Rôle</Label>
|
||||||
<Select value={form.role} onValueChange={(v) => setForm((f) => ({ ...f, role: v as Role }))}>
|
<Select value={form.role} onValueChange={(v) => setForm((f) => ({ ...f, role: v as Role }))}>
|
||||||
@@ -271,11 +367,15 @@ export default function UsersAdmin() {
|
|||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Statut (modification uniquement) */}
|
||||||
{editingUser && (
|
{editingUser && (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Statut</Label>
|
<Label>Statut</Label>
|
||||||
<div className="flex items-center gap-2 pt-2">
|
<div className="flex items-center gap-2 pt-2">
|
||||||
<Switch checked={form.isActive} onCheckedChange={(v) => setForm((f) => ({ ...f, isActive: v }))} />
|
<Switch
|
||||||
|
checked={form.isActive}
|
||||||
|
onCheckedChange={(v) => setForm((f) => ({ ...f, isActive: v }))}
|
||||||
|
/>
|
||||||
<span className="text-sm">{form.isActive ? "Actif" : "Inactif"}</span>
|
<span className="text-sm">{form.isActive ? "Actif" : "Inactif"}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -283,9 +383,16 @@ export default function UsersAdmin() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button variant="outline" onClick={() => setShowDialog(false)}>Annuler</Button>
|
<Button variant="outline" onClick={() => setShowDialog(false)}>
|
||||||
<Button onClick={handleSubmit} disabled={createMutation.isPending || updateMutation.isPending}>
|
Annuler
|
||||||
{(createMutation.isPending || updateMutation.isPending) && <Loader2 size={14} className="animate-spin mr-2" />}
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={createMutation.isPending || updateMutation.isPending}
|
||||||
|
>
|
||||||
|
{(createMutation.isPending || updateMutation.isPending) && (
|
||||||
|
<Loader2 size={14} className="animate-spin mr-2" />
|
||||||
|
)}
|
||||||
{editingUser ? "Enregistrer" : "Créer"}
|
{editingUser ? "Enregistrer" : "Créer"}
|
||||||
</Button>
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
@@ -297,11 +404,19 @@ export default function UsersAdmin() {
|
|||||||
<DialogContent className="sm:max-w-sm">
|
<DialogContent className="sm:max-w-sm">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Supprimer l'utilisateur</DialogTitle>
|
<DialogTitle>Supprimer l'utilisateur</DialogTitle>
|
||||||
<DialogDescription>Cette action est irréversible. L'utilisateur ne pourra plus se connecter.</DialogDescription>
|
<DialogDescription>
|
||||||
|
Cette action est irréversible. L'utilisateur ne pourra plus se connecter.
|
||||||
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button variant="outline" onClick={() => setDeleteId(null)}>Annuler</Button>
|
<Button variant="outline" onClick={() => setDeleteId(null)}>
|
||||||
<Button variant="destructive" onClick={() => deleteId && deleteMutation.mutate({ id: deleteId })} disabled={deleteMutation.isPending}>
|
Annuler
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
onClick={() => deleteId && deleteMutation.mutate({ id: deleteId })}
|
||||||
|
disabled={deleteMutation.isPending}
|
||||||
|
>
|
||||||
{deleteMutation.isPending && <Loader2 size={14} className="animate-spin mr-2" />}
|
{deleteMutation.isPending && <Loader2 size={14} className="animate-spin mr-2" />}
|
||||||
Supprimer
|
Supprimer
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ export type InsertUser = typeof users.$inferInsert;
|
|||||||
export const localUsers = mysqlTable("local_users", {
|
export const localUsers = mysqlTable("local_users", {
|
||||||
id: int("id").autoincrement().primaryKey(),
|
id: int("id").autoincrement().primaryKey(),
|
||||||
name: varchar("name", { length: 255 }).notNull(),
|
name: varchar("name", { length: 255 }).notNull(),
|
||||||
email: varchar("email", { length: 320 }).notNull().unique(),
|
username: varchar("username", { length: 128 }).unique(),
|
||||||
|
email: varchar("email", { length: 320 }),
|
||||||
passwordHash: varchar("passwordHash", { length: 255 }).notNull(),
|
passwordHash: varchar("passwordHash", { length: 255 }).notNull(),
|
||||||
role: mysqlEnum("role", ["admin", "user", "readonly"]).default("user").notNull(),
|
role: mysqlEnum("role", ["admin", "user", "readonly"]).default("user").notNull(),
|
||||||
isActive: boolean("isActive").default(true).notNull(),
|
isActive: boolean("isActive").default(true).notNull(),
|
||||||
@@ -115,3 +116,22 @@ export const importLogs = mysqlTable("import_logs", {
|
|||||||
|
|
||||||
export type ImportLog = typeof importLogs.$inferSelect;
|
export type ImportLog = typeof importLogs.$inferSelect;
|
||||||
export type InsertImportLog = typeof importLogs.$inferInsert;
|
export type InsertImportLog = typeof importLogs.$inferInsert;
|
||||||
|
|
||||||
|
// ─── Boîte à idées ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export const ideas = mysqlTable("ideas", {
|
||||||
|
id: int("id").autoincrement().primaryKey(),
|
||||||
|
userId: int("userId").notNull(),
|
||||||
|
userName: varchar("userName", { length: 255 }).notNull(),
|
||||||
|
titre: varchar("titre", { length: 512 }).notNull(),
|
||||||
|
message: text("message").notNull(),
|
||||||
|
statut: mysqlEnum("statut", ["ouvert", "en_cours", "resolu", "ferme"]).default("ouvert").notNull(),
|
||||||
|
reponseAdmin: text("reponseAdmin"),
|
||||||
|
reponduPar: varchar("reponduPar", { length: 255 }),
|
||||||
|
reponduAt: timestamp("reponduAt"),
|
||||||
|
createdAt: timestamp("createdAt").defaultNow().notNull(),
|
||||||
|
updatedAt: timestamp("updatedAt").defaultNow().onUpdateNow().notNull(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type Idea = typeof ideas.$inferSelect;
|
||||||
|
export type InsertIdea = typeof ideas.$inferInsert;
|
||||||
|
|||||||
45
server/db.ts
45
server/db.ts
@@ -9,6 +9,8 @@ import {
|
|||||||
appSettings,
|
appSettings,
|
||||||
importLogs,
|
importLogs,
|
||||||
InsertLocalUser,
|
InsertLocalUser,
|
||||||
|
ideas,
|
||||||
|
InsertIdea,
|
||||||
} from "../drizzle/schema";
|
} from "../drizzle/schema";
|
||||||
import { ENV } from "./_core/env";
|
import { ENV } from "./_core/env";
|
||||||
|
|
||||||
@@ -70,6 +72,7 @@ export async function getLocalUsers() {
|
|||||||
.select({
|
.select({
|
||||||
id: localUsers.id,
|
id: localUsers.id,
|
||||||
name: localUsers.name,
|
name: localUsers.name,
|
||||||
|
username: localUsers.username,
|
||||||
email: localUsers.email,
|
email: localUsers.email,
|
||||||
role: localUsers.role,
|
role: localUsers.role,
|
||||||
isActive: localUsers.isActive,
|
isActive: localUsers.isActive,
|
||||||
@@ -315,3 +318,45 @@ export async function getImportStats() {
|
|||||||
totalNewRows,
|
totalNewRows,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Boîte à idées ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export async function createIdea(data: InsertIdea) {
|
||||||
|
const db = await getDb();
|
||||||
|
if (!db) throw new Error("Database not available");
|
||||||
|
await db.insert(ideas).values(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getAllIdeas() {
|
||||||
|
const db = await getDb();
|
||||||
|
if (!db) return [];
|
||||||
|
return db.select().from(ideas).orderBy(desc(ideas.createdAt));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getIdeasByUser(userId: number) {
|
||||||
|
const db = await getDb();
|
||||||
|
if (!db) return [];
|
||||||
|
return db.select().from(ideas).where(eq(ideas.userId, userId)).orderBy(desc(ideas.createdAt));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function repondreIdea(
|
||||||
|
id: number,
|
||||||
|
reponseAdmin: string,
|
||||||
|
reponduPar: string,
|
||||||
|
statut: "ouvert" | "en_cours" | "resolu" | "ferme"
|
||||||
|
) {
|
||||||
|
const db = await getDb();
|
||||||
|
if (!db) throw new Error("Database not available");
|
||||||
|
await db.update(ideas).set({
|
||||||
|
reponseAdmin,
|
||||||
|
reponduPar,
|
||||||
|
reponduAt: new Date(),
|
||||||
|
statut,
|
||||||
|
}).where(eq(ideas.id, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateIdeaStatut(id: number, statut: "ouvert" | "en_cours" | "resolu" | "ferme") {
|
||||||
|
const db = await getDb();
|
||||||
|
if (!db) throw new Error("Database not available");
|
||||||
|
await db.update(ideas).set({ statut }).where(eq(ideas.id, id));
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { ENV } from "./_core/env";
|
|||||||
|
|
||||||
const SALT_ROUNDS = 12;
|
const SALT_ROUNDS = 12;
|
||||||
const JWT_EXPIRY = "7d";
|
const JWT_EXPIRY = "7d";
|
||||||
const LOCAL_AUTH_COOKIE = "veille_local_auth";
|
export const LOCAL_AUTH_COOKIE = "veille_local_auth";
|
||||||
|
|
||||||
export async function hashPassword(password: string): Promise<string> {
|
export async function hashPassword(password: string): Promise<string> {
|
||||||
return bcrypt.hash(password, SALT_ROUNDS);
|
return bcrypt.hash(password, SALT_ROUNDS);
|
||||||
@@ -37,24 +37,30 @@ export async function verifyLocalToken(token: string): Promise<{ userId: number;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loginLocalUser(email: string, password: string) {
|
/**
|
||||||
|
* Connexion par username OU email (insensible à la casse pour l'email).
|
||||||
|
* Le champ `identifier` peut être un nom d'utilisateur libre ou une adresse e-mail.
|
||||||
|
*/
|
||||||
|
export async function loginLocalUser(identifier: string, password: string) {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
if (!db) throw new Error("Base de données indisponible");
|
if (!db) throw new Error("Base de données indisponible");
|
||||||
|
|
||||||
// Recherche par e-mail (insensible à la casse) OU par identifiant exact
|
const id = identifier.trim();
|
||||||
const identifier = email.trim();
|
|
||||||
const users = await db
|
// Cherche d'abord par username exact, puis par email (insensible à la casse)
|
||||||
|
const results = await db
|
||||||
.select()
|
.select()
|
||||||
.from(localUsers)
|
.from(localUsers)
|
||||||
.where(
|
.where(
|
||||||
or(
|
or(
|
||||||
eq(localUsers.email, identifier.toLowerCase()),
|
eq(localUsers.username, id),
|
||||||
eq(localUsers.email, identifier)
|
eq(localUsers.email, id.toLowerCase()),
|
||||||
|
eq(localUsers.email, id)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
const user = users[0];
|
const user = results[0];
|
||||||
if (!user || !user.isActive) {
|
if (!user || !user.isActive) {
|
||||||
throw new Error("Identifiants incorrects ou compte désactivé");
|
throw new Error("Identifiants incorrects ou compte désactivé");
|
||||||
}
|
}
|
||||||
@@ -62,21 +68,29 @@ export async function loginLocalUser(email: string, password: string) {
|
|||||||
const valid = await verifyPassword(password, user.passwordHash);
|
const valid = await verifyPassword(password, user.passwordHash);
|
||||||
if (!valid) throw new Error("Identifiants incorrects ou compte désactivé");
|
if (!valid) throw new Error("Identifiants incorrects ou compte désactivé");
|
||||||
|
|
||||||
// Mise à jour lastSignedIn
|
|
||||||
await db
|
await db
|
||||||
.update(localUsers)
|
.update(localUsers)
|
||||||
.set({ lastSignedIn: new Date() })
|
.set({ lastSignedIn: new Date() })
|
||||||
.where(eq(localUsers.id, user.id));
|
.where(eq(localUsers.id, user.id));
|
||||||
|
|
||||||
const token = await generateLocalToken(user.id, user.role);
|
const token = await generateLocalToken(user.id, user.role);
|
||||||
return { token, user: { id: user.id, name: user.name, email: user.email, role: user.role } };
|
return {
|
||||||
|
token,
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
name: user.name,
|
||||||
|
username: user.username ?? null,
|
||||||
|
email: user.email ?? null,
|
||||||
|
role: user.role,
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLocalUserById(id: number) {
|
export async function getLocalUserById(id: number) {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
if (!db) return null;
|
if (!db) return null;
|
||||||
const users = await db.select().from(localUsers).where(eq(localUsers.id, id)).limit(1);
|
const results = await db.select().from(localUsers).where(eq(localUsers.id, id)).limit(1);
|
||||||
return users[0] ?? null;
|
return results[0] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function ensureAdminExists() {
|
export async function ensureAdminExists() {
|
||||||
@@ -93,11 +107,12 @@ export async function ensureAdminExists() {
|
|||||||
const hash = await hashPassword("Admin@Itinova2024!");
|
const hash = await hashPassword("Admin@Itinova2024!");
|
||||||
await db.insert(localUsers).values({
|
await db.insert(localUsers).values({
|
||||||
name: "Administrateur",
|
name: "Administrateur",
|
||||||
|
username: "admin",
|
||||||
email: "admin@itinova.fr",
|
email: "admin@itinova.fr",
|
||||||
passwordHash: hash,
|
passwordHash: hash,
|
||||||
role: "admin",
|
role: "admin",
|
||||||
isActive: true,
|
isActive: true,
|
||||||
});
|
});
|
||||||
console.log("[LocalAuth] Compte admin par défaut créé : admin@itinova.fr / Admin@Itinova2024!");
|
console.log("[LocalAuth] Compte admin par défaut créé : admin / Admin@Itinova2024!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ import {
|
|||||||
createLocalUser,
|
createLocalUser,
|
||||||
updateLocalUser,
|
updateLocalUser,
|
||||||
deleteLocalUser,
|
deleteLocalUser,
|
||||||
|
createIdea,
|
||||||
|
getAllIdeas,
|
||||||
|
getIdeasByUser,
|
||||||
|
repondreIdea,
|
||||||
|
updateIdeaStatut,
|
||||||
} from "./db";
|
} from "./db";
|
||||||
import { importVeille, importAAP, runFullImport, getImportConfig } from "./importer";
|
import { importVeille, importAAP, runFullImport, getImportConfig } from "./importer";
|
||||||
import { loginLocalUser, hashPassword, ensureAdminExists } from "./localAuth";
|
import { loginLocalUser, hashPassword, ensureAdminExists } from "./localAuth";
|
||||||
@@ -190,7 +195,8 @@ export const appRouter = router({
|
|||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
name: z.string().min(2).max(255),
|
name: z.string().min(2).max(255),
|
||||||
email: z.string().email(),
|
username: z.string().min(2).max(128).optional(),
|
||||||
|
email: z.string().email().optional(),
|
||||||
password: z.string().min(8),
|
password: z.string().min(8),
|
||||||
role: z.enum(["admin", "user", "readonly"]).default("user"),
|
role: z.enum(["admin", "user", "readonly"]).default("user"),
|
||||||
})
|
})
|
||||||
@@ -199,7 +205,8 @@ export const appRouter = router({
|
|||||||
const passwordHash = await hashPassword(input.password);
|
const passwordHash = await hashPassword(input.password);
|
||||||
await createLocalUser({
|
await createLocalUser({
|
||||||
name: input.name,
|
name: input.name,
|
||||||
email: input.email.toLowerCase(),
|
username: input.username ?? null,
|
||||||
|
email: input.email ? input.email.toLowerCase() : null,
|
||||||
passwordHash,
|
passwordHash,
|
||||||
role: input.role,
|
role: input.role,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
@@ -212,6 +219,7 @@ export const appRouter = router({
|
|||||||
z.object({
|
z.object({
|
||||||
id: z.number().int().positive(),
|
id: z.number().int().positive(),
|
||||||
name: z.string().min(2).max(255).optional(),
|
name: z.string().min(2).max(255).optional(),
|
||||||
|
username: z.string().min(2).max(128).optional(),
|
||||||
email: z.string().email().optional(),
|
email: z.string().email().optional(),
|
||||||
password: z.string().min(8).optional(),
|
password: z.string().min(8).optional(),
|
||||||
role: z.enum(["admin", "user", "readonly"]).optional(),
|
role: z.enum(["admin", "user", "readonly"]).optional(),
|
||||||
@@ -238,6 +246,67 @@ export const appRouter = router({
|
|||||||
return { success: true };
|
return { success: true };
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
// ─── Boîte à idées ───────────────────────────────────────────────────────────
|
||||||
|
ideas: router({
|
||||||
|
// Créer une nouvelle idée / question
|
||||||
|
create: protectedProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
titre: z.string().min(3).max(512),
|
||||||
|
message: z.string().min(10),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
await createIdea({
|
||||||
|
userId: ctx.user.id,
|
||||||
|
userName: ctx.user.name ?? ctx.user.email ?? "Utilisateur",
|
||||||
|
titre: input.titre,
|
||||||
|
message: input.message,
|
||||||
|
});
|
||||||
|
return { success: true };
|
||||||
|
}),
|
||||||
|
|
||||||
|
// Lister toutes les idées (admin) ou les siennes (user)
|
||||||
|
list: protectedProcedure.query(async ({ ctx }) => {
|
||||||
|
if (ctx.user.role === "admin") {
|
||||||
|
return getAllIdeas();
|
||||||
|
}
|
||||||
|
return getIdeasByUser(ctx.user.id);
|
||||||
|
}),
|
||||||
|
|
||||||
|
// Répondre à une idée (admin uniquement)
|
||||||
|
repondre: adminProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
id: z.number().int().positive(),
|
||||||
|
reponseAdmin: z.string().min(1),
|
||||||
|
statut: z.enum(["ouvert", "en_cours", "resolu", "ferme"]),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
await repondreIdea(
|
||||||
|
input.id,
|
||||||
|
input.reponseAdmin,
|
||||||
|
ctx.user.name ?? ctx.user.email ?? "Admin",
|
||||||
|
input.statut
|
||||||
|
);
|
||||||
|
return { success: true };
|
||||||
|
}),
|
||||||
|
|
||||||
|
// Changer le statut (admin uniquement)
|
||||||
|
updateStatut: adminProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
id: z.number().int().positive(),
|
||||||
|
statut: z.enum(["ouvert", "en_cours", "resolu", "ferme"]),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.mutation(async ({ input }) => {
|
||||||
|
await updateIdeaStatut(input.id, input.statut);
|
||||||
|
return { success: true };
|
||||||
|
}),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type AppRouter = typeof appRouter;
|
export type AppRouter = typeof appRouter;
|
||||||
|
|||||||
Reference in New Issue
Block a user