Просмотр исходного кода

Stefan/enterprise forms waitlist (#23158)

Co-authored-by: Ryan Vogel <me@ryan.ceo>
Ryan Vogel 4 месяцев назад
Родитель
Сommit
1d54b0e540
1 измененных файлов с 69 добавлено и 2 удалено
  1. 69 2
      packages/console/app/src/routes/api/enterprise.ts

+ 69 - 2
packages/console/app/src/routes/api/enterprise.ts

@@ -1,5 +1,6 @@
 import type { APIEvent } from "@solidjs/start/server"
 import { AWS } from "@opencode-ai/console-core/aws.js"
+import { Resource } from "@opencode-ai/console-resource"
 import { i18n } from "~/i18n"
 import { localeFromRequest } from "~/lib/language"
 import { createLead } from "~/lib/salesforce"
@@ -14,6 +15,64 @@ interface EnterpriseFormData {
   message: string
 }
 
+const EMAIL_OCTOPUS_LIST_ID = "1b381e5e-39bd-11f1-ba4a-cdd4791f0c43"
+
+function splitFullName(fullName: string) {
+  const parts = fullName
+    .trim()
+    .split(/\s+/)
+    .filter((p) => p.length > 0)
+  if (parts.length === 0) return { firstName: "", lastName: "" }
+  if (parts.length === 1) return { firstName: parts[0], lastName: "" }
+  return { firstName: parts[0], lastName: parts.slice(1).join(" ") }
+}
+
+function getEmailOctopusApiKey() {
+  if (process.env.EMAILOCTOPUS_API_KEY) return process.env.EMAILOCTOPUS_API_KEY
+  try {
+    return Resource.EMAILOCTOPUS_API_KEY.value
+  } catch {
+    return
+  }
+}
+
+function subscribe(email: string, fullName: string) {
+  const apiKey = getEmailOctopusApiKey()
+  if (!apiKey) {
+    console.warn("Skipping EmailOctopus subscribe: missing API key")
+    return Promise.resolve(false)
+  }
+
+  const name = splitFullName(fullName)
+  const fields: Record<string, string> = {}
+  if (name.firstName) fields.FirstName = name.firstName
+  if (name.lastName) fields.LastName = name.lastName
+
+  const payload: { email_address: string; fields?: Record<string, string> } = { email_address: email }
+  if (Object.keys(fields).length) payload.fields = fields
+
+  return fetch(`https://api.emailoctopus.com/lists/${EMAIL_OCTOPUS_LIST_ID}/contacts`, {
+    method: "PUT",
+    headers: {
+      Authorization: `Bearer ${apiKey}`,
+      "Content-Type": "application/json",
+    },
+    body: JSON.stringify(payload),
+  }).then(
+    (res) => {
+      if (!res.ok) {
+        console.error("EmailOctopus subscribe failed:", res.status, res.statusText)
+        return false
+      }
+      return true
+    },
+    (err) => {
+      console.error("Failed to subscribe enterprise email:", err)
+      return false
+    },
+  )
+}
+
 export async function POST(event: APIEvent) {
   const dict = i18n(localeFromRequest(event.request))
   try {
@@ -41,7 +100,7 @@ ${body.role}<br>
 ${body.company ? `${body.company}<br>` : ""}${body.email}<br>
 ${body.phone ? `${body.phone}<br>` : ""}`.trim()
 
-    const [lead, mail] = await Promise.all([
+    const [lead, mail, octopus] = await Promise.all([
       createLead({
         name: body.name,
         role: body.role,
@@ -49,6 +108,9 @@ ${body.phone ? `${body.phone}<br>` : ""}`.trim()
         email: body.email,
         phone: body.phone,
         message: body.message,
+      }).catch((err) => {
+        console.error("Failed to create Salesforce lead:", err)
+        return false
       }),
       AWS.sendEmail({
         to: "contact@anoma.ly",
@@ -62,9 +124,14 @@ ${body.phone ? `${body.phone}<br>` : ""}`.trim()
           return false
         },
       ),
+      subscribe(body.email, body.name),
     ])
 
-    if (!lead && !mail) {
+    if (!lead && !mail && !octopus) {
+      if (import.meta.env.DEV) {
+        console.warn("Enterprise inquiry accepted in dev mode without integrations", { email: body.email })
+        return Response.json({ success: true, message: dict["enterprise.form.success.submitted"] }, { status: 200 })
+      }
       console.error("Enterprise inquiry delivery failed", { email: body.email })
       return Response.json({ error: dict["enterprise.form.error.internalServer"] }, { status: 500 })
     }