Compare commits
49 Commits
a1755e181c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7c522b18e | ||
|
|
071376c9b1 | ||
|
|
582ca77f28 | ||
|
|
da19629ee0 | ||
|
|
0b2f1b781d | ||
|
|
f53bb82256 | ||
|
|
4ad6cb2705 | ||
|
|
d692e228de | ||
|
|
9997e383cc | ||
|
|
5128322a6d | ||
|
|
43fc8d523c | ||
|
|
cd73dd3d58 | ||
|
|
28a00cc771 | ||
|
|
e2d067ff4b | ||
|
|
0b01ebe331 | ||
|
|
a9a2a4b312 | ||
|
|
deb5b5735c | ||
|
|
a0c440dfae | ||
|
|
a88b342a0d | ||
|
|
7e1253615b | ||
|
|
1a7d5cb64e | ||
|
|
0536035303 | ||
|
|
d7677df41b | ||
|
|
d9c1130359 | ||
|
|
401659ec99 | ||
|
|
3528fc6905 | ||
|
|
8b47a78e4a | ||
|
|
a281212bab | ||
|
|
b603541adf | ||
|
|
1b3e3cdd67 | ||
|
|
e4328ab652 | ||
|
|
692fbbe912 | ||
|
|
ece737e11d | ||
|
|
75e7a9256c | ||
|
|
52b5e49082 | ||
|
|
d27703f878 | ||
|
|
098d707289 | ||
|
|
66c4940aba | ||
|
|
147dd6e5a0 | ||
|
|
b7525ab51e | ||
|
|
d729a94a96 | ||
|
|
957494f8ef | ||
|
|
269823fd78 | ||
|
|
295bb26378 | ||
|
|
de797c1c0b | ||
|
|
de76a761a3 | ||
|
|
8759d85f3d | ||
|
|
89c65ca979 | ||
|
|
eecbd07b5c |
17
Dockerfile
17
Dockerfile
@@ -29,7 +29,8 @@ RUN pnpm install --frozen-lockfile
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Build frontend + backend
|
||||
# Build frontend + backend. Les dépendances de développement restent confinées
|
||||
# au builder et ne sont jamais copiées dans l'image d'exécution.
|
||||
RUN pnpm build
|
||||
|
||||
# ============================================================
|
||||
@@ -46,15 +47,19 @@ RUN apt-get update && apt-get install -y \
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy node_modules from builder (already compiled, including sharp native binaries)
|
||||
COPY --from=builder /app/node_modules ./node_modules
|
||||
ENV NODE_ENV=production
|
||||
|
||||
# Installer uniquement les dépendances d'exécution réduit fortement la taille
|
||||
# de la couche exportée. Les binaires Sharp sont fournis par ses paquets
|
||||
# optionnels de plateforme et ne nécessitent pas de script post-installation.
|
||||
RUN npm install -g pnpm@10.4.1
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
COPY patches/ ./patches/
|
||||
RUN pnpm install --prod --frozen-lockfile --ignore-scripts
|
||||
|
||||
# Copy built assets from builder (vite outputs to dist/public, esbuild to dist/)
|
||||
COPY --from=builder /app/dist ./dist
|
||||
|
||||
# Copy package.json (needed for module resolution)
|
||||
COPY package.json ./
|
||||
|
||||
# Copy drizzle migrations
|
||||
COPY drizzle/ ./drizzle/
|
||||
COPY drizzle.config.ts ./
|
||||
|
||||
2
app.json
2
app.json
@@ -8,7 +8,7 @@
|
||||
},
|
||||
"containerName": "demat-facturation-app",
|
||||
"image": "images/demat-facturation-dsi.jpg",
|
||||
"giteaRepo": "demat-facturation",
|
||||
"giteaRepo": "demat-facturation-dsi",
|
||||
"giteaOwner": "manus-admin",
|
||||
"ci": {
|
||||
"required": true
|
||||
|
||||
@@ -26,6 +26,15 @@ const ImportReport = lazy(() => import("./pages/ImportReport"));
|
||||
const LearningSettings = lazy(() => import("./pages/LearningSettings"));
|
||||
const VentilationFreePro = lazy(() => import("./pages/VentilationFreePro"));
|
||||
const WebImportSources = lazy(() => import("./pages/WebImportSources"));
|
||||
const RealBudget = lazy(() => import("./pages/RealBudget"));
|
||||
|
||||
function SubscriptionInvoices() {
|
||||
return <Invoices scope="subscriptions" />;
|
||||
}
|
||||
|
||||
function AllInvoices() {
|
||||
return <Invoices />;
|
||||
}
|
||||
|
||||
function RouteFallback() {
|
||||
return <div className="min-h-screen bg-background" aria-busy="true" aria-label="Chargement" />;
|
||||
@@ -38,8 +47,10 @@ function Router() {
|
||||
<Route path="/login" component={Login} />
|
||||
<Route path="/dashboard" component={Dashboard} />
|
||||
<Route path="/upload" component={Upload} />
|
||||
<Route path="/invoices" component={Invoices} />
|
||||
<Route path="/invoices" component={AllInvoices} />
|
||||
<Route path="/invoices-bap" component={InvoicesBAP} />
|
||||
<Route path="/invoices-subscriptions" component={SubscriptionInvoices} />
|
||||
<Route path="/real-budget" component={RealBudget} />
|
||||
<Route path="/invoices/:id" component={InvoiceDetail} />
|
||||
<Route path="/settings" component={Settings} />
|
||||
<Route path="/import-settings" component={ImportSettings} />
|
||||
|
||||
101
client/src/components/AzureAdExportSettingsCard.tsx
Normal file
101
client/src/components/AzureAdExportSettingsCard.tsx
Normal file
@@ -0,0 +1,101 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Calendar, CheckCircle2, Cloud, Loader2, Save, Upload, Wifi } from "lucide-react";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { toast } from "sonner";
|
||||
|
||||
/** Les identifiants Azure restent centralisés dans Paramètres, hors des règles métier. */
|
||||
export default function AzureAdExportSettingsCard() {
|
||||
const utils = trpc.useUtils();
|
||||
const { data: settings } = trpc.importSettings.get.useQuery();
|
||||
const [sharepointUrl, setSharepointUrl] = useState("");
|
||||
const [tenantId, setTenantId] = useState("");
|
||||
const [clientId, setClientId] = useState("");
|
||||
const [clientSecret, setClientSecret] = useState("");
|
||||
const [secretExpiresAt, setSecretExpiresAt] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (!settings) return;
|
||||
setSharepointUrl(settings.exportFolder || "");
|
||||
setTenantId(settings.azureTenantId || "");
|
||||
setClientId(settings.azureClientId || "");
|
||||
setSecretExpiresAt(settings.azureSecretExpiresAt ? new Date(settings.azureSecretExpiresAt).toISOString().slice(0, 10) : "");
|
||||
}, [settings]);
|
||||
|
||||
const updateMutation = trpc.importSettings.update.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success("Configuration Microsoft Azure AD enregistrée");
|
||||
setClientSecret("");
|
||||
utils.importSettings.get.invalidate();
|
||||
},
|
||||
onError: (error) => toast.error(error.message || "Impossible d’enregistrer la configuration Azure AD"),
|
||||
});
|
||||
const testConnectionMutation = trpc.importSettings.testAzureConnection.useMutation();
|
||||
const testUploadMutation = trpc.importSettings.testSharePointUpload.useMutation();
|
||||
|
||||
const expiration = useMemo(() => {
|
||||
if (!secretExpiresAt) return null;
|
||||
const date = new Date(`${secretExpiresAt}T12:00:00Z`);
|
||||
if (Number.isNaN(date.getTime())) return null;
|
||||
return Math.ceil((date.getTime() - Date.now()) / 86_400_000);
|
||||
}, [secretExpiresAt]);
|
||||
|
||||
const save = () => {
|
||||
if (!settings) return;
|
||||
if (!sharepointUrl.trim() || !tenantId.trim() || !clientId.trim()) {
|
||||
toast.error("URL SharePoint, Tenant ID et Client ID sont obligatoires");
|
||||
return;
|
||||
}
|
||||
updateMutation.mutate({
|
||||
manualImportEnabled: settings.manualImportEnabled,
|
||||
autoImportEnabled: settings.autoImportEnabled,
|
||||
emailImportEnabled: settings.emailImportEnabled,
|
||||
exportFolder: sharepointUrl.trim(),
|
||||
exportFolderType: "sharepoint",
|
||||
bapExportMode: settings.bapExportMode,
|
||||
azureTenantId: tenantId.trim(),
|
||||
azureClientId: clientId.trim(),
|
||||
azureSecretExpiresAt: secretExpiresAt || null,
|
||||
...(clientSecret ? { azureClientSecret: clientSecret } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
const testConnection = async () => {
|
||||
try {
|
||||
const result = await testConnectionMutation.mutateAsync();
|
||||
result.success ? toast.success(result.message || "Connexion Azure AD réussie") : toast.error(result.error || "Connexion Azure AD impossible");
|
||||
} catch (error: any) {
|
||||
toast.error(error.message || "Connexion Azure AD impossible");
|
||||
}
|
||||
};
|
||||
|
||||
const testUpload = async () => {
|
||||
try {
|
||||
const result = await testUploadMutation.mutateAsync();
|
||||
if (!result.success) {
|
||||
toast.error(result.error || "Upload SharePoint impossible");
|
||||
return;
|
||||
}
|
||||
toast.success("Fichier test déposé dans SharePoint");
|
||||
if (result.webUrl) window.open(result.webUrl, "_blank", "noopener,noreferrer");
|
||||
} catch (error: any) {
|
||||
toast.error(error.message || "Upload SharePoint impossible");
|
||||
}
|
||||
};
|
||||
|
||||
const canTest = Boolean(sharepointUrl && tenantId && clientId);
|
||||
|
||||
return <Card className="border-2 border-cyan-200">
|
||||
<CardHeader className="border-b bg-gradient-to-r from-cyan-50 to-sky-50"><div className="flex items-center gap-3"><div className="rounded-lg bg-cyan-600 p-2"><Cloud className="h-6 w-6 text-white" /></div><div><CardTitle>Configuration Microsoft Azure AD</CardTitle><CardDescription>Accès partagé utilisé par les destinations SharePoint et Teams des automatismes d’export.</CardDescription></div></div></CardHeader>
|
||||
<CardContent className="space-y-4 pt-6">
|
||||
<div className="space-y-2"><Label htmlFor="sharepoint-url">URL SharePoint / chemin</Label><Input id="sharepoint-url" value={sharepointUrl} onChange={(event) => setSharepointUrl(event.target.value)} placeholder="https://…sharepoint.com/:f:/r/sites/…/Documents/Factures" /><p className="text-xs text-muted-foreground">Pour un canal Teams, utilisez l’URL SharePoint du dossier Fichiers concerné.</p></div>
|
||||
<div className="grid gap-4 md:grid-cols-2"><div className="space-y-2"><Label htmlFor="azure-tenant-id">ID de l’annuaire (Tenant ID)</Label><Input id="azure-tenant-id" value={tenantId} onChange={(event) => setTenantId(event.target.value)} /></div><div className="space-y-2"><Label htmlFor="azure-client-id">ID d’application (Client ID)</Label><Input id="azure-client-id" value={clientId} onChange={(event) => setClientId(event.target.value)} /></div></div>
|
||||
<div className="grid gap-4 md:grid-cols-2"><div className="space-y-2"><Label htmlFor="azure-client-secret">Secret client</Label><Input id="azure-client-secret" type="password" value={clientSecret} onChange={(event) => setClientSecret(event.target.value)} placeholder="Conserver le secret configuré" /><p className="text-xs text-muted-foreground">Laissez vide pour conserver le secret enregistré. Permissions requises : Files.ReadWrite.All et Sites.ReadWrite.All.</p></div><div className="space-y-2"><Label htmlFor="azure-secret-expiration">Date d’expiration du secret</Label><Input id="azure-secret-expiration" type="date" value={secretExpiresAt} onChange={(event) => setSecretExpiresAt(event.target.value)} />{expiration !== null && <p className={expiration <= 30 ? "text-xs text-amber-700" : "text-xs text-emerald-700"}><Calendar className="mr-1 inline h-3 w-3" />{expiration < 0 ? "Secret expiré" : `Secret valide encore ${expiration} jours`}</p>}</div></div>
|
||||
<div className="flex flex-wrap gap-2 border-t pt-4"><Button onClick={save} disabled={updateMutation.isPending}><Save className="mr-2 h-4 w-4" />{updateMutation.isPending ? "Enregistrement…" : "Enregistrer Azure AD"}</Button><Button variant="outline" onClick={testConnection} disabled={testConnectionMutation.isPending || !canTest}>{testConnectionMutation.isPending ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <Wifi className="mr-2 h-4 w-4" />}Tester la connexion Azure AD</Button><Button variant="outline" onClick={testUpload} disabled={testUploadMutation.isPending || !canTest}>{testUploadMutation.isPending ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <Upload className="mr-2 h-4 w-4" />}Tester l’upload SharePoint</Button></div>
|
||||
{settings?.azureClientSecret && <p className="flex items-center gap-1 text-xs text-emerald-700"><CheckCircle2 className="h-3.5 w-3.5" />Un secret client est déjà enregistré de manière masquée.</p>}
|
||||
</CardContent>
|
||||
</Card>;
|
||||
}
|
||||
@@ -53,6 +53,8 @@ const menuStructure: MenuItem[] = [
|
||||
{ icon: Upload, label: "Import", path: "/upload" },
|
||||
{ icon: FileText, label: "Factures", path: "/invoices" },
|
||||
{ icon: FileText, label: "Factures BAP", path: "/invoices-bap" },
|
||||
{ icon: FileText, label: "Factures abonnements", path: "/invoices-subscriptions" },
|
||||
{ icon: BarChart2, label: "Budget réel", path: "/real-budget" },
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -69,7 +71,7 @@ const menuStructure: MenuItem[] = [
|
||||
color: "from-orange-500 to-amber-500",
|
||||
children: [
|
||||
{ icon: Settings, label: "Paramètres IA et Signatures", path: "/settings" },
|
||||
{ icon: Download, label: "Paramètres import / export", path: "/import-settings" },
|
||||
{ icon: Download, label: "Paramètres", path: "/import-settings" },
|
||||
{ icon: List, label: "Administration des listes", path: "/lists-admin" },
|
||||
{ icon: Zap, label: "Automatismes", path: "/automation-rules" },
|
||||
{ icon: Brain, label: "Apprentissages IA", path: "/learning-settings" },
|
||||
|
||||
177
client/src/components/ExportAutomationRulesPanel.tsx
Normal file
177
client/src/components/ExportAutomationRulesPanel.tsx
Normal file
@@ -0,0 +1,177 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { FolderOpen, Monitor, Pencil, Plus, Power, PowerOff, Save, Trash2 } from "lucide-react";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { toast } from "sonner";
|
||||
|
||||
type ConditionField = "recipientName" | "ventilationComptable";
|
||||
type DestinationType = "local" | "teams" | "sharepoint";
|
||||
|
||||
const LABELS: Record<ConditionField, string> = {
|
||||
recipientName: "Destinataire",
|
||||
ventilationComptable: "Ventilation",
|
||||
};
|
||||
|
||||
const DESTINATION_LABELS: Record<DestinationType, string> = {
|
||||
local: "Dossier local",
|
||||
teams: "Teams",
|
||||
sharepoint: "SharePoint",
|
||||
};
|
||||
|
||||
export default function ExportAutomationRulesPanel() {
|
||||
const utils = trpc.useUtils();
|
||||
const { data: rules = [], isLoading } = trpc.exportAutomationRules.list.useQuery();
|
||||
const { data: invoices = [] } = trpc.invoices.list.useQuery();
|
||||
const { data: importSettings } = trpc.importSettings.get.useQuery();
|
||||
const [editingId, setEditingId] = useState<number | null>(null);
|
||||
const [name, setName] = useState("");
|
||||
const [conditionField, setConditionField] = useState<ConditionField>("recipientName");
|
||||
const [conditionValue, setConditionValue] = useState("");
|
||||
const [destinationType, setDestinationType] = useState<DestinationType>("local");
|
||||
const [destinationPath, setDestinationPath] = useState("");
|
||||
const [openInBrowser, setOpenInBrowser] = useState(false);
|
||||
const [defaultExportMode, setDefaultExportMode] = useState<"browser" | "folder" | "both">("browser");
|
||||
const [defaultDestinationType, setDefaultDestinationType] = useState<DestinationType>("local");
|
||||
const [defaultDestinationPath, setDefaultDestinationPath] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (!importSettings) return;
|
||||
setDefaultExportMode(importSettings.bapExportMode || "browser");
|
||||
setDefaultDestinationType((importSettings.exportFolderType || "local") as DestinationType);
|
||||
setDefaultDestinationPath(importSettings.exportFolder || "");
|
||||
}, [importSettings]);
|
||||
|
||||
const conditionValues = useMemo(() => {
|
||||
const field = conditionField === "recipientName" ? "recipientName" : "ventilationComptable";
|
||||
return Array.from(new Set(invoices.map((invoice: any) => invoice[field]).filter(Boolean))).sort();
|
||||
}, [conditionField, invoices]);
|
||||
|
||||
const resetForm = () => {
|
||||
setEditingId(null);
|
||||
setName("");
|
||||
setConditionField("recipientName");
|
||||
setConditionValue("");
|
||||
setDestinationType("local");
|
||||
setDestinationPath("");
|
||||
setOpenInBrowser(false);
|
||||
};
|
||||
|
||||
const createMutation = trpc.exportAutomationRules.create.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success("Règle d’export créée");
|
||||
utils.exportAutomationRules.list.invalidate();
|
||||
resetForm();
|
||||
},
|
||||
onError: (error) => toast.error(error.message || "Impossible de créer la règle"),
|
||||
});
|
||||
const updateMutation = trpc.exportAutomationRules.update.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success("Règle d’export mise à jour");
|
||||
utils.exportAutomationRules.list.invalidate();
|
||||
resetForm();
|
||||
},
|
||||
onError: (error) => toast.error(error.message || "Impossible de mettre à jour la règle"),
|
||||
});
|
||||
const deleteMutation = trpc.exportAutomationRules.delete.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success("Règle d’export supprimée");
|
||||
utils.exportAutomationRules.list.invalidate();
|
||||
},
|
||||
onError: (error) => toast.error(error.message || "Impossible de supprimer la règle"),
|
||||
});
|
||||
const updateDefaultExportMutation = trpc.importSettings.update.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success("Paramètres d’export enregistrés");
|
||||
utils.importSettings.get.invalidate();
|
||||
},
|
||||
onError: (error) => toast.error(error.message || "Impossible d’enregistrer les paramètres d’export"),
|
||||
});
|
||||
|
||||
const saveRule = () => {
|
||||
if (!name.trim() || !conditionValue.trim() || !destinationPath.trim()) {
|
||||
toast.error("Nom, valeur de condition et destination sont obligatoires");
|
||||
return;
|
||||
}
|
||||
const payload = {
|
||||
name: name.trim(),
|
||||
conditionField,
|
||||
conditionValue: conditionValue.trim(),
|
||||
destinationType,
|
||||
destinationPath: destinationPath.trim(),
|
||||
openInBrowser: openInBrowser ? 1 : 0,
|
||||
};
|
||||
if (editingId) updateMutation.mutate({ id: editingId, ...payload });
|
||||
else createMutation.mutate({ ...payload, isActive: 1, priority: rules.length });
|
||||
};
|
||||
|
||||
const editRule = (rule: any) => {
|
||||
setEditingId(rule.id);
|
||||
setName(rule.name);
|
||||
setConditionField(rule.conditionField);
|
||||
setConditionValue(rule.conditionValue);
|
||||
setDestinationType(rule.destinationType);
|
||||
setDestinationPath(rule.destinationPath);
|
||||
setOpenInBrowser(rule.openInBrowser === 1);
|
||||
};
|
||||
|
||||
const isSaving = createMutation.isPending || updateMutation.isPending;
|
||||
const isMicrosoftDestination = destinationType === "teams" || destinationType === "sharepoint";
|
||||
|
||||
const saveDefaultExport = () => {
|
||||
if (!importSettings) return;
|
||||
if (defaultExportMode !== "browser" && !defaultDestinationPath.trim()) {
|
||||
toast.error("Une destination est obligatoire lorsque l’export vers un dossier est activé");
|
||||
return;
|
||||
}
|
||||
updateDefaultExportMutation.mutate({
|
||||
manualImportEnabled: importSettings.manualImportEnabled,
|
||||
autoImportEnabled: importSettings.autoImportEnabled,
|
||||
emailImportEnabled: importSettings.emailImportEnabled,
|
||||
exportFolder: defaultDestinationPath.trim() || null,
|
||||
exportFolderType: defaultDestinationType,
|
||||
bapExportMode: defaultExportMode,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-5">
|
||||
<Card className="border-orange-200">
|
||||
<CardHeader><CardTitle>Paramètres d’export par défaut</CardTitle><CardDescription>Ils s’appliquent lorsqu’aucun automatisme d’export ne correspond à la facture.</CardDescription></CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid gap-4 md:grid-cols-2"><div className="space-y-2"><Label>Mode d’export</Label><Select value={defaultExportMode} onValueChange={(value) => setDefaultExportMode(value as "browser" | "folder" | "both")}><SelectTrigger><SelectValue /></SelectTrigger><SelectContent><SelectItem value="browser">Ouvrir dans le navigateur</SelectItem><SelectItem value="folder">Exporter vers la destination</SelectItem><SelectItem value="both">Destination et navigateur</SelectItem></SelectContent></Select></div><div className="space-y-2"><Label>Type de destination</Label><Select value={defaultDestinationType} onValueChange={(value) => setDefaultDestinationType(value as DestinationType)} disabled={defaultExportMode === "browser"}><SelectTrigger><SelectValue /></SelectTrigger><SelectContent><SelectItem value="local">Dossier local</SelectItem><SelectItem value="teams">Teams</SelectItem><SelectItem value="sharepoint">SharePoint</SelectItem></SelectContent></Select></div></div>
|
||||
<div className="space-y-2"><Label htmlFor="default-export-path">{defaultDestinationType === "local" ? "Chemin du dossier local" : "URL SharePoint du dossier"}</Label><Input id="default-export-path" value={defaultDestinationPath} disabled={defaultExportMode === "browser"} onChange={(event) => setDefaultDestinationPath(event.target.value)} placeholder={defaultDestinationType === "local" ? "/chemin/serveur/factures-bap" : "https://…sharepoint.com/sites/…/Documents/Factures"} /></div>
|
||||
{(defaultDestinationType === "teams" || defaultDestinationType === "sharepoint") && <p className="rounded-lg border border-cyan-200 bg-cyan-50/40 p-3 text-xs text-cyan-800">La configuration Microsoft Azure AD est centralisée dans <strong>Paramètres</strong>. Teams utilise l’URL SharePoint du dossier Fichiers du canal.</p>}
|
||||
<Button onClick={saveDefaultExport} disabled={!importSettings || updateDefaultExportMutation.isPending}><Save className="mr-2 h-4 w-4" />{updateDefaultExportMutation.isPending ? "Enregistrement…" : "Enregistrer les paramètres d’export"}</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card className="border-violet-200">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2"><FolderOpen className="h-5 w-5 text-violet-600" />Nouvelle règle de destination</CardTitle>
|
||||
<CardDescription>La première règle active par priorité qui correspond à la facture est appliquée lors de la validation BAP.</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="space-y-2"><Label htmlFor="export-rule-name">Nom de la règle</Label><Input id="export-rule-name" value={name} onChange={(event) => setName(event.target.value)} placeholder="Ex. DSI SANTINOVA vers SharePoint" /></div>
|
||||
<div className="space-y-2"><Label>Critère</Label><Select value={conditionField} onValueChange={(value) => { setConditionField(value as ConditionField); setConditionValue(""); }}><SelectTrigger><SelectValue /></SelectTrigger><SelectContent><SelectItem value="recipientName">Destinataire</SelectItem><SelectItem value="ventilationComptable">Ventilation comptable</SelectItem></SelectContent></Select></div>
|
||||
</div>
|
||||
<div className="space-y-2"><Label htmlFor="export-rule-value">Valeur du critère</Label><Input id="export-rule-value" list="export-condition-values" value={conditionValue} onChange={(event) => setConditionValue(event.target.value)} placeholder={conditionField === "recipientName" ? "Choisir ou saisir un destinataire" : "Choisir ou saisir une ventilation"} /><datalist id="export-condition-values">{conditionValues.map((value) => <option value={value} key={value} />)}</datalist></div>
|
||||
<div className="space-y-2"><Label>Destination</Label><div className="grid gap-2 sm:grid-cols-3">{(["local", "teams", "sharepoint"] as DestinationType[]).map((type) => <Button key={type} type="button" variant={destinationType === type ? "default" : "outline"} className={destinationType === type ? "bg-violet-600 hover:bg-violet-700" : ""} onClick={() => setDestinationType(type)}>{DESTINATION_LABELS[type]}</Button>)}</div></div>
|
||||
<div className="space-y-2"><Label htmlFor="export-rule-path">{isMicrosoftDestination ? "URL SharePoint du dossier" : "Chemin du dossier local"}</Label><Input id="export-rule-path" value={destinationPath} onChange={(event) => setDestinationPath(event.target.value)} placeholder={isMicrosoftDestination ? "https://…sharepoint.com/sites/…/Documents/Factures" : "/chemin/serveur/factures-bap"} /><p className="text-xs text-muted-foreground">{isMicrosoftDestination ? "Pour Teams, indiquez l’URL SharePoint du dossier Fichiers du canal. Les droits Microsoft existants sont réutilisés." : "Ce chemin est créé si nécessaire sur le serveur qui héberge l’application."}</p></div>
|
||||
<div className="flex items-center justify-between rounded-lg border bg-slate-50 p-3"><div><Label htmlFor="export-open-browser">Ouvrir aussi dans le navigateur</Label><p className="text-xs text-muted-foreground">Conserve le téléchargement dans le navigateur en complément de la destination définie.</p></div><Switch id="export-open-browser" checked={openInBrowser} onCheckedChange={setOpenInBrowser} /></div>
|
||||
<div className="flex gap-2"><Button onClick={saveRule} disabled={isSaving}><Save className="mr-2 h-4 w-4" />{editingId ? "Mettre à jour" : "Créer la règle"}</Button>{editingId && <Button variant="outline" onClick={resetForm}>Annuler</Button>}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader><CardTitle>Règles d’export</CardTitle><CardDescription>Les règles inactives restent enregistrées mais ne sont jamais appliquées.</CardDescription></CardHeader>
|
||||
<CardContent>{isLoading ? <p className="text-sm text-muted-foreground">Chargement…</p> : rules.length === 0 ? <p className="text-sm text-muted-foreground">Aucune règle de destination. Les paramètres existants restent le comportement de repli.</p> : <Table><TableHeader><TableRow><TableHead>Nom</TableHead><TableHead>Condition</TableHead><TableHead>Destination</TableHead><TableHead>Statut</TableHead><TableHead className="w-32">Actions</TableHead></TableRow></TableHeader><TableBody>{rules.map((rule: any) => <TableRow key={rule.id}><TableCell className="font-medium">{rule.name}{rule.openInBrowser === 1 && <span className="ml-2 inline-flex items-center text-xs text-slate-500"><Monitor className="mr-1 h-3 w-3" />Navigateur</span>}</TableCell><TableCell>{LABELS[rule.conditionField as ConditionField]} : {rule.conditionValue}</TableCell><TableCell><Badge variant="outline">{DESTINATION_LABELS[rule.destinationType as DestinationType]}</Badge><div className="mt-1 max-w-xs truncate text-xs text-muted-foreground" title={rule.destinationPath}>{rule.destinationPath}</div></TableCell><TableCell>{rule.isActive === 1 ? <Badge className="bg-green-100 text-green-800 hover:bg-green-100">Actif</Badge> : <Badge variant="secondary">Inactif</Badge>}</TableCell><TableCell><div className="flex gap-1"><Button size="icon" variant="ghost" title={rule.isActive === 1 ? "Désactiver" : "Activer"} onClick={() => updateMutation.mutate({ id: rule.id, isActive: rule.isActive === 1 ? 0 : 1 })}>{rule.isActive === 1 ? <PowerOff className="h-4 w-4" /> : <Power className="h-4 w-4" />}</Button><Button size="icon" variant="ghost" title="Modifier" onClick={() => editRule(rule)}><Pencil className="h-4 w-4" /></Button><Button size="icon" variant="ghost" title="Supprimer" onClick={() => { if (window.confirm(`Supprimer la règle « ${rule.name} » ?`)) deleteMutation.mutate({ id: rule.id }); }}><Trash2 className="h-4 w-4 text-red-600" /></Button></div></TableCell></TableRow>)}</TableBody></Table>}</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
96
client/src/lib/bapNavigation.ts
Normal file
96
client/src/lib/bapNavigation.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Paramètres d’affichage qui doivent survivre au passage par le détail d’une
|
||||
* facture BAP. Seules ces clés sont sérialisées afin d’éviter de propager des
|
||||
* paramètres de navigation arbitraires.
|
||||
*/
|
||||
export type BapFilters = {
|
||||
searchQuery: string;
|
||||
statusFilter: string;
|
||||
selectedYear: string;
|
||||
selectedMonth: string;
|
||||
recipientFilter: string;
|
||||
entityFilter: string;
|
||||
ventilationFilter: string;
|
||||
sortField: "invoiceDate" | "createdAt";
|
||||
sortDir: "asc" | "desc";
|
||||
};
|
||||
|
||||
const BAP_STATUSES = new Set([
|
||||
"all",
|
||||
"exported",
|
||||
"not_exported",
|
||||
"export_error",
|
||||
"bap_validated",
|
||||
"bap_pending",
|
||||
"to_complete",
|
||||
]);
|
||||
|
||||
function asSearchParams(search: string): URLSearchParams {
|
||||
return new URLSearchParams(search.startsWith("?") ? search.slice(1) : search);
|
||||
}
|
||||
|
||||
function isMonth(value: string | null): value is string {
|
||||
return value !== null && /^(0[1-9]|1[0-2])$/.test(value);
|
||||
}
|
||||
|
||||
function isYear(value: string | null): value is string {
|
||||
return value === "all" || (value !== null && /^\d{4}$/.test(value));
|
||||
}
|
||||
|
||||
function isSafeFilterValue(value: string | null): value is string {
|
||||
return value !== null && value.length > 0 && value.length <= 200;
|
||||
}
|
||||
|
||||
/** Lit et valide les filtres BAP transmis dans l’URL. */
|
||||
export function parseBapFilters(search: string, defaultYear: string): BapFilters {
|
||||
const params = asSearchParams(search);
|
||||
const status = params.get("status");
|
||||
const year = params.get("year");
|
||||
const month = params.get("month");
|
||||
const sort = params.get("sort");
|
||||
const direction = params.get("dir");
|
||||
|
||||
return {
|
||||
searchQuery: params.get("q") || "",
|
||||
statusFilter: status && BAP_STATUSES.has(status) ? status : "all",
|
||||
selectedYear: isYear(year) ? year : defaultYear,
|
||||
selectedMonth: month === "all" || isMonth(month) ? month : "all",
|
||||
recipientFilter: isSafeFilterValue(params.get("recipient")) ? params.get("recipient")! : "all",
|
||||
entityFilter: ["all", "santinova", "itinova"].includes(params.get("entity") || "") ? params.get("entity")! : "all",
|
||||
ventilationFilter: isSafeFilterValue(params.get("ventilation")) ? params.get("ventilation")! : "all",
|
||||
sortField: sort === "invoiceDate" ? "invoiceDate" : "createdAt",
|
||||
sortDir: direction === "asc" ? "asc" : "desc",
|
||||
};
|
||||
}
|
||||
|
||||
/** Construit une URL de détail qui conserve explicitement le contexte BAP. */
|
||||
export function buildBapDetailLocation(invoiceId: number, filters: BapFilters): string {
|
||||
const params = new URLSearchParams({
|
||||
returnTo: "bap",
|
||||
q: filters.searchQuery,
|
||||
status: filters.statusFilter,
|
||||
year: filters.selectedYear,
|
||||
month: filters.selectedMonth,
|
||||
recipient: filters.recipientFilter,
|
||||
entity: filters.entityFilter,
|
||||
ventilation: filters.ventilationFilter,
|
||||
sort: filters.sortField,
|
||||
dir: filters.sortDir,
|
||||
});
|
||||
|
||||
return `/invoices/${invoiceId}?${params.toString()}`;
|
||||
}
|
||||
|
||||
/** Retourne la liste d’origine. Sans contexte BAP, le comportement historique est conservé. */
|
||||
export function getBapReturnLocation(search: string): string {
|
||||
const params = asSearchParams(search);
|
||||
if (params.get("returnTo") !== "bap") return "/invoices";
|
||||
|
||||
const allowed = new URLSearchParams();
|
||||
for (const key of ["q", "status", "year", "month", "recipient", "entity", "ventilation", "sort", "dir"]) {
|
||||
const value = params.get(key);
|
||||
if (value) allowed.set(key, value);
|
||||
}
|
||||
const query = allowed.toString();
|
||||
return query ? `/invoices-bap?${query}` : "/invoices-bap";
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { useMemo, useState } from "react";
|
||||
import DashboardLayout from "@/components/DashboardLayout";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
@@ -33,6 +33,13 @@ import {
|
||||
} from "@/components/ui/table";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import ExportAutomationRulesPanel from "@/components/ExportAutomationRulesPanel";
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import {
|
||||
AUTOMATION_ACTION_FILTERS,
|
||||
type AutomationActionFilter,
|
||||
matchesAutomationActionFilter,
|
||||
} from "@shared/automationActions";
|
||||
import { toast } from "sonner";
|
||||
import { Plus, Edit, Trash2, Power, PowerOff, Copy } from "lucide-react";
|
||||
|
||||
@@ -46,6 +53,7 @@ interface Actions {
|
||||
typeAchat?: string;
|
||||
serviceConcerne?: string;
|
||||
ventilationComptable?: string;
|
||||
isSubscription?: 0 | 1;
|
||||
}
|
||||
|
||||
export default function AutomationRules() {
|
||||
@@ -62,6 +70,7 @@ export default function AutomationRules() {
|
||||
const [wizardStep, setWizardStep] = useState(1);
|
||||
const [testResultsOpen, setTestResultsOpen] = useState(false);
|
||||
const [testResults, setTestResults] = useState<any>(null);
|
||||
const [actionFilter, setActionFilter] = useState<AutomationActionFilter>("all");
|
||||
|
||||
// Form state
|
||||
const [ruleName, setRuleName] = useState("");
|
||||
@@ -84,6 +93,12 @@ export default function AutomationRules() {
|
||||
return val;
|
||||
};
|
||||
|
||||
const normalizeSubscriptionAction = (value: unknown): 0 | 1 | undefined => {
|
||||
if (value === 1 || value === "1" || value === "OUI") return 1;
|
||||
if (value === 0 || value === "0" || value === "NON") return 0;
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const createMutation = trpc.automationRules.create.useMutation({
|
||||
onSuccess: (rule) => {
|
||||
if (rule.isActive === 1) {
|
||||
@@ -204,7 +219,7 @@ export default function AutomationRules() {
|
||||
} else {
|
||||
setCustomVentilation("");
|
||||
}
|
||||
setActions(parsedActions);
|
||||
setActions({ ...parsedActions, isSubscription: normalizeSubscriptionAction(parsedActions.isSubscription) });
|
||||
} catch {
|
||||
setActions({});
|
||||
setCustomTypeAchat("");
|
||||
@@ -231,11 +246,13 @@ export default function AutomationRules() {
|
||||
typeAchat: resolveAction(actions.typeAchat, customTypeAchat),
|
||||
serviceConcerne: resolveAction(actions.serviceConcerne, customServiceConcerne),
|
||||
ventilationComptable: resolveAction(actions.ventilationComptable, customVentilation),
|
||||
isSubscription: actions.isSubscription,
|
||||
};
|
||||
// Supprimer les clés undefined
|
||||
if (!resolvedActions.typeAchat) delete resolvedActions.typeAchat;
|
||||
if (!resolvedActions.serviceConcerne) delete resolvedActions.serviceConcerne;
|
||||
if (!resolvedActions.ventilationComptable) delete resolvedActions.ventilationComptable;
|
||||
if (resolvedActions.isSubscription === undefined) delete resolvedActions.isSubscription;
|
||||
const actionsJSON = JSON.stringify(resolvedActions);
|
||||
|
||||
if (editingRule) {
|
||||
@@ -311,13 +328,18 @@ export default function AutomationRules() {
|
||||
{ value: "<=", label: "inférieur ou égal à" },
|
||||
];
|
||||
|
||||
const filteredRules = useMemo(
|
||||
() => rules.filter((rule) => matchesAutomationActionFilter(rule.actions, actionFilter)),
|
||||
[rules, actionFilter],
|
||||
);
|
||||
|
||||
return (
|
||||
<DashboardLayout>
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Automatismes</h1>
|
||||
<p className="text-gray-500 mt-1">Gérez les règles de remplissage automatique des champs</p>
|
||||
<p className="text-gray-500 mt-1">Gérez séparément le classement à l’import et les destinations d’export</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button onClick={() => { setWizardMode(true); setWizardStep(1); openCreateDialog(); }}>
|
||||
@@ -331,6 +353,12 @@ export default function AutomationRules() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Tabs defaultValue="import" className="space-y-4">
|
||||
<TabsList className="h-11 bg-slate-100 p-1">
|
||||
<TabsTrigger value="import" className="px-4 text-blue-700 hover:bg-blue-50 data-[state=active]:bg-blue-600 data-[state=active]:text-white data-[state=active]:shadow-sm">Automatismes d’import</TabsTrigger>
|
||||
<TabsTrigger value="export" className="px-4 text-violet-700 hover:bg-violet-50 data-[state=active]:bg-violet-600 data-[state=active]:text-white data-[state=active]:shadow-sm">Automatismes d’export</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="import" className="space-y-4">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Règles d'automatisme</CardTitle>
|
||||
@@ -339,12 +367,36 @@ export default function AutomationRules() {
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="mb-4 flex flex-wrap items-center gap-3 rounded-lg border border-slate-200 bg-slate-50 p-3">
|
||||
<Label htmlFor="automation-action-filter" className="font-medium text-slate-700">
|
||||
Filtrer par action
|
||||
</Label>
|
||||
<Select value={actionFilter} onValueChange={(value) => setActionFilter(value as AutomationActionFilter)}>
|
||||
<SelectTrigger id="automation-action-filter" className="w-[260px] bg-white">
|
||||
<SelectValue placeholder="Toutes les actions" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{AUTOMATION_ACTION_FILTERS.map((filter) => (
|
||||
<SelectItem key={filter.value} value={filter.value}>
|
||||
{filter.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<span className="text-sm text-slate-500">
|
||||
{filteredRules.length} règle{filteredRules.length > 1 ? "s" : ""} affichée{filteredRules.length > 1 ? "s" : ""}
|
||||
</span>
|
||||
</div>
|
||||
{isLoading ? (
|
||||
<div className="text-center py-8 text-gray-500">Chargement...</div>
|
||||
) : rules.length === 0 ? (
|
||||
<div className="text-center py-8 text-gray-500">
|
||||
Aucune règle d'automatisme configurée
|
||||
</div>
|
||||
) : filteredRules.length === 0 ? (
|
||||
<div className="text-center py-8 text-gray-500">
|
||||
Aucune règle ne correspond à cette action
|
||||
</div>
|
||||
) : (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
@@ -357,7 +409,7 @@ export default function AutomationRules() {
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{rules.map((rule) => {
|
||||
{filteredRules.map((rule) => {
|
||||
let conditionsDisplay = "";
|
||||
let actionsDisplay = "";
|
||||
try {
|
||||
@@ -372,6 +424,7 @@ export default function AutomationRules() {
|
||||
if (acts.typeAchat) actionsArr.push(`Type: ${acts.typeAchat}`);
|
||||
if (acts.serviceConcerne) actionsArr.push(`Service: ${acts.serviceConcerne}`);
|
||||
if (acts.ventilationComptable) actionsArr.push(`Ventilation: ${acts.ventilationComptable}`);
|
||||
if (acts.isSubscription !== undefined) actionsArr.push(`Abonnement: ${normalizeSubscriptionAction(acts.isSubscription) === 1 ? "Oui" : "Non"}`);
|
||||
actionsDisplay = actionsArr.join(", ");
|
||||
} catch {}
|
||||
|
||||
@@ -435,6 +488,11 @@ export default function AutomationRules() {
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
<TabsContent value="export">
|
||||
<ExportAutomationRulesPanel />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
|
||||
{/* Create/Edit Dialog */}
|
||||
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
|
||||
@@ -610,6 +668,23 @@ export default function AutomationRules() {
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="isSubscription">Abonnement</Label>
|
||||
<Select
|
||||
value={actions.isSubscription === undefined ? "__NONE__" : String(actions.isSubscription)}
|
||||
onValueChange={(value) => setActions({ ...actions, isSubscription: value === "__NONE__" ? undefined : Number(value) as 0 | 1 })}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Ne pas modifier" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="__NONE__">Ne pas modifier</SelectItem>
|
||||
<SelectItem value="1">Oui</SelectItem>
|
||||
<SelectItem value="0">Non</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -642,7 +717,8 @@ export default function AutomationRules() {
|
||||
{actions.typeAchat && <div>• Type d'achat : {actions.typeAchat}</div>}
|
||||
{actions.serviceConcerne && <div>• Service : {actions.serviceConcerne}</div>}
|
||||
{actions.ventilationComptable && <div>• Ventilation : {actions.ventilationComptable}</div>}
|
||||
{!actions.typeAchat && !actions.serviceConcerne && !actions.ventilationComptable && (
|
||||
{actions.isSubscription !== undefined && <div>• Abonnement : {actions.isSubscription === 1 ? "Oui" : "Non"}</div>}
|
||||
{!actions.typeAchat && !actions.serviceConcerne && !actions.ventilationComptable && actions.isSubscription === undefined && (
|
||||
<div className="text-gray-500 italic">Aucune action définie</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -745,6 +821,24 @@ export default function AutomationRules() {
|
||||
<div className="space-y-2">
|
||||
<Label>Actions (ALORS)</Label>
|
||||
<div className="space-y-2">
|
||||
<div className="space-y-2 rounded-md border border-emerald-200 bg-emerald-50 p-3">
|
||||
<Label htmlFor="isSubscription" className="font-medium text-emerald-950">Abonnement</Label>
|
||||
<Select
|
||||
value={actions.isSubscription === undefined ? "__NONE__" : String(actions.isSubscription)}
|
||||
onValueChange={(value) => setActions({ ...actions, isSubscription: value === "__NONE__" ? undefined : Number(value) as 0 | 1 })}
|
||||
>
|
||||
<SelectTrigger className="bg-white">
|
||||
<SelectValue placeholder="Ne pas modifier" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="__NONE__">Ne pas modifier</SelectItem>
|
||||
<SelectItem value="1">Oui</SelectItem>
|
||||
<SelectItem value="0">Non</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<p className="text-xs text-emerald-800">Définit si les factures correspondant à la règle sont des abonnements.</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="typeAchat">Type d'achat</Label>
|
||||
<Select
|
||||
@@ -829,6 +923,7 @@ export default function AutomationRules() {
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -148,6 +148,38 @@ export default function Dashboard() {
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Récapitulatif annuel</CardTitle>
|
||||
<CardDescription>Volumes et montants des factures finalisées, séparés entre BAP (hors abonnement) et abonnements.</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{stats?.annualSummary?.length ? (
|
||||
<div className="space-y-6">
|
||||
<div className="h-[300px] rounded-lg border bg-white p-4">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart data={[...stats.annualSummary].reverse()} margin={{ top: 8, right: 16, left: 12, bottom: 4 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" vertical={false} />
|
||||
<XAxis dataKey="year" />
|
||||
<YAxis tickFormatter={(value) => new Intl.NumberFormat("fr-FR", { notation: "compact", maximumFractionDigits: 1 }).format(value)} />
|
||||
<Tooltip formatter={(value: number) => formatCurrency(Number(value))} labelFormatter={(year) => `Année ${year}`} />
|
||||
<Legend />
|
||||
<Bar dataKey="bapAmount" name="Montant BAP" fill="#2563eb" radius={[4, 4, 0, 0]} />
|
||||
<Bar dataKey="subscriptionAmount" name="Montant abonnements" fill="#7c3aed" radius={[4, 4, 0, 0]} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
<div className="overflow-x-auto rounded-lg border">
|
||||
<table className="w-full min-w-[780px] text-sm">
|
||||
<thead className="bg-muted/60 text-left text-muted-foreground"><tr><th className="px-4 py-3 font-medium">Année</th><th className="px-4 py-3 text-right font-medium">Factures BAP</th><th className="px-4 py-3 text-right font-medium">Montant BAP</th><th className="px-4 py-3 text-right font-medium">Abonnements</th><th className="px-4 py-3 text-right font-medium">Montant abonnements</th><th className="px-4 py-3 text-right font-medium">Total annuel</th></tr></thead>
|
||||
<tbody>{stats.annualSummary.map((row) => <tr key={row.year} className="border-t hover:bg-muted/30"><td className="px-4 py-3 font-semibold">{row.year}</td><td className="px-4 py-3 text-right">{row.bapCount}</td><td className="px-4 py-3 text-right text-blue-700">{formatCurrency(row.bapAmount)}</td><td className="px-4 py-3 text-right">{row.subscriptionCount}</td><td className="px-4 py-3 text-right text-violet-700">{formatCurrency(row.subscriptionAmount)}</td><td className="px-4 py-3 text-right font-semibold text-emerald-700">{formatCurrency(row.totalAmount)}</td></tr>)}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
) : <div className="py-8 text-center text-muted-foreground">Aucune facture finalisée avec une date disponible.</div>}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Detailed Stats */}
|
||||
{showDetailedStats && (
|
||||
<>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState, useMemo } from "react";
|
||||
import { useMemo, useState } from "react";
|
||||
import DashboardLayout from "@/components/DashboardLayout";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { useAuth } from "@/_core/hooks/useAuth";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
@@ -12,20 +13,24 @@ import {
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import {
|
||||
History as HistoryIcon,
|
||||
FileText,
|
||||
Trash2,
|
||||
Eye,
|
||||
CheckCircle2,
|
||||
XCircle,
|
||||
AlertTriangle,
|
||||
CalendarDays,
|
||||
RotateCcw,
|
||||
Package,
|
||||
X,
|
||||
Upload,
|
||||
CheckCircle2,
|
||||
Clock3,
|
||||
Eye,
|
||||
FileText,
|
||||
FolderOpen,
|
||||
History as HistoryIcon,
|
||||
Loader2,
|
||||
Mail,
|
||||
Package,
|
||||
Play,
|
||||
RotateCcw,
|
||||
Trash2,
|
||||
Upload,
|
||||
UserRound,
|
||||
X,
|
||||
XCircle,
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { toast } from "sonner";
|
||||
@@ -55,6 +60,39 @@ import {
|
||||
} from "@/components/ui/select";
|
||||
|
||||
type StatusFilter = "all" | "imported" | "not_imported" | "duplicates" | "errors";
|
||||
type SourceFilter = "all" | "email" | "folder" | "file";
|
||||
type TriggerFilter = "all" | "manual" | "automatic" | "unknown";
|
||||
|
||||
type DetailedImportLog = {
|
||||
id: number;
|
||||
userId: number;
|
||||
sourceFileId: number;
|
||||
fileName: string;
|
||||
totalInvoicesDetected: number;
|
||||
invoicesImported: number;
|
||||
duplicatesIgnored: number;
|
||||
errors: number;
|
||||
duplicateDetails: string | null;
|
||||
errorDetails: string | null;
|
||||
warningMessage: string | null;
|
||||
importSource: "file" | "folder" | "email";
|
||||
importTrigger: "manual" | "automatic" | "unknown";
|
||||
importedAt: Date;
|
||||
userName: string | null;
|
||||
userEmail: string | null;
|
||||
sourceCreatedAt: Date | null;
|
||||
sourceStatus: "processing" | "completed" | "error" | null;
|
||||
};
|
||||
|
||||
type EmailAccount = {
|
||||
userId: number;
|
||||
userName: string | null;
|
||||
userEmail: string;
|
||||
emailAddress: string | null;
|
||||
authMode: "basic" | "oauth2";
|
||||
automaticEnabled: number;
|
||||
isConfigured: number;
|
||||
};
|
||||
|
||||
const MONTHS = [
|
||||
{ value: "1", label: "Janvier" },
|
||||
@@ -71,111 +109,149 @@ const MONTHS = [
|
||||
{ value: "12", label: "Décembre" },
|
||||
];
|
||||
|
||||
const YEARS = Array.from({ length: 5 }, (_, i) => String(new Date().getFullYear() - i));
|
||||
const YEARS = Array.from({ length: 5 }, (_, index) => String(new Date().getFullYear() - index));
|
||||
|
||||
function formatAccount(account: Pick<EmailAccount, "userName" | "userEmail" | "emailAddress">) {
|
||||
const owner = account.userName?.trim() || account.userEmail;
|
||||
return account.emailAddress ? `${owner} — ${account.emailAddress}` : owner;
|
||||
}
|
||||
|
||||
function SourceBadge({ source }: { source: DetailedImportLog["importSource"] }) {
|
||||
if (source === "email") {
|
||||
return <Badge className="gap-1 border border-blue-200 bg-blue-100 text-blue-700 hover:bg-blue-100"><Mail className="h-3 w-3" />E-mail</Badge>;
|
||||
}
|
||||
if (source === "folder") {
|
||||
return <Badge className="gap-1 border border-purple-200 bg-purple-100 text-purple-700 hover:bg-purple-100"><FolderOpen className="h-3 w-3" />Dossier</Badge>;
|
||||
}
|
||||
return <Badge className="gap-1 border border-slate-200 bg-slate-100 text-slate-700 hover:bg-slate-100"><Upload className="h-3 w-3" />Fichier</Badge>;
|
||||
}
|
||||
|
||||
function TriggerBadge({ trigger }: { trigger: DetailedImportLog["importTrigger"] }) {
|
||||
if (trigger === "manual") {
|
||||
return <Badge variant="outline" className="border-teal-200 bg-teal-50 text-teal-700">Manuel</Badge>;
|
||||
}
|
||||
if (trigger === "automatic") {
|
||||
return <Badge variant="outline" className="border-indigo-200 bg-indigo-50 text-indigo-700">Planifié</Badge>;
|
||||
}
|
||||
return <Badge variant="outline" className="border-slate-200 bg-slate-50 text-slate-500">Non tracé</Badge>;
|
||||
}
|
||||
|
||||
export default function History() {
|
||||
const { user } = useAuth();
|
||||
const isAdmin = user?.role === "admin";
|
||||
const { data: logs, isLoading } = trpc.importLogs.getByUser.useQuery();
|
||||
const { data: emailAccounts, isLoading: accountsLoading } = trpc.emailImportService.getConfiguredAccounts.useQuery(undefined, {
|
||||
enabled: isAdmin,
|
||||
staleTime: 30_000,
|
||||
});
|
||||
const deleteAllMutation = trpc.importLogs.deleteAll.useMutation();
|
||||
const checkAccountNowMutation = trpc.emailImportService.checkAccountNow.useMutation();
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
// Filtres
|
||||
const detailedLogs = (logs ?? []) as DetailedImportLog[];
|
||||
const accounts = (emailAccounts ?? []) as EmailAccount[];
|
||||
const configuredAccounts = useMemo(() => accounts.filter((account) => account.isConfigured === 1), [accounts]);
|
||||
|
||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all");
|
||||
const [sourceFilter, setSourceFilter] = useState<SourceFilter>("all");
|
||||
const [triggerFilter, setTriggerFilter] = useState<TriggerFilter>("all");
|
||||
const [yearFilter, setYearFilter] = useState<string>(String(new Date().getFullYear()));
|
||||
const [monthFilter, setMonthFilter] = useState<string>("all");
|
||||
|
||||
// Dialog visualisation détails
|
||||
const [detailDialogOpen, setDetailDialogOpen] = useState(false);
|
||||
const [selectedLog, setSelectedLog] = useState<(typeof logs extends (infer T)[] | undefined ? T : never) | null>(null);
|
||||
const [selectedLog, setSelectedLog] = useState<DetailedImportLog | null>(null);
|
||||
const [selectedAccountId, setSelectedAccountId] = useState("none");
|
||||
const [confirmManualCheckOpen, setConfirmManualCheckOpen] = useState(false);
|
||||
|
||||
const selectedAccount = useMemo(
|
||||
() => configuredAccounts.find((account) => String(account.userId) === selectedAccountId),
|
||||
[configuredAccounts, selectedAccountId],
|
||||
);
|
||||
|
||||
const handleDeleteAll = async () => {
|
||||
try {
|
||||
await deleteAllMutation.mutateAsync();
|
||||
await utils.importLogs.getByUser.invalidate();
|
||||
toast.success("Logs supprimés", {
|
||||
description: "Tous les logs d'import ont été supprimés avec succès.",
|
||||
});
|
||||
toast.success("Historique supprimé", { description: "Les enregistrements visibles pour votre rôle ont été supprimés." });
|
||||
} catch {
|
||||
toast.error("Erreur", {
|
||||
description: "Impossible de supprimer les logs d'import.",
|
||||
});
|
||||
toast.error("Impossible de supprimer l’historique des imports.");
|
||||
}
|
||||
};
|
||||
|
||||
// Filtrage par période
|
||||
const periodFiltered = useMemo(() => {
|
||||
if (!logs) return [];
|
||||
return logs.filter((log) => {
|
||||
const d = new Date(log.importedAt);
|
||||
if (yearFilter !== "all" && d.getFullYear() !== Number(yearFilter)) return false;
|
||||
if (monthFilter !== "all" && (d.getMonth() + 1) !== Number(monthFilter)) return false;
|
||||
return true;
|
||||
});
|
||||
}, [logs, yearFilter, monthFilter]);
|
||||
const handleManualEmailCheck = async () => {
|
||||
if (!selectedAccount) return;
|
||||
try {
|
||||
toast.info("Lecture ponctuelle de la boîte e-mail en cours…");
|
||||
const result = await checkAccountNowMutation.mutateAsync({ userId: selectedAccount.userId });
|
||||
if (!result.success) {
|
||||
toast.error(result.message);
|
||||
return;
|
||||
}
|
||||
await utils.importLogs.getByUser.invalidate();
|
||||
toast.success("Lecture e-mail terminée", { description: result.message });
|
||||
} catch (error: any) {
|
||||
toast.error(error?.message || "Impossible de vérifier cette boîte e-mail.");
|
||||
} finally {
|
||||
setConfirmManualCheckOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
const periodFiltered = useMemo(() => detailedLogs.filter((log) => {
|
||||
const date = new Date(log.importedAt);
|
||||
if (yearFilter !== "all" && date.getFullYear() !== Number(yearFilter)) return false;
|
||||
if (monthFilter !== "all" && date.getMonth() + 1 !== Number(monthFilter)) return false;
|
||||
return true;
|
||||
}), [detailedLogs, yearFilter, monthFilter]);
|
||||
|
||||
// Compteurs sur la période filtrée
|
||||
const counts = useMemo(() => {
|
||||
const totalImported = periodFiltered.reduce((s, l) => s + l.invoicesImported, 0);
|
||||
const totalDuplicates = periodFiltered.reduce((s, l) => s + l.duplicatesIgnored, 0);
|
||||
const totalErrors = periodFiltered.reduce((s, l) => s + l.errors, 0);
|
||||
const totalDetected = periodFiltered.reduce((s, l) => s + l.totalInvoicesDetected, 0);
|
||||
const totalNotImported = totalDuplicates + totalErrors;
|
||||
return { totalImported, totalDuplicates, totalErrors, totalDetected, totalNotImported };
|
||||
const totalImported = periodFiltered.reduce((sum, log) => sum + log.invoicesImported, 0);
|
||||
const totalDuplicates = periodFiltered.reduce((sum, log) => sum + log.duplicatesIgnored, 0);
|
||||
const totalErrors = periodFiltered.reduce((sum, log) => sum + log.errors, 0);
|
||||
const totalDetected = periodFiltered.reduce((sum, log) => sum + log.totalInvoicesDetected, 0);
|
||||
return { totalImported, totalDuplicates, totalErrors, totalDetected, totalNotImported: totalDuplicates + totalErrors };
|
||||
}, [periodFiltered]);
|
||||
|
||||
// Filtrage par statut
|
||||
const filteredLogs = useMemo(() => {
|
||||
return periodFiltered.filter((log) => {
|
||||
if (statusFilter === "all") return true;
|
||||
const filteredLogs = useMemo(() => periodFiltered.filter((log) => {
|
||||
if (sourceFilter !== "all" && log.importSource !== sourceFilter) return false;
|
||||
if (triggerFilter !== "all" && log.importTrigger !== triggerFilter) return false;
|
||||
if (statusFilter === "imported") return log.invoicesImported > 0;
|
||||
if (statusFilter === "not_imported") return log.duplicatesIgnored > 0 || log.errors > 0;
|
||||
if (statusFilter === "duplicates") return log.duplicatesIgnored > 0;
|
||||
if (statusFilter === "errors") return log.errors > 0;
|
||||
return true;
|
||||
});
|
||||
}, [periodFiltered, statusFilter]);
|
||||
}), [periodFiltered, sourceFilter, statusFilter, triggerFilter]);
|
||||
|
||||
const hasPeriodFilter = yearFilter !== "all" || monthFilter !== "all";
|
||||
|
||||
const filterButtons: { key: StatusFilter; label: string; count: number; color: string; activeColor: string }[] = [
|
||||
{ key: "all", label: "Tous", count: periodFiltered.length, color: "bg-gray-100 text-gray-700 hover:bg-gray-200", activeColor: "bg-gray-700 text-white" },
|
||||
{ key: "imported", label: "Importées", count: counts.totalImported, color: "bg-green-50 text-green-700 hover:bg-green-100 border border-green-200", activeColor: "bg-green-600 text-white" },
|
||||
{ key: "not_imported", label: "Non importées", count: counts.totalNotImported, color: "bg-orange-50 text-orange-700 hover:bg-orange-100 border border-orange-200", activeColor: "bg-orange-500 text-white" },
|
||||
{ key: "duplicates", label: "Doublons", count: counts.totalDuplicates, color: "bg-yellow-50 text-yellow-700 hover:bg-yellow-100 border border-yellow-200", activeColor: "bg-yellow-500 text-white" },
|
||||
{ key: "errors", label: "Erreurs", count: counts.totalErrors, color: "bg-red-50 text-red-700 hover:bg-red-100 border border-red-200", activeColor: "bg-red-600 text-white" },
|
||||
const statusButtons: { key: StatusFilter; label: string; count: number; base: string; active: string }[] = [
|
||||
{ key: "all", label: "Tous", count: periodFiltered.length, base: "bg-slate-100 text-slate-700 hover:bg-slate-200", active: "bg-slate-700 text-white" },
|
||||
{ key: "imported", label: "Importées", count: counts.totalImported, base: "border border-green-200 bg-green-50 text-green-700 hover:bg-green-100", active: "bg-green-600 text-white" },
|
||||
{ key: "not_imported", label: "Non importées", count: counts.totalNotImported, base: "border border-orange-200 bg-orange-50 text-orange-700 hover:bg-orange-100", active: "bg-orange-500 text-white" },
|
||||
{ key: "duplicates", label: "Doublons", count: counts.totalDuplicates, base: "border border-yellow-200 bg-yellow-50 text-yellow-700 hover:bg-yellow-100", active: "bg-yellow-500 text-white" },
|
||||
{ key: "errors", label: "Erreurs", count: counts.totalErrors, base: "border border-red-200 bg-red-50 text-red-700 hover:bg-red-100", active: "bg-red-600 text-white" },
|
||||
];
|
||||
|
||||
const hasFilters = yearFilter !== "all" || monthFilter !== "all" || sourceFilter !== "all" || triggerFilter !== "all";
|
||||
|
||||
return (
|
||||
<DashboardLayout>
|
||||
<div className="space-y-6">
|
||||
{/* En-tête */}
|
||||
<div className="flex justify-between items-start">
|
||||
<div className="flex flex-col justify-between gap-4 sm:flex-row sm:items-start">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Historique des imports</h1>
|
||||
<p className="text-gray-500 mt-1">Consultez l'historique de tous vos imports de factures</p>
|
||||
<p className="mt-1 text-gray-500">Suivez la date, la source, le compte et le mode de déclenchement de chaque ajout.</p>
|
||||
</div>
|
||||
{logs && logs.length > 0 && (
|
||||
{detailedLogs.length > 0 && (
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="destructive" size="sm">
|
||||
<Trash2 className="mr-2 h-4 w-4" />
|
||||
Effacer les logs
|
||||
</Button>
|
||||
<Button variant="destructive" size="sm"><Trash2 className="mr-2 h-4 w-4" />Effacer l’historique</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Êtes-vous sûr ?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Cette action supprimera définitivement tous les logs d'import. Cette opération est irréversible.
|
||||
</AlertDialogDescription>
|
||||
<AlertDialogTitle>Supprimer l’historique ?</AlertDialogTitle>
|
||||
<AlertDialogDescription>Cette action est irréversible et supprime les enregistrements accessibles avec votre rôle.</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Annuler</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={handleDeleteAll}
|
||||
disabled={deleteAllMutation.isPending}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteAllMutation.isPending ? "Suppression..." : "Supprimer"}
|
||||
<AlertDialogAction onClick={handleDeleteAll} disabled={deleteAllMutation.isPending} className="bg-destructive text-destructive-foreground hover:bg-destructive/90">
|
||||
{deleteAllMutation.isPending ? "Suppression…" : "Supprimer"}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
@@ -183,340 +259,108 @@ export default function History() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Compteurs */}
|
||||
<div className="grid grid-cols-2 sm:grid-cols-4 gap-3">
|
||||
<Card className="border-green-200 bg-green-50">
|
||||
<CardContent className="p-4 flex items-center gap-3">
|
||||
<CheckCircle2 className="h-8 w-8 text-green-600 shrink-0" />
|
||||
{isAdmin && (
|
||||
<Card className="border-teal-200 bg-gradient-to-r from-teal-50 to-cyan-50">
|
||||
<CardHeader className="pb-4">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div className="flex gap-3">
|
||||
<div className="rounded-lg bg-teal-600 p-2 text-white"><Mail className="h-5 w-5" /></div>
|
||||
<div>
|
||||
<p className="text-2xl font-bold text-green-700">{counts.totalImported}</p>
|
||||
<p className="text-xs text-green-600 font-medium">Factures importées</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card className="border-orange-200 bg-orange-50">
|
||||
<CardContent className="p-4 flex items-center gap-3">
|
||||
<XCircle className="h-8 w-8 text-orange-500 shrink-0" />
|
||||
<div>
|
||||
<p className="text-2xl font-bold text-orange-600">{counts.totalNotImported}</p>
|
||||
<p className="text-xs text-orange-500 font-medium">Non importées</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card className="border-yellow-200 bg-yellow-50">
|
||||
<CardContent className="p-4 flex items-center gap-3">
|
||||
<RotateCcw className="h-8 w-8 text-yellow-600 shrink-0" />
|
||||
<div>
|
||||
<p className="text-2xl font-bold text-yellow-700">{counts.totalDuplicates}</p>
|
||||
<p className="text-xs text-yellow-600 font-medium">Doublons ignorés</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card className="border-red-200 bg-red-50">
|
||||
<CardContent className="p-4 flex items-center gap-3">
|
||||
<AlertTriangle className="h-8 w-8 text-red-500 shrink-0" />
|
||||
<div>
|
||||
<p className="text-2xl font-bold text-red-600">{counts.totalErrors}</p>
|
||||
<p className="text-xs text-red-500 font-medium">Erreurs</p>
|
||||
<CardTitle className="text-lg">Lecture e-mail ponctuelle</CardTitle>
|
||||
<CardDescription>Déclenchez une seule vérification pour une boîte configurée, sans réactiver le traitement automatique.</CardDescription>
|
||||
</div>
|
||||
</div>
|
||||
<Badge variant="outline" className="w-fit border-teal-200 bg-white text-teal-700">Aucune planification créée</Badge>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col gap-3 sm:flex-row sm:items-end">
|
||||
<div className="min-w-0 flex-1 space-y-2">
|
||||
<label className="text-sm font-medium" htmlFor="manual-email-account">Compte à vérifier</label>
|
||||
<Select value={selectedAccountId} onValueChange={setSelectedAccountId} disabled={accountsLoading || configuredAccounts.length === 0}>
|
||||
<SelectTrigger id="manual-email-account" className="bg-white"><SelectValue placeholder={accountsLoading ? "Chargement des comptes…" : "Choisir une boîte e-mail"} /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="none">Choisir une boîte e-mail</SelectItem>
|
||||
{configuredAccounts.map((account) => <SelectItem key={account.userId} value={String(account.userId)}>{formatAccount(account)}</SelectItem>)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<AlertDialog open={confirmManualCheckOpen} onOpenChange={setConfirmManualCheckOpen}>
|
||||
<Button onClick={() => setConfirmManualCheckOpen(true)} disabled={!selectedAccount || checkAccountNowMutation.isPending} className="bg-teal-600 hover:bg-teal-700">
|
||||
{checkAccountNowMutation.isPending ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <Play className="mr-2 h-4 w-4" />}Vérifier maintenant
|
||||
</Button>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Lancer une lecture e-mail ponctuelle ?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
La boîte « {selectedAccount ? formatAccount(selectedAccount) : ""} » sera vérifiée une fois. Les nouveaux PDF pourront être analysés, mais l’import automatique restera désactivé.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Annuler</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleManualEmailCheck} disabled={checkAccountNowMutation.isPending} className="bg-teal-600 hover:bg-teal-700">
|
||||
{checkAccountNowMutation.isPending ? "Vérification…" : "Lancer la vérification"}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
|
||||
<Card className="border-green-200 bg-green-50"><CardContent className="flex items-center gap-3 p-4"><CheckCircle2 className="h-8 w-8 shrink-0 text-green-600" /><div><p className="text-2xl font-bold text-green-700">{counts.totalImported}</p><p className="text-xs font-medium text-green-600">Factures importées</p></div></CardContent></Card>
|
||||
<Card className="border-orange-200 bg-orange-50"><CardContent className="flex items-center gap-3 p-4"><XCircle className="h-8 w-8 shrink-0 text-orange-500" /><div><p className="text-2xl font-bold text-orange-600">{counts.totalNotImported}</p><p className="text-xs font-medium text-orange-500">Non importées</p></div></CardContent></Card>
|
||||
<Card className="border-yellow-200 bg-yellow-50"><CardContent className="flex items-center gap-3 p-4"><RotateCcw className="h-8 w-8 shrink-0 text-yellow-600" /><div><p className="text-2xl font-bold text-yellow-700">{counts.totalDuplicates}</p><p className="text-xs font-medium text-yellow-600">Doublons ignorés</p></div></CardContent></Card>
|
||||
<Card className="border-red-200 bg-red-50"><CardContent className="flex items-center gap-3 p-4"><AlertTriangle className="h-8 w-8 shrink-0 text-red-500" /><div><p className="text-2xl font-bold text-red-600">{counts.totalErrors}</p><p className="text-xs font-medium text-red-600">Erreurs</p></div></CardContent></Card>
|
||||
</div>
|
||||
|
||||
{/* Filtres */}
|
||||
<div className="flex flex-wrap gap-3 items-center">
|
||||
{/* Filtres statut */}
|
||||
<Card className="border-blue-100 bg-blue-50/40"><CardContent className="flex flex-wrap items-center gap-3 p-4">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{filterButtons.map((btn) => (
|
||||
<button
|
||||
key={btn.key}
|
||||
onClick={() => setStatusFilter(btn.key)}
|
||||
className={`px-3 py-1.5 rounded-full text-sm font-medium transition-all ${
|
||||
statusFilter === btn.key ? btn.activeColor : btn.color
|
||||
}`}
|
||||
>
|
||||
{btn.label}
|
||||
<span className={`ml-1.5 inline-flex items-center justify-center rounded-full text-xs px-1.5 py-0.5 font-bold ${
|
||||
statusFilter === btn.key ? "bg-white/20" : "bg-white/60"
|
||||
}`}>
|
||||
{btn.count}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
{statusButtons.map((button) => <button key={button.key} onClick={() => setStatusFilter(button.key)} className={`rounded-full px-3 py-1.5 text-sm font-medium transition-colors ${statusFilter === button.key ? button.active : button.base}`}>
|
||||
{button.label}<span className={`ml-1.5 rounded-full px-1.5 py-0.5 text-xs font-bold ${statusFilter === button.key ? "bg-white/20" : "bg-white/70"}`}>{button.count}</span>
|
||||
</button>)}
|
||||
</div>
|
||||
|
||||
{/* Séparateur */}
|
||||
<div className="h-6 w-px bg-gray-200" />
|
||||
|
||||
{/* Filtre Période */}
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="hidden h-6 w-px bg-blue-200 sm:block" />
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<CalendarDays className="h-4 w-4 text-gray-500" />
|
||||
<Select value={yearFilter} onValueChange={(v) => { setYearFilter(v); if (v === "all") setMonthFilter("all"); }}>
|
||||
<SelectTrigger className="h-8 w-28 text-sm">
|
||||
<SelectValue placeholder="Année" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Toute année</SelectItem>
|
||||
{YEARS.map((y) => (
|
||||
<SelectItem key={y} value={y}>{y}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select
|
||||
value={monthFilter}
|
||||
onValueChange={setMonthFilter}
|
||||
disabled={yearFilter === "all"}
|
||||
>
|
||||
<SelectTrigger className="h-8 w-32 text-sm">
|
||||
<SelectValue placeholder="Mois" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Tous les mois</SelectItem>
|
||||
{MONTHS.map((m) => (
|
||||
<SelectItem key={m.value} value={m.value}>{m.label}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{hasPeriodFilter && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-8 px-2 text-gray-500 hover:text-gray-700"
|
||||
onClick={() => { setYearFilter("all"); setMonthFilter("all"); }}
|
||||
title="Réinitialiser la période"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
<Select value={yearFilter} onValueChange={(value) => { setYearFilter(value); if (value === "all") setMonthFilter("all"); }}><SelectTrigger className="h-8 w-28 bg-white text-sm"><SelectValue placeholder="Année" /></SelectTrigger><SelectContent><SelectItem value="all">Toute année</SelectItem>{YEARS.map((year) => <SelectItem key={year} value={year}>{year}</SelectItem>)}</SelectContent></Select>
|
||||
<Select value={monthFilter} onValueChange={setMonthFilter} disabled={yearFilter === "all"}><SelectTrigger className="h-8 w-32 bg-white text-sm"><SelectValue placeholder="Mois" /></SelectTrigger><SelectContent><SelectItem value="all">Tous les mois</SelectItem>{MONTHS.map((month) => <SelectItem key={month.value} value={month.value}>{month.label}</SelectItem>)}</SelectContent></Select>
|
||||
<Select value={sourceFilter} onValueChange={(value) => setSourceFilter(value as SourceFilter)}><SelectTrigger className="h-8 w-28 bg-white text-sm"><SelectValue placeholder="Source" /></SelectTrigger><SelectContent><SelectItem value="all">Toute source</SelectItem><SelectItem value="email">E-mail</SelectItem><SelectItem value="folder">Dossier</SelectItem><SelectItem value="file">Fichier</SelectItem></SelectContent></Select>
|
||||
<Select value={triggerFilter} onValueChange={(value) => setTriggerFilter(value as TriggerFilter)}><SelectTrigger className="h-8 w-32 bg-white text-sm"><SelectValue placeholder="Déclencheur" /></SelectTrigger><SelectContent><SelectItem value="all">Tout mode</SelectItem><SelectItem value="manual">Manuel</SelectItem><SelectItem value="automatic">Planifié</SelectItem><SelectItem value="unknown">Non tracé</SelectItem></SelectContent></Select>
|
||||
{hasFilters && <Button variant="ghost" size="sm" className="h-8 px-2 text-gray-500" onClick={() => { setYearFilter("all"); setMonthFilter("all"); setSourceFilter("all"); setTriggerFilter("all"); }} title="Réinitialiser les filtres"><X className="h-4 w-4" /></Button>}
|
||||
</div>
|
||||
<span className="ml-auto text-sm text-gray-500">{filteredLogs.length} import{filteredLogs.length > 1 ? "s" : ""} · {counts.totalDetected} facture{counts.totalDetected > 1 ? "s" : ""} détectée{counts.totalDetected > 1 ? "s" : ""}</span>
|
||||
</CardContent></Card>
|
||||
|
||||
{/* Résumé */}
|
||||
<span className="text-sm text-gray-500 ml-auto">
|
||||
{filteredLogs.length} import{filteredLogs.length > 1 ? "s" : ""} — {counts.totalDetected} facture{counts.totalDetected > 1 ? "s" : ""} détectée{counts.totalDetected > 1 ? "s" : ""}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Tableau */}
|
||||
<Card>
|
||||
<CardContent className="p-0">
|
||||
{isLoading ? (
|
||||
<div className="text-center py-12 text-gray-500">
|
||||
<Package className="w-10 h-10 mx-auto mb-3 text-gray-300 animate-pulse" />
|
||||
<p>Chargement...</p>
|
||||
</div>
|
||||
) : filteredLogs.length > 0 ? (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow className="bg-gray-50">
|
||||
<TableHead className="font-semibold">Fichier importé</TableHead>
|
||||
<TableHead className="font-semibold">Date d'import</TableHead>
|
||||
<TableHead className="font-semibold text-center">Source</TableHead>
|
||||
<TableHead className="font-semibold text-center">Détectées</TableHead>
|
||||
<TableHead className="font-semibold text-center">Importées</TableHead>
|
||||
<TableHead className="font-semibold text-center">Doublons</TableHead>
|
||||
<TableHead className="font-semibold text-center">Erreurs</TableHead>
|
||||
<TableHead className="font-semibold text-center w-20">Détails</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filteredLogs.map((log) => (
|
||||
<TableRow key={log.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<TableCell className="font-medium max-w-xs truncate" title={log.fileName}>
|
||||
<div className="flex items-center gap-2">
|
||||
<FileText className="h-4 w-4 text-blue-500 shrink-0" />
|
||||
<span className="truncate">{log.fileName}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm text-gray-600">
|
||||
{new Date(log.importedAt).toLocaleString("fr-FR")}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
{log.importSource === "email" ? (
|
||||
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-700 border border-blue-200">
|
||||
<Mail className="h-3 w-3" />
|
||||
Email
|
||||
</span>
|
||||
) : log.importSource === "folder" ? (
|
||||
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-700 border border-purple-200">
|
||||
<FolderOpen className="h-3 w-3" />
|
||||
Dossier
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-700 border border-gray-200">
|
||||
<Upload className="h-3 w-3" />
|
||||
Fichier
|
||||
</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
<Badge variant="outline" className="font-mono">
|
||||
{log.totalInvoicesDetected}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
{log.invoicesImported > 0 ? (
|
||||
<Badge className="bg-green-100 text-green-800 hover:bg-green-100 font-mono">
|
||||
{log.invoicesImported}
|
||||
</Badge>
|
||||
) : (
|
||||
<span className="text-gray-400 text-sm">0</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
{log.duplicatesIgnored > 0 ? (
|
||||
<Badge className="bg-yellow-100 text-yellow-800 hover:bg-yellow-100 font-mono">
|
||||
{log.duplicatesIgnored}
|
||||
</Badge>
|
||||
) : (
|
||||
<span className="text-gray-400">-</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
{log.errors > 0 ? (
|
||||
<Badge className="bg-red-100 text-red-800 hover:bg-red-100 font-mono">
|
||||
{log.errors}
|
||||
</Badge>
|
||||
) : (
|
||||
<span className="text-gray-400">-</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-8 px-2 text-blue-600 border-blue-200 hover:bg-blue-50 hover:border-blue-400"
|
||||
title="Voir les détails de cet import"
|
||||
onClick={() => {
|
||||
setSelectedLog(log);
|
||||
setDetailDialogOpen(true);
|
||||
}}
|
||||
>
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
<Card><CardContent className="overflow-x-auto p-0">
|
||||
{isLoading ? <div className="py-12 text-center text-gray-500"><Package className="mx-auto mb-3 h-10 w-10 animate-pulse text-gray-300" /><p>Chargement de l’historique…</p></div> : filteredLogs.length > 0 ? (
|
||||
<Table><TableHeader><TableRow className="bg-gray-50"><TableHead className="font-semibold">Fichier importé</TableHead><TableHead className="font-semibold">Date et heure</TableHead><TableHead className="font-semibold">Compte</TableHead><TableHead className="text-center font-semibold">Source</TableHead><TableHead className="text-center font-semibold">Mode</TableHead><TableHead className="text-center font-semibold">Résultat</TableHead><TableHead className="w-20 text-center font-semibold">Détails</TableHead></TableRow></TableHeader>
|
||||
<TableBody>{filteredLogs.map((log) => <TableRow key={log.id} className="hover:bg-gray-50/50"><TableCell className="max-w-xs font-medium" title={log.fileName}><div className="flex items-center gap-2"><FileText className="h-4 w-4 shrink-0 text-blue-500" /><span className="truncate">{log.fileName}</span></div></TableCell><TableCell className="whitespace-nowrap text-sm text-gray-600"><div className="flex items-center gap-1"><Clock3 className="h-3.5 w-3.5 text-gray-400" />{new Date(log.importedAt).toLocaleString("fr-FR")}</div></TableCell><TableCell className="max-w-48 text-sm text-gray-600"><div className="flex items-center gap-1.5 truncate"><UserRound className="h-3.5 w-3.5 shrink-0 text-gray-400" /><span className="truncate" title={log.userEmail || "Compte supprimé"}>{log.userName?.trim() || log.userEmail || "Compte supprimé"}</span></div></TableCell><TableCell className="text-center"><SourceBadge source={log.importSource} /></TableCell><TableCell className="text-center"><TriggerBadge trigger={log.importTrigger} /></TableCell><TableCell className="text-center"><span className="font-mono text-sm text-green-700">+{log.invoicesImported}</span>{log.duplicatesIgnored > 0 && <span className="ml-1 font-mono text-xs text-yellow-700">/ {log.duplicatesIgnored} d.</span>}{log.errors > 0 && <span className="ml-1 font-mono text-xs text-red-700">/ {log.errors} e.</span>}</TableCell><TableCell className="text-center"><Button size="sm" variant="outline" className="h-8 px-2 text-blue-600" title="Voir les détails de cet import" onClick={() => { setSelectedLog(log); setDetailDialogOpen(true); }}><Eye className="h-4 w-4" /></Button></TableCell></TableRow>)}</TableBody>
|
||||
</Table>
|
||||
) : (
|
||||
<div className="text-center py-12 text-gray-500">
|
||||
<HistoryIcon className="w-12 h-12 mx-auto mb-3 text-gray-300" />
|
||||
<p className="font-medium">Aucun import trouvé</p>
|
||||
<p className="text-sm mt-1">Modifiez les filtres pour afficher plus de résultats</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : <div className="py-12 text-center text-gray-500"><HistoryIcon className="mx-auto mb-3 h-12 w-12 text-gray-300" /><p className="font-medium">Aucun import trouvé</p><p className="mt-1 text-sm">Modifiez les filtres pour afficher plus de résultats.</p></div>}
|
||||
</CardContent></Card>
|
||||
</div>
|
||||
|
||||
{/* Dialog visualisation détails */}
|
||||
<Dialog open={detailDialogOpen} onOpenChange={setDetailDialogOpen}>
|
||||
<DialogContent className="max-w-2xl max-h-[80vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<FileText className="h-5 w-5 text-blue-500" />
|
||||
Détails de l'import
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
{selectedLog && (
|
||||
<div className="space-y-4">
|
||||
{/* Infos générales */}
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="bg-gray-50 rounded-lg p-3">
|
||||
<p className="text-xs text-gray-500 font-medium mb-1">Fichier</p>
|
||||
<p className="text-sm font-semibold break-all">{selectedLog.fileName}</p>
|
||||
</div>
|
||||
<div className="bg-gray-50 rounded-lg p-3">
|
||||
<p className="text-xs text-gray-500 font-medium mb-1">Date d'import</p>
|
||||
<p className="text-sm font-semibold">{new Date(selectedLog.importedAt).toLocaleString("fr-FR")}</p>
|
||||
</div>
|
||||
<div className="bg-gray-50 rounded-lg p-3">
|
||||
<p className="text-xs text-gray-500 font-medium mb-1">Source import</p>
|
||||
<p className="text-sm font-semibold">
|
||||
{selectedLog.importSource === "email" ? (
|
||||
<span className="inline-flex items-center gap-1 text-blue-700"><Mail className="h-4 w-4" /> Email</span>
|
||||
) : selectedLog.importSource === "folder" ? (
|
||||
<span className="inline-flex items-center gap-1 text-purple-700"><FolderOpen className="h-4 w-4" /> Dossier</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 text-gray-700"><Upload className="h-4 w-4" /> Fichier</span>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Statistiques */}
|
||||
<div className="grid grid-cols-4 gap-2">
|
||||
<div className="text-center bg-gray-50 rounded-lg p-3">
|
||||
<p className="text-2xl font-bold text-gray-700">{selectedLog.totalInvoicesDetected}</p>
|
||||
<p className="text-xs text-gray-500 mt-1">Détectées</p>
|
||||
</div>
|
||||
<div className="text-center bg-green-50 rounded-lg p-3">
|
||||
<p className="text-2xl font-bold text-green-700">{selectedLog.invoicesImported}</p>
|
||||
<p className="text-xs text-green-600 mt-1">Importées</p>
|
||||
</div>
|
||||
<div className="text-center bg-yellow-50 rounded-lg p-3">
|
||||
<p className="text-2xl font-bold text-yellow-700">{selectedLog.duplicatesIgnored}</p>
|
||||
<p className="text-xs text-yellow-600 mt-1">Doublons</p>
|
||||
</div>
|
||||
<div className="text-center bg-red-50 rounded-lg p-3">
|
||||
<p className="text-2xl font-bold text-red-600">{selectedLog.errors}</p>
|
||||
<p className="text-xs text-red-500 mt-1">Erreurs</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Détails doublons */}
|
||||
{selectedLog.duplicateDetails && (() => {
|
||||
try {
|
||||
const details = JSON.parse(selectedLog.duplicateDetails);
|
||||
if (Array.isArray(details) && details.length > 0) {
|
||||
return (
|
||||
<div>
|
||||
<h3 className="text-sm font-semibold text-yellow-700 mb-2 flex items-center gap-1">
|
||||
<RotateCcw className="h-4 w-4" />
|
||||
Doublons ignorés ({details.length})
|
||||
</h3>
|
||||
<div className="space-y-1 max-h-40 overflow-y-auto">
|
||||
{details.map((d: string | { supplier?: string; invoiceNumber?: string; reason?: string }, i: number) => (
|
||||
<div key={i} className="text-xs bg-yellow-50 border border-yellow-100 rounded px-3 py-1.5 text-yellow-800">
|
||||
{typeof d === "string" ? d : `${d.supplier || ""} — ${d.invoiceNumber || ""} ${d.reason ? `(${d.reason})` : ""}`}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
} catch { /* JSON invalide */ }
|
||||
return null;
|
||||
})()}
|
||||
|
||||
{/* Détails erreurs */}
|
||||
{selectedLog.errorDetails && (() => {
|
||||
try {
|
||||
const details = JSON.parse(selectedLog.errorDetails);
|
||||
if (Array.isArray(details) && details.length > 0) {
|
||||
return (
|
||||
<div>
|
||||
<h3 className="text-sm font-semibold text-red-600 mb-2 flex items-center gap-1">
|
||||
<AlertTriangle className="h-4 w-4" />
|
||||
Erreurs ({details.length})
|
||||
</h3>
|
||||
<div className="space-y-1 max-h-40 overflow-y-auto">
|
||||
{details.map((d: string | { message?: string; file?: string }, i: number) => (
|
||||
<div key={i} className="text-xs bg-red-50 border border-red-100 rounded px-3 py-1.5 text-red-700">
|
||||
{typeof d === "string" ? d : `${d.file ? `[${d.file}] ` : ""}${d.message || ""}`}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
} catch { /* JSON invalide */ }
|
||||
return null;
|
||||
})()}
|
||||
</div>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
<Dialog open={detailDialogOpen} onOpenChange={setDetailDialogOpen}><DialogContent className="max-h-[80vh] max-w-2xl overflow-y-auto"><DialogHeader><DialogTitle className="flex items-center gap-2"><FileText className="h-5 w-5 text-blue-500" />Détails de l’import</DialogTitle></DialogHeader>
|
||||
{selectedLog && <div className="space-y-4"><div className="grid gap-3 sm:grid-cols-2"><div className="rounded-lg bg-gray-50 p-3"><p className="mb-1 text-xs font-medium text-gray-500">Fichier</p><p className="break-all text-sm font-semibold">{selectedLog.fileName}</p></div><div className="rounded-lg bg-gray-50 p-3"><p className="mb-1 text-xs font-medium text-gray-500">Date et heure d’import</p><p className="text-sm font-semibold">{new Date(selectedLog.importedAt).toLocaleString("fr-FR")}</p></div><div className="rounded-lg bg-gray-50 p-3"><p className="mb-1 text-xs font-medium text-gray-500">Compte responsable</p><p className="text-sm font-semibold">{selectedLog.userName?.trim() || selectedLog.userEmail || "Compte supprimé"}</p>{selectedLog.userName && selectedLog.userEmail && <p className="mt-0.5 text-xs text-gray-500">{selectedLog.userEmail}</p>}</div><div className="rounded-lg bg-gray-50 p-3"><p className="mb-1 text-xs font-medium text-gray-500">Origine</p><div className="flex items-center gap-2"><SourceBadge source={selectedLog.importSource} /><TriggerBadge trigger={selectedLog.importTrigger} /></div></div><div className="rounded-lg bg-gray-50 p-3"><p className="mb-1 text-xs font-medium text-gray-500">Fichier source</p><p className="text-sm font-semibold">ID {selectedLog.sourceFileId} · {selectedLog.sourceStatus || "indisponible"}</p>{selectedLog.sourceCreatedAt && <p className="mt-0.5 text-xs text-gray-500">Créé le {new Date(selectedLog.sourceCreatedAt).toLocaleString("fr-FR")}</p>}</div></div>
|
||||
<div className="grid grid-cols-4 gap-2"><div className="rounded-lg bg-gray-50 p-3 text-center"><p className="text-2xl font-bold text-gray-700">{selectedLog.totalInvoicesDetected}</p><p className="mt-1 text-xs text-gray-500">Détectées</p></div><div className="rounded-lg bg-green-50 p-3 text-center"><p className="text-2xl font-bold text-green-700">{selectedLog.invoicesImported}</p><p className="mt-1 text-xs text-green-600">Importées</p></div><div className="rounded-lg bg-yellow-50 p-3 text-center"><p className="text-2xl font-bold text-yellow-700">{selectedLog.duplicatesIgnored}</p><p className="mt-1 text-xs text-yellow-600">Doublons</p></div><div className="rounded-lg bg-red-50 p-3 text-center"><p className="text-2xl font-bold text-red-600">{selectedLog.errors}</p><p className="mt-1 text-xs text-red-600">Erreurs</p></div></div>
|
||||
{selectedLog.warningMessage && <div className="rounded-lg border border-amber-200 bg-amber-50 p-3 text-sm text-amber-800"><strong>Avertissement :</strong> {selectedLog.warningMessage}</div>}
|
||||
{selectedLog.duplicateDetails && <DetailsList title="Doublons ignorés" tone="yellow" value={selectedLog.duplicateDetails} icon={<RotateCcw className="h-4 w-4" />} />}
|
||||
{selectedLog.errorDetails && <DetailsList title="Erreurs" tone="red" value={selectedLog.errorDetails} icon={<AlertTriangle className="h-4 w-4" />} />}
|
||||
</div>}
|
||||
</DialogContent></Dialog>
|
||||
</DashboardLayout>
|
||||
);
|
||||
}
|
||||
|
||||
function DetailsList({ title, tone, value, icon }: { title: string; tone: "yellow" | "red"; value: string; icon: React.ReactNode }) {
|
||||
try {
|
||||
const details = JSON.parse(value);
|
||||
if (!Array.isArray(details) || details.length === 0) return null;
|
||||
const styles = tone === "yellow" ? "border-yellow-100 bg-yellow-50 text-yellow-800" : "border-red-100 bg-red-50 text-red-700";
|
||||
const heading = tone === "yellow" ? "text-yellow-700" : "text-red-600";
|
||||
return <div><h3 className={`mb-2 flex items-center gap-1 text-sm font-semibold ${heading}`}>{icon}{title} ({details.length})</h3><div className="max-h-40 space-y-1 overflow-y-auto">{details.map((detail: string | Record<string, unknown>, index: number) => <div key={index} className={`rounded border px-3 py-1.5 text-xs ${styles}`}>{typeof detail === "string" ? detail : Object.values(detail).filter(Boolean).join(" — ")}</div>)}</div></div>;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import AzureAdExportSettingsCard from "@/components/AzureAdExportSettingsCard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
@@ -333,25 +334,21 @@ export default function ImportSettings() {
|
||||
</div>
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">
|
||||
Paramètres import / export
|
||||
Paramètres
|
||||
</h1>
|
||||
<p className="text-muted-foreground mt-0.5 text-sm">
|
||||
Configurez les méthodes d'importation et les options d'export des factures BAP
|
||||
Configurez les méthodes d’importation et la sauvegarde de la base de données
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Onglets Import / Export */}
|
||||
{/* Les destinations d’export sont administrées dans Automatismes d’export. */}
|
||||
<Tabs defaultValue="import" className="w-full">
|
||||
<TabsList className="grid w-full grid-cols-3 h-12 mb-6">
|
||||
<TabsList className="grid w-full grid-cols-2 h-12 mb-6">
|
||||
<TabsTrigger value="import" className="flex items-center gap-2 text-base">
|
||||
<ArrowDownToLine className="w-4 h-4" />
|
||||
<Inbox className="w-4 h-4" />
|
||||
Paramètres d'import
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="export" className="flex items-center gap-2 text-base">
|
||||
<Share2 className="w-4 h-4" />
|
||||
Paramètres d'export
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="backup" className="flex items-center gap-2 text-base">
|
||||
<DatabaseBackup className="w-4 h-4" />
|
||||
Sauvegarde DB
|
||||
@@ -361,6 +358,8 @@ export default function ImportSettings() {
|
||||
{/* ===== ONGLET IMPORT ===== */}
|
||||
<TabsContent value="import" className="space-y-6">
|
||||
|
||||
<AzureAdExportSettingsCard />
|
||||
|
||||
{/* Import manuel */}
|
||||
<Card className="border-2 hover:border-primary/50 transition-colors">
|
||||
<CardHeader className="bg-gradient-to-r from-purple-50 to-pink-50 dark:from-purple-950/20 dark:to-pink-950/20 border-b">
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useParams, useLocation } from "wouter";
|
||||
import { useParams, useLocation, useSearch } from "wouter";
|
||||
import DashboardLayout from "@/components/DashboardLayout";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@@ -9,10 +9,13 @@ import { Badge } from "@/components/ui/badge";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { ArrowLeft, Save, FileText } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { getBapReturnLocation } from "@/lib/bapNavigation";
|
||||
|
||||
export default function InvoiceDetail() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const [, setLocation] = useLocation();
|
||||
const detailSearch = useSearch();
|
||||
const returnLocation = getBapReturnLocation(detailSearch);
|
||||
const invoiceId = parseInt(id || "0");
|
||||
|
||||
const { data: invoice, isLoading } = trpc.invoices.getById.useQuery({ id: invoiceId });
|
||||
@@ -172,7 +175,7 @@ export default function InvoiceDetail() {
|
||||
<FileText className="w-16 h-16 text-gray-300 mb-4" />
|
||||
<h2 className="text-2xl font-bold mb-2">Facture introuvable</h2>
|
||||
<p className="text-gray-500 mb-4">Cette facture n'existe pas ou a été supprimée</p>
|
||||
<Button onClick={() => setLocation("/invoices")}>
|
||||
<Button onClick={() => setLocation(returnLocation)}>
|
||||
<ArrowLeft className="w-4 h-4 mr-2" />
|
||||
Retour aux factures
|
||||
</Button>
|
||||
@@ -187,7 +190,7 @@ export default function InvoiceDetail() {
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" onClick={() => setLocation("/invoices")}>
|
||||
<Button variant="ghost" onClick={() => setLocation(returnLocation)}>
|
||||
<ArrowLeft className="w-4 h-4 mr-2" />
|
||||
Retour
|
||||
</Button>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { useMemo, useState } from "react";
|
||||
import DashboardLayout from "@/components/DashboardLayout";
|
||||
import { useAuth } from "@/_core/hooks/useAuth";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
@@ -34,17 +34,28 @@ import {
|
||||
} from "@/components/ui/table";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Search, FileText, Download, FileSpreadsheet, Trash2, Edit, Trash, Filter, LayoutList, LayoutGrid, ArrowUpDown, ArrowUp, ArrowDown } from "lucide-react";
|
||||
import { Search, FileText, Download, FileSpreadsheet, Trash2, Edit, Trash, Filter, LayoutList, LayoutGrid, ArrowUpDown, ArrowUp, ArrowDown, CalendarDays } from "lucide-react";
|
||||
import * as XLSX from 'xlsx';
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { toast } from "sonner";
|
||||
import { useLocation } from "wouter";
|
||||
import { matchesInvoicePeriod } from "@shared/invoicePeriod";
|
||||
|
||||
type SortField = "invoiceDate" | "createdAt";
|
||||
type SortDir = "asc" | "desc";
|
||||
|
||||
export default function Invoices() {
|
||||
type InvoiceScope = "all" | "subscriptions";
|
||||
|
||||
const MONTHS = [
|
||||
["01", "Janvier"], ["02", "Février"], ["03", "Mars"], ["04", "Avril"],
|
||||
["05", "Mai"], ["06", "Juin"], ["07", "Août"], ["08", "Septembre"],
|
||||
["09", "Septembre"], ["10", "Octobre"], ["11", "Novembre"], ["12", "Décembre"],
|
||||
] as const;
|
||||
|
||||
export default function Invoices({ scope = "all" }: { scope?: InvoiceScope }) {
|
||||
const [, setLocation] = useLocation();
|
||||
const currentYear = new Date().getFullYear();
|
||||
const isSubscriptionPage = scope === "subscriptions";
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [compactMode, setCompactMode] = useState(true);
|
||||
const [sortField, setSortField] = useState<SortField>("createdAt");
|
||||
@@ -52,9 +63,11 @@ export default function Invoices() {
|
||||
const [selectedIds, setSelectedIds] = useState<number[]>([]);
|
||||
const [statusFilter, setStatusFilter] = useState<string>("all");
|
||||
const [recipientFilter, setRecipientFilter] = useState<string>("all");
|
||||
const [subscriptionFilter, setSubscriptionFilter] = useState<string>("all"); // all | yes | no
|
||||
const [subscriptionFilter, setSubscriptionFilter] = useState<string>(isSubscriptionPage ? "yes" : "all"); // all | yes | no
|
||||
const [entityFilter, setEntityFilter] = useState<string>("all"); // all | santinova | itinova
|
||||
const [ventilationFilter, setVentilationFilter] = useState<string>("all");
|
||||
const [selectedYear, setSelectedYear] = useState<string>(String(currentYear));
|
||||
const [selectedMonth, setSelectedMonth] = useState<string>("all");
|
||||
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||
const [invoiceToDelete, setInvoiceToDelete] = useState<number | null>(null);
|
||||
const [addDialogOpen, setAddDialogOpen] = useState(false);
|
||||
@@ -194,17 +207,20 @@ export default function Invoices() {
|
||||
};
|
||||
|
||||
// Collect unique recipients for the filter dropdown
|
||||
const uniqueRecipients = Array.from(
|
||||
new Set(
|
||||
(invoices || []).map(inv => (inv as any).recipientName).filter(Boolean)
|
||||
)
|
||||
).sort();
|
||||
const scopedInvoices = useMemo(
|
||||
() => (invoices || []).filter(inv => !isSubscriptionPage || inv.isSubscription === 1),
|
||||
[invoices, isSubscriptionPage],
|
||||
);
|
||||
|
||||
const uniqueVentilations = Array.from(
|
||||
new Set(
|
||||
(invoices || []).map(inv => (inv as any).ventilationComptable).filter(Boolean)
|
||||
)
|
||||
).sort();
|
||||
const uniqueRecipients = useMemo(
|
||||
() => Array.from(new Set(scopedInvoices.map(inv => (inv as any).recipientName).filter(Boolean))).sort(),
|
||||
[scopedInvoices],
|
||||
);
|
||||
|
||||
const uniqueVentilations = useMemo(
|
||||
() => Array.from(new Set(scopedInvoices.map(inv => (inv as any).ventilationComptable).filter(Boolean))).sort(),
|
||||
[scopedInvoices],
|
||||
);
|
||||
|
||||
const handleSort = (field: SortField) => {
|
||||
if (sortField === field) {
|
||||
@@ -220,7 +236,7 @@ export default function Invoices() {
|
||||
return sortDir === "asc" ? <ArrowUp className="w-3 h-3 ml-1" /> : <ArrowDown className="w-3 h-3 ml-1" />;
|
||||
};
|
||||
|
||||
const filteredInvoices = invoices?.filter((inv) => {
|
||||
const filteredInvoices = scopedInvoices.filter((inv) => {
|
||||
// Filter by search query
|
||||
if (searchQuery) {
|
||||
const query = searchQuery.toLowerCase();
|
||||
@@ -230,6 +246,9 @@ export default function Invoices() {
|
||||
if (!matchesSearch) return false;
|
||||
}
|
||||
|
||||
// Même convention que Factures BAP : date de facture, sinon réception.
|
||||
if (!matchesInvoicePeriod(inv, selectedYear, selectedMonth)) return false;
|
||||
|
||||
// Filter by export status
|
||||
if (statusFilter !== "all") {
|
||||
if (statusFilter === "exported" && inv.exportStatus !== "exported") return false;
|
||||
@@ -268,22 +287,17 @@ export default function Invoices() {
|
||||
return true;
|
||||
});
|
||||
|
||||
const statusCounts = {
|
||||
all: invoices?.length || 0,
|
||||
exported: invoices?.filter(inv => inv.exportStatus === "exported").length || 0,
|
||||
not_exported: invoices?.filter(inv => inv.exportStatus === "not_exported").length || 0,
|
||||
export_error: invoices?.filter(inv => inv.exportStatus === "export_error").length || 0,
|
||||
};
|
||||
|
||||
// Old filter logic (to be removed)
|
||||
const _oldFilteredInvoices = invoices?.filter((inv) => {
|
||||
if (!searchQuery) return true;
|
||||
const query = searchQuery.toLowerCase();
|
||||
return (
|
||||
inv.supplierName?.toLowerCase().includes(query) ||
|
||||
inv.invoiceNumber?.toLowerCase().includes(query)
|
||||
const invoicesInPeriod = useMemo(
|
||||
() => scopedInvoices.filter(inv => matchesInvoicePeriod(inv, selectedYear, selectedMonth)),
|
||||
[scopedInvoices, selectedYear, selectedMonth],
|
||||
);
|
||||
});
|
||||
|
||||
const statusCounts = {
|
||||
all: invoicesInPeriod.length,
|
||||
exported: invoicesInPeriod.filter(inv => inv.exportStatus === "exported").length,
|
||||
not_exported: invoicesInPeriod.filter(inv => inv.exportStatus === "not_exported").length,
|
||||
export_error: invoicesInPeriod.filter(inv => inv.exportStatus === "export_error").length,
|
||||
};
|
||||
|
||||
const handleSelectAll = (checked: boolean) => {
|
||||
if (checked) {
|
||||
@@ -370,8 +384,10 @@ export default function Invoices() {
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Factures</h1>
|
||||
<p className="text-gray-500 mt-1">Gérez toutes vos factures importées</p>
|
||||
<h1 className="text-3xl font-bold">{isSubscriptionPage ? "Factures abonnements" : "Factures"}</h1>
|
||||
<p className="text-gray-500 mt-1">
|
||||
{isSubscriptionPage ? "Gérez les factures identifiées comme abonnements" : "Gérez toutes vos factures importées"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
@@ -456,9 +472,30 @@ export default function Invoices() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Ligne 2 : Filtres destinataire, abonnement, entité, ventilation */}
|
||||
{/* Ligne 2 : Filtres de période, destinataire, abonnement, entité, ventilation */}
|
||||
<div className="flex gap-2 items-center flex-wrap mb-3">
|
||||
<Filter className="w-4 h-4 text-blue-400 shrink-0" />
|
||||
<div className="flex items-center gap-1.5 text-sm font-medium text-blue-600">
|
||||
<CalendarDays className="w-4 h-4" />
|
||||
Période :
|
||||
</div>
|
||||
<Select value={selectedYear} onValueChange={(value) => { setSelectedYear(value); if (value === "all") setSelectedMonth("all"); }}>
|
||||
<SelectTrigger className="w-28 h-8 bg-white border-blue-200 text-sm"><SelectValue placeholder="Année" /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Toute année</SelectItem>
|
||||
{Array.from({ length: 5 }, (_, index) => currentYear - index).map(year => <SelectItem key={year} value={String(year)}>{year}</SelectItem>)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={selectedMonth} onValueChange={setSelectedMonth} disabled={selectedYear === "all"}>
|
||||
<SelectTrigger className="w-36 h-8 bg-white border-blue-200 text-sm"><SelectValue placeholder="Mois" /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Tous les mois</SelectItem>
|
||||
{MONTHS.map(([value, label]) => <SelectItem key={value} value={value}>{label}</SelectItem>)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{(selectedYear !== String(currentYear) || selectedMonth !== "all") && (
|
||||
<button onClick={() => { setSelectedYear(String(currentYear)); setSelectedMonth("all"); }} className="text-xs text-blue-600 hover:text-blue-800 underline">Réinitialiser</button>
|
||||
)}
|
||||
<Select value={recipientFilter} onValueChange={setRecipientFilter}>
|
||||
<SelectTrigger className="w-[180px] bg-white border-blue-200 text-sm">
|
||||
<SelectValue placeholder="Destinataire" />
|
||||
@@ -471,7 +508,7 @@ export default function Invoices() {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={subscriptionFilter} onValueChange={setSubscriptionFilter}>
|
||||
<Select value={isSubscriptionPage ? "yes" : subscriptionFilter} onValueChange={setSubscriptionFilter} disabled={isSubscriptionPage}>
|
||||
<SelectTrigger className="w-[170px] bg-white border-blue-200 text-sm">
|
||||
<SelectValue placeholder="Abonnement" />
|
||||
</SelectTrigger>
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { BAP_MIN_QUALITY_SCORE, meetsBapQualityThreshold } from "@shared/bapEligibility";
|
||||
import { Search, FileText, Download, FileSpreadsheet, Trash2, Edit, Trash, CheckCircle, CheckCircle2, ShieldCheck, RefreshCw, FolderDown, ChevronDown, ChevronRight, ChevronsUpDown, CalendarDays, ArrowUpDown, ArrowUp, ArrowDown } from "lucide-react";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
|
||||
@@ -87,7 +88,8 @@ function downloadBapPdf(
|
||||
}
|
||||
import * as XLSX from 'xlsx';
|
||||
import { toast } from "sonner";
|
||||
import { useLocation } from "wouter";
|
||||
import { useLocation, useSearch } from "wouter";
|
||||
import { buildBapDetailLocation, parseBapFilters } from "@/lib/bapNavigation";
|
||||
|
||||
// Helper function to determine field color based on origin
|
||||
const getFieldColor = (invoice: any, fieldName: string): string => {
|
||||
@@ -108,9 +110,15 @@ const getFieldColor = (invoice: any, fieldName: string): string => {
|
||||
|
||||
export default function InvoicesBAP() {
|
||||
const [, setLocation] = useLocation();
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const queryString = useSearch();
|
||||
const currentYear = new Date().getFullYear();
|
||||
const initialFilters = parseBapFilters(queryString, String(currentYear));
|
||||
const [searchQuery, setSearchQuery] = useState(initialFilters.searchQuery);
|
||||
const [selectedIds, setSelectedIds] = useState<number[]>([]);
|
||||
const [statusFilter, setStatusFilter] = useState<string>("all");
|
||||
const [statusFilter, setStatusFilter] = useState<string>(initialFilters.statusFilter);
|
||||
const [recipientFilter, setRecipientFilter] = useState<string>(initialFilters.recipientFilter);
|
||||
const [entityFilter, setEntityFilter] = useState<string>(initialFilters.entityFilter);
|
||||
const [ventilationFilter, setVentilationFilter] = useState<string>(initialFilters.ventilationFilter);
|
||||
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||
const [invoiceToDelete, setInvoiceToDelete] = useState<number | null>(null);
|
||||
const [addDialogOpen, setAddDialogOpen] = useState(false);
|
||||
@@ -126,12 +134,25 @@ export default function InvoicesBAP() {
|
||||
const [expandedIds, setExpandedIds] = useState<Set<number>>(new Set());
|
||||
const [allExpanded, setAllExpanded] = useState(false);
|
||||
// Filtre par période
|
||||
const currentYear = new Date().getFullYear();
|
||||
const [selectedYear, setSelectedYear] = useState<string>(String(currentYear));
|
||||
const [selectedMonth, setSelectedMonth] = useState<string>("all"); // "all" ou "01".."12"
|
||||
const [selectedYear, setSelectedYear] = useState<string>(initialFilters.selectedYear);
|
||||
const [selectedMonth, setSelectedMonth] = useState<string>(initialFilters.selectedMonth); // "all" ou "01".."12"
|
||||
// Tri
|
||||
const [sortField, setSortField] = useState<"invoiceDate" | "createdAt">("createdAt");
|
||||
const [sortDir, setSortDir] = useState<"asc" | "desc">("desc");
|
||||
const [sortField, setSortField] = useState<"invoiceDate" | "createdAt">(initialFilters.sortField);
|
||||
const [sortDir, setSortDir] = useState<"asc" | "desc">(initialFilters.sortDir);
|
||||
|
||||
const openInvoiceDetail = (invoiceId: number) => {
|
||||
setLocation(buildBapDetailLocation(invoiceId, {
|
||||
searchQuery,
|
||||
statusFilter,
|
||||
selectedYear,
|
||||
selectedMonth,
|
||||
recipientFilter,
|
||||
entityFilter,
|
||||
ventilationFilter,
|
||||
sortField,
|
||||
sortDir,
|
||||
}));
|
||||
};
|
||||
|
||||
const toggleRow = (id: number) => {
|
||||
setExpandedIds(prev => {
|
||||
@@ -154,6 +175,14 @@ export default function InvoicesBAP() {
|
||||
const { data: allInvoices, isLoading } = trpc.invoices.list.useQuery();
|
||||
// Filter for BAP invoices only (Abonnement = NON, isSubscription = 0)
|
||||
const invoices = allInvoices?.filter(inv => inv.isSubscription === 0);
|
||||
const uniqueRecipients = useMemo(
|
||||
() => Array.from(new Set((invoices || []).map(inv => (inv as any).recipientName).filter(Boolean))).sort(),
|
||||
[invoices],
|
||||
);
|
||||
const uniqueVentilations = useMemo(
|
||||
() => Array.from(new Set((invoices || []).map(inv => (inv as any).ventilationComptable).filter(Boolean))).sort(),
|
||||
[invoices],
|
||||
);
|
||||
// IDs des factures déjà validées BAP (pour charger leurs pdfUrl depuis bapHistory)
|
||||
// Stabilisé avec useMemo pour éviter les re-renders infinis (anti-pattern tRPC)
|
||||
const validatedInvoiceIds = useMemo(
|
||||
@@ -380,7 +409,7 @@ export default function InvoicesBAP() {
|
||||
// Déclarée ici (avant filteredInvoices et statusCounts) pour éviter le hoisting error
|
||||
const isEligibleForBAPValidation = (invoice: any) => {
|
||||
return (
|
||||
(invoice.qualityScore || 0) === 100 &&
|
||||
meetsBapQualityThreshold(invoice.qualityScore) &&
|
||||
invoice.exportStatus !== "exported" &&
|
||||
invoice.isSubscription === 0 &&
|
||||
invoice.serviceConcerne &&
|
||||
@@ -426,6 +455,27 @@ export default function InvoicesBAP() {
|
||||
if (statusFilter === "to_complete" && !isToComplete(inv)) return false;
|
||||
}
|
||||
|
||||
// Même convention que la page Factures : valeur vide et entités déduites du service.
|
||||
if (recipientFilter !== "all") {
|
||||
if (recipientFilter === "__empty__") {
|
||||
if ((inv as any).recipientName) return false;
|
||||
} else if ((inv as any).recipientName !== recipientFilter) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (entityFilter !== "all") {
|
||||
const service = ((inv as any).serviceConcerne || "").trim().toUpperCase();
|
||||
if (entityFilter === "santinova" && service !== "DSI SANTINOVA") return false;
|
||||
if (entityFilter === "itinova" && service === "DSI SANTINOVA") return false;
|
||||
}
|
||||
if (ventilationFilter !== "all") {
|
||||
if (ventilationFilter === "__empty__") {
|
||||
if ((inv as any).ventilationComptable) return false;
|
||||
} else if ((inv as any).ventilationComptable !== ventilationFilter) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -512,19 +562,19 @@ export default function InvoicesBAP() {
|
||||
|
||||
const getQualityBadge = (score: number | null) => {
|
||||
if (score === null) return <Badge variant="outline">-</Badge>;
|
||||
if (score === 100) return <Badge className="bg-green-100 text-green-800 hover:bg-green-100">{score}%</Badge>;
|
||||
if (meetsBapQualityThreshold(score)) return <Badge className="bg-green-100 text-green-800 hover:bg-green-100">{score}%</Badge>;
|
||||
if (score >= 80) return <Badge className="bg-yellow-100 text-yellow-800 hover:bg-yellow-100">{score}%</Badge>;
|
||||
return <Badge className="bg-red-100 text-red-800 hover:bg-red-100">{score}%</Badge>;
|
||||
};
|
||||
|
||||
const isEligibleForExport = (invoice: any) => {
|
||||
// Une facture BAP est exportable si :
|
||||
// 1. Score = 100%
|
||||
// 1. Score >= 90 % (seuil BAP)
|
||||
// 2. Type d'achat rempli
|
||||
// 3. Service concerné rempli
|
||||
// 4. Ventilation comptable remplie
|
||||
return (
|
||||
(invoice.qualityScore || 0) === 100 &&
|
||||
meetsBapQualityThreshold(invoice.qualityScore) &&
|
||||
invoice.typeAchat &&
|
||||
invoice.serviceConcerne &&
|
||||
invoice.ventilationComptable
|
||||
@@ -535,7 +585,7 @@ export default function InvoicesBAP() {
|
||||
|
||||
const getBAPValidationTooltip = (invoice: any): string => {
|
||||
const reasons: string[] = [];
|
||||
if ((invoice.qualityScore || 0) < 100) reasons.push("Score < 100%");
|
||||
if (!meetsBapQualityThreshold(invoice.qualityScore)) reasons.push(`Score < ${BAP_MIN_QUALITY_SCORE}%`);
|
||||
if (invoice.exportStatus === "exported") reasons.push("Déjà exportée");
|
||||
if (invoice.isSubscription !== 0) reasons.push("Marquée comme abonnement");
|
||||
if (!invoice.serviceConcerne) reasons.push("Service manquant");
|
||||
@@ -641,7 +691,7 @@ export default function InvoicesBAP() {
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (confirm("Valider en BAP toutes les factures éligibles (score 100%, champs remplis, non abonnement) ? Les PDFs annotés seront générés automatiquement.")) {
|
||||
if (confirm(`Valider en BAP toutes les factures éligibles (score ≥ ${BAP_MIN_QUALITY_SCORE}%, champs remplis, non abonnement) ? Les PDFs annotés seront générés automatiquement.`)) {
|
||||
validateBAPBulkMutation.mutate();
|
||||
}
|
||||
}}
|
||||
@@ -763,6 +813,40 @@ export default function InvoicesBAP() {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={recipientFilter} onValueChange={setRecipientFilter}>
|
||||
<SelectTrigger className="w-[180px] h-8 text-sm bg-white border-emerald-200">
|
||||
<SelectValue placeholder="Destinataire" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Tous destinataires</SelectItem>
|
||||
<SelectItem value="__empty__">Sans destinataire</SelectItem>
|
||||
{uniqueRecipients.map((recipient) => (
|
||||
<SelectItem key={recipient} value={recipient}>{recipient}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={entityFilter} onValueChange={setEntityFilter}>
|
||||
<SelectTrigger className="w-[150px] h-8 text-sm bg-white border-emerald-200">
|
||||
<SelectValue placeholder="Entité" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Toutes entités</SelectItem>
|
||||
<SelectItem value="santinova">SANTINOVA</SelectItem>
|
||||
<SelectItem value="itinova">ITINOVA</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select value={ventilationFilter} onValueChange={setVentilationFilter}>
|
||||
<SelectTrigger className="w-[170px] h-8 text-sm bg-white border-emerald-200">
|
||||
<SelectValue placeholder="Ventilation" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Toutes ventilations</SelectItem>
|
||||
<SelectItem value="__empty__">Sans ventilation</SelectItem>
|
||||
{uniqueVentilations.map((ventilation) => (
|
||||
<SelectItem key={ventilation} value={ventilation}>{ventilation}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{(selectedYear !== "all" || selectedMonth !== "all") && (
|
||||
<button
|
||||
onClick={() => { setSelectedYear(String(currentYear)); setSelectedMonth("all"); }}
|
||||
@@ -926,7 +1010,7 @@ export default function InvoicesBAP() {
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
<button
|
||||
onClick={() => setLocation(`/invoices/${invoice.id}`)}
|
||||
onClick={() => openInvoiceDetail(invoice.id)}
|
||||
className="text-blue-600 hover:text-blue-800 hover:underline"
|
||||
>
|
||||
{invoice.supplierName || "Inconnu"}
|
||||
@@ -976,7 +1060,7 @@ export default function InvoicesBAP() {
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => setLocation(`/invoices/${invoice.id}`)}
|
||||
onClick={() => openInvoiceDetail(invoice.id)}
|
||||
className="h-8 px-2"
|
||||
title="Modifier"
|
||||
>
|
||||
|
||||
90
client/src/pages/RealBudget.tsx
Normal file
90
client/src/pages/RealBudget.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
import DashboardLayout from "@/components/DashboardLayout";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { buildSupplierBudgetSummary } from "@shared/invoiceAnalytics";
|
||||
import { CalendarDays, Euro, FileText } from "lucide-react";
|
||||
import { useMemo, useState } from "react";
|
||||
|
||||
const MONTHS = [
|
||||
["01", "Janvier"], ["02", "Février"], ["03", "Mars"], ["04", "Avril"],
|
||||
["05", "Mai"], ["06", "Juin"], ["07", "Juillet"], ["08", "Août"],
|
||||
["09", "Septembre"], ["10", "Octobre"], ["11", "Novembre"], ["12", "Décembre"],
|
||||
] as const;
|
||||
|
||||
function formatCurrency(value: number) {
|
||||
return new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" }).format(value);
|
||||
}
|
||||
|
||||
export default function RealBudget() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
const [selectedYear, setSelectedYear] = useState(String(currentYear));
|
||||
const [selectedMonth, setSelectedMonth] = useState("all");
|
||||
const { data: invoices, isLoading } = trpc.invoices.list.useQuery();
|
||||
|
||||
const availableYears = useMemo(() => {
|
||||
const years = new Set<number>([currentYear]);
|
||||
(invoices || []).forEach((invoice) => {
|
||||
const value = invoice.invoiceDate ?? invoice.createdAt;
|
||||
if (!value) return;
|
||||
const date = new Date(value);
|
||||
if (!Number.isNaN(date.getTime())) years.add(date.getFullYear());
|
||||
});
|
||||
return Array.from(years).sort((a, b) => b - a);
|
||||
}, [invoices, currentYear]);
|
||||
|
||||
const rows = useMemo(
|
||||
() => buildSupplierBudgetSummary(invoices || [], selectedYear, selectedMonth),
|
||||
[invoices, selectedYear, selectedMonth],
|
||||
);
|
||||
const totals = useMemo(() => rows.reduce((total, row) => ({
|
||||
subscriptionAmount: total.subscriptionAmount + row.subscriptionAmount,
|
||||
nonSubscriptionAmount: total.nonSubscriptionAmount + row.nonSubscriptionAmount,
|
||||
totalAmount: total.totalAmount + row.totalAmount,
|
||||
}), { subscriptionAmount: 0, nonSubscriptionAmount: 0, totalAmount: 0 }), [rows]);
|
||||
|
||||
return (
|
||||
<DashboardLayout>
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Budget réel</h1>
|
||||
<p className="mt-1 text-muted-foreground">Montants réellement facturés, regroupés par fournisseur.</p>
|
||||
</div>
|
||||
|
||||
<Card className="border-blue-100 bg-blue-50/70">
|
||||
<CardContent className="flex flex-wrap items-center gap-3 py-4">
|
||||
<div className="flex items-center gap-1.5 text-sm font-medium text-blue-700"><CalendarDays className="h-4 w-4" /> Période :</div>
|
||||
<Select value={selectedYear} onValueChange={(value) => { setSelectedYear(value); if (value === "all") setSelectedMonth("all"); }}>
|
||||
<SelectTrigger className="w-28 bg-white"><SelectValue placeholder="Année" /></SelectTrigger>
|
||||
<SelectContent><SelectItem value="all">Toute année</SelectItem>{availableYears.map((year) => <SelectItem key={year} value={String(year)}>{year}</SelectItem>)}</SelectContent>
|
||||
</Select>
|
||||
<Select value={selectedMonth} onValueChange={setSelectedMonth} disabled={selectedYear === "all"}>
|
||||
<SelectTrigger className="w-40 bg-white"><SelectValue placeholder="Mois" /></SelectTrigger>
|
||||
<SelectContent><SelectItem value="all">Tous les mois</SelectItem>{MONTHS.map(([value, label]) => <SelectItem key={value} value={value}>{label}</SelectItem>)}</SelectContent>
|
||||
</Select>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<Card><CardHeader className="pb-2"><CardTitle className="text-sm font-medium">Abonnements</CardTitle></CardHeader><CardContent><div className="text-2xl font-bold text-violet-700">{formatCurrency(totals.subscriptionAmount)}</div></CardContent></Card>
|
||||
<Card><CardHeader className="pb-2"><CardTitle className="text-sm font-medium">Hors abonnement</CardTitle></CardHeader><CardContent><div className="text-2xl font-bold text-blue-700">{formatCurrency(totals.nonSubscriptionAmount)}</div></CardContent></Card>
|
||||
<Card><CardHeader className="pb-2"><CardTitle className="flex items-center gap-2 text-sm font-medium"><Euro className="h-4 w-4" /> Total facturé</CardTitle></CardHeader><CardContent><div className="text-2xl font-bold text-emerald-700">{formatCurrency(totals.totalAmount)}</div></CardContent></Card>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader><CardTitle>Budget réel par fournisseur</CardTitle><CardDescription>Les factures finalisées sont séparées entre abonnements et hors abonnement.</CardDescription></CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? <div className="py-12 text-center text-muted-foreground">Chargement du budget…</div> : rows.length === 0 ? <div className="flex flex-col items-center gap-2 py-12 text-muted-foreground"><FileText className="h-10 w-10" />Aucune facture finalisée pour cette période.</div> : (
|
||||
<div className="overflow-x-auto rounded-lg border">
|
||||
<table className="w-full min-w-[850px] text-sm"><thead className="bg-muted/60 text-left text-muted-foreground"><tr><th className="px-4 py-3 font-medium">Fournisseur</th><th className="px-4 py-3 text-right font-medium">Abonnements</th><th className="px-4 py-3 text-right font-medium">Montant abonnements</th><th className="px-4 py-3 text-right font-medium">Hors abonnement</th><th className="px-4 py-3 text-right font-medium">Montant hors abonnement</th><th className="px-4 py-3 text-right font-medium">Total</th></tr></thead>
|
||||
<tbody>{rows.map((row) => <tr key={row.supplierName} className="border-t hover:bg-muted/30"><td className="px-4 py-3 font-medium">{row.supplierName}</td><td className="px-4 py-3 text-right">{row.subscriptionCount}</td><td className="px-4 py-3 text-right text-violet-700">{formatCurrency(row.subscriptionAmount)}</td><td className="px-4 py-3 text-right">{row.nonSubscriptionCount}</td><td className="px-4 py-3 text-right text-blue-700">{formatCurrency(row.nonSubscriptionAmount)}</td><td className="px-4 py-3 text-right font-semibold">{formatCurrency(row.totalAmount)}</td></tr>)}</tbody>
|
||||
<tfoot className="border-t-2 bg-muted/50 font-semibold"><tr><td className="px-4 py-3">Total</td><td colSpan={2} className="px-4 py-3 text-right text-violet-700">{formatCurrency(totals.subscriptionAmount)}</td><td colSpan={2} className="px-4 py-3 text-right text-blue-700">{formatCurrency(totals.nonSubscriptionAmount)}</td><td className="px-4 py-3 text-right text-emerald-700">{formatCurrency(totals.totalAmount)}</td></tr></tfoot>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</DashboardLayout>
|
||||
);
|
||||
}
|
||||
2
drizzle/0037_goofy_quentin_quire.sql
Normal file
2
drizzle/0037_goofy_quentin_quire.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE `sourceFiles` ADD `contentHash` varchar(64);--> statement-breakpoint
|
||||
ALTER TABLE `sourceFiles` ADD CONSTRAINT `source_file_content_hash_unique` UNIQUE(`contentHash`);
|
||||
1
drizzle/0038_late_thunderbolt.sql
Normal file
1
drizzle/0038_late_thunderbolt.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE `importLogs` ADD `importTrigger` enum('manual','automatic','unknown') DEFAULT 'unknown' NOT NULL;
|
||||
17
drizzle/0039_good_carmella_unuscione.sql
Normal file
17
drizzle/0039_good_carmella_unuscione.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
CREATE TABLE `exportAutomationRules` (
|
||||
`id` int AUTO_INCREMENT NOT NULL,
|
||||
`userId` int NOT NULL,
|
||||
`name` varchar(255) NOT NULL,
|
||||
`isActive` int NOT NULL DEFAULT 1,
|
||||
`priority` int NOT NULL DEFAULT 0,
|
||||
`conditionField` enum('recipientName','ventilationComptable') NOT NULL,
|
||||
`conditionValue` varchar(255) NOT NULL,
|
||||
`destinationType` enum('local','teams','sharepoint') NOT NULL,
|
||||
`destinationPath` text NOT NULL,
|
||||
`openInBrowser` int NOT NULL DEFAULT 0,
|
||||
`createdAt` timestamp NOT NULL DEFAULT (now()),
|
||||
`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT `exportAutomationRules_id` PRIMARY KEY(`id`)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX `export_rule_user_priority_idx` ON `exportAutomationRules` (`userId`,`priority`);
|
||||
2372
drizzle/meta/0037_snapshot.json
Normal file
2372
drizzle/meta/0037_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
2380
drizzle/meta/0038_snapshot.json
Normal file
2380
drizzle/meta/0038_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
2496
drizzle/meta/0039_snapshot.json
Normal file
2496
drizzle/meta/0039_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -260,6 +260,27 @@
|
||||
"when": 1785419093588,
|
||||
"tag": "0036_broken_rattler",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 37,
|
||||
"version": "5",
|
||||
"when": 1787394418464,
|
||||
"tag": "0037_goofy_quentin_quire",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 38,
|
||||
"version": "5",
|
||||
"when": 1788349114141,
|
||||
"tag": "0038_late_thunderbolt",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 39,
|
||||
"version": "5",
|
||||
"when": 1788350633569,
|
||||
"tag": "0039_good_carmella_unuscione",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { int, mysqlEnum, mysqlTable, text, timestamp, varchar, uniqueIndex, decimal } from "drizzle-orm/mysql-core";
|
||||
import { decimal, index, int, mysqlEnum, mysqlTable, text, timestamp, uniqueIndex, varchar } from "drizzle-orm/mysql-core";
|
||||
|
||||
/**
|
||||
* Core user table backing auth flow.
|
||||
@@ -36,12 +36,16 @@ export const sourceFiles = mysqlTable("sourceFiles", {
|
||||
fileName: varchar("fileName", { length: 255 }).notNull(),
|
||||
fileKey: text("fileKey").notNull(), // Local storage key with YYYY-MM prefix
|
||||
fileUrl: text("fileUrl").notNull(), // Public URL
|
||||
/** Empreinte du PDF source, globale à l'application pour bloquer tout réimport identique. */
|
||||
contentHash: varchar("contentHash", { length: 64 }),
|
||||
totalInvoicesDetected: int("totalInvoicesDetected").default(0).notNull(),
|
||||
processingStatus: mysqlEnum("processingStatus", ["processing", "completed", "error"]).default("processing").notNull(),
|
||||
processingProgress: varchar("processingProgress", { length: 255 }), // Progress message (e.g., "Extraction 3/9 factures...")
|
||||
createdAt: timestamp("createdAt").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updatedAt").defaultNow().onUpdateNow().notNull(),
|
||||
});
|
||||
}, (table) => ({
|
||||
contentHashIdx: uniqueIndex("source_file_content_hash_unique").on(table.contentHash),
|
||||
}));
|
||||
|
||||
export type SourceFile = typeof sourceFiles.$inferSelect;
|
||||
export type InsertSourceFile = typeof sourceFiles.$inferInsert;
|
||||
@@ -227,6 +231,8 @@ export const importLogs = mysqlTable("importLogs", {
|
||||
warningMessage: text("warningMessage"), // Warning message (e.g. quota exhausted)
|
||||
/** Source du mode d'import : 'file' = upload manuel, 'folder' = dossier automatique, 'email' = import par email */
|
||||
importSource: mysqlEnum("importSource", ["file", "folder", "email"]).default("file").notNull(),
|
||||
/** Déclencheur : manuel, planifié automatiquement, ou non tracé pour les historiques antérieurs. */
|
||||
importTrigger: mysqlEnum("importTrigger", ["manual", "automatic", "unknown"]).default("unknown").notNull(),
|
||||
importedAt: timestamp("importedAt").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
@@ -318,6 +324,31 @@ export const automationRules = mysqlTable("automationRules", {
|
||||
export type AutomationRule = typeof automationRules.$inferSelect;
|
||||
export type InsertAutomationRule = typeof automationRules.$inferInsert;
|
||||
|
||||
/**
|
||||
* Règles de destination appliquées lors de la validation BAP.
|
||||
* Elles sont séparées des règles d’import, afin de ne jamais modifier les
|
||||
* champs d’une facture au moment de son export.
|
||||
*/
|
||||
export const exportAutomationRules = mysqlTable("exportAutomationRules", {
|
||||
id: int("id").autoincrement().primaryKey(),
|
||||
userId: int("userId").notNull(),
|
||||
name: varchar("name", { length: 255 }).notNull(),
|
||||
isActive: int("isActive").default(1).notNull(),
|
||||
priority: int("priority").default(0).notNull(),
|
||||
conditionField: mysqlEnum("conditionField", ["recipientName", "ventilationComptable"]).notNull(),
|
||||
conditionValue: varchar("conditionValue", { length: 255 }).notNull(),
|
||||
destinationType: mysqlEnum("destinationType", ["local", "teams", "sharepoint"]).notNull(),
|
||||
destinationPath: text("destinationPath").notNull(),
|
||||
openInBrowser: int("openInBrowser").default(0).notNull(),
|
||||
createdAt: timestamp("createdAt").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updatedAt").defaultNow().onUpdateNow().notNull(),
|
||||
}, (table) => ({
|
||||
userPriorityIdx: index("export_rule_user_priority_idx").on(table.userId, table.priority),
|
||||
}));
|
||||
|
||||
export type ExportAutomationRule = typeof exportAutomationRules.$inferSelect;
|
||||
export type InsertExportAutomationRule = typeof exportAutomationRules.$inferInsert;
|
||||
|
||||
/**
|
||||
* LLM Fields Configuration table
|
||||
* Stores configuration for each field used in invoice extraction
|
||||
|
||||
@@ -53,7 +53,6 @@
|
||||
"@types/archiver": "^7.0.0",
|
||||
"@types/bcrypt": "^6.0.0",
|
||||
"@types/chokidar": "^2.1.7",
|
||||
"@types/imap": "^0.8.43",
|
||||
"@types/jsonwebtoken": "^9.0.10",
|
||||
"@types/mailparser": "^3.4.6",
|
||||
"@types/ssh2-sftp-client": "^9.0.6",
|
||||
@@ -71,7 +70,7 @@
|
||||
"embla-carousel-react": "^8.6.0",
|
||||
"express": "^4.21.2",
|
||||
"framer-motion": "^12.23.22",
|
||||
"imap": "^0.8.19",
|
||||
"imapflow": "^1.7.2",
|
||||
"input-otp": "^1.4.2",
|
||||
"jose": "6.1.0",
|
||||
"jsonwebtoken": "^9.0.3",
|
||||
|
||||
209
pnpm-lock.yaml
generated
209
pnpm-lock.yaml
generated
@@ -133,9 +133,6 @@ importers:
|
||||
'@types/chokidar':
|
||||
specifier: ^2.1.7
|
||||
version: 2.1.7
|
||||
'@types/imap':
|
||||
specifier: ^0.8.43
|
||||
version: 0.8.43
|
||||
'@types/jsonwebtoken':
|
||||
specifier: ^9.0.10
|
||||
version: 9.0.10
|
||||
@@ -187,9 +184,9 @@ importers:
|
||||
framer-motion:
|
||||
specifier: ^12.23.22
|
||||
version: 12.23.22(react-dom@19.2.1(react@19.2.1))(react@19.2.1)
|
||||
imap:
|
||||
specifier: ^0.8.19
|
||||
version: 0.8.19
|
||||
imapflow:
|
||||
specifier: ^1.7.2
|
||||
version: 1.7.2
|
||||
input-otp:
|
||||
specifier: ^1.4.2
|
||||
version: 1.4.2(react-dom@19.2.1(react@19.2.1))(react@19.2.1)
|
||||
@@ -1438,6 +1435,9 @@ packages:
|
||||
'@pdf-lib/upng@1.0.1':
|
||||
resolution: {integrity: sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==}
|
||||
|
||||
'@pinojs/redact@0.4.0':
|
||||
resolution: {integrity: sha512-k2ENnmBugE/rzQfEcdWHcCY+/FM3VLzH9cYEsbdsoqrvzAKRhUZeRNhAZvB8OitQJ1TBed3yqWtdjzS6wJKBwg==}
|
||||
|
||||
'@pkgjs/parseargs@0.11.0':
|
||||
resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==}
|
||||
engines: {node: '>=14'}
|
||||
@@ -2580,9 +2580,6 @@ packages:
|
||||
'@types/http-errors@2.0.5':
|
||||
resolution: {integrity: sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg==}
|
||||
|
||||
'@types/imap@0.8.43':
|
||||
resolution: {integrity: sha512-POPoqrDax9mxM2N4ITZYCWaFtg1ORVfzJe4S7xwSh9aHawdEb7FwWTJYiAhzIvWp7DM+6BajnzYOwZ1BUrqtow==}
|
||||
|
||||
'@types/jsonwebtoken@9.0.10':
|
||||
resolution: {integrity: sha512-asx5hIG9Qmf/1oStypjanR7iKTv0gXQ1Ov/jfrX6kS/EO0OFni8orbmGCn0672NHR3kXHwpAwR+B368ZGN/2rA==}
|
||||
|
||||
@@ -2681,6 +2678,9 @@ packages:
|
||||
'@vitest/utils@2.1.9':
|
||||
resolution: {integrity: sha512-v0psaMSkNJ3A2NMrUEHFRzJtDPFn+/VWZ5WxImB21T9fjucJRmS7xCS3ppEnARb9y11OAzaD+P2Ps+b+BGX5iQ==}
|
||||
|
||||
'@zone-eu/mailsplit@5.4.15':
|
||||
resolution: {integrity: sha512-c7ZpxauvF4AEkDJlKDYO7iMUtMuqJMBnDWNff1cyx+d7zaBVR3iFEmXhNHOVoMmVyVF3pTZLsLIJsEFKDldOAA==}
|
||||
|
||||
'@zone-eu/mailsplit@5.4.8':
|
||||
resolution: {integrity: sha512-eEyACj4JZ7sjzRvy26QhLgKEMWwQbsw1+QZnlLX+/gihcNH07lVPOcnwf5U6UAL7gkc//J3jVd76o/WS+taUiA==}
|
||||
|
||||
@@ -2744,6 +2744,10 @@ packages:
|
||||
asynckit@0.4.0:
|
||||
resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
|
||||
|
||||
atomic-sleep@1.0.0:
|
||||
resolution: {integrity: sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==}
|
||||
engines: {node: '>=8.0.0'}
|
||||
|
||||
autoprefixer@10.4.21:
|
||||
resolution: {integrity: sha512-O+A6LWV5LDHSJD3LjHYoNi4VLsj/Whi7k6zG12xTYaU4cQ8oxQGckXNX8cRHK5yOZ/ppVHe0ZBXGzSV9jXdVbQ==}
|
||||
engines: {node: ^10 || ^12 || >=14}
|
||||
@@ -3542,12 +3546,15 @@ packages:
|
||||
resolution: {integrity: sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
|
||||
iconv-lite@0.7.3:
|
||||
resolution: {integrity: sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
|
||||
ieee754@1.2.1:
|
||||
resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
|
||||
|
||||
imap@0.8.19:
|
||||
resolution: {integrity: sha512-z5DxEA1uRnZG73UcPA4ES5NSCGnPuuouUx43OPX7KZx1yzq3N8/vx2mtXEShT5inxB3pRgnfG1hijfu7XN2YMw==}
|
||||
engines: {node: '>=0.8.0'}
|
||||
imapflow@1.7.2:
|
||||
resolution: {integrity: sha512-1pWZgWQ/M2Q7kPSW7Sp7QDn+ZPEqs/9IymYh34RY+3J7d3vfPayhSmRAl0tB7weblGU0SR/t7eYES3TW6vSiOQ==}
|
||||
|
||||
inherits@2.0.4:
|
||||
resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==}
|
||||
@@ -3565,6 +3572,10 @@ packages:
|
||||
iobuffer@5.4.0:
|
||||
resolution: {integrity: sha512-DRebOWuqDvxunfkNJAlc3IzWIPD5xVxwUNbHr7xKB8E6aLJxIPfNX3CoMJghcFjpv6RWQsrcJbghtEwSPoJqMA==}
|
||||
|
||||
ip-address@10.5.0:
|
||||
resolution: {integrity: sha512-R5SnVLJmgYYvf2F2ZgwSBnelz5G4q5AxIC277GDfUaNbrZKNANcBC7RHqYYePlszf4kBolVkJauG0ZjHHFh55g==}
|
||||
engines: {node: '>= 12'}
|
||||
|
||||
ipaddr.js@1.9.1:
|
||||
resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==}
|
||||
engines: {node: '>= 0.10'}
|
||||
@@ -3598,9 +3609,6 @@ packages:
|
||||
resolution: {integrity: sha512-e6rvdUCiQCAuumZslxRJWR/Doq4VpPR82kqclvcS0efgt430SlGIk05vdCN58+VrzgtIcfNODjozVielycD4Sw==}
|
||||
engines: {node: '>=16'}
|
||||
|
||||
isarray@0.0.1:
|
||||
resolution: {integrity: sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==}
|
||||
|
||||
isarray@1.0.0:
|
||||
resolution: {integrity: sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==}
|
||||
|
||||
@@ -3661,6 +3669,9 @@ packages:
|
||||
libmime@5.3.7:
|
||||
resolution: {integrity: sha512-FlDb3Wtha8P01kTL3P9M+ZDNDWPKPmKHWaU/cG/lg5pfuAwdflVpZE+wm9m7pKmC5ww6s+zTxBKS1p6yl3KpSw==}
|
||||
|
||||
libmime@5.4.2:
|
||||
resolution: {integrity: sha512-+IQnCOdPiufGBkOii+Ze8F7iniyBzOwvWDbn1DyExBpc9pT2B3IEMQi7GUc/PpqhNUh/sr1SG9UXDITQoR0VIA==}
|
||||
|
||||
libqp@2.1.1:
|
||||
resolution: {integrity: sha512-0Wd+GPz1O134cP62YU2GTOPNA7Qgl09XwCqM5zpBv87ERCXdfDtyKXvV7c9U22yWJh44QZqBocFnXN11K96qow==}
|
||||
|
||||
@@ -3931,6 +3942,10 @@ packages:
|
||||
resolution: {integrity: sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==}
|
||||
engines: {node: '>= 0.4'}
|
||||
|
||||
on-exit-leak-free@2.1.2:
|
||||
resolution: {integrity: sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==}
|
||||
engines: {node: '>=14.0.0'}
|
||||
|
||||
on-finished@2.4.1:
|
||||
resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==}
|
||||
engines: {node: '>= 0.8'}
|
||||
@@ -4008,6 +4023,16 @@ packages:
|
||||
resolution: {integrity: sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
pino-abstract-transport@3.0.0:
|
||||
resolution: {integrity: sha512-wlfUczU+n7Hy/Ha5j9a/gZNy7We5+cXp8YL+X+PG8S0KXxw7n/JXA3c46Y0zQznIJ83URJiwy7Lh56WLokNuxg==}
|
||||
|
||||
pino-std-serializers@7.1.0:
|
||||
resolution: {integrity: sha512-BndPH67/JxGExRgiX1dX0w1FvZck5Wa4aal9198SrRhZjH3GxKQUKIBnYJTdj2HDN3UQAS06HlfcSbQj2OHmaw==}
|
||||
|
||||
pino@10.3.1:
|
||||
resolution: {integrity: sha512-r34yH/GlQpKZbU1BvFFqOjhISRo1MNx1tWYsYvmj6KIRHSPMT2+yHOEb1SG6NMvRoHRF0a07kCOox/9yakl1vg==}
|
||||
hasBin: true
|
||||
|
||||
pnpm@10.18.0:
|
||||
resolution: {integrity: sha512-6AT4ifHOzEDVctsITuw+SIFzn43sacD/ENLRvv+aTjCTg7ontbdQBZ1/TBSVNbbNDSyx7Trrc5I5pChKaPQM+g==}
|
||||
engines: {node: '>=18.12'}
|
||||
@@ -4032,6 +4057,9 @@ packages:
|
||||
process-nextick-args@2.0.1:
|
||||
resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==}
|
||||
|
||||
process-warning@5.1.0:
|
||||
resolution: {integrity: sha512-jQSaVHsPgtyw60e1rQ/A+/ArPEj/S8pS/vFnyGa/gYFXrKk/6RuDkoqVDQ5NI5MmS01698ltlAk0NoDBNLujRw==}
|
||||
|
||||
process@0.11.10:
|
||||
resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==}
|
||||
engines: {node: '>= 0.6.0'}
|
||||
@@ -4054,6 +4082,9 @@ packages:
|
||||
resolution: {integrity: sha512-+38qI9SOr8tfZ4QmJNplMUxqjbe7LKvvZgWdExBOmd+egZTtjLB67Gu0HRX3u/XOq7UU2Nx6nsjvS16Z9uwfpg==}
|
||||
engines: {node: '>=0.6'}
|
||||
|
||||
quick-format-unescaped@4.0.4:
|
||||
resolution: {integrity: sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==}
|
||||
|
||||
raf@3.4.1:
|
||||
resolution: {integrity: sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA==}
|
||||
|
||||
@@ -4154,9 +4185,6 @@ packages:
|
||||
resolution: {integrity: sha512-DGrYcCWK7tvYMnWh79yrPHt+vdx9tY+1gPZa7nJQtO/p8bLTDaHp4dzwEhQB7pZ4Xe3ok4XKuEPrVuc+wlpkmw==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
|
||||
readable-stream@1.1.14:
|
||||
resolution: {integrity: sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==}
|
||||
|
||||
readable-stream@2.3.8:
|
||||
resolution: {integrity: sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==}
|
||||
|
||||
@@ -4175,6 +4203,13 @@ packages:
|
||||
resolution: {integrity: sha512-9u/XQ1pvrQtYyMpZe7DXKv2p5CNvyVwzUB6uhLAnQwHMSgKMBR62lc7AHljaeteeHXn11XTAaLLUVZYVZyuRBQ==}
|
||||
engines: {node: '>= 20.19.0'}
|
||||
|
||||
real-require@0.2.0:
|
||||
resolution: {integrity: sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==}
|
||||
engines: {node: '>= 12.13.0'}
|
||||
|
||||
real-require@1.0.0:
|
||||
resolution: {integrity: sha512-P4nbQYQfePJxRSmY+v/KINxVucm4NF3p3s7pJveMTtom52FR4YGltUQLB8idDXwDDWW+eYrWDFbuzUnjoWHF7g==}
|
||||
|
||||
recharts-scale@0.4.5:
|
||||
resolution: {integrity: sha512-kivNFO+0OcUNu7jQquLXAxz1FIwZj8nrj+YkOKc5694NbjCvcT6aSZiIzNzd2Kul4o4rTto8QVR9lMNtxD4G1w==}
|
||||
|
||||
@@ -4214,6 +4249,10 @@ packages:
|
||||
safe-buffer@5.2.1:
|
||||
resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==}
|
||||
|
||||
safe-stable-stringify@2.5.0:
|
||||
resolution: {integrity: sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
safer-buffer@2.1.2:
|
||||
resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}
|
||||
|
||||
@@ -4223,10 +4262,6 @@ packages:
|
||||
selderee@0.11.0:
|
||||
resolution: {integrity: sha512-5TF+l7p4+OsnP8BCCvSyZiSPc4x4//p5uPwK8TCnVPJYRmU2aYKMpOXvw8zM5a5JvuuCGN1jmsMwuU2W02ukfA==}
|
||||
|
||||
semver@5.3.0:
|
||||
resolution: {integrity: sha512-mfmm3/H9+67MCVix1h+IXTpDwL6710LyHuk7+cWC9T1mE0qz4iHhh6r4hU2wrIT9iTsAAC2XQRvfblL028cpLw==}
|
||||
hasBin: true
|
||||
|
||||
semver@6.3.1:
|
||||
resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==}
|
||||
hasBin: true
|
||||
@@ -4285,6 +4320,17 @@ packages:
|
||||
resolution: {integrity: sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==}
|
||||
engines: {node: '>=14'}
|
||||
|
||||
smart-buffer@4.2.0:
|
||||
resolution: {integrity: sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==}
|
||||
engines: {node: '>= 6.0.0', npm: '>= 3.0.0'}
|
||||
|
||||
socks@2.8.9:
|
||||
resolution: {integrity: sha512-LJhUYUvItdQ0LkJTmPeaEObWXAqFyfmP85x0tch/ez9cahmhlBBLbIqDFnvBnUJGagb0JbIQrkBs1wJ+yRYpEw==}
|
||||
engines: {node: '>= 10.0.0', npm: '>= 3.0.0'}
|
||||
|
||||
sonic-boom@4.2.1:
|
||||
resolution: {integrity: sha512-w6AxtubXa2wTXAUsZMMWERrsIRAdrK0Sc+FUytWvYAhBJLyuI4llrMIC1DtlNSdI99EI86KZum2MMq3EAZlF9Q==}
|
||||
|
||||
sonner@2.0.7:
|
||||
resolution: {integrity: sha512-W6ZN4p58k8aDKA4XPcx2hpIQXBRAgyiWVkYhT7CvK6D3iAu7xjvVyhQHg2/iaKJZ1XVJ4r7XuwGL+WGEK37i9w==}
|
||||
peerDependencies:
|
||||
@@ -4302,6 +4348,10 @@ packages:
|
||||
resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
|
||||
split2@4.2.0:
|
||||
resolution: {integrity: sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==}
|
||||
engines: {node: '>= 10.x'}
|
||||
|
||||
sqlstring@2.3.3:
|
||||
resolution: {integrity: sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==}
|
||||
engines: {node: '>= 0.6'}
|
||||
@@ -4343,9 +4393,6 @@ packages:
|
||||
resolution: {integrity: sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
string_decoder@0.10.31:
|
||||
resolution: {integrity: sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==}
|
||||
|
||||
string_decoder@1.1.1:
|
||||
resolution: {integrity: sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==}
|
||||
|
||||
@@ -4402,6 +4449,10 @@ packages:
|
||||
text-segmentation@1.0.3:
|
||||
resolution: {integrity: sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw==}
|
||||
|
||||
thread-stream@4.2.0:
|
||||
resolution: {integrity: sha512-e2zZ96wSChazBsbENf/Pcm/4swHt2cEKQ92rhUjkL9GCKiTDJIaTBenjE/m9DXi0QBmTMDkFDdOomUy20A1tDQ==}
|
||||
engines: {node: '>=20'}
|
||||
|
||||
tiny-invariant@1.3.3:
|
||||
resolution: {integrity: sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==}
|
||||
|
||||
@@ -4508,9 +4559,6 @@ packages:
|
||||
peerDependencies:
|
||||
react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
|
||||
|
||||
utf7@1.0.2:
|
||||
resolution: {integrity: sha512-qQrPtYLLLl12NF4DrM9CvfkxkYI97xOb5dsnGZHE3teFr0tWiEZ9UdgMPczv24vl708cYMpe6mGXGHrotIp3Bw==}
|
||||
|
||||
util-deprecate@1.0.2:
|
||||
resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==}
|
||||
|
||||
@@ -5883,6 +5931,8 @@ snapshots:
|
||||
dependencies:
|
||||
pako: 1.0.11
|
||||
|
||||
'@pinojs/redact@0.4.0': {}
|
||||
|
||||
'@pkgjs/parseargs@0.11.0':
|
||||
optional: true
|
||||
|
||||
@@ -7136,10 +7186,6 @@ snapshots:
|
||||
|
||||
'@types/http-errors@2.0.5': {}
|
||||
|
||||
'@types/imap@0.8.43':
|
||||
dependencies:
|
||||
'@types/node': 24.7.0
|
||||
|
||||
'@types/jsonwebtoken@9.0.10':
|
||||
dependencies:
|
||||
'@types/ms': 2.1.0
|
||||
@@ -7269,6 +7315,12 @@ snapshots:
|
||||
loupe: 3.2.1
|
||||
tinyrainbow: 1.2.0
|
||||
|
||||
'@zone-eu/mailsplit@5.4.15':
|
||||
dependencies:
|
||||
libbase64: 1.3.0
|
||||
libmime: 5.4.2
|
||||
libqp: 2.1.1
|
||||
|
||||
'@zone-eu/mailsplit@5.4.8':
|
||||
dependencies:
|
||||
libbase64: 1.3.0
|
||||
@@ -7338,6 +7390,8 @@ snapshots:
|
||||
|
||||
asynckit@0.4.0: {}
|
||||
|
||||
atomic-sleep@1.0.0: {}
|
||||
|
||||
autoprefixer@10.4.21(postcss@8.5.6):
|
||||
dependencies:
|
||||
browserslist: 4.26.3
|
||||
@@ -8118,12 +8172,22 @@ snapshots:
|
||||
dependencies:
|
||||
safer-buffer: 2.1.2
|
||||
|
||||
iconv-lite@0.7.3:
|
||||
dependencies:
|
||||
safer-buffer: 2.1.2
|
||||
|
||||
ieee754@1.2.1: {}
|
||||
|
||||
imap@0.8.19:
|
||||
imapflow@1.7.2:
|
||||
dependencies:
|
||||
readable-stream: 1.1.14
|
||||
utf7: 1.0.2
|
||||
'@zone-eu/mailsplit': 5.4.15
|
||||
encoding-japanese: 2.2.0
|
||||
iconv-lite: 0.7.3
|
||||
libbase64: 1.3.0
|
||||
libmime: 5.4.2
|
||||
libqp: 2.1.1
|
||||
pino: 10.3.1
|
||||
socks: 2.8.9
|
||||
|
||||
inherits@2.0.4: {}
|
||||
|
||||
@@ -8136,6 +8200,8 @@ snapshots:
|
||||
|
||||
iobuffer@5.4.0: {}
|
||||
|
||||
ip-address@10.5.0: {}
|
||||
|
||||
ipaddr.js@1.9.1: {}
|
||||
|
||||
is-docker@3.0.0: {}
|
||||
@@ -8156,8 +8222,6 @@ snapshots:
|
||||
dependencies:
|
||||
is-inside-container: 1.0.0
|
||||
|
||||
isarray@0.0.1: {}
|
||||
|
||||
isarray@1.0.0: {}
|
||||
|
||||
isexe@2.0.0: {}
|
||||
@@ -8232,6 +8296,13 @@ snapshots:
|
||||
libbase64: 1.3.0
|
||||
libqp: 2.1.1
|
||||
|
||||
libmime@5.4.2:
|
||||
dependencies:
|
||||
encoding-japanese: 2.2.0
|
||||
iconv-lite: 0.7.3
|
||||
libbase64: 1.3.0
|
||||
libqp: 2.1.1
|
||||
|
||||
libqp@2.1.1: {}
|
||||
|
||||
lightningcss-darwin-arm64@1.30.1:
|
||||
@@ -8439,6 +8510,8 @@ snapshots:
|
||||
|
||||
object-inspect@1.13.4: {}
|
||||
|
||||
on-exit-leak-free@2.1.2: {}
|
||||
|
||||
on-finished@2.4.1:
|
||||
dependencies:
|
||||
ee-first: 1.1.1
|
||||
@@ -8508,6 +8581,26 @@ snapshots:
|
||||
|
||||
picomatch@4.0.3: {}
|
||||
|
||||
pino-abstract-transport@3.0.0:
|
||||
dependencies:
|
||||
split2: 4.2.0
|
||||
|
||||
pino-std-serializers@7.1.0: {}
|
||||
|
||||
pino@10.3.1:
|
||||
dependencies:
|
||||
'@pinojs/redact': 0.4.0
|
||||
atomic-sleep: 1.0.0
|
||||
on-exit-leak-free: 2.1.2
|
||||
pino-abstract-transport: 3.0.0
|
||||
pino-std-serializers: 7.1.0
|
||||
process-warning: 5.1.0
|
||||
quick-format-unescaped: 4.0.4
|
||||
real-require: 0.2.0
|
||||
safe-stable-stringify: 2.5.0
|
||||
sonic-boom: 4.2.1
|
||||
thread-stream: 4.2.0
|
||||
|
||||
pnpm@10.18.0: {}
|
||||
|
||||
postcss-selector-parser@6.0.10:
|
||||
@@ -8527,6 +8620,8 @@ snapshots:
|
||||
|
||||
process-nextick-args@2.0.1: {}
|
||||
|
||||
process-warning@5.1.0: {}
|
||||
|
||||
process@0.11.10: {}
|
||||
|
||||
prop-types@15.8.1:
|
||||
@@ -8548,6 +8643,8 @@ snapshots:
|
||||
dependencies:
|
||||
side-channel: 1.1.0
|
||||
|
||||
quick-format-unescaped@4.0.4: {}
|
||||
|
||||
raf@3.4.1:
|
||||
dependencies:
|
||||
performance-now: 2.1.0
|
||||
@@ -8650,13 +8747,6 @@ snapshots:
|
||||
|
||||
react@19.2.1: {}
|
||||
|
||||
readable-stream@1.1.14:
|
||||
dependencies:
|
||||
core-util-is: 1.0.3
|
||||
inherits: 2.0.4
|
||||
isarray: 0.0.1
|
||||
string_decoder: 0.10.31
|
||||
|
||||
readable-stream@2.3.8:
|
||||
dependencies:
|
||||
core-util-is: 1.0.3
|
||||
@@ -8687,6 +8777,10 @@ snapshots:
|
||||
|
||||
readdirp@5.0.0: {}
|
||||
|
||||
real-require@0.2.0: {}
|
||||
|
||||
real-require@1.0.0: {}
|
||||
|
||||
recharts-scale@0.4.5:
|
||||
dependencies:
|
||||
decimal.js-light: 2.5.1
|
||||
@@ -8748,6 +8842,8 @@ snapshots:
|
||||
|
||||
safe-buffer@5.2.1: {}
|
||||
|
||||
safe-stable-stringify@2.5.0: {}
|
||||
|
||||
safer-buffer@2.1.2: {}
|
||||
|
||||
scheduler@0.27.0: {}
|
||||
@@ -8756,8 +8852,6 @@ snapshots:
|
||||
dependencies:
|
||||
parseley: 0.12.1
|
||||
|
||||
semver@5.3.0: {}
|
||||
|
||||
semver@6.3.1: {}
|
||||
|
||||
semver@7.7.3: {}
|
||||
@@ -8862,6 +8956,17 @@ snapshots:
|
||||
|
||||
signal-exit@4.1.0: {}
|
||||
|
||||
smart-buffer@4.2.0: {}
|
||||
|
||||
socks@2.8.9:
|
||||
dependencies:
|
||||
ip-address: 10.5.0
|
||||
smart-buffer: 4.2.0
|
||||
|
||||
sonic-boom@4.2.1:
|
||||
dependencies:
|
||||
atomic-sleep: 1.0.0
|
||||
|
||||
sonner@2.0.7(react-dom@19.2.1(react@19.2.1))(react@19.2.1):
|
||||
dependencies:
|
||||
react: 19.2.1
|
||||
@@ -8876,6 +8981,8 @@ snapshots:
|
||||
|
||||
source-map@0.6.1: {}
|
||||
|
||||
split2@4.2.0: {}
|
||||
|
||||
sqlstring@2.3.3: {}
|
||||
|
||||
ssf@0.11.2:
|
||||
@@ -8925,8 +9032,6 @@ snapshots:
|
||||
emoji-regex: 9.2.2
|
||||
strip-ansi: 7.2.0
|
||||
|
||||
string_decoder@0.10.31: {}
|
||||
|
||||
string_decoder@1.1.1:
|
||||
dependencies:
|
||||
safe-buffer: 5.1.2
|
||||
@@ -8999,6 +9104,10 @@ snapshots:
|
||||
utrie: 1.0.2
|
||||
optional: true
|
||||
|
||||
thread-stream@4.2.0:
|
||||
dependencies:
|
||||
real-require: 1.0.0
|
||||
|
||||
tiny-invariant@1.3.3: {}
|
||||
|
||||
tinybench@2.9.0: {}
|
||||
@@ -9077,10 +9186,6 @@ snapshots:
|
||||
dependencies:
|
||||
react: 19.2.1
|
||||
|
||||
utf7@1.0.2:
|
||||
dependencies:
|
||||
semver: 5.3.0
|
||||
|
||||
util-deprecate@1.0.2: {}
|
||||
|
||||
utils-merge@1.0.1: {}
|
||||
|
||||
70
scripts/test-imapflow-oauth-from-db.mjs
Normal file
70
scripts/test-imapflow-oauth-from-db.mjs
Normal file
@@ -0,0 +1,70 @@
|
||||
import { ImapFlow } from "imapflow";
|
||||
import mysql from "mysql2/promise";
|
||||
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
if (!databaseUrl) throw new Error("Variable DATABASE_URL manquante");
|
||||
|
||||
const connection = await mysql.createConnection(databaseUrl);
|
||||
try {
|
||||
const [rows] = await connection.query(`
|
||||
SELECT emailImportAddress, emailImportHost, emailImportPort,
|
||||
azureTenantId, azureClientId, azureClientSecret
|
||||
FROM importSettings
|
||||
WHERE emailImportEnabled = 1
|
||||
AND emailImportAuthMode = 'oauth2'
|
||||
ORDER BY id
|
||||
LIMIT 1
|
||||
`);
|
||||
|
||||
const settings = rows[0];
|
||||
if (!settings) throw new Error("Aucune configuration OAuth2 IMAP active");
|
||||
|
||||
const tenantId = settings.azureTenantId || process.env.AZURE_AD_TENANT_ID;
|
||||
const clientId = settings.azureClientId || process.env.AZURE_AD_CLIENT_ID;
|
||||
const clientSecret = settings.azureClientSecret || process.env.AZURE_AD_CLIENT_SECRET;
|
||||
if (!tenantId || !clientId || !clientSecret) {
|
||||
throw new Error("Configuration Azure AD incomplète");
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
scope: "https://outlook.office365.com/.default",
|
||||
grant_type: "client_credentials",
|
||||
}),
|
||||
},
|
||||
);
|
||||
const tokenResponse = await response.json();
|
||||
if (!response.ok || !tokenResponse.access_token) {
|
||||
throw new Error(`Échec OAuth2 : ${tokenResponse.error || response.status}`);
|
||||
}
|
||||
|
||||
const host = settings.emailImportHost || "outlook.office365.com";
|
||||
const client = new ImapFlow({
|
||||
host,
|
||||
port: settings.emailImportPort || 993,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: settings.emailImportAddress,
|
||||
accessToken: tokenResponse.access_token,
|
||||
},
|
||||
tls: { servername: host, rejectUnauthorized: true },
|
||||
verifyOnly: true,
|
||||
logger: false,
|
||||
});
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
console.log(`Authentification ImapFlow OAuth2 réussie pour ${settings.emailImportAddress}`);
|
||||
} finally {
|
||||
if (client.usable) await client.logout().catch(() => client.close());
|
||||
else client.close();
|
||||
}
|
||||
} finally {
|
||||
await connection.end();
|
||||
}
|
||||
49
scripts/test-imapflow-oauth.mjs
Normal file
49
scripts/test-imapflow-oauth.mjs
Normal file
@@ -0,0 +1,49 @@
|
||||
import { ImapFlow } from "imapflow";
|
||||
|
||||
const required = (name) => {
|
||||
const value = process.env[name];
|
||||
if (!value) throw new Error(`Variable ${name} manquante`);
|
||||
return value;
|
||||
};
|
||||
|
||||
const tenantId = required("AZURE_AD_TENANT_ID");
|
||||
const clientId = required("AZURE_AD_CLIENT_ID");
|
||||
const clientSecret = required("AZURE_AD_CLIENT_SECRET");
|
||||
const email = required("IMAP_TEST_EMAIL");
|
||||
const host = process.env.IMAP_TEST_HOST || "outlook.office365.com";
|
||||
|
||||
const response = await fetch(
|
||||
`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
scope: "https://outlook.office365.com/.default",
|
||||
grant_type: "client_credentials",
|
||||
}),
|
||||
},
|
||||
);
|
||||
const tokenResponse = await response.json();
|
||||
if (!response.ok || !tokenResponse.access_token) {
|
||||
throw new Error(`Échec OAuth2 : ${tokenResponse.error || response.status}`);
|
||||
}
|
||||
|
||||
const client = new ImapFlow({
|
||||
host,
|
||||
port: 993,
|
||||
secure: true,
|
||||
auth: { user: email, accessToken: tokenResponse.access_token },
|
||||
tls: { servername: host, rejectUnauthorized: true },
|
||||
verifyOnly: true,
|
||||
logger: false,
|
||||
});
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
console.log(`Authentification ImapFlow OAuth2 réussie pour ${email}`);
|
||||
} finally {
|
||||
if (client.usable) await client.logout().catch(() => client.close());
|
||||
else client.close();
|
||||
}
|
||||
@@ -17,7 +17,8 @@ import { startEmailImportService } from "../emailImportService";
|
||||
import { startFolderImportService } from "../folderImportService";
|
||||
import { handleAzureCallback, isAzureAdConfigured, generateToken, verifyToken } from "../auth";
|
||||
import { createDatabaseBackup } from "../databaseBackup";
|
||||
import { generateStorageKey, localStoragePut } from "../localStorage";
|
||||
import { generateStorageKey, localStorageDelete, localStoragePut } from "../localStorage";
|
||||
import { calculateFileSha256 } from "../fileFingerprint";
|
||||
|
||||
const MAX_WEB_IMPORT_BYTES = 20 * 1024 * 1024;
|
||||
|
||||
@@ -305,7 +306,7 @@ async function startServer() {
|
||||
return;
|
||||
}
|
||||
|
||||
const { getWebImportSourceByToken, createInvoice, findDuplicateInvoice, isInvoiceBlacklisted, updateWebImportSourceStatus, createSourceFile } = await import('../db');
|
||||
const { getWebImportSourceByToken, createInvoice, findDuplicateInvoice, isInvoiceBlacklisted, updateWebImportSourceStatus, createSourceFile, getSourceFileByContentHash } = await import('../db');
|
||||
const source = await getWebImportSourceByToken(apiToken);
|
||||
if (!source) {
|
||||
res.status(401).json({ error: "Token invalide" });
|
||||
@@ -317,15 +318,35 @@ async function startServer() {
|
||||
return;
|
||||
}
|
||||
|
||||
const contentHash = calculateFileSha256(pdfBuffer);
|
||||
const existingSource = await getSourceFileByContentHash(contentHash);
|
||||
if (existingSource) {
|
||||
await updateWebImportSourceStatus(source.id, "success", 0, true);
|
||||
res.json({ success: true, imported: 0, duplicates: 1, total: 1 });
|
||||
return;
|
||||
}
|
||||
|
||||
// Stocker d'abord le PDF de façon persistante, comme les autres sources d'import.
|
||||
const storageKey = generateStorageKey(source.userId, safeFileName);
|
||||
const { url: fileUrl } = await localStoragePut(storageKey, pdfBuffer, "application/pdf");
|
||||
const sourceFile = await createSourceFile({
|
||||
let sourceFile;
|
||||
try {
|
||||
sourceFile = await createSourceFile({
|
||||
userId: source.userId,
|
||||
fileName: safeFileName,
|
||||
fileKey: storageKey,
|
||||
fileUrl,
|
||||
contentHash,
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (error?.code === "ER_DUP_ENTRY" || error?.errno === 1062) {
|
||||
await localStorageDelete(storageKey).catch(() => undefined);
|
||||
await updateWebImportSourceStatus(source.id, "success", 0, true);
|
||||
res.json({ success: true, imported: 0, duplicates: 1, total: 1 });
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const userSettings = await getUserSettings(source.userId);
|
||||
const aiSettings = {
|
||||
aiProvider: userSettings?.aiProvider || "manus",
|
||||
|
||||
@@ -3,10 +3,18 @@ import fs from "fs";
|
||||
import { type Server } from "http";
|
||||
import { nanoid } from "nanoid";
|
||||
import path from "path";
|
||||
import { createServer as createViteServer } from "vite";
|
||||
import viteConfig from "../../vite.config";
|
||||
|
||||
export async function setupVite(app: Express, server: Server) {
|
||||
// Vite et sa configuration sont des dépendances de développement. Les imports
|
||||
// indirects empêchent esbuild de les intégrer au bundle serveur de production.
|
||||
const vitePackageName = "vite";
|
||||
const viteConfigPath = "../../vite.config";
|
||||
const [viteModule, viteConfigModule] = await Promise.all([
|
||||
import(vitePackageName),
|
||||
import(viteConfigPath),
|
||||
]);
|
||||
const createViteServer = viteModule.createServer as typeof import("vite").createServer;
|
||||
const viteConfig = viteConfigModule.default;
|
||||
const serverOptions = {
|
||||
middlewareMode: true,
|
||||
hmr: { server },
|
||||
|
||||
30
server/automationActions.test.ts
Normal file
30
server/automationActions.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { matchesAutomationActionFilter } from "@shared/automationActions";
|
||||
|
||||
describe("matchesAutomationActionFilter", () => {
|
||||
it("filtre chaque type d’action métier", () => {
|
||||
const actions = JSON.stringify({
|
||||
typeAchat: "OPEX",
|
||||
serviceConcerne: "DSI SANTINOVA",
|
||||
ventilationComptable: "SANTINOVA",
|
||||
});
|
||||
|
||||
expect(matchesAutomationActionFilter(actions, "typeAchat")).toBe(true);
|
||||
expect(matchesAutomationActionFilter(actions, "serviceConcerne")).toBe(true);
|
||||
expect(matchesAutomationActionFilter(actions, "ventilationComptable")).toBe(true);
|
||||
expect(matchesAutomationActionFilter(actions, "subscription")).toBe(false);
|
||||
});
|
||||
|
||||
it("distingue les règles Abonnement Oui et Non", () => {
|
||||
expect(matchesAutomationActionFilter('{"isSubscription":1}', "subscription")).toBe(true);
|
||||
expect(matchesAutomationActionFilter('{"isSubscription":1}', "subscriptionYes")).toBe(true);
|
||||
expect(matchesAutomationActionFilter('{"isSubscription":1}', "subscriptionNo")).toBe(false);
|
||||
expect(matchesAutomationActionFilter('{"isSubscription":0}', "subscriptionYes")).toBe(false);
|
||||
expect(matchesAutomationActionFilter('{"isSubscription":0}', "subscriptionNo")).toBe(true);
|
||||
});
|
||||
|
||||
it("tolère les règles historiques invalides sans masquer le filtre Toutes les actions", () => {
|
||||
expect(matchesAutomationActionFilter("json-invalide", "all")).toBe(true);
|
||||
expect(matchesAutomationActionFilter("json-invalide", "subscription")).toBe(false);
|
||||
});
|
||||
});
|
||||
66
server/automationEngine.test.ts
Normal file
66
server/automationEngine.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Invoice } from "../drizzle/schema";
|
||||
import { getAutomationRulesByUser } from "./db";
|
||||
import { applyAutomationRules } from "./automationEngine";
|
||||
|
||||
vi.mock("./db", () => ({
|
||||
getAutomationRulesByUser: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockedGetAutomationRules = vi.mocked(getAutomationRulesByUser);
|
||||
|
||||
const invoice = {
|
||||
id: 1,
|
||||
supplierName: "Microsoft Ireland Operations Ltd",
|
||||
isSubscription: 0,
|
||||
} as Invoice;
|
||||
|
||||
describe("applyAutomationRules", () => {
|
||||
beforeEach(() => {
|
||||
mockedGetAutomationRules.mockReset();
|
||||
});
|
||||
|
||||
it("applique Abonnement = Oui et trace le champ automatisé", async () => {
|
||||
mockedGetAutomationRules.mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
userId: 1,
|
||||
name: "Microsoft est un abonnement",
|
||||
isActive: 1,
|
||||
priority: 1,
|
||||
conditions: JSON.stringify([{ field: "supplierName", operator: "contains", value: "microsoft" }]),
|
||||
conditionsLogic: "AND",
|
||||
actions: JSON.stringify({ isSubscription: 1 }),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
] as never);
|
||||
|
||||
await expect(applyAutomationRules(1, invoice)).resolves.toMatchObject({
|
||||
isSubscription: 1,
|
||||
autoFilledFields: JSON.stringify(["isSubscription"]),
|
||||
});
|
||||
});
|
||||
|
||||
it("autorise Abonnement = Non sans le confondre avec une absence d’action", async () => {
|
||||
mockedGetAutomationRules.mockResolvedValue([
|
||||
{
|
||||
id: 2,
|
||||
userId: 1,
|
||||
name: "Microsoft n’est pas un abonnement",
|
||||
isActive: 1,
|
||||
priority: 1,
|
||||
conditions: JSON.stringify([{ field: "supplierName", operator: "contains", value: "microsoft" }]),
|
||||
conditionsLogic: "AND",
|
||||
actions: JSON.stringify({ isSubscription: 0 }),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
] as never);
|
||||
|
||||
await expect(applyAutomationRules(1, invoice)).resolves.toMatchObject({
|
||||
isSubscription: 0,
|
||||
autoFilledFields: JSON.stringify(["isSubscription"]),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ interface Actions {
|
||||
typeAchat?: string;
|
||||
serviceConcerne?: string;
|
||||
ventilationComptable?: string;
|
||||
isSubscription?: 0 | 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +112,11 @@ export async function applyAutomationRules(
|
||||
updates.ventilationComptable = actions.ventilationComptable;
|
||||
autoFilledFieldsList.push("ventilationComptable");
|
||||
}
|
||||
// 0 est une valeur métier valide : ne jamais la tester par vérité.
|
||||
if (actions.isSubscription !== undefined && updates.isSubscription === undefined) {
|
||||
updates.isSubscription = actions.isSubscription;
|
||||
autoFilledFieldsList.push("isSubscription");
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[AutomationEngine] Error processing rule ${rule.id}:`, error);
|
||||
|
||||
16
server/bapEligibility.test.ts
Normal file
16
server/bapEligibility.test.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { BAP_MIN_QUALITY_SCORE, meetsBapQualityThreshold } from "@shared/bapEligibility";
|
||||
|
||||
describe("meetsBapQualityThreshold", () => {
|
||||
it("autorise exactement le seuil de 90 %", () => {
|
||||
expect(BAP_MIN_QUALITY_SCORE).toBe(90);
|
||||
expect(meetsBapQualityThreshold(90)).toBe(true);
|
||||
expect(meetsBapQualityThreshold(100)).toBe(true);
|
||||
});
|
||||
|
||||
it("refuse les scores inférieurs ou absents", () => {
|
||||
expect(meetsBapQualityThreshold(89)).toBe(false);
|
||||
expect(meetsBapQualityThreshold(null)).toBe(false);
|
||||
expect(meetsBapQualityThreshold(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
61
server/bapNavigation.test.ts
Normal file
61
server/bapNavigation.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildBapDetailLocation,
|
||||
getBapReturnLocation,
|
||||
parseBapFilters,
|
||||
} from "../client/src/lib/bapNavigation";
|
||||
|
||||
describe("navigation Factures BAP", () => {
|
||||
it("transmet tous les filtres et le tri à la page de détail", () => {
|
||||
const location = buildBapDetailLocation(42, {
|
||||
searchQuery: "microsoft europe",
|
||||
statusFilter: "bap_pending",
|
||||
selectedYear: "2026",
|
||||
selectedMonth: "05",
|
||||
recipientFilter: "Direction générale",
|
||||
entityFilter: "santinova",
|
||||
ventilationFilter: "615200",
|
||||
sortField: "invoiceDate",
|
||||
sortDir: "asc",
|
||||
});
|
||||
|
||||
expect(location).toBe(
|
||||
"/invoices/42?returnTo=bap&q=microsoft+europe&status=bap_pending&year=2026&month=05&recipient=Direction+g%C3%A9n%C3%A9rale&entity=santinova&ventilation=615200&sort=invoiceDate&dir=asc"
|
||||
);
|
||||
});
|
||||
|
||||
it("restaure la liste BAP et les filtres de façon sûre", () => {
|
||||
const detailSearch =
|
||||
"?returnTo=bap&q=microsoft+europe&status=bap_pending&year=2026&month=05&recipient=Direction+g%C3%A9n%C3%A9rale&entity=santinova&ventilation=615200&sort=invoiceDate&dir=asc";
|
||||
|
||||
expect(getBapReturnLocation(detailSearch)).toBe(
|
||||
"/invoices-bap?q=microsoft+europe&status=bap_pending&year=2026&month=05&recipient=Direction+g%C3%A9n%C3%A9rale&entity=santinova&ventilation=615200&sort=invoiceDate&dir=asc"
|
||||
);
|
||||
expect(parseBapFilters(detailSearch, "2026")).toEqual({
|
||||
searchQuery: "microsoft europe",
|
||||
statusFilter: "bap_pending",
|
||||
selectedYear: "2026",
|
||||
selectedMonth: "05",
|
||||
recipientFilter: "Direction générale",
|
||||
entityFilter: "santinova",
|
||||
ventilationFilter: "615200",
|
||||
sortField: "invoiceDate",
|
||||
sortDir: "asc",
|
||||
});
|
||||
});
|
||||
|
||||
it("retombe sur les valeurs BAP sûres si les paramètres sont absents ou invalides", () => {
|
||||
expect(parseBapFilters("?returnTo=bap&status=invalid&year=x&sort=nope&dir=down", "2026")).toEqual({
|
||||
searchQuery: "",
|
||||
statusFilter: "all",
|
||||
selectedYear: "2026",
|
||||
selectedMonth: "all",
|
||||
recipientFilter: "all",
|
||||
entityFilter: "all",
|
||||
ventilationFilter: "all",
|
||||
sortField: "createdAt",
|
||||
sortDir: "desc",
|
||||
});
|
||||
expect(getBapReturnLocation("?returnTo=invoices")).toBe("/invoices");
|
||||
});
|
||||
});
|
||||
147
server/db.ts
147
server/db.ts
@@ -1,4 +1,4 @@
|
||||
import { eq, and, desc, sql, inArray } from "drizzle-orm";
|
||||
import { eq, and, desc, sql, inArray, getTableColumns } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import {
|
||||
InsertUser,
|
||||
@@ -30,6 +30,9 @@ import {
|
||||
automationRules,
|
||||
InsertAutomationRule,
|
||||
AutomationRule,
|
||||
exportAutomationRules,
|
||||
InsertExportAutomationRule,
|
||||
ExportAutomationRule,
|
||||
llmFieldsConfig,
|
||||
InsertLlmFieldConfig,
|
||||
LlmFieldConfig,
|
||||
@@ -50,6 +53,7 @@ import {
|
||||
WebImportSource
|
||||
} from "../drizzle/schema";
|
||||
import { ENV } from './_core/env';
|
||||
import { buildAnnualInvoiceSummary } from "@shared/invoiceAnalytics";
|
||||
|
||||
let _db: ReturnType<typeof drizzle> | null = null;
|
||||
|
||||
@@ -204,6 +208,22 @@ export async function createSourceFile(data: InsertSourceFile): Promise<SourceFi
|
||||
return inserted[0]!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche un PDF déjà ingéré, quel que soit le compte utilisateur.
|
||||
* L'import email est partagé entre plusieurs identités : la détection doit donc
|
||||
* être globale pour éviter qu'une même pièce soit retraitée sous chaque compte.
|
||||
*/
|
||||
export async function getSourceFileByContentHash(contentHash: string): Promise<SourceFile | undefined> {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const result = await db
|
||||
.select()
|
||||
.from(sourceFiles)
|
||||
.where(eq(sourceFiles.contentHash, contentHash))
|
||||
.limit(1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export async function getSourceFileById(id: number): Promise<SourceFile | undefined> {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
@@ -316,7 +336,7 @@ export async function searchInvoices(userId: number | null, query: string): Prom
|
||||
.orderBy(desc(invoices.createdAt));
|
||||
}
|
||||
|
||||
export async function getInvoiceStats(userId: number) {
|
||||
export async function getInvoiceStats(userId?: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return {
|
||||
total: 0,
|
||||
@@ -330,10 +350,12 @@ export async function getInvoiceStats(userId: number) {
|
||||
topSuppliers: [],
|
||||
suppliersList: [],
|
||||
topRecipients: [],
|
||||
recipientsList: []
|
||||
recipientsList: [],
|
||||
annualSummary: [],
|
||||
};
|
||||
|
||||
const allInvoices = await getInvoicesByUserId(userId);
|
||||
// Le tableau de bord est global pour les administrateurs, comme les listes Factures.
|
||||
const allInvoices = userId ? await getInvoicesByUserId(userId) : await getAllInvoices();
|
||||
|
||||
// Calculate basic stats
|
||||
const completed = allInvoices.filter(i => i.status === "completed");
|
||||
@@ -442,7 +464,8 @@ export async function getInvoiceStats(userId: number) {
|
||||
topSuppliers,
|
||||
suppliersList,
|
||||
topRecipients,
|
||||
recipientsList
|
||||
recipientsList,
|
||||
annualSummary: buildAnnualInvoiceSummary(allInvoices),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -518,6 +541,44 @@ export async function getAllImportLogs(): Promise<ImportLog[]> {
|
||||
return db.select().from(importLogs).orderBy(desc(importLogs.importedAt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne les informations nécessaires à l'audit d'un import sans exposer
|
||||
* les paramètres sensibles de messagerie. L'administrateur peut ainsi relier
|
||||
* chaque import à son compte, fichier source et déclencheur.
|
||||
*/
|
||||
export async function getImportLogsWithDetailsByUser(userId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
...getTableColumns(importLogs),
|
||||
userName: users.name,
|
||||
userEmail: users.email,
|
||||
sourceCreatedAt: sourceFiles.createdAt,
|
||||
sourceStatus: sourceFiles.processingStatus,
|
||||
})
|
||||
.from(importLogs)
|
||||
.leftJoin(users, eq(importLogs.userId, users.id))
|
||||
.leftJoin(sourceFiles, eq(importLogs.sourceFileId, sourceFiles.id))
|
||||
.where(eq(importLogs.userId, userId))
|
||||
.orderBy(desc(importLogs.importedAt));
|
||||
}
|
||||
|
||||
export async function getAllImportLogsWithDetails() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
...getTableColumns(importLogs),
|
||||
userName: users.name,
|
||||
userEmail: users.email,
|
||||
sourceCreatedAt: sourceFiles.createdAt,
|
||||
sourceStatus: sourceFiles.processingStatus,
|
||||
})
|
||||
.from(importLogs)
|
||||
.leftJoin(users, eq(importLogs.userId, users.id))
|
||||
.leftJoin(sourceFiles, eq(importLogs.sourceFileId, sourceFiles.id))
|
||||
.orderBy(desc(importLogs.importedAt));
|
||||
}
|
||||
|
||||
export async function deleteAllImportLogs(userId: number | null): Promise<void> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
@@ -591,6 +652,24 @@ export async function upsertImportSettings(data: InsertImportSettings): Promise<
|
||||
}
|
||||
}
|
||||
|
||||
/** Liste administrative des comptes e-mail configurés, sans secret IMAP ni Azure AD. */
|
||||
export async function getEmailImportAccounts() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select({
|
||||
userId: importSettings.userId,
|
||||
userName: users.name,
|
||||
userEmail: users.email,
|
||||
emailAddress: importSettings.emailImportAddress,
|
||||
authMode: importSettings.emailImportAuthMode,
|
||||
automaticEnabled: importSettings.emailImportEnabled,
|
||||
isConfigured: sql<number>`CASE WHEN ${importSettings.emailImportAddress} IS NOT NULL AND ${importSettings.emailImportAddress} <> '' AND ${importSettings.emailImportHost} IS NOT NULL AND ${importSettings.emailImportHost} <> '' THEN 1 ELSE 0 END`.as("isConfigured"),
|
||||
})
|
||||
.from(importSettings)
|
||||
.innerJoin(users, eq(importSettings.userId, users.id))
|
||||
.orderBy(users.name, users.email);
|
||||
}
|
||||
|
||||
// ============= DEPARTMENT LIST OPERATIONS =============
|
||||
|
||||
export async function getDepartmentsByUser(_userId?: number): Promise<Department[]> {
|
||||
@@ -671,6 +750,47 @@ export async function deleteAutomationRule(id: number): Promise<void> {
|
||||
await db.delete(automationRules).where(eq(automationRules.id, id));
|
||||
}
|
||||
|
||||
// ============= EXPORT AUTOMATION RULES OPERATIONS =============
|
||||
|
||||
export async function getExportAutomationRulesByUser(userId: number): Promise<ExportAutomationRule[]> {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(exportAutomationRules)
|
||||
.where(eq(exportAutomationRules.userId, userId))
|
||||
.orderBy(exportAutomationRules.priority, exportAutomationRules.id);
|
||||
}
|
||||
|
||||
export async function getExportAutomationRuleById(id: number): Promise<ExportAutomationRule | null> {
|
||||
const db = await getDb();
|
||||
if (!db) return null;
|
||||
const [rule] = await db.select().from(exportAutomationRules).where(eq(exportAutomationRules.id, id));
|
||||
return rule || null;
|
||||
}
|
||||
|
||||
export async function createExportAutomationRule(data: InsertExportAutomationRule): Promise<ExportAutomationRule> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(exportAutomationRules).values(data);
|
||||
const rule = await getExportAutomationRuleById(result[0].insertId);
|
||||
if (!rule) throw new Error("Export automation rule could not be created");
|
||||
return rule;
|
||||
}
|
||||
|
||||
export async function updateExportAutomationRule(id: number, data: Partial<InsertExportAutomationRule>): Promise<ExportAutomationRule> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.update(exportAutomationRules).set(data).where(eq(exportAutomationRules.id, id));
|
||||
const rule = await getExportAutomationRuleById(id);
|
||||
if (!rule) throw new Error("Export automation rule not found");
|
||||
return rule;
|
||||
}
|
||||
|
||||
export async function deleteExportAutomationRule(id: number): Promise<void> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
await db.delete(exportAutomationRules).where(eq(exportAutomationRules.id, id));
|
||||
}
|
||||
|
||||
// ============= INITIALIZE DEFAULT VALUES =============
|
||||
|
||||
export async function initializeDefaultLists(userId: number): Promise<void> {
|
||||
@@ -1218,20 +1338,3 @@ export async function updateWebImportSourceStatus(
|
||||
if (success) update.lastSuccessAt = new Date();
|
||||
await db.update(webImportSources).set(update).where(eq(webImportSources.id, id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a source file with the same fileName already exists for this user
|
||||
* Used to prevent duplicate file storage during email import
|
||||
*/
|
||||
export async function findSourceFileByFileName(userId: number, fileName: string): Promise<any | null> {
|
||||
const db = await getDb();
|
||||
if (!db) return null;
|
||||
const result = await db.select()
|
||||
.from(sourceFiles)
|
||||
.where(and(
|
||||
eq(sourceFiles.userId, userId),
|
||||
eq(sourceFiles.fileName, fileName)
|
||||
))
|
||||
.limit(1);
|
||||
return result[0] || null;
|
||||
}
|
||||
|
||||
78
server/emailImportService.test.ts
Normal file
78
server/emailImportService.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
createImapFlowOptions,
|
||||
type EmailImportConfig,
|
||||
validateEmailImportConfiguration,
|
||||
} from "./emailImportService";
|
||||
|
||||
const baseConfig: EmailImportConfig = {
|
||||
userId: 2,
|
||||
emailAddress: "compta@example.org",
|
||||
password: "secret",
|
||||
host: "outlook.office365.com",
|
||||
port: 993,
|
||||
};
|
||||
|
||||
describe("createImapFlowOptions", () => {
|
||||
it("transmet le jeton brut à ImapFlow pour une authentification OAuth2", () => {
|
||||
const options = createImapFlowOptions(
|
||||
{ ...baseConfig, authMode: "oauth2" },
|
||||
"access-token-value",
|
||||
);
|
||||
|
||||
expect(options.secure).toBe(true);
|
||||
expect(options.auth).toEqual({
|
||||
user: "compta@example.org",
|
||||
accessToken: "access-token-value",
|
||||
});
|
||||
expect(options.auth).not.toHaveProperty("pass");
|
||||
expect(options.tls?.rejectUnauthorized).toBe(true);
|
||||
expect(options.disableAutoIdle).toBe(true);
|
||||
});
|
||||
|
||||
it("conserve le mot de passe uniquement pour le mode basique", () => {
|
||||
const options = createImapFlowOptions({ ...baseConfig, authMode: "basic" });
|
||||
|
||||
expect(options.auth).toEqual({
|
||||
user: "compta@example.org",
|
||||
pass: "secret",
|
||||
});
|
||||
expect(options.auth).not.toHaveProperty("accessToken");
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateEmailImportConfiguration", () => {
|
||||
it("accepte une boîte OAuth2 configurée même lorsque la planification automatique est désactivée", () => {
|
||||
const error = validateEmailImportConfiguration({
|
||||
emailImportEnabled: 0,
|
||||
emailImportAddress: "compta@example.org",
|
||||
emailImportPassword: null,
|
||||
emailImportHost: "outlook.office365.com",
|
||||
emailImportPort: 993,
|
||||
emailImportSinceDate: null,
|
||||
emailImportAuthMode: "oauth2",
|
||||
azureTenantId: "tenant",
|
||||
azureClientId: "client",
|
||||
azureClientSecret: "secret",
|
||||
});
|
||||
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
|
||||
it("refuse une boîte dont la configuration IMAP est incomplète", () => {
|
||||
const error = validateEmailImportConfiguration({
|
||||
emailImportEnabled: 0,
|
||||
emailImportAddress: "compta@example.org",
|
||||
emailImportPassword: null,
|
||||
emailImportHost: null,
|
||||
emailImportPort: 993,
|
||||
emailImportSinceDate: null,
|
||||
emailImportAuthMode: "oauth2",
|
||||
azureTenantId: "tenant",
|
||||
azureClientId: "client",
|
||||
azureClientSecret: "secret",
|
||||
});
|
||||
|
||||
expect(error).toBe("Configuration IMAP incomplète");
|
||||
});
|
||||
});
|
||||
@@ -1,23 +1,24 @@
|
||||
import Imap from "imap";
|
||||
import { ImapFlow, type ImapFlowOptions, type SearchObject } from "imapflow";
|
||||
import { simpleParser, ParsedMail, Attachment } from "mailparser";
|
||||
import {
|
||||
getImportSettingsByUser,
|
||||
createSourceFile,
|
||||
updateSourceFile,
|
||||
getUserSettings,
|
||||
getSourceFileByContentHash,
|
||||
findDuplicateInvoice,
|
||||
isInvoiceBlacklisted,
|
||||
createInvoice,
|
||||
createImportLog,
|
||||
findSourceFileByFileName,
|
||||
} from "./db";
|
||||
import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor";
|
||||
import { localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { localStorageDelete, localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { calculateFileSha256 } from "./fileFingerprint";
|
||||
import { sendImportNotification } from "./notificationService";
|
||||
import { getOffice365ImapToken, buildXOAuth2String } from "./office365OAuth";
|
||||
import { getOffice365ImapToken } from "./office365OAuth";
|
||||
import { applyAutomationRules } from "./automationEngine";
|
||||
|
||||
interface EmailImportConfig {
|
||||
export interface EmailImportConfig {
|
||||
userId: number;
|
||||
emailAddress: string;
|
||||
password: string;
|
||||
@@ -31,10 +32,91 @@ interface EmailImportConfig {
|
||||
azureClientSecret?: string;
|
||||
}
|
||||
|
||||
export type EmailImportTrigger = "manual" | "automatic";
|
||||
|
||||
type EmailImportSettingsSnapshot = {
|
||||
emailImportEnabled: number;
|
||||
emailImportAddress: string | null;
|
||||
emailImportPassword: string | null;
|
||||
emailImportHost: string | null;
|
||||
emailImportPort: number | null;
|
||||
emailImportSinceDate: number | null;
|
||||
emailImportAuthMode: "basic" | "oauth2";
|
||||
azureTenantId: string | null;
|
||||
azureClientId: string | null;
|
||||
azureClientSecret: string | null;
|
||||
};
|
||||
|
||||
/** Vérifie une configuration IMAP sans tenir compte de l'activation de la planification. */
|
||||
export function validateEmailImportConfiguration(
|
||||
settings: EmailImportSettingsSnapshot | null | undefined,
|
||||
): string | null {
|
||||
if (!settings || !settings.emailImportAddress || !settings.emailImportHost) {
|
||||
return "Configuration IMAP incomplète";
|
||||
}
|
||||
if (settings.emailImportAuthMode === "basic" && !settings.emailImportPassword) {
|
||||
return "Mot de passe IMAP manquant";
|
||||
}
|
||||
if (
|
||||
settings.emailImportAuthMode === "oauth2" &&
|
||||
(!settings.azureTenantId || !settings.azureClientId || !settings.azureClientSecret)
|
||||
) {
|
||||
return "Configuration OAuth2 IMAP incomplète";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construit les options ImapFlow sans effectuer d'appel réseau.
|
||||
* ImapFlow reçoit le jeton brut et construit lui-même SASL XOAUTH2.
|
||||
*/
|
||||
export function createImapFlowOptions(
|
||||
config: EmailImportConfig,
|
||||
accessToken?: string,
|
||||
): ImapFlowOptions {
|
||||
const auth = config.authMode === "oauth2"
|
||||
? { user: config.emailAddress, accessToken }
|
||||
: { user: config.emailAddress, pass: config.password };
|
||||
|
||||
return {
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
secure: true,
|
||||
auth,
|
||||
tls: {
|
||||
servername: config.host,
|
||||
rejectUnauthorized: true,
|
||||
},
|
||||
logger: false,
|
||||
disableAutoIdle: true,
|
||||
connectionTimeout: 30_000,
|
||||
greetingTimeout: 20_000,
|
||||
socketTimeout: 120_000,
|
||||
};
|
||||
}
|
||||
|
||||
// Store active intervals for each user
|
||||
const activeIntervals = new Map<number, NodeJS.Timeout>();
|
||||
// Verrou anti-concurrence par userId
|
||||
const runningChecks = new Set<number>();
|
||||
// Une extraction IA peut dépasser la fréquence configurée : ce verrou évite
|
||||
// qu'un second cycle IMAP traite les mêmes messages avant la fin du premier.
|
||||
const activeChecks = new Map<number, Promise<void>>();
|
||||
|
||||
function runEmailCheckExclusive(
|
||||
config: EmailImportConfig,
|
||||
trigger: EmailImportTrigger = "automatic",
|
||||
): Promise<void> {
|
||||
const runningCheck = activeChecks.get(config.userId);
|
||||
if (runningCheck) {
|
||||
console.log(`[EmailImport] Vérification déjà en cours pour user ${config.userId}, cycle ignoré`);
|
||||
return runningCheck;
|
||||
}
|
||||
|
||||
const check = checkEmailsForPDFs(config, trigger).finally(() => {
|
||||
if (activeChecks.get(config.userId) === check) activeChecks.delete(config.userId);
|
||||
});
|
||||
activeChecks.set(config.userId, check);
|
||||
return check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a single email attachment (PDF)
|
||||
@@ -43,7 +125,8 @@ const runningChecks = new Set<number>();
|
||||
async function processEmailAttachment(
|
||||
userId: number,
|
||||
attachment: Attachment,
|
||||
emailSubject: string
|
||||
emailSubject: string,
|
||||
trigger: EmailImportTrigger,
|
||||
): Promise<{ success: boolean; totalInvoices: number; imported: number; duplicates: number; errors: number; quotaError?: boolean }> {
|
||||
const fileName = attachment.filename || `email-attachment-${Date.now()}.pdf`;
|
||||
console.log(`[EmailImport] Processing attachment: ${fileName} from email: ${emailSubject}`);
|
||||
@@ -53,13 +136,22 @@ async function processEmailAttachment(
|
||||
const fileBuffer = attachment.content;
|
||||
console.log(`[EmailImport] File size: ${fileBuffer.length} bytes`);
|
||||
|
||||
// Store source file
|
||||
// ANTI-DUPLICATION : vérifier si ce fichier a déjà été importé pour cet utilisateur
|
||||
const existingSourceFile = await findSourceFileByFileName(userId, fileName);
|
||||
if (existingSourceFile) {
|
||||
console.log(`[EmailImport] File ${fileName} already imported for user ${userId} (sourceFile #${existingSourceFile.id}), skipping`);
|
||||
return { success: true, totalInvoices: 0, imported: 0, duplicates: 1, errors: 0 };
|
||||
const contentHash = calculateFileSha256(fileBuffer);
|
||||
const existingSource = await getSourceFileByContentHash(contentHash);
|
||||
if (existingSource) {
|
||||
console.log(
|
||||
`[EmailImport] PDF déjà importé, extraction ignorée: ${fileName} -> source ${existingSource.id}`,
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
totalInvoices: Math.max(existingSource.totalInvoicesDetected, 1),
|
||||
imported: 0,
|
||||
duplicates: Math.max(existingSource.totalInvoicesDetected, 1),
|
||||
errors: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Store source file
|
||||
const sourceFileKey = generateStorageKey(userId, fileName);
|
||||
console.log(`[EmailImport] Generated storage key: ${sourceFileKey}`);
|
||||
|
||||
@@ -74,13 +166,25 @@ async function processEmailAttachment(
|
||||
}
|
||||
|
||||
// Create source file record
|
||||
const sourceFile = await createSourceFile({
|
||||
let sourceFile;
|
||||
try {
|
||||
sourceFile = await createSourceFile({
|
||||
userId,
|
||||
fileName,
|
||||
fileKey: sourceFileKey,
|
||||
fileUrl: sourceFileUrl,
|
||||
contentHash,
|
||||
processingStatus: "processing",
|
||||
});
|
||||
} catch (error: any) {
|
||||
// La contrainte unique protège également contre deux imports concurrents.
|
||||
if (error?.code === "ER_DUP_ENTRY" || error?.errno === 1062) {
|
||||
await localStorageDelete(sourceFileKey).catch(() => undefined);
|
||||
console.log(`[EmailImport] PDF réservé par un autre traitement: ${fileName}`);
|
||||
return { success: true, totalInvoices: 1, imported: 0, duplicates: 1, errors: 0 };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`[EmailImport] Source file record created with ID: ${sourceFile.id}`);
|
||||
|
||||
@@ -257,6 +361,7 @@ async function processEmailAttachment(
|
||||
errorDetails: errorDetails.length > 0 ? JSON.stringify(errorDetails) : null,
|
||||
warningMessage,
|
||||
importSource: "email",
|
||||
importTrigger: trigger,
|
||||
});
|
||||
|
||||
console.log(`[EmailImport] Successfully processed attachment: ${fileName}`);
|
||||
@@ -295,7 +400,7 @@ async function processEmailAttachment(
|
||||
* - basic : login/password classique
|
||||
* - oauth2 : obtient un token Azure AD et utilise XOAUTH2
|
||||
*/
|
||||
async function buildImapConfig(config: EmailImportConfig): Promise<Imap.Config> {
|
||||
async function buildImapConfig(config: EmailImportConfig): Promise<ImapFlowOptions> {
|
||||
if (config.authMode === "oauth2") {
|
||||
if (!config.azureTenantId || !config.azureClientId || !config.azureClientSecret) {
|
||||
throw new Error(
|
||||
@@ -309,135 +414,88 @@ async function buildImapConfig(config: EmailImportConfig): Promise<Imap.Config>
|
||||
config.azureClientId,
|
||||
config.azureClientSecret
|
||||
);
|
||||
const xoauth2 = buildXOAuth2String(config.emailAddress, accessToken);
|
||||
console.log(`[EmailImport] OAuth2 token obtained successfully`);
|
||||
|
||||
return {
|
||||
user: config.emailAddress,
|
||||
xoauth2,
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
tls: true,
|
||||
tlsOptions: { rejectUnauthorized: false },
|
||||
authTimeout: 30000,
|
||||
} as any;
|
||||
return createImapFlowOptions(config, accessToken);
|
||||
}
|
||||
|
||||
// Basic auth (par défaut)
|
||||
return {
|
||||
user: config.emailAddress,
|
||||
password: config.password,
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
tls: true,
|
||||
tlsOptions: { rejectUnauthorized: false },
|
||||
authTimeout: 30000,
|
||||
};
|
||||
return createImapFlowOptions(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to IMAP and process unread emails with PDF attachments
|
||||
*/
|
||||
async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> {
|
||||
// Anti-concurrence : ne pas lancer si un check est déjà en cours pour cet utilisateur
|
||||
if (runningChecks.has(config.userId)) {
|
||||
console.log(`[EmailImport] Check already running for user ${config.userId}, skipping`);
|
||||
return;
|
||||
}
|
||||
runningChecks.add(config.userId);
|
||||
// Build IMAP config (may involve async OAuth2 token fetch)
|
||||
async function checkEmailsForPDFs(
|
||||
config: EmailImportConfig,
|
||||
trigger: EmailImportTrigger,
|
||||
): Promise<void> {
|
||||
const imapConfig = await buildImapConfig(config);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const imap = new Imap(imapConfig);
|
||||
|
||||
function openInbox(cb: (err: Error | null, box?: any) => void) {
|
||||
imap.openBox("INBOX", false, cb);
|
||||
}
|
||||
|
||||
imap.once("ready", () => {
|
||||
console.log(`[EmailImport] Connected to IMAP server for user ${config.userId} (mode: ${config.authMode || "basic"})`);
|
||||
|
||||
openInbox((err) => {
|
||||
if (err) {
|
||||
console.error("[EmailImport] Error opening inbox:", err);
|
||||
imap.end();
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Build search criteria: unread emails, optionally filtered by date
|
||||
const searchCriteria: any[] = ["UNSEEN"];
|
||||
if (config.sinceDate) {
|
||||
// IMAP SINCE expects a date string like "1-Jan-2026"
|
||||
const since = new Date(config.sinceDate * 1000);
|
||||
const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
|
||||
const sinceStr = `${since.getDate()}-${months[since.getMonth()]}-${since.getFullYear()}`;
|
||||
searchCriteria.push(["SINCE", sinceStr]);
|
||||
console.log(`[EmailImport] Filtering emails since ${sinceStr} for user ${config.userId}`);
|
||||
}
|
||||
imap.search(searchCriteria, (err, results) => {
|
||||
if (err) {
|
||||
console.error("[EmailImport] Error searching emails:", err);
|
||||
imap.end();
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!results || results.length === 0) {
|
||||
console.log(`[EmailImport] No unread emails found for user ${config.userId}`);
|
||||
imap.end();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[EmailImport] Found ${results.length} unread emails for user ${config.userId}`);
|
||||
|
||||
const fetch = imap.fetch(results, {
|
||||
bodies: "",
|
||||
markSeen: true, // Mark as seen immediately to prevent re-processing
|
||||
const client = new ImapFlow(imapConfig);
|
||||
client.on("error", (error) => {
|
||||
console.error(`[EmailImport] IMAP connection error for user ${config.userId}:`, error);
|
||||
});
|
||||
|
||||
const processedEmails: number[] = [];
|
||||
|
||||
fetch.on("message", (msg, seqno) => {
|
||||
msg.on("body", (stream) => {
|
||||
simpleParser(stream as any, async (err, parsed: ParsedMail) => {
|
||||
if (err) {
|
||||
console.error("[EmailImport] Error parsing email:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if email has PDF attachments
|
||||
const pdfAttachments = parsed.attachments.filter(
|
||||
(att) =>
|
||||
att.contentType === "application/pdf" ||
|
||||
att.filename?.toLowerCase().endsWith(".pdf")
|
||||
);
|
||||
|
||||
if (pdfAttachments.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let lock: Awaited<ReturnType<ImapFlow["getMailboxLock"]>> | undefined;
|
||||
try {
|
||||
await client.connect();
|
||||
console.log(
|
||||
`[EmailImport] Email ${seqno} has ${pdfAttachments.length} PDF attachment(s)`
|
||||
`[EmailImport] Connected to IMAP server for user ${config.userId} (mode: ${config.authMode || "basic"})`,
|
||||
);
|
||||
|
||||
// Process each PDF attachment
|
||||
lock = await client.getMailboxLock("INBOX", {
|
||||
readOnly: false,
|
||||
acquireTimeout: 30_000,
|
||||
description: `invoice-import-user-${config.userId}`,
|
||||
});
|
||||
|
||||
const searchCriteria: SearchObject = { seen: false };
|
||||
if (config.sinceDate) {
|
||||
searchCriteria.since = new Date(config.sinceDate * 1000);
|
||||
console.log(
|
||||
`[EmailImport] Filtering emails since ${searchCriteria.since.toISOString()} for user ${config.userId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const unreadUids = await client.search(searchCriteria, { uid: true });
|
||||
if (!unreadUids || unreadUids.length === 0) {
|
||||
console.log(`[EmailImport] No unread emails found for user ${config.userId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[EmailImport] Found ${unreadUids.length} unread emails for user ${config.userId}`);
|
||||
|
||||
// Le traitement reste séquentiel afin d'éviter plusieurs extractions IA
|
||||
// concurrentes sur les mêmes pièces jointes.
|
||||
for (const uid of unreadUids) {
|
||||
const message = await client.fetchOne(uid, { source: true }, { uid: true });
|
||||
if (!message || !message.source) {
|
||||
console.warn(`[EmailImport] Message UID ${uid} without source, skipped`);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed: ParsedMail = await simpleParser(message.source);
|
||||
const pdfAttachments = parsed.attachments.filter(
|
||||
(attachment) =>
|
||||
attachment.contentType === "application/pdf" ||
|
||||
attachment.filename?.toLowerCase().endsWith(".pdf"),
|
||||
);
|
||||
|
||||
if (pdfAttachments.length === 0) continue;
|
||||
|
||||
console.log(`[EmailImport] Email UID ${uid} has ${pdfAttachments.length} PDF attachment(s)`);
|
||||
let allAttachmentsSucceeded = true;
|
||||
|
||||
for (const attachment of pdfAttachments) {
|
||||
try {
|
||||
const result = await processEmailAttachment(
|
||||
config.userId,
|
||||
attachment,
|
||||
parsed.subject || "No subject"
|
||||
parsed.subject || "No subject",
|
||||
trigger,
|
||||
);
|
||||
allAttachmentsSucceeded = allAttachmentsSucceeded && result.success;
|
||||
|
||||
// Mark this email as successfully processed
|
||||
if (!processedEmails.includes(seqno)) {
|
||||
processedEmails.push(seqno);
|
||||
}
|
||||
|
||||
// Send notification after successful processing
|
||||
if (result.success) {
|
||||
await sendImportNotification(config.userId, {
|
||||
source: "email",
|
||||
@@ -449,58 +507,26 @@ async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> {
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[EmailImport] Failed to process attachment from email ${seqno}:`,
|
||||
error
|
||||
);
|
||||
allAttachmentsSucceeded = false;
|
||||
console.error(`[EmailImport] Failed to process attachment from UID ${uid}:`, error);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
fetch.once("error", (err) => {
|
||||
console.error("[EmailImport] Fetch error:", err);
|
||||
imap.end();
|
||||
reject(err);
|
||||
});
|
||||
|
||||
fetch.once("end", () => {
|
||||
console.log(`[EmailImport] Finished fetching emails for user ${config.userId}`);
|
||||
|
||||
// Mark successfully processed emails as seen
|
||||
if (processedEmails.length > 0) {
|
||||
imap.addFlags(processedEmails, ["\\Seen"], (err) => {
|
||||
if (err) {
|
||||
console.error("[EmailImport] Error marking emails as seen:", err);
|
||||
} else {
|
||||
console.log(`[EmailImport] Marked ${processedEmails.length} emails as seen`);
|
||||
if (allAttachmentsSucceeded) {
|
||||
await client.messageFlagsAdd(uid, ["\\Seen"], { uid: true, silent: true });
|
||||
console.log(`[EmailImport] Marked email UID ${uid} as seen`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[EmailImport] Error parsing email UID ${uid}:`, error);
|
||||
}
|
||||
imap.end();
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
imap.end();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
imap.once("error", (err) => {
|
||||
runningChecks.delete(config.userId);
|
||||
console.error("[EmailImport] IMAP connection error:", err);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
imap.once("end", () => {
|
||||
runningChecks.delete(config.userId);
|
||||
console.log(`[EmailImport] IMAP connection ended for user ${config.userId}`);
|
||||
});
|
||||
|
||||
imap.connect();
|
||||
});
|
||||
console.log(`[EmailImport] Finished processing emails for user ${config.userId}`);
|
||||
} finally {
|
||||
lock?.release();
|
||||
if (client.usable) await client.logout().catch(() => client.close());
|
||||
else client.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -508,57 +534,34 @@ async function checkEmailsForPDFs(config: EmailImportConfig): Promise<void> {
|
||||
* Returns detailed error message if connection fails
|
||||
*/
|
||||
export async function testImapConnection(config: EmailImportConfig): Promise<{ success: boolean; message: string }> {
|
||||
let client: ImapFlow | undefined;
|
||||
try {
|
||||
const imapConfig = await buildImapConfig(config);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const imap = new Imap(imapConfig);
|
||||
let resolved = false;
|
||||
|
||||
const done = (result: { success: boolean; message: string }) => {
|
||||
if (!resolved) {
|
||||
resolved = true;
|
||||
try { imap.destroy(); } catch {}
|
||||
resolve(result);
|
||||
}
|
||||
};
|
||||
|
||||
imap.once("ready", () => {
|
||||
client = new ImapFlow({ ...imapConfig, verifyOnly: true });
|
||||
await client.connect();
|
||||
console.log(`[EmailImport] Test connection successful for ${config.emailAddress}`);
|
||||
done({ success: true, message: `Connexion IMAP réussie pour ${config.emailAddress}` });
|
||||
});
|
||||
|
||||
imap.once("error", (err: any) => {
|
||||
console.error(`[EmailImport] Test connection failed:`, err);
|
||||
let message = `Erreur de connexion IMAP : ${err.message || err}`;
|
||||
|
||||
// Messages d'erreur plus clairs
|
||||
if (err.message?.includes("Invalid credentials") || err.message?.includes("AUTHENTICATE")) {
|
||||
if (config.authMode === "oauth2") {
|
||||
message = "Authentification OAuth2 refusée. Vérifiez que l'application Azure AD a bien la permission IMAP.AccessAsApp et que le consentement admin a été accordé.";
|
||||
} else {
|
||||
message = "Identifiants invalides. Pour Office 365, l'authentification basique est désactivée. Activez le mode OAuth2 et configurez les credentials Azure AD.";
|
||||
}
|
||||
} else if (err.message?.includes("ECONNREFUSED") || err.message?.includes("ENOTFOUND")) {
|
||||
message = `Impossible de se connecter au serveur ${config.host}:${config.port}. Vérifiez l'adresse et le port IMAP.`;
|
||||
} else if (err.message?.includes("certificate") || err.message?.includes("SSL")) {
|
||||
message = `Erreur SSL/TLS lors de la connexion à ${config.host}. Vérifiez le port (993 pour SSL).`;
|
||||
} else if (err.message?.includes("timeout") || err.message?.includes("Timeout")) {
|
||||
message = `Timeout de connexion à ${config.host}:${config.port}. Vérifiez l'adresse du serveur IMAP.`;
|
||||
}
|
||||
|
||||
done({ success: false, message });
|
||||
});
|
||||
|
||||
// Timeout de sécurité
|
||||
setTimeout(() => {
|
||||
done({ success: false, message: `Timeout : impossible de se connecter à ${config.host}:${config.port} dans les 15 secondes.` });
|
||||
}, 15000);
|
||||
|
||||
imap.connect();
|
||||
});
|
||||
return { success: true, message: `Connexion IMAP OAuth2 réussie pour ${config.emailAddress}` };
|
||||
} catch (error: any) {
|
||||
return { success: false, message: `Erreur : ${error.message || error}` };
|
||||
console.error(`[EmailImport] Test connection failed:`, error);
|
||||
const rawMessage = error?.response || error?.message || String(error);
|
||||
let message = `Erreur de connexion IMAP : ${rawMessage}`;
|
||||
|
||||
if (/AUTHENTICATE|authentication|invalid credentials/i.test(rawMessage)) {
|
||||
message = config.authMode === "oauth2"
|
||||
? "Authentification OAuth2 refusée. Vérifiez IMAP.AccessAsApp, le consentement administrateur, le service principal Exchange et l’autorisation de la boîte."
|
||||
: "Identifiants invalides. Pour Microsoft 365, utilisez OAuth2 au lieu de l’authentification basique.";
|
||||
} else if (/ECONNREFUSED|ENOTFOUND/i.test(rawMessage)) {
|
||||
message = `Impossible de joindre ${config.host}:${config.port}. Vérifiez l’adresse et le port IMAP.`;
|
||||
} else if (/certificate|TLS|SSL/i.test(rawMessage)) {
|
||||
message = `Erreur TLS lors de la connexion à ${config.host}. Vérifiez le certificat et le port 993.`;
|
||||
} else if (/timeout/i.test(rawMessage)) {
|
||||
message = `Timeout lors de la connexion à ${config.host}:${config.port}.`;
|
||||
}
|
||||
|
||||
return { success: false, message };
|
||||
} finally {
|
||||
if (client?.usable) await client.logout().catch(() => client?.close());
|
||||
else client?.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -615,13 +618,13 @@ export async function startEmailImportService(userId: number): Promise<boolean>
|
||||
);
|
||||
|
||||
// Run immediately on start
|
||||
checkEmailsForPDFs(config).catch((error) => {
|
||||
runEmailCheckExclusive(config).catch((error) => {
|
||||
console.error(`[EmailImport] Error checking emails for user ${userId}:`, error);
|
||||
});
|
||||
|
||||
// Set up interval for periodic checks
|
||||
const interval = setInterval(() => {
|
||||
checkEmailsForPDFs(config).catch((error) => {
|
||||
runEmailCheckExclusive(config).catch((error) => {
|
||||
console.error(`[EmailImport] Error checking emails for user ${userId}:`, error);
|
||||
});
|
||||
}, frequencyMs);
|
||||
@@ -655,29 +658,28 @@ export function isEmailImportServiceRunning(userId: number): boolean {
|
||||
return activeIntervals.has(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually trigger an immediate email check for a user
|
||||
*/
|
||||
export async function triggerEmailCheck(userId: number): Promise<{ success: boolean; message: string }> {
|
||||
async function triggerConfiguredEmailCheck(
|
||||
userId: number,
|
||||
requireAutomaticEnabled: boolean,
|
||||
): Promise<{ success: boolean; message: string }> {
|
||||
try {
|
||||
// Get user's import settings
|
||||
const settings = await getImportSettingsByUser(userId);
|
||||
|
||||
if (!settings || settings.emailImportEnabled !== 1) {
|
||||
if (!settings || (requireAutomaticEnabled && settings.emailImportEnabled !== 1)) {
|
||||
return { success: false, message: "Import par email non activé" };
|
||||
}
|
||||
|
||||
if (!settings.emailImportAddress || !settings.emailImportHost) {
|
||||
return { success: false, message: "Configuration IMAP incomplète" };
|
||||
}
|
||||
const validationError = validateEmailImportConfiguration(settings);
|
||||
if (validationError) return { success: false, message: validationError };
|
||||
|
||||
const authMode = (settings as any).emailImportAuthMode as "basic" | "oauth2" || "basic";
|
||||
|
||||
const config: EmailImportConfig = {
|
||||
userId,
|
||||
emailAddress: settings.emailImportAddress,
|
||||
// validateEmailImportConfiguration garantit ces deux valeurs avant ce point.
|
||||
emailAddress: settings.emailImportAddress!,
|
||||
password: settings.emailImportPassword || "",
|
||||
host: settings.emailImportHost,
|
||||
host: settings.emailImportHost!,
|
||||
port: settings.emailImportPort || 993,
|
||||
sinceDate: settings.emailImportSinceDate ?? undefined,
|
||||
authMode,
|
||||
@@ -687,7 +689,8 @@ export async function triggerEmailCheck(userId: number): Promise<{ success: bool
|
||||
};
|
||||
|
||||
console.log(`[EmailImport] Manual check triggered for user ${userId}`);
|
||||
await checkEmailsForPDFs(config);
|
||||
// Cette voie exécute une vérification unique : elle ne crée pas de setInterval.
|
||||
await runEmailCheckExclusive(config, "manual");
|
||||
|
||||
return { success: true, message: "Vérification terminée avec succès" };
|
||||
} catch (error: any) {
|
||||
@@ -696,6 +699,16 @@ export async function triggerEmailCheck(userId: number): Promise<{ success: bool
|
||||
}
|
||||
}
|
||||
|
||||
/** Déclenchement manuel réservé aux appels administrateurs, même si le planificateur est désactivé. */
|
||||
export async function triggerManualEmailCheck(userId: number): Promise<{ success: boolean; message: string }> {
|
||||
return triggerConfiguredEmailCheck(userId, false);
|
||||
}
|
||||
|
||||
/** Compatibilité avec le bouton existant : la vérification personnelle exige toujours l’activation automatique. */
|
||||
export async function triggerEmailCheck(userId: number): Promise<{ success: boolean; message: string }> {
|
||||
return triggerConfiguredEmailCheck(userId, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop all email import services
|
||||
*/
|
||||
|
||||
26
server/exportAutomation.test.ts
Normal file
26
server/exportAutomation.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { findMatchingExportRule } from "../shared/exportAutomation";
|
||||
|
||||
const rules = [
|
||||
{ isActive: 1, priority: 2, conditionField: "recipientName" as const, conditionValue: "Direction générale" },
|
||||
{ isActive: 1, priority: 1, conditionField: "ventilationComptable" as const, conditionValue: "615200" },
|
||||
{ isActive: 0, priority: 0, conditionField: "recipientName" as const, conditionValue: "Direction générale" },
|
||||
];
|
||||
|
||||
describe("règles de destination d’export", () => {
|
||||
it("sélectionne une règle de ventilation active", () => {
|
||||
expect(findMatchingExportRule(rules, { recipientName: "Direction générale", ventilationComptable: "615200" })).toBe(rules[1]);
|
||||
});
|
||||
|
||||
it("compare les destinataires sans tenir compte de la casse et des espaces", () => {
|
||||
expect(findMatchingExportRule(rules, { recipientName: " direction GÉNÉRALE " })).toBe(rules[0]);
|
||||
});
|
||||
|
||||
it("ignore les règles inactives", () => {
|
||||
expect(findMatchingExportRule([rules[2]], { recipientName: "Direction générale" })).toBeUndefined();
|
||||
});
|
||||
|
||||
it("ne retourne aucune règle si la facture ne correspond à aucun critère", () => {
|
||||
expect(findMatchingExportRule(rules, { recipientName: "Service achats", ventilationComptable: "606000" })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
42
server/exportDestinationResolver.ts
Normal file
42
server/exportDestinationResolver.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { ImportSettings, Invoice } from "../drizzle/schema";
|
||||
import { findMatchingExportRule } from "@shared/exportAutomation";
|
||||
import { getExportAutomationRulesByUser } from "./db";
|
||||
|
||||
export type ResolvedExportDestination = {
|
||||
exportMode: "browser" | "folder" | "both";
|
||||
destinationPath: string | null;
|
||||
destinationType: "local" | "teams" | "sharepoint";
|
||||
source: "rule" | "default";
|
||||
ruleName?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Résout une destination pour une validation BAP. Une règle active ciblant le
|
||||
* destinataire ou la ventilation est prioritaire sur les anciens paramètres
|
||||
* globaux, qui restent le comportement de repli pour préserver l’existant.
|
||||
*/
|
||||
export async function resolveBapExportDestination(
|
||||
userId: number,
|
||||
invoice: Pick<Invoice, "recipientName" | "ventilationComptable">,
|
||||
settings: ImportSettings | null,
|
||||
): Promise<ResolvedExportDestination> {
|
||||
const rules = await getExportAutomationRulesByUser(userId);
|
||||
const matchingRule = findMatchingExportRule(rules, invoice);
|
||||
|
||||
if (matchingRule) {
|
||||
return {
|
||||
exportMode: matchingRule.openInBrowser === 1 ? "both" : "folder",
|
||||
destinationPath: matchingRule.destinationPath,
|
||||
destinationType: matchingRule.destinationType,
|
||||
source: "rule",
|
||||
ruleName: matchingRule.name,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
exportMode: settings?.bapExportMode || "browser",
|
||||
destinationPath: settings?.exportFolder || null,
|
||||
destinationType: settings?.exportFolderType || "local",
|
||||
source: "default",
|
||||
};
|
||||
}
|
||||
19
server/fileFingerprint.test.ts
Normal file
19
server/fileFingerprint.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { calculateFileSha256 } from "./fileFingerprint";
|
||||
|
||||
describe("calculateFileSha256", () => {
|
||||
it("retourne la même empreinte pour un contenu identique", () => {
|
||||
const content = Buffer.from("facture-pdf");
|
||||
expect(calculateFileSha256(content)).toBe(calculateFileSha256(Buffer.from(content)));
|
||||
});
|
||||
|
||||
it("distingue deux contenus différents", () => {
|
||||
expect(calculateFileSha256(Buffer.from("facture-a"))).not.toBe(
|
||||
calculateFileSha256(Buffer.from("facture-b")),
|
||||
);
|
||||
});
|
||||
|
||||
it("produit une empreinte SHA-256 hexadécimale", () => {
|
||||
expect(calculateFileSha256(Buffer.from("facture"))).toMatch(/^[a-f0-9]{64}$/);
|
||||
});
|
||||
});
|
||||
12
server/fileFingerprint.ts
Normal file
12
server/fileFingerprint.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
/**
|
||||
* Calcule une empreinte déterministe sur les octets du document original.
|
||||
*
|
||||
* L'empreinte est calculée avant tout stockage ou traitement IA : deux imports
|
||||
* du même PDF sont donc reconnus même si le nom du fichier ou l'utilisateur
|
||||
* diffèrent.
|
||||
*/
|
||||
export function calculateFileSha256(buffer: Buffer): string {
|
||||
return createHash("sha256").update(buffer).digest("hex");
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import path from "path";
|
||||
import {
|
||||
getImportSettingsByUser,
|
||||
createSourceFile,
|
||||
getSourceFileByContentHash,
|
||||
updateSourceFile,
|
||||
getUserSettings,
|
||||
findDuplicateInvoice,
|
||||
@@ -11,7 +12,8 @@ import {
|
||||
createImportLog,
|
||||
} from "./db";
|
||||
import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor";
|
||||
import { localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { localStorageDelete, localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { calculateFileSha256 } from "./fileFingerprint";
|
||||
import { sendImportNotification } from "./notificationService";
|
||||
|
||||
interface FolderImportConfig {
|
||||
@@ -42,6 +44,13 @@ async function processFolderFile(
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
console.log(`[FolderImport] File size: ${fileBuffer.length} bytes`);
|
||||
|
||||
const contentHash = calculateFileSha256(fileBuffer);
|
||||
const existingSource = await getSourceFileByContentHash(contentHash);
|
||||
if (existingSource) {
|
||||
console.log(`[FolderImport] PDF déjà importé, fichier ignoré: ${fileName}`);
|
||||
return { success: true, imported: 0, duplicates: 1, errors: 0 };
|
||||
}
|
||||
|
||||
// Store source file
|
||||
const sourceFileKey = generateStorageKey(userId, fileName);
|
||||
console.log(`[FolderImport] Generated storage key: ${sourceFileKey}`);
|
||||
@@ -57,13 +66,23 @@ async function processFolderFile(
|
||||
}
|
||||
|
||||
// Create source file record
|
||||
const sourceFile = await createSourceFile({
|
||||
let sourceFile;
|
||||
try {
|
||||
sourceFile = await createSourceFile({
|
||||
userId,
|
||||
fileName,
|
||||
fileKey: sourceFileKey,
|
||||
fileUrl: sourceFileUrl,
|
||||
contentHash,
|
||||
processingStatus: "processing",
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (error?.code === "ER_DUP_ENTRY" || error?.errno === 1062) {
|
||||
await localStorageDelete(sourceFileKey).catch(() => undefined);
|
||||
return { success: true, imported: 0, duplicates: 1, errors: 0 };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`[FolderImport] Source file record created with ID: ${sourceFile.id}`);
|
||||
|
||||
@@ -192,6 +211,7 @@ async function processFolderFile(
|
||||
duplicateDetails: duplicateDetails.length > 0 ? JSON.stringify(duplicateDetails) : null,
|
||||
errorDetails: errorDetails.length > 0 ? JSON.stringify(errorDetails) : null,
|
||||
importSource: "folder",
|
||||
importTrigger: "automatic",
|
||||
});
|
||||
|
||||
// Move file to processed folder
|
||||
|
||||
33
server/invoiceAnalytics.test.ts
Normal file
33
server/invoiceAnalytics.test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAnnualInvoiceSummary, buildSupplierBudgetSummary } from "@shared/invoiceAnalytics";
|
||||
|
||||
const invoices = [
|
||||
{ invoiceDate: "2026-05-15", createdAt: "2026-05-16", status: "completed", isSubscription: 0, supplierName: "SFR", totalAmount: "100" },
|
||||
{ invoiceDate: "2026-05-20", createdAt: "2026-05-21", status: "completed", isSubscription: 1, supplierName: "SFR", totalAmount: "20" },
|
||||
{ invoiceDate: "2025-03-10", createdAt: "2025-03-11", status: "completed", isSubscription: 1, supplierName: "Microsoft", totalAmount: "50" },
|
||||
{ invoiceDate: "2026-05-01", createdAt: "2026-05-01", status: "error", isSubscription: 0, supplierName: "Ignorée", totalAmount: "999" },
|
||||
];
|
||||
|
||||
describe("agrégats de facturation", () => {
|
||||
it("calcule les volumes et montants BAP et abonnements par année", () => {
|
||||
expect(buildAnnualInvoiceSummary(invoices)).toEqual([
|
||||
{ year: 2026, bapCount: 1, bapAmount: 100, subscriptionCount: 1, subscriptionAmount: 20, totalAmount: 120 },
|
||||
{ year: 2025, bapCount: 0, bapAmount: 0, subscriptionCount: 1, subscriptionAmount: 50, totalAmount: 50 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("filtre le budget par période et ventile chaque fournisseur par type", () => {
|
||||
expect(buildSupplierBudgetSummary(invoices, "2026", "05")).toEqual([
|
||||
{ supplierName: "SFR", subscriptionCount: 1, subscriptionAmount: 20, nonSubscriptionCount: 1, nonSubscriptionAmount: 100, totalAmount: 120 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("classe le budget du fournisseur le plus facturé au moins facturé", () => {
|
||||
const rows = buildSupplierBudgetSummary([
|
||||
...invoices,
|
||||
{ invoiceDate: "2026-05-22", createdAt: "2026-05-22", status: "completed", isSubscription: 0, supplierName: "Orange", totalAmount: "300" },
|
||||
], "2026", "05");
|
||||
|
||||
expect(rows.map((row) => row.supplierName)).toEqual(["Orange", "SFR"]);
|
||||
});
|
||||
});
|
||||
17
server/invoicePeriod.test.ts
Normal file
17
server/invoicePeriod.test.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getInvoicePeriodDate, matchesInvoicePeriod } from "@shared/invoicePeriod";
|
||||
|
||||
describe("filtres de période des factures", () => {
|
||||
it("privilégie la date de facture et accepte le mois demandé", () => {
|
||||
const invoice = { invoiceDate: "2026-05-11T00:00:00.000Z", createdAt: "2026-06-02T00:00:00.000Z" };
|
||||
expect(getInvoicePeriodDate(invoice)?.getFullYear()).toBe(2026);
|
||||
expect(matchesInvoicePeriod(invoice, "2026", "05")).toBe(true);
|
||||
expect(matchesInvoicePeriod(invoice, "2026", "06")).toBe(false);
|
||||
});
|
||||
|
||||
it("utilise la date de réception lorsque la date de facture est absente", () => {
|
||||
const invoice = { invoiceDate: null, createdAt: "2025-01-31T00:00:00.000Z" };
|
||||
expect(matchesInvoicePeriod(invoice, "2025", "01")).toBe(true);
|
||||
expect(matchesInvoicePeriod(invoice, "2026", "all")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { COOKIE_NAME } from "@shared/const";
|
||||
import { BAP_MIN_QUALITY_SCORE, meetsBapQualityThreshold } from "@shared/bapEligibility";
|
||||
|
||||
interface Condition {
|
||||
field: string;
|
||||
@@ -11,6 +12,7 @@ interface Actions {
|
||||
typeAchat?: string;
|
||||
serviceConcerne?: string;
|
||||
ventilationComptable?: string;
|
||||
isSubscription?: 0 | 1;
|
||||
}
|
||||
import { getSessionCookieOptions } from "./_core/cookies";
|
||||
import { systemRouter } from "./_core/systemRouter";
|
||||
@@ -24,6 +26,7 @@ import {
|
||||
searchInvoices,
|
||||
getInvoiceStats,
|
||||
createSourceFile,
|
||||
getSourceFileByContentHash,
|
||||
getSourceFileById,
|
||||
updateSourceFile,
|
||||
getUserSettings,
|
||||
@@ -37,13 +40,16 @@ import {
|
||||
isInvoiceBlacklisted,
|
||||
getAllInvoices,
|
||||
getAllImportLogs,
|
||||
getAllImportLogsWithDetails,
|
||||
getAllBapHistory,
|
||||
createImportLog,
|
||||
getImportLogsByUser,
|
||||
getImportLogsWithDetailsByUser,
|
||||
deleteAllImportLogs,
|
||||
getLlmLogsBySourceFile,
|
||||
getLlmLogsByInvoice,
|
||||
getImportSettingsByUser,
|
||||
getEmailImportAccounts,
|
||||
upsertImportSettings,
|
||||
getDepartmentsByUser,
|
||||
createDepartment,
|
||||
@@ -60,6 +66,11 @@ import {
|
||||
createAutomationRule,
|
||||
updateAutomationRule,
|
||||
deleteAutomationRule,
|
||||
getExportAutomationRulesByUser,
|
||||
getExportAutomationRuleById,
|
||||
createExportAutomationRule,
|
||||
updateExportAutomationRule,
|
||||
deleteExportAutomationRule,
|
||||
getSignaturesByUser,
|
||||
getSignatureById,
|
||||
createSignature,
|
||||
@@ -87,10 +98,12 @@ import { loginLocal, hashPassword, getAzureAuthUrl, isAzureAdConfigured } from "
|
||||
import fsSync from "fs";
|
||||
import pathSync from "path";
|
||||
import { extractInvoicesWithMistral, generateMetadataJSON } from "./invoiceExtractor";
|
||||
import { calculateFileSha256 } from "./fileFingerprint";
|
||||
import { localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { testSftpConnection, exportInvoiceToSftp, getUserSftpConfig } from "./sftpExport";
|
||||
import { drawBapCartouche } from "./bapCartouche";
|
||||
import { startEmailImportService, stopEmailImportService, isEmailImportServiceRunning, triggerEmailCheck, testImapConnection } from "./emailImportService";
|
||||
import { resolveBapExportDestination } from "./exportDestinationResolver";
|
||||
import { startEmailImportService, stopEmailImportService, isEmailImportServiceRunning, triggerEmailCheck, triggerManualEmailCheck, testImapConnection } from "./emailImportService";
|
||||
import { startFolderImportService, stopFolderImportService, isFolderImportServiceRunning } from "./folderImportService";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { processFreeproExcel } from "./freeproService";
|
||||
@@ -182,6 +195,17 @@ export const appRouter = router({
|
||||
const fileBuffer = Buffer.from(input.fileData, "base64");
|
||||
console.log(`[Upload] Received file: ${input.fileName}, size: ${fileBuffer.length} bytes`);
|
||||
|
||||
// Le contrôle sur les octets du PDF intervient avant le stockage et l'appel IA.
|
||||
// Il reste fiable même si le nom du fichier ou le compte utilisateur diffère.
|
||||
const contentHash = calculateFileSha256(fileBuffer);
|
||||
const existingSource = await getSourceFileByContentHash(contentHash);
|
||||
if (existingSource) {
|
||||
throw new TRPCError({
|
||||
code: "CONFLICT",
|
||||
message: `Ce PDF a déjà été importé (${existingSource.fileName}).`,
|
||||
});
|
||||
}
|
||||
|
||||
// Store source file
|
||||
const sourceFileKey = generateStorageKey(userId, input.fileName);
|
||||
console.log(`[Upload] Generated storage key: ${sourceFileKey}`);
|
||||
@@ -202,6 +226,7 @@ export const appRouter = router({
|
||||
fileName: input.fileName,
|
||||
fileKey: sourceFileKey,
|
||||
fileUrl: sourceFileUrl,
|
||||
contentHash,
|
||||
processingStatus: "processing",
|
||||
});
|
||||
|
||||
@@ -393,6 +418,7 @@ export const appRouter = router({
|
||||
duplicateDetails: JSON.stringify(duplicateDetails),
|
||||
errorDetails: JSON.stringify(errorDetails),
|
||||
importSource: "file",
|
||||
importTrigger: "manual",
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
@@ -484,8 +510,8 @@ export const appRouter = router({
|
||||
const hasService = !!invoice.serviceConcerne;
|
||||
const hasTypeAchat = !!invoice.typeAchat;
|
||||
const hasVentilation = !!invoice.ventilationComptable;
|
||||
if (score < 100) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Le score de qualité doit être à 100% pour valider" });
|
||||
if (!meetsBapQualityThreshold(score)) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: `Le score de qualité doit être au moins de ${BAP_MIN_QUALITY_SCORE}% pour valider` });
|
||||
}
|
||||
if (!isNotSubscription) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "La facture est marquée comme abonnement" });
|
||||
@@ -501,9 +527,10 @@ export const appRouter = router({
|
||||
const { localStoragePut, generateStorageKey } = await import('./localStorage');
|
||||
|
||||
const importSettings = await getImportSettingsByUser(ctx.user.id);
|
||||
const bapExportMode = importSettings?.bapExportMode || 'browser';
|
||||
const exportFolder = importSettings?.exportFolder || null;
|
||||
const exportFolderType = (importSettings as any)?.exportFolderType || 'local';
|
||||
const resolvedDestination = await resolveBapExportDestination(ctx.user.id, invoice, importSettings);
|
||||
const bapExportMode = resolvedDestination.exportMode;
|
||||
const exportFolder = resolvedDestination.destinationPath;
|
||||
const exportFolderType = resolvedDestination.destinationType;
|
||||
const STORAGE_BASE_PATH = process.env.STORAGE_BASE_PATH || path.join(process.cwd(), 'storage');
|
||||
|
||||
let pdfUrl: string | null = null;
|
||||
@@ -589,11 +616,11 @@ export const appRouter = router({
|
||||
const _bapNumber = (invoice.invoiceNumber || '').replace(/[^a-zA-Z0-9\-]/g, '').trim();
|
||||
const bapFilename = [_bapDateStr, _bapSupplier, _bapNumber].filter(Boolean).join(' - ') + '.pdf';
|
||||
|
||||
console.log(`[BAP] Mode export: ${bapExportMode}, type: ${exportFolderType}, dossier: ${exportFolder ? 'configuré' : 'non configuré'}`);
|
||||
console.log(`[BAP] Mode export: ${bapExportMode}, type: ${exportFolderType}, source: ${resolvedDestination.source}, dossier: ${exportFolder ? 'configuré' : 'non configuré'}`);
|
||||
if ((bapExportMode === 'folder' || bapExportMode === 'both') && exportFolder) {
|
||||
if (exportFolderType === 'sharepoint') {
|
||||
// Mode SharePoint : upload via Microsoft Graph
|
||||
console.log('[BAP] Démarrage upload SharePoint pour:', bapFilename);
|
||||
if (exportFolderType === 'sharepoint' || exportFolderType === 'teams') {
|
||||
// Les fichiers Teams sont déposés via le site SharePoint de l’équipe.
|
||||
console.log('[BAP] Démarrage upload Microsoft 365 pour:', bapFilename);
|
||||
const { uploadToSharePoint } = await import('./sharepoint');
|
||||
const spResult = await uploadToSharePoint(
|
||||
{
|
||||
@@ -705,7 +732,7 @@ export const appRouter = router({
|
||||
const allInvoices = ctx.user.role === 'admin' ? await getAllInvoices() : await getInvoicesByUser(ctx.user.id);
|
||||
// Filtrer les factures éligibles (non déjà validées)
|
||||
const eligible = allInvoices.filter((inv: any) =>
|
||||
(inv.qualityScore || 0) >= 100 &&
|
||||
meetsBapQualityThreshold(inv.qualityScore) &&
|
||||
inv.isSubscription === 0 &&
|
||||
!!inv.serviceConcerne &&
|
||||
!!inv.typeAchat &&
|
||||
@@ -720,9 +747,6 @@ export const appRouter = router({
|
||||
const { PDFDocument, rgb, StandardFonts } = await import('pdf-lib');
|
||||
const { localStoragePut, generateStorageKey } = await import('./localStorage');
|
||||
const importSettings = await getImportSettingsByUser(ctx.user.id);
|
||||
const bapExportMode = importSettings?.bapExportMode || 'browser';
|
||||
const exportFolder = importSettings?.exportFolder || null;
|
||||
const exportFolderType = (importSettings as any)?.exportFolderType || 'local';
|
||||
const STORAGE_BASE_PATH = process.env.STORAGE_BASE_PATH || path.join(process.cwd(), 'storage');
|
||||
const serviceSignaturesList = await getServiceSignaturesByUser(ctx.user.id);
|
||||
const results: Array<{ id: number; success: boolean; pdfUrl?: string | null; exportPath?: string | null; error?: string }> = [];
|
||||
@@ -736,6 +760,10 @@ export const appRouter = router({
|
||||
let sharepointUploadStatus: 'success' | 'error' | 'skipped' | null = null;
|
||||
let sharepointUploadPath: string | null = null;
|
||||
let sharepointUploadError: string | null = null;
|
||||
const resolvedDestination = await resolveBapExportDestination(ctx.user.id, invoice, importSettings);
|
||||
const bapExportMode = resolvedDestination.exportMode;
|
||||
const exportFolder = resolvedDestination.destinationPath;
|
||||
const exportFolderType = resolvedDestination.destinationType;
|
||||
try {
|
||||
// ─ Lecture du PDF source ─
|
||||
let sourcePdfBytes: Buffer;
|
||||
@@ -806,7 +834,7 @@ export const appRouter = router({
|
||||
const _bapNumber2 = (invoice.invoiceNumber || '').replace(/[^a-zA-Z0-9\-]/g, '').trim();
|
||||
const bapFilename = [_bapDateStr2, _bapSupplier2, _bapNumber2].filter(Boolean).join(' - ') + '.pdf';
|
||||
if ((bapExportMode === 'folder' || bapExportMode === 'both') && exportFolder) {
|
||||
if (exportFolderType === 'sharepoint') {
|
||||
if (exportFolderType === 'sharepoint' || exportFolderType === 'teams') {
|
||||
const { uploadToSharePoint } = await import('./sharepoint');
|
||||
const spResult = await uploadToSharePoint(
|
||||
{
|
||||
@@ -1029,7 +1057,8 @@ export const appRouter = router({
|
||||
}),
|
||||
|
||||
getStats: protectedProcedure.query(async ({ ctx }) => {
|
||||
return getInvoiceStats(ctx.user.id);
|
||||
// Les administrateurs consultent les mêmes données globales que les listes Factures.
|
||||
return getInvoiceStats(ctx.user.role === "admin" ? undefined : ctx.user.id);
|
||||
}),
|
||||
}),
|
||||
|
||||
@@ -1629,9 +1658,9 @@ export const appRouter = router({
|
||||
getByUser: protectedProcedure.query(async ({ ctx }) => {
|
||||
// Les admins voient tous les logs d'import
|
||||
if (ctx.user.role === 'admin') {
|
||||
return getAllImportLogs();
|
||||
return getAllImportLogsWithDetails();
|
||||
}
|
||||
return getImportLogsByUser(ctx.user.id);
|
||||
return getImportLogsWithDetailsByUser(ctx.user.id);
|
||||
}),
|
||||
|
||||
deleteAll: protectedProcedure.mutation(async ({ ctx }) => {
|
||||
@@ -1698,6 +1727,18 @@ export const appRouter = router({
|
||||
const result = await triggerEmailCheck(ctx.user.id);
|
||||
return result;
|
||||
}),
|
||||
|
||||
/** Liste sans secrets les boîtes IMAP disponibles pour une action manuelle d’administrateur. */
|
||||
getConfiguredAccounts: adminProcedure.query(async () => {
|
||||
return getEmailImportAccounts();
|
||||
}),
|
||||
|
||||
/** Déclenche une seule lecture IMAP sans activer ni planifier le service automatique. */
|
||||
checkAccountNow: adminProcedure
|
||||
.input(z.object({ userId: z.number().int().positive() }))
|
||||
.mutation(async ({ input }) => {
|
||||
return triggerManualEmailCheck(input.userId);
|
||||
}),
|
||||
}),
|
||||
|
||||
// ============= IMPORT SETTINGS ROUTES =============
|
||||
@@ -1725,6 +1766,7 @@ export const appRouter = router({
|
||||
azureTenantId: null,
|
||||
azureClientId: null,
|
||||
azureClientSecret: null,
|
||||
azureSecretExpiresAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2014,7 +2056,7 @@ export const appRouter = router({
|
||||
|
||||
for (const invoice of bapInvoices) {
|
||||
const updates = await applyAutomationRules(ctx.user.id, invoice);
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.autoFilledFields) {
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.isSubscription !== undefined || updates.autoFilledFields) {
|
||||
await updateInvoice(invoice.id, updates);
|
||||
}
|
||||
}
|
||||
@@ -2045,7 +2087,7 @@ export const appRouter = router({
|
||||
|
||||
for (const invoice of bapInvoices) {
|
||||
const updates = await applyAutomationRules(ctx.user.id, invoice);
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.autoFilledFields) {
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.isSubscription !== undefined || updates.autoFilledFields) {
|
||||
await updateInvoice(invoice.id, updates);
|
||||
}
|
||||
}
|
||||
@@ -2243,6 +2285,55 @@ export const appRouter = router({
|
||||
}),
|
||||
}),
|
||||
|
||||
// ============= EXPORT AUTOMATION RULES =============
|
||||
exportAutomationRules: router({
|
||||
list: protectedProcedure.query(async ({ ctx }) => {
|
||||
return getExportAutomationRulesByUser(ctx.user.id);
|
||||
}),
|
||||
create: protectedProcedure
|
||||
.input(z.object({
|
||||
name: z.string().trim().min(1).max(255),
|
||||
conditionField: z.enum(["recipientName", "ventilationComptable"]),
|
||||
conditionValue: z.string().trim().min(1).max(255),
|
||||
destinationType: z.enum(["local", "teams", "sharepoint"]),
|
||||
destinationPath: z.string().trim().min(1),
|
||||
openInBrowser: z.number().int().min(0).max(1).default(0),
|
||||
isActive: z.number().int().min(0).max(1).default(1),
|
||||
priority: z.number().int().min(0).default(0),
|
||||
}))
|
||||
.mutation(async ({ input, ctx }) => createExportAutomationRule({ userId: ctx.user.id, ...input })),
|
||||
update: protectedProcedure
|
||||
.input(z.object({
|
||||
id: z.number().int().positive(),
|
||||
name: z.string().trim().min(1).max(255).optional(),
|
||||
conditionField: z.enum(["recipientName", "ventilationComptable"]).optional(),
|
||||
conditionValue: z.string().trim().min(1).max(255).optional(),
|
||||
destinationType: z.enum(["local", "teams", "sharepoint"]).optional(),
|
||||
destinationPath: z.string().trim().min(1).optional(),
|
||||
openInBrowser: z.number().int().min(0).max(1).optional(),
|
||||
isActive: z.number().int().min(0).max(1).optional(),
|
||||
priority: z.number().int().min(0).optional(),
|
||||
}))
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const existing = await getExportAutomationRuleById(input.id);
|
||||
if (!existing || (ctx.user.role !== "admin" && existing.userId !== ctx.user.id)) {
|
||||
throw new TRPCError({ code: "NOT_FOUND" });
|
||||
}
|
||||
const { id, ...data } = input;
|
||||
return updateExportAutomationRule(id, data);
|
||||
}),
|
||||
delete: protectedProcedure
|
||||
.input(z.object({ id: z.number().int().positive() }))
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const existing = await getExportAutomationRuleById(input.id);
|
||||
if (!existing || (ctx.user.role !== "admin" && existing.userId !== ctx.user.id)) {
|
||||
throw new TRPCError({ code: "NOT_FOUND" });
|
||||
}
|
||||
await deleteExportAutomationRule(input.id);
|
||||
return { success: true };
|
||||
}),
|
||||
}),
|
||||
|
||||
// ============= SIGNATURES ROUTES =============
|
||||
signatures: router({
|
||||
list: protectedProcedure.query(async ({ ctx }) => {
|
||||
|
||||
51
shared/automationActions.ts
Normal file
51
shared/automationActions.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/** Les catégories d’actions proposées dans les règles d’automatisme. */
|
||||
export const AUTOMATION_ACTION_FILTERS = [
|
||||
{ value: "all", label: "Toutes les actions" },
|
||||
{ value: "typeAchat", label: "Type d’achat" },
|
||||
{ value: "serviceConcerne", label: "Service concerné" },
|
||||
{ value: "ventilationComptable", label: "Ventilation comptable" },
|
||||
{ value: "subscription", label: "Abonnement (Oui ou Non)" },
|
||||
{ value: "subscriptionYes", label: "Abonnement : Oui" },
|
||||
{ value: "subscriptionNo", label: "Abonnement : Non" },
|
||||
] as const;
|
||||
|
||||
export type AutomationActionFilter = (typeof AUTOMATION_ACTION_FILTERS)[number]["value"];
|
||||
|
||||
type RuleActions = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Lit défensivement le JSON stocké en base : une règle historique invalide ne
|
||||
* doit jamais empêcher l’affichage ni le filtrage de la liste complète.
|
||||
*/
|
||||
function parseRuleActions(actionsJson: string): RuleActions {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(actionsJson);
|
||||
return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
|
||||
? parsed as RuleActions
|
||||
: {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function getSubscriptionValue(actions: RuleActions): 0 | 1 | undefined {
|
||||
const value = actions.isSubscription;
|
||||
if (value === 1 || value === "1" || value === "OUI") return 1;
|
||||
if (value === 0 || value === "0" || value === "NON") return 0;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Retourne si les actions JSON d’une règle correspondent au filtre choisi. */
|
||||
export function matchesAutomationActionFilter(
|
||||
actionsJson: string,
|
||||
filter: AutomationActionFilter,
|
||||
): boolean {
|
||||
if (filter === "all") return true;
|
||||
|
||||
const actions = parseRuleActions(actionsJson);
|
||||
if (filter === "subscription") return Object.hasOwn(actions, "isSubscription");
|
||||
if (filter === "subscriptionYes") return getSubscriptionValue(actions) === 1;
|
||||
if (filter === "subscriptionNo") return getSubscriptionValue(actions) === 0;
|
||||
|
||||
return typeof actions[filter] === "string" && actions[filter].trim().length > 0;
|
||||
}
|
||||
10
shared/bapEligibility.ts
Normal file
10
shared/bapEligibility.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/** Score minimal requis pour générer et valider un BAP. */
|
||||
export const BAP_MIN_QUALITY_SCORE = 90;
|
||||
|
||||
/**
|
||||
* Centralise le seuil BAP afin que l’interface et le serveur appliquent la
|
||||
* même règle métier, y compris pour les scores nuls ou absents.
|
||||
*/
|
||||
export function meetsBapQualityThreshold(score: number | null | undefined): boolean {
|
||||
return typeof score === "number" && Number.isFinite(score) && score >= BAP_MIN_QUALITY_SCORE;
|
||||
}
|
||||
32
shared/exportAutomation.ts
Normal file
32
shared/exportAutomation.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/** Valeurs nécessaires pour déterminer une destination d’export. */
|
||||
export type ExportableInvoice = {
|
||||
recipientName?: string | null;
|
||||
ventilationComptable?: string | null;
|
||||
};
|
||||
|
||||
export type ExportRuleCandidate = {
|
||||
isActive: number;
|
||||
priority: number;
|
||||
conditionField: "recipientName" | "ventilationComptable";
|
||||
conditionValue: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sélectionne la première règle active par priorité. Les comparaisons sont
|
||||
* volontairement insensibles à la casse et aux espaces superflus.
|
||||
*/
|
||||
export function findMatchingExportRule<T extends ExportRuleCandidate>(
|
||||
rules: T[],
|
||||
invoice: ExportableInvoice,
|
||||
): T | undefined {
|
||||
const normalized = (value: string | null | undefined) => value?.trim().toLocaleLowerCase("fr-FR") || "";
|
||||
const ordered = [...rules].sort((a, b) => a.priority - b.priority || a.conditionValue.localeCompare(b.conditionValue, "fr-FR"));
|
||||
|
||||
return ordered.find((rule) => {
|
||||
if (rule.isActive !== 1) return false;
|
||||
const invoiceValue = rule.conditionField === "recipientName"
|
||||
? invoice.recipientName
|
||||
: invoice.ventilationComptable;
|
||||
return normalized(invoiceValue) === normalized(rule.conditionValue);
|
||||
});
|
||||
}
|
||||
108
shared/invoiceAnalytics.ts
Normal file
108
shared/invoiceAnalytics.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { getInvoicePeriodDate, matchesInvoicePeriod } from "./invoicePeriod";
|
||||
|
||||
export type AnalyticsInvoice = {
|
||||
invoiceDate?: Date | string | number | null;
|
||||
createdAt?: Date | string | number | null;
|
||||
isSubscription?: number | null;
|
||||
status?: string | null;
|
||||
supplierName?: string | null;
|
||||
totalAmount?: number | string | null;
|
||||
};
|
||||
|
||||
export type AnnualInvoiceSummary = {
|
||||
year: number;
|
||||
bapCount: number;
|
||||
bapAmount: number;
|
||||
subscriptionCount: number;
|
||||
subscriptionAmount: number;
|
||||
totalAmount: number;
|
||||
};
|
||||
|
||||
export type SupplierBudgetSummary = {
|
||||
supplierName: string;
|
||||
subscriptionCount: number;
|
||||
subscriptionAmount: number;
|
||||
nonSubscriptionCount: number;
|
||||
nonSubscriptionAmount: number;
|
||||
totalAmount: number;
|
||||
};
|
||||
|
||||
function getAmount(value: AnalyticsInvoice["totalAmount"]): number {
|
||||
const amount = Number(value);
|
||||
return Number.isFinite(amount) ? amount : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Agrège les factures finalisées par année métier. « BAP » désigne ici les
|
||||
* factures hors abonnement, éligibles au circuit BAP, validées ou non.
|
||||
*/
|
||||
export function buildAnnualInvoiceSummary(invoices: AnalyticsInvoice[]): AnnualInvoiceSummary[] {
|
||||
const summaryByYear = new Map<number, AnnualInvoiceSummary>();
|
||||
|
||||
for (const invoice of invoices) {
|
||||
if (invoice.status !== "completed") continue;
|
||||
const date = getInvoicePeriodDate(invoice);
|
||||
if (!date) continue;
|
||||
|
||||
const year = date.getFullYear();
|
||||
const current = summaryByYear.get(year) ?? {
|
||||
year,
|
||||
bapCount: 0,
|
||||
bapAmount: 0,
|
||||
subscriptionCount: 0,
|
||||
subscriptionAmount: 0,
|
||||
totalAmount: 0,
|
||||
};
|
||||
const amount = getAmount(invoice.totalAmount);
|
||||
|
||||
if (invoice.isSubscription === 1) {
|
||||
current.subscriptionCount += 1;
|
||||
current.subscriptionAmount += amount;
|
||||
} else {
|
||||
current.bapCount += 1;
|
||||
current.bapAmount += amount;
|
||||
}
|
||||
current.totalAmount += amount;
|
||||
summaryByYear.set(year, current);
|
||||
}
|
||||
|
||||
return Array.from(summaryByYear.values()).sort((a, b) => b.year - a.year);
|
||||
}
|
||||
|
||||
/** Agrège le budget réellement facturé par fournisseur sur la période choisie. */
|
||||
export function buildSupplierBudgetSummary(
|
||||
invoices: AnalyticsInvoice[],
|
||||
year: string,
|
||||
month: string,
|
||||
): SupplierBudgetSummary[] {
|
||||
const summaryBySupplier = new Map<string, SupplierBudgetSummary>();
|
||||
|
||||
for (const invoice of invoices) {
|
||||
if (invoice.status !== "completed" || !matchesInvoicePeriod(invoice, year, month)) continue;
|
||||
|
||||
const supplierName = invoice.supplierName?.trim() || "Fournisseur inconnu";
|
||||
const current = summaryBySupplier.get(supplierName) ?? {
|
||||
supplierName,
|
||||
subscriptionCount: 0,
|
||||
subscriptionAmount: 0,
|
||||
nonSubscriptionCount: 0,
|
||||
nonSubscriptionAmount: 0,
|
||||
totalAmount: 0,
|
||||
};
|
||||
const amount = getAmount(invoice.totalAmount);
|
||||
|
||||
if (invoice.isSubscription === 1) {
|
||||
current.subscriptionCount += 1;
|
||||
current.subscriptionAmount += amount;
|
||||
} else {
|
||||
current.nonSubscriptionCount += 1;
|
||||
current.nonSubscriptionAmount += amount;
|
||||
}
|
||||
current.totalAmount += amount;
|
||||
summaryBySupplier.set(supplierName, current);
|
||||
}
|
||||
|
||||
return Array.from(summaryBySupplier.values()).sort((a, b) =>
|
||||
b.totalAmount - a.totalAmount || a.supplierName.localeCompare(b.supplierName, "fr"),
|
||||
);
|
||||
}
|
||||
27
shared/invoicePeriod.ts
Normal file
27
shared/invoicePeriod.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Date métier utilisée pour les listes de factures : date de facture si elle
|
||||
* est connue, sinon date de réception. Cette règle est partagée avec BAP.
|
||||
*/
|
||||
export function getInvoicePeriodDate(invoice: {
|
||||
invoiceDate?: Date | string | number | null;
|
||||
createdAt?: Date | string | number | null;
|
||||
}): Date | null {
|
||||
const value = invoice.invoiceDate ?? invoice.createdAt;
|
||||
if (!value) return null;
|
||||
|
||||
const date = value instanceof Date ? value : new Date(value);
|
||||
return Number.isNaN(date.getTime()) ? null : date;
|
||||
}
|
||||
|
||||
/** Indique si une facture correspond au filtre Année/Mois sélectionné. */
|
||||
export function matchesInvoicePeriod(
|
||||
invoice: Parameters<typeof getInvoicePeriodDate>[0],
|
||||
year: string,
|
||||
month: string,
|
||||
): boolean {
|
||||
if (year === "all") return true;
|
||||
|
||||
const date = getInvoicePeriodDate(invoice);
|
||||
if (!date || date.getFullYear() !== Number(year)) return false;
|
||||
return month === "all" || date.getMonth() + 1 === Number(month);
|
||||
}
|
||||
178
todo.md
178
todo.md
@@ -703,7 +703,177 @@
|
||||
- [x] Ajouter des tests de non-régression ciblés et vérifier build, types et tests
|
||||
- [x] Normaliser les valeurs OAuth de loginMethod avant écriture en base
|
||||
|
||||
## Déploiement recette — audit de robustesse
|
||||
- [ ] Pousser le checkpoint d’audit vers Gitea recette
|
||||
- [ ] Reconstruire l’application sur le serveur de recette
|
||||
- [ ] Vérifier le commit, les conteneurs et la disponibilité HTTP en recette
|
||||
## Incident production — erreur HTTP 404
|
||||
- [x] Reproduire la 404 et contrôler le domaine, Traefik et les conteneurs
|
||||
- [x] Identifier et corriger la cause racine sans modifier les données
|
||||
- [x] Vérifier le retour HTTP 200 et la santé des conteneurs
|
||||
|
||||
## Audit production — 674 factures affichées
|
||||
- [x] Compter les factures par utilisateur, source et statut
|
||||
- [x] Identifier les groupes de doublons selon plusieurs clés métier
|
||||
- [x] Vérifier les références de stockage et les effets de la fusion précédente
|
||||
- [x] Préparer une correction réversible sans suppression immédiate
|
||||
- [x] Sauvegarder la base et le volume puis suspendre les imports email
|
||||
- [x] Bloquer les réimports par empreinte PDF et fiabiliser le traitement IMAP
|
||||
- [x] Déployer le correctif anti-réimport et migrer la base de production
|
||||
- [x] Appliquer la correction confirmée et vérifier le comptage final
|
||||
|
||||
## Audit authentification IMAP Microsoft 365
|
||||
- [x] Vérifier la génération du jeton Azure et le format XOAUTH2 envoyé à IMAP
|
||||
- [x] Comparer les scopes, permissions et méthode d’authentification aux exigences Microsoft 365
|
||||
- [x] Tester la configuration active de production sans exposer les secrets
|
||||
- [x] Documenter la cause du refus IMAP et le correctif requis
|
||||
|
||||
## Migration import email vers ImapFlow
|
||||
- [x] Remplacer la dépendance `imap` par `imapflow`
|
||||
- [x] Réécrire la connexion OAuth2, la recherche UNSEEN et la lecture des messages
|
||||
- [x] Conserver le traitement séquentiel, le verrou anti-concurrence et le marquage Seen après succès
|
||||
- [x] Adapter le test de connexion IMAP et les messages d’erreur
|
||||
- [x] Ajouter des tests de non-régression du flux ImapFlow
|
||||
- [x] Vérifier TypeScript, tests, build et authentification OAuth2 réelle
|
||||
|
||||
## Déploiement ImapFlow — recette et production
|
||||
- [x] Pousser la version ImapFlow vers le dépôt Gitea de recette
|
||||
- [x] Réduire l’image runtime Docker pour fiabiliser le build sur le serveur de recette
|
||||
- [x] Charger Vite uniquement en développement pour l’exclure de l’image runtime
|
||||
- [x] Déployer et valider HTTP et conteneurs ImapFlow en recette (aucune source OAuth2 active à tester)
|
||||
- [x] Corriger les règles Docker orphelines qui bloquaient MySQL en recette
|
||||
- [x] Pousser la version validée vers le dépôt Gitea de production
|
||||
- [x] Déployer et valider HTTP, conteneurs et OAuth2 ImapFlow en production
|
||||
|
||||
## Navigation depuis l’édition des factures BAP
|
||||
- [x] Reproduire et identifier la perte du contexte de navigation depuis Factures BAP
|
||||
- [x] Restaurer la page d’origine après fermeture de l’édition
|
||||
- [x] Conserver les filtres et tris sélectionnés sur Factures BAP
|
||||
- [x] Ajouter des tests de non-régression du retour contextuel
|
||||
- [x] Vérifier TypeScript, tests, build et parcours en sandbox
|
||||
- [x] Pousser le correctif vers Gitea recette et redéployer
|
||||
- [x] Vérifier le parcours et la santé de la recette
|
||||
|
||||
## Automatisation de l’indicateur Abonnement
|
||||
- [x] Analyser le modèle, le formulaire et l’exécution actuelle des automatismes
|
||||
- [x] Ajouter l’option Abonnement : Oui, Non ou Ne pas modifier aux règles
|
||||
- [x] Appliquer l’option Abonnement lors du traitement automatique des factures
|
||||
- [x] Ajouter des tests de non-régression des règles d’abonnement
|
||||
- [x] Valider les deux correctifs en sandbox
|
||||
|
||||
## Déploiement recette — navigation BAP et abonnement
|
||||
- [x] Pousser les deux correctifs vers Gitea recette
|
||||
- [x] Déployer, vérifier HTTP et valider le parcours de retour en recette
|
||||
|
||||
## Correctif recette — dialogue d’automatisme
|
||||
- [x] Vérifier le commit et localiser l’emplacement absent de l’option Abonnement
|
||||
- [x] Corriger le dialogue d’édition et le valider en recette
|
||||
- [x] Arrêter le processus non applicatif autorisé qui bloque le build Docker
|
||||
|
||||
## Déploiement production — dialogue Abonnement
|
||||
- [x] Pousser le correctif vers Gitea production
|
||||
- [x] Construire et redémarrer uniquement le conteneur applicatif
|
||||
- [x] Vérifier le commit, la santé, HTTP et le bundle de production
|
||||
|
||||
## Filtre par action des automatismes
|
||||
- [x] Identifier les types d’action disponibles et leurs règles de détection
|
||||
- [x] Ajouter un filtre d’action dans la liste des automatismes
|
||||
- [x] Permettre d’isoler les règles Abonnement, Oui ou Non
|
||||
- [x] Ajouter les tests et valider TypeScript, tests et build
|
||||
|
||||
## Déploiement — filtre par action des automatismes
|
||||
- [x] Pousser le filtre par action vers Gitea recette
|
||||
- [x] Déployer et vérifier le filtre par action en recette
|
||||
- [x] Pousser la version validée vers Gitea production
|
||||
- [x] Déployer et vérifier le filtre par action en production
|
||||
|
||||
## Seuil d’éligibilité BAP à 90 %
|
||||
- [x] Identifier les validations BAP fondées sur le score de qualité
|
||||
- [x] Abaisser le seuil de 100 % à 90 % pour les opérations BAP
|
||||
- [x] Ajouter les tests de seuil et valider TypeScript, tests et build
|
||||
|
||||
## Déploiement — seuil BAP à 90 %
|
||||
- [x] Pousser le correctif vers Gitea recette
|
||||
- [x] Déployer et vérifier le seuil BAP à 90 % en recette
|
||||
- [x] Pousser la version validée vers Gitea production
|
||||
- [x] Déployer et vérifier le seuil BAP à 90 % en production
|
||||
|
||||
## Rétablissement du serveur de recette
|
||||
- [x] Redémarrer le serveur de recette autorisé par l’utilisateur
|
||||
- [x] Rétablir le conteneur applicatif et terminer le déploiement BAP à 90 %
|
||||
|
||||
## Factures abonnements et filtres de période
|
||||
- [x] Analyser les listes Factures, Factures BAP et le menu Facturation
|
||||
- [x] Ajouter les filtres Année et Mois à la page Factures
|
||||
- [x] Créer la page Factures abonnements affichant uniquement les abonnements
|
||||
- [x] Reprendre les recherches, filtres et actions de la page Factures
|
||||
- [x] Ajouter l’entrée Factures abonnements après Factures BAP dans le menu
|
||||
- [x] Ajouter les tests et valider TypeScript, build et affichage en sandbox
|
||||
- [x] Déployer et vérifier le correctif en recette
|
||||
- [x] Déployer et vérifier le correctif en production
|
||||
|
||||
## Synthèse annuelle du tableau de bord
|
||||
- [x] Analyser les données et le tableau de bord existant
|
||||
- [x] Calculer par année les volumes et montants BAP et abonnements
|
||||
- [x] Afficher le nombre de factures et les montants par type, puis le total
|
||||
- [x] Ajouter les tests et valider TypeScript, build et affichage en sandbox
|
||||
|
||||
## Budget réel par fournisseur
|
||||
- [x] Définir l’agrégation des montants par fournisseur et statut Abonnement
|
||||
- [x] Ajouter les filtres Année et Mois à l’écran Budget réel
|
||||
- [x] Créer l’écran Budget réel à la fin du menu Facturation
|
||||
- [x] Afficher par fournisseur les montants Abonnement, Hors abonnement et le total
|
||||
- [x] Ajouter les tests et valider TypeScript, build et affichage en sandbox
|
||||
|
||||
## Améliorations Budget réel et tableau de bord
|
||||
- [x] Confirmer le tri décroissant par montant total dans le budget
|
||||
- [x] Ajouter un graphique annuel BAP versus abonnements au tableau de bord
|
||||
- [x] Ajouter les tests et valider TypeScript, build et rendu sandbox
|
||||
|
||||
## Déploiement et visibilité production — budget et graphique
|
||||
- [x] Pousser les améliorations vers Gitea recette
|
||||
- [x] Déployer et vérifier les améliorations en recette
|
||||
- [x] Pousser la version validée vers Gitea production
|
||||
- [x] Déployer et vérifier les améliorations en production
|
||||
- [x] Contrôler le dashboard et le portail de production : aucun changement requis, le catalogue et les statuts sont déjà à jour
|
||||
|
||||
## Contrôle de l’import automatique e-mail
|
||||
- [x] Vérifier en production les paramètres et traces de l’import e-mail automatique : un compte reste activé dans la configuration, sans trace récente de déclenchement
|
||||
|
||||
## Désactivation de l’import automatique e-mail
|
||||
- [x] Désactiver la planification e-mail pour tous les comptes en recette
|
||||
- [x] Désactiver la planification e-mail pour tous les comptes en production
|
||||
- [x] Redémarrer uniquement les conteneurs applicatifs pour vider les planifications en mémoire
|
||||
- [x] Vérifier les paramètres, la santé et l’absence de redémarrage automatique sur les deux environnements
|
||||
|
||||
## Investigation des factures production du 1er septembre 2026
|
||||
- [x] Identifier les deux factures créées et leur source d’import sans modifier les données
|
||||
- [x] Corréler les factures avec l’historique, les fichiers source et les journaux applicatifs
|
||||
- [x] Vérifier l’état de l’import e-mail au moment de leur création et déterminer la cause : le compte utilisateur 2 était activé, avec une cadence de 30 minutes
|
||||
|
||||
## Historique détaillé et déclenchement manuel des imports
|
||||
- [x] Analyser les données d’import, les routes et la page Historique existantes
|
||||
- [x] Ajouter une vue détaillée de l’historique avec source, compte, date, résultats et erreurs
|
||||
- [x] Ajouter une action administrateur d’import e-mail manuel par compte configuré
|
||||
- [x] Garantir que le déclenchement manuel ne modifie jamais la planification automatique
|
||||
- [x] Ajouter les tests de non-régression et valider le parcours en sandbox
|
||||
|
||||
## Alignement des filtres Factures BAP
|
||||
- [x] Ajouter les filtres Destinataires, Entités et Ventilations après le filtre Mois
|
||||
- [x] Appliquer ces filtres à la liste BAP en préservant les filtres et le tri existants
|
||||
- [x] Ajouter les tests et valider l’affichage de Factures BAP en sandbox
|
||||
|
||||
## Automatismes d’export et simplification des Paramètres
|
||||
- [x] Ajouter les onglets Automatismes d’import et Automatismes d’export
|
||||
- [x] Créer des règles de destination d’export par destinataire ou ventilation comptable
|
||||
- [x] Prendre en charge les destinations dossier local, Teams et SharePoint sans perdre les réglages existants
|
||||
- [x] Appliquer les règles de destination lors des exports de factures BAP
|
||||
- [x] Déplacer les paramètres d’export dans Automatismes et renommer la page en Paramètres
|
||||
- [x] Ajouter les tests et valider les nouveaux parcours en sandbox
|
||||
|
||||
## Répartition des paramètres Azure AD
|
||||
- [x] Rétablir dans Paramètres l’URL SharePoint, Tenant ID, Client ID, secret et date d’expiration
|
||||
- [x] Rétablir les tests Azure AD et upload SharePoint depuis Paramètres
|
||||
- [x] Retirer les accès Microsoft Azure AD des automatismes d’export
|
||||
- [x] Valider les deux emplacements en sandbox
|
||||
|
||||
## Présentation des automatismes
|
||||
- [x] Coloriser les onglets Import et Export
|
||||
- [x] Simplifier les libellés de l’onglet Export comme demandé
|
||||
- [x] Vérifier le rendu en sandbox
|
||||
|
||||
12
verification_notes.md
Normal file
12
verification_notes.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Vérification visuelle — Historique des imports
|
||||
|
||||
- Le 2 septembre 2026, la page `/history` a été vérifiée en format bureau et mobile.
|
||||
- Le cartouche administrateur de lecture e-mail ponctuelle est visible, explicite sur l’absence de planification créée et ne propose aucune activation automatique.
|
||||
- Les filtres et indicateurs restent lisibles sur mobile.
|
||||
- Le tableau est enveloppé dans un conteneur à défilement horizontal afin de préserver toutes les colonnes de traçabilité sur les écrans étroits.
|
||||
|
||||
## Vérification visuelle — Automatismes et Paramètres
|
||||
|
||||
- Le 2 septembre 2026, les routes `/automation-rules` et `/import-settings` ont été vérifiées en format bureau.
|
||||
- Les onglets « Automatismes d’import » et « Automatismes d’export » sont visibles dans la page Automatismes.
|
||||
- La navigation et le titre affichent désormais « Paramètres » ; seuls les onglets Paramètres d’import et Sauvegarde DB restent accessibles depuis cette page.
|
||||
Reference in New Issue
Block a user