|
| 1 | +"use server"; |
| 2 | +import { type ActionResponse } from "@/app/actions/actions"; |
| 3 | +import { env } from "@/env.mjs"; |
| 4 | +import { db } from "@comp/db"; |
| 5 | +import FirecrawlApp, { ScrapeResponse } from "@mendable/firecrawl-js"; |
| 6 | +import ky from "ky"; |
| 7 | +import { z } from "zod"; |
| 8 | +import { authActionClient } from "./safe-action"; |
| 9 | + |
| 10 | +let firecrawl: FirecrawlApp | null = null; |
| 11 | + |
| 12 | +if (process.env.FIRECRAWL_API_KEY) { |
| 13 | + firecrawl = new FirecrawlApp({ |
| 14 | + apiKey: process.env.FIRECRAWL_API_KEY, |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +const schema = z.object({ |
| 19 | + company_name: z.string(), |
| 20 | + legal_name: z.string(), |
| 21 | + company_description: z.string(), |
| 22 | + company_hq_address: z.string(), |
| 23 | + privacy_policy_url: z.string(), |
| 24 | + terms_of_service_url: z.string(), |
| 25 | + service_level_agreement_url: z.string(), |
| 26 | + security_overview_url: z.string(), |
| 27 | + trust_portal_url: z.string(), |
| 28 | + certified_security_frameworks: z.array(z.string()), |
| 29 | + subprocessors: z.array(z.string()), |
| 30 | + type_of_company: z.string(), |
| 31 | +}); |
| 32 | + |
| 33 | +export const researchVendorAction = authActionClient |
| 34 | + .schema( |
| 35 | + z.object({ |
| 36 | + website: z.string().url({ message: "Invalid URL format" }), |
| 37 | + }), |
| 38 | + ) |
| 39 | + .metadata({ |
| 40 | + name: "research-vendor", |
| 41 | + }) |
| 42 | + .action(async ({ parsedInput: { website }, ctx: { user } }) => { |
| 43 | + try { |
| 44 | + if (!firecrawl) { |
| 45 | + return { |
| 46 | + success: false, |
| 47 | + error: { message: "Firecrawl client not initialized" }, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + const existingVendor = await db.globalVendors.findUnique({ |
| 52 | + where: { |
| 53 | + website: website, |
| 54 | + }, |
| 55 | + select: { website: true }, |
| 56 | + }); |
| 57 | + |
| 58 | + if (existingVendor) { |
| 59 | + return { |
| 60 | + success: true, |
| 61 | + data: { |
| 62 | + message: "Vendor already exists.", |
| 63 | + vendorExists: true, |
| 64 | + }, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + const scrapeResult = await firecrawl.extract([website], { |
| 69 | + prompt: "You're a cyber security researcher, researching a vendor.", |
| 70 | + schema: schema, |
| 71 | + enableWebSearch: true, |
| 72 | + scrapeOptions: { |
| 73 | + onlyMainContent: true, |
| 74 | + removeBase64Images: true, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + if (!scrapeResult.success || !scrapeResult.data) { |
| 79 | + return { |
| 80 | + success: false, |
| 81 | + error: { |
| 82 | + message: `Failed to scrape vendor data: ${ |
| 83 | + scrapeResult.error || "Unknown error" |
| 84 | + }`, |
| 85 | + }, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + const vendorData = scrapeResult.data as z.infer<typeof schema>; |
| 90 | + |
| 91 | + await db.globalVendors.create({ |
| 92 | + data: { |
| 93 | + website: website, |
| 94 | + company_name: vendorData.company_name, |
| 95 | + legal_name: vendorData.legal_name, |
| 96 | + company_description: vendorData.company_description, |
| 97 | + company_hq_address: vendorData.company_hq_address, |
| 98 | + privacy_policy_url: vendorData.privacy_policy_url, |
| 99 | + terms_of_service_url: vendorData.terms_of_service_url, |
| 100 | + service_level_agreement_url: |
| 101 | + vendorData.service_level_agreement_url, |
| 102 | + security_page_url: vendorData.security_overview_url, |
| 103 | + trust_page_url: vendorData.trust_portal_url, |
| 104 | + security_certifications: |
| 105 | + vendorData.certified_security_frameworks, |
| 106 | + subprocessors: vendorData.subprocessors, |
| 107 | + type_of_company: vendorData.type_of_company, |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + return { |
| 112 | + success: true, |
| 113 | + data: { |
| 114 | + message: "Vendor researched and added successfully.", |
| 115 | + vendorExists: false, |
| 116 | + }, |
| 117 | + }; |
| 118 | + } catch (error) { |
| 119 | + console.error("Error in researchVendorAction:", error); |
| 120 | + |
| 121 | + return { |
| 122 | + success: false, |
| 123 | + error: { |
| 124 | + message: |
| 125 | + error instanceof Error |
| 126 | + ? error.message |
| 127 | + : "An unexpected error occurred.", |
| 128 | + }, |
| 129 | + }; |
| 130 | + } |
| 131 | + }); |
0 commit comments