11import type { APIEvent } from "@solidjs/start/server"
22import { AWS } from "@opencode-ai/console-core/aws.js"
3+ import { Resource } from "@opencode-ai/console-resource"
34import { i18n } from "~/i18n"
45import { localeFromRequest } from "~/lib/language"
56import { createLead } from "~/lib/salesforce"
@@ -14,6 +15,64 @@ interface EnterpriseFormData {
1415 message : string
1516}
1617
18+ const EMAIL_OCTOPUS_LIST_ID = "1b381e5e-39bd-11f1-ba4a-cdd4791f0c43"
19+
20+ function splitFullName ( fullName : string ) {
21+ const parts = fullName
22+ . trim ( )
23+ . split ( / \s + / )
24+ . filter ( ( p ) => p . length > 0 )
25+ if ( parts . length === 0 ) return { firstName : "" , lastName : "" }
26+ if ( parts . length === 1 ) return { firstName : parts [ 0 ] , lastName : "" }
27+ return { firstName : parts [ 0 ] , lastName : parts . slice ( 1 ) . join ( " " ) }
28+ }
29+
30+ function getEmailOctopusApiKey ( ) {
31+ if ( process . env . EMAILOCTOPUS_API_KEY ) return process . env . EMAILOCTOPUS_API_KEY
32+ try {
33+ return Resource . EMAILOCTOPUS_API_KEY . value
34+ } catch {
35+ return
36+ }
37+ }
38+
39+ function subscribe ( email : string , fullName : string ) {
40+ const apiKey = getEmailOctopusApiKey ( )
41+ if ( ! apiKey ) {
42+ console . warn ( "Skipping EmailOctopus subscribe: missing API key" )
43+ return Promise . resolve ( false )
44+ }
45+
46+ const name = splitFullName ( fullName )
47+ const fields : Record < string , string > = { }
48+ if ( name . firstName ) fields . FirstName = name . firstName
49+ if ( name . lastName ) fields . LastName = name . lastName
50+
51+ const payload : { email_address : string ; fields ?: Record < string , string > } = { email_address : email }
52+ if ( Object . keys ( fields ) . length ) payload . fields = fields
53+
54+ return fetch ( `https://api.emailoctopus.com/lists/${ EMAIL_OCTOPUS_LIST_ID } /contacts` , {
55+ method : "PUT" ,
56+ headers : {
57+ Authorization : `Bearer ${ apiKey } ` ,
58+ "Content-Type" : "application/json" ,
59+ } ,
60+ body : JSON . stringify ( payload ) ,
61+ } ) . then (
62+ ( res ) => {
63+ if ( ! res . ok ) {
64+ console . error ( "EmailOctopus subscribe failed:" , res . status , res . statusText )
65+ return false
66+ }
67+ return true
68+ } ,
69+ ( err ) => {
70+ console . error ( "Failed to subscribe enterprise email:" , err )
71+ return false
72+ } ,
73+ )
74+ }
75+
1776export async function POST ( event : APIEvent ) {
1877 const dict = i18n ( localeFromRequest ( event . request ) )
1978 try {
@@ -41,14 +100,17 @@ ${body.role}<br>
41100${ body . company ? `${ body . company } <br>` : "" } ${ body . email } <br>
42101${ body . phone ? `${ body . phone } <br>` : "" } `. trim ( )
43102
44- const [ lead , mail ] = await Promise . all ( [
103+ const [ lead , mail , octopus ] = await Promise . all ( [
45104 createLead ( {
46105 name : body . name ,
47106 role : body . role ,
48107 company : body . company ,
49108 email : body . email ,
50109 phone : body . phone ,
51110 message : body . message ,
111+ } ) . catch ( ( err ) => {
112+ console . error ( "Failed to create Salesforce lead:" , err )
113+ return false
52114 } ) ,
53115 AWS . sendEmail ( {
54116@@ -62,9 +124,14 @@ ${body.phone ? `${body.phone}<br>` : ""}`.trim()
62124 return false
63125 } ,
64126 ) ,
127+ subscribe ( body . email , body . name ) ,
65128 ] )
66129
67- if ( ! lead && ! mail ) {
130+ if ( ! lead && ! mail && ! octopus ) {
131+ if ( import . meta. env . DEV ) {
132+ console . warn ( "Enterprise inquiry accepted in dev mode without integrations" , { email : body . email } )
133+ return Response . json ( { success : true , message : dict [ "enterprise.form.success.submitted" ] } , { status : 200 } )
134+ }
68135 console . error ( "Enterprise inquiry delivery failed" , { email : body . email } )
69136 return Response . json ( { error : dict [ "enterprise.form.error.internalServer" ] } , { status : 500 } )
70137 }
0 commit comments