Pre-Final Backup

This commit is contained in:
2026-05-03 13:26:31 +01:00
parent cd11e76c07
commit 165af509ef
19 changed files with 2829 additions and 1223 deletions

View File

@@ -23,23 +23,57 @@ type StatusData = Array<{ name: string; count: number }>;
const PIE_COLORS: Record<string, string> = {
Uploaded: "hsl(38 92% 50%)",
Processing: "hsl(var(--primary))",
Processing: "hsl(217 91% 60%)",
Analyzed: "hsl(160 84% 39%)",
Failed: "hsl(var(--destructive))",
Failed: "hsl(0 84% 60%)",
};
const FALLBACK_COLORS = [
"hsl(var(--primary))",
"hsl(var(--secondary))",
"hsl(var(--accent))",
"hsl(var(--destructive))",
"hsl(217 91% 60%)",
"hsl(260 89% 65%)",
"hsl(190 85% 50%)",
"hsl(340 82% 52%)",
];
const tooltipStyle = {
backgroundColor: "hsl(var(--background))",
border: "1px solid hsl(var(--border))",
borderRadius: "12px",
backgroundColor: "hsl(var(--background) / 0.95)",
border: "1px solid hsl(var(--border) / 0.6)",
borderRadius: "16px",
color: "hsl(var(--foreground))",
backdropFilter: "blur(12px)",
boxShadow: "0 8px 32px rgba(0,0,0,0.12)",
padding: "12px 16px",
fontSize: "13px",
};
const CustomTooltip = ({ active, payload, label }: any) => {
if (!active || !payload?.length) return null;
return (
<div style={tooltipStyle} className="space-y-1.5 min-w-[140px]">
{label && (
<p className="text-[11px] font-bold uppercase tracking-wider text-muted-foreground border-b border-border/40 pb-1.5 mb-1.5">
{label}
</p>
)}
{payload.map((entry: any, index: number) => (
<div key={index} className="flex items-center justify-between gap-4">
<div className="flex items-center gap-2">
<span
className="h-2 w-2 rounded-full shrink-0"
style={{ backgroundColor: entry.color }}
/>
<span className="text-xs text-muted-foreground">{entry.name}</span>
</div>
<span className="text-xs font-bold text-foreground tabular-nums">
{typeof entry.value === "number"
? entry.value.toLocaleString()
: entry.value}
</span>
</div>
))}
</div>
);
};
export function TrendChart({ data }: { data: TrendData }) {
@@ -72,64 +106,82 @@ export function TrendChart({ data }: { data: TrendData }) {
<defs>
<linearGradient id="trendFill" x1="0" y1="0" x2="0" y2="1">
<stop
offset="5%"
stopColor="hsl(var(--primary))"
stopOpacity={0.65}
offset="0%"
stopColor="hsl(217 91% 60%)"
stopOpacity={0.5}
/>
<stop
offset="95%"
stopColor="hsl(var(--primary))"
stopOpacity={0.05}
offset="60%"
stopColor="hsl(217 91% 60%)"
stopOpacity={0.15}
/>
<stop
offset="100%"
stopColor="hsl(217 91% 60%)"
stopOpacity={0.02}
/>
</linearGradient>
<linearGradient id="trendStroke" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stopColor="hsl(217 91% 60%)" />
<stop offset="100%" stopColor="hsl(260 89% 65%)" />
</linearGradient>
<linearGradient id="avgStroke" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stopColor="hsl(260 89% 65%)" />
<stop offset="100%" stopColor="hsl(190 85% 50%)" />
</linearGradient>
<filter id="glow" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur stdDeviation="3" result="coloredBlur" />
<feMerge>
<feMergeNode in="coloredBlur" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<CartesianGrid
strokeDasharray="3 3"
stroke="hsl(var(--border))"
strokeDasharray="3 6"
stroke="hsl(var(--border) / 0.4)"
vertical={false}
/>
<XAxis
dataKey="date"
stroke="hsl(var(--muted-foreground))"
stroke="hsl(var(--muted-foreground) / 0.5)"
interval={xAxisInterval}
tick={{ fontSize: 12 }}
tick={{ fontSize: 11, fontWeight: 500 }}
tickLine={false}
axisLine={false}
tickMargin={8}
/>
<YAxis
stroke="hsl(var(--muted-foreground))"
stroke="hsl(var(--muted-foreground) / 0.5)"
allowDecimals={false}
tick={{ fontSize: 11, fontWeight: 500 }}
tickLine={false}
axisLine={false}
tickMargin={8}
/>
<Tooltip
contentStyle={tooltipStyle}
formatter={(
value: number | string | undefined,
name: string | number | undefined,
) => {
const numericValue = Number(value ?? 0);
if (name === "movingAverage") {
return [numericValue.toFixed(1), "7-day avg"];
}
return [numericValue, "Uploads"];
}}
/>
<Tooltip content={<CustomTooltip />} />
<Area
type="monotone"
dataKey="count"
stroke="hsl(var(--primary))"
strokeWidth={2.25}
stroke="url(#trendStroke)"
strokeWidth={2.5}
fillOpacity={1}
fill="url(#trendFill)"
activeDot={{ r: 5 }}
activeDot={{
r: 6,
stroke: "hsl(var(--background))",
strokeWidth: 3,
fill: "hsl(217 91% 60%)",
filter: "url(#glow)",
}}
dot={false}
/>
<Line
type="monotone"
dataKey="movingAverage"
stroke="hsl(var(--secondary))"
stroke="url(#avgStroke)"
strokeWidth={2}
strokeDasharray="6 4"
dot={false}
/>
</AreaChart>
@@ -152,43 +204,65 @@ export function ContractTypeChart({ data }: { data: TypeData }) {
layout="vertical"
margin={{ top: 10, right: 10, left: 0, bottom: 0 }}
>
<defs>
<linearGradient id="barGradient" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stopColor="hsl(217 91% 60%)" />
<stop offset="100%" stopColor="hsl(260 89% 65%)" />
</linearGradient>
<linearGradient id="barGradient2" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stopColor="hsl(260 89% 65%)" />
<stop offset="100%" stopColor="hsl(190 85% 50%)" />
</linearGradient>
<linearGradient id="barGradient3" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stopColor="hsl(190 85% 50%)" />
<stop offset="100%" stopColor="hsl(340 82% 52%)" />
</linearGradient>
<linearGradient id="barGradient4" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stopColor="hsl(340 82% 52%)" />
<stop offset="100%" stopColor="hsl(38 92% 50%)" />
</linearGradient>
</defs>
<CartesianGrid
strokeDasharray="3 3"
stroke="hsl(var(--border))"
strokeDasharray="3 6"
stroke="hsl(var(--border) / 0.4)"
horizontal={false}
/>
<XAxis
type="number"
stroke="hsl(var(--muted-foreground))"
stroke="hsl(var(--muted-foreground) / 0.5)"
allowDecimals={false}
tick={{ fontSize: 12 }}
tick={{ fontSize: 11, fontWeight: 500 }}
tickLine={false}
axisLine={false}
tickMargin={8}
/>
<YAxis
type="category"
dataKey="type"
width={128}
stroke="hsl(var(--muted-foreground))"
tick={{ fontSize: 12 }}
stroke="hsl(var(--muted-foreground) / 0.5)"
tick={{ fontSize: 11, fontWeight: 600 }}
tickLine={false}
axisLine={false}
tickMargin={8}
/>
<Tooltip
contentStyle={tooltipStyle}
cursor={false}
formatter={(value: number | string | undefined) => [
Number(value ?? 0),
"Files",
]}
content={<CustomTooltip />}
cursor={{ fill: "hsl(var(--muted) / 0.15)", radius: 8 }}
/>
<Bar dataKey="count" radius={[0, 8, 8, 0]}>
<Bar dataKey="count" radius={[0, 10, 10, 0]} maxBarSize={32}>
{sortedData.map((item, index) => {
const opacity = Math.max(0.35, 0.95 - index * 0.12);
const gradients = [
"url(#barGradient)",
"url(#barGradient2)",
"url(#barGradient3)",
"url(#barGradient4)",
];
return (
<Cell
key={`${item.type}-${index}`}
fill={`hsl(var(--primary) / ${opacity})`}
fill={gradients[index % gradients.length]}
className="transition-all duration-300 hover:opacity-80"
/>
);
})}
@@ -210,16 +284,26 @@ export function ContractStatusChart({ data }: { data: StatusData }) {
<div className="h-[76%] w-full">
<ResponsiveContainer width="100%" height="100%">
<PieChart>
<defs>
<filter id="pieGlow" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur stdDeviation="4" result="coloredBlur" />
<feMerge>
<feMergeNode in="coloredBlur" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<Pie
data={data}
cx="50%"
cy="50%"
innerRadius={62}
outerRadius={94}
paddingAngle={3}
innerRadius={58}
outerRadius={88}
paddingAngle={4}
dataKey="count"
stroke="hsl(var(--background))"
strokeWidth={2}
strokeWidth={3}
cornerRadius={6}
>
{data.map((entry, index) => (
<Cell
@@ -228,44 +312,40 @@ export function ContractStatusChart({ data }: { data: StatusData }) {
PIE_COLORS[entry.name] ??
FALLBACK_COLORS[index % FALLBACK_COLORS.length]
}
className="transition-all duration-300 hover:opacity-90"
style={{ filter: "drop-shadow(0 2px 8px rgba(0,0,0,0.1))" }}
/>
))}
</Pie>
{total > 0 && (
<text
x="50%"
y="50%"
y="48%"
textAnchor="middle"
dominantBaseline="middle"
>
<tspan
x="50%"
y="50%"
className="fill-foreground text-base font-semibold"
y="48%"
className="fill-foreground text-xl font-bold tracking-tight"
>
{total}
{total.toLocaleString()}
</tspan>
<tspan
x="50%"
dy="16"
className="fill-muted-foreground text-[11px]"
dy="18"
className="fill-muted-foreground text-[11px] font-medium uppercase tracking-wider"
>
Files
</tspan>
</text>
)}
<Tooltip
contentStyle={tooltipStyle}
formatter={(value: number | string | undefined) => [
Number(value ?? 0),
"Files",
]}
/>
<Tooltip content={<CustomTooltip />} />
</PieChart>
</ResponsiveContainer>
</div>
<div className="grid grid-cols-2 gap-2 pt-2">
<div className="grid grid-cols-2 gap-2.5 pt-2">
{data.map((item, index) => {
const color =
PIE_COLORS[item.name] ??
@@ -274,16 +354,16 @@ export function ContractStatusChart({ data }: { data: StatusData }) {
return (
<div
key={`${item.name}-legend`}
className="flex items-center gap-2 rounded-lg border border-border/50 bg-muted/25 px-2.5 py-1.5"
className="group flex items-center gap-2.5 rounded-xl border border-border/40 bg-background/40 backdrop-blur-md px-3 py-2 hover:bg-background/60 hover:border-border/60 transition-all cursor-default"
>
<span
className="h-2.5 w-2.5 rounded-full"
style={{ backgroundColor: color }}
className="h-2.5 w-2.5 rounded-full ring-2 ring-offset-1 ring-offset-background"
style={{ backgroundColor: color, "--tw-ring-color": color } as React.CSSProperties}
/>
<span className="text-[11px] text-muted-foreground truncate">
<span className="text-[11px] text-muted-foreground truncate font-medium">
{item.name}
</span>
<span className="ml-auto text-[11px] font-medium text-foreground">
<span className="ml-auto text-[11px] font-bold text-foreground tabular-nums">
{item.count}
</span>
</div>

View File

@@ -20,6 +20,7 @@
"use server";
import { auth } from "@clerk/nextjs/server";
import { clerkClient } from "@clerk/nextjs/server";
import { revalidatePath } from "next/cache";
import {
ContractService,
@@ -29,6 +30,7 @@ import { AIService } from "@/lib/services/ai.service";
import { RAGService } from "@/lib/services/rag.service";
import { NotificationService } from "@/lib/services/notification.service";
import { BlockchainService } from "@/lib/services/blockchain.service";
import { EmailService } from "@/lib/services/email.service";
import { prisma } from "@/lib/db/prisma";
import type { NormalizedAnalysis } from "@/lib/services/ai/analysis.types";
@@ -209,7 +211,9 @@ export async function getContracts(filters?: Record<string, unknown>) {
documentHash: contract.documentHash || null,
txHash: contract.txHash || null,
blockNumber: contract.blockNumber || null,
blockTimestamp: contract.blockTimestamp ? contract.blockTimestamp.toISOString() : null,
blockTimestamp: contract.blockTimestamp
? contract.blockTimestamp.toISOString()
: null,
blockchainNetwork: contract.blockchainNetwork || null,
contractAddress: contract.contractAddress || null,
}));
@@ -517,6 +521,16 @@ export async function analyzeContractAction(id: string) {
keyPoints: keyPointsWithLearning,
});
let blockchainEmailData: {
documentHash: string;
txHash: string;
blockNumber: number;
blockTimestamp: Date;
network: string;
contractAddress: string;
explorerUrl: string | null;
} | null = null;
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BLOCKCHAIN: Auto-register document on-chain
// This is non-blocking — if blockchain fails, analysis still succeeds
@@ -525,7 +539,7 @@ export async function analyzeContractAction(id: string) {
if (BlockchainService.isConfigured()) {
const proof = await BlockchainService.hashAndRegister(
contract.fileUrl,
contract.fileName
contract.fileName,
);
// Save blockchain proof to the contract record
@@ -556,7 +570,19 @@ export async function analyzeContractAction(id: string) {
},
});
console.log(`🔗 Blockchain proof stored: ${proof.txHash.slice(0, 16)}...`);
blockchainEmailData = {
documentHash: proof.documentHash,
txHash: proof.txHash,
blockNumber: proof.blockNumber,
blockTimestamp: proof.blockTimestamp,
network: proof.network,
contractAddress: proof.contractAddress,
explorerUrl: proof.explorerUrl,
};
console.log(
`🔗 Blockchain proof stored: ${proof.txHash.slice(0, 16)}...`,
);
}
} catch (blockchainError) {
// Blockchain failure should NOT fail the analysis
@@ -581,6 +607,71 @@ export async function analyzeContractAction(id: string) {
expiresIn: 7 * 24 * 60 * 60 * 1000, // 7 days
});
// Email summary + blockchain proof (non-blocking)
try {
let recipientEmail = user.email;
if (!recipientEmail) {
const clerk = await clerkClient();
const clerkUser = await clerk.users.getUser(clerkId);
recipientEmail =
clerkUser.emailAddresses.find(
(address) => address.id === clerkUser.primaryEmailAddressId,
)?.emailAddress ??
clerkUser.emailAddresses[0]?.emailAddress ??
"";
}
if (recipientEmail) {
const premiumValue =
aiResults.premium === null || aiResults.premium === undefined
? null
: aiResults.premium;
const keyPointsRecord =
typeof keyPointsWithLearning === "object" &&
keyPointsWithLearning !== null
? (keyPointsWithLearning as Record<string, unknown>)
: null;
await EmailService.sendContractAnalysisCompletedEmail({
to: recipientEmail,
userDisplayName:
`${user.firstName ?? ""} ${user.lastName ?? ""}`.trim() || null,
contractId: id,
contractFileName: contract.fileName,
contractTitle: aiResults.title,
blueprint: {
type: aiResults.type,
provider: aiResults.provider ?? null,
policyNumber: aiResults.policyNumber ?? null,
startDate: aiResults.startDate ?? null,
endDate: aiResults.endDate ?? null,
premium: premiumValue,
premiumCurrency:
aiAnalysis.premiumCurrency ??
(keyPointsRecord?.aiMeta &&
typeof keyPointsRecord.aiMeta === "object" &&
keyPointsRecord.aiMeta !== null &&
"premiumCurrency" in keyPointsRecord.aiMeta
? String(
(keyPointsRecord.aiMeta as Record<string, unknown>)
.premiumCurrency ?? "",
) || null
: null),
summary: aiResults.summary,
},
blockchain: blockchainEmailData,
});
} else {
console.warn(
`⚠️ Contract analysis email skipped: no recipient email found for user ${user.id}`,
);
}
} catch (emailError) {
console.warn("⚠️ Contract analysis email skipped:", emailError);
}
revalidatePath("/contacts");
revalidatePath("/dashboard");

File diff suppressed because it is too large Load Diff