Pre-Final Backup
This commit is contained in:
@@ -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
Reference in New Issue
Block a user