mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
feat(console): route non-sales enterprise inquiries by reason and map compliance fields (#61279)
This commit is contained in:
committed by
GitHub
Unverified
parent
394d49d487
commit
cce323bfcd
@@ -0,0 +1,8 @@
|
||||
// Why a visitor who isn't asking sales reached out, as the Console site's contact form offers it.
|
||||
export const contactReasons = [
|
||||
{ value: "account", label: "Get help with my account" },
|
||||
{ value: "list-models", label: "List my models on OpenCode" },
|
||||
{ value: "partnership", label: "Partnership, reseller, or MSP" },
|
||||
{ value: "hackathon-education", label: "Hackathon or education" },
|
||||
{ value: "other", label: "Something else" },
|
||||
] as const
|
||||
@@ -1,5 +1,8 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { enterpriseEmailContent } from "./enterprise"
|
||||
import { afterEach, expect, mock, spyOn, test } from "bun:test"
|
||||
import type { sendEmail } from "./aws"
|
||||
import { enterpriseEmailContent, handleEnterpriseRequest } from "./enterprise"
|
||||
|
||||
afterEach(() => mock.restore())
|
||||
|
||||
const submission = {
|
||||
name: "Fake Visitor",
|
||||
@@ -26,6 +29,24 @@ test("the sales inbox email escapes submitted HTML", () => {
|
||||
expect(content.html).toContain("<b>Name</b><br>")
|
||||
})
|
||||
|
||||
test("the sales inbox email lists the country and compliance requirements under the spend", () => {
|
||||
const content = enterpriseEmailContent(
|
||||
{ ...submission, country: "Germany", inRegionRouting: true, dpa: true, alias: "" },
|
||||
"$10k+",
|
||||
)
|
||||
expect(content.text.split("\n").slice(0, 6)).toEqual([
|
||||
submission.message,
|
||||
"",
|
||||
"Current monthly inference spend: $10k+",
|
||||
"Country: Germany",
|
||||
"Compliance requirements: In-region routing (EU or US), Data Processing Agreement (DPA)",
|
||||
"",
|
||||
])
|
||||
expect(content.html).toContain(
|
||||
"Current monthly inference spend: $10k+<br>Country: Germany<br>Compliance requirements: In-region routing (EU or US), Data Processing Agreement (DPA)<br><br>",
|
||||
)
|
||||
})
|
||||
|
||||
test("the sales inbox plain-text email is not HTML-escaped", () => {
|
||||
const content = enterpriseEmailContent(
|
||||
{ ...submission, name: "Ada <CTO>", message: "Pricing & SSO?", phone: "+1 555 0100", alias: "" },
|
||||
@@ -49,3 +70,90 @@ test("the sales inbox plain-text email is not HTML-escaped", () => {
|
||||
].join("\n"),
|
||||
)
|
||||
})
|
||||
|
||||
const contact = async (fields: Record<string, unknown>) => {
|
||||
const sent: Parameters<typeof sendEmail>[0][] = []
|
||||
const leads: Record<string, unknown>[] = []
|
||||
const response = await handleEnterpriseRequest(
|
||||
new Request("http://localhost/api/enterprise", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
email: "fake-visitor@example.test",
|
||||
message: "We'd like to list our models.",
|
||||
captcha: "token",
|
||||
...fields,
|
||||
}),
|
||||
}),
|
||||
{
|
||||
captcha: { secret: "test-secret", fetch: async () => Response.json({ success: true }) },
|
||||
spam: { apiKey: "test-key", fetch: async () => new Response(null, { status: 503 }) },
|
||||
salesforceSubmit: async (payload) => {
|
||||
leads.push(payload)
|
||||
return true
|
||||
},
|
||||
send: async (email) => {
|
||||
sent.push(email)
|
||||
},
|
||||
},
|
||||
)
|
||||
return { status: response.status, sent, leads }
|
||||
}
|
||||
|
||||
test("an inquiry that isn't for sales is emailed to the contact inbox, titled with its reason, without a lead", async () => {
|
||||
spyOn(console, "error").mockImplementation(() => {})
|
||||
const result = await contact({ reason: "list-models", message: "<b>Our</b> models" })
|
||||
expect(result.status).toBe(200)
|
||||
expect(result.leads).toHaveLength(0)
|
||||
expect(result.sent).toEqual([
|
||||
{
|
||||
to: "stefan@anoma.ly",
|
||||
subject: "List my models on OpenCode",
|
||||
body: "<b>Our</b> models<br><br>--<br>fake-visitor@example.test",
|
||||
text: "<b>Our</b> models\n\n--\nfake-visitor@example.test",
|
||||
replyTo: "fake-visitor@example.test",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test("an inquiry with an unknown reason or no details is rejected before anything is sent", async () => {
|
||||
expect(await contact({ reason: "Get help with my account" })).toEqual({ status: 400, sent: [], leads: [] })
|
||||
expect(await contact({ reason: "account", message: "" })).toEqual({ status: 400, sent: [], leads: [] })
|
||||
})
|
||||
|
||||
test("a contact email that fails to send is reported", async () => {
|
||||
spyOn(console, "error").mockImplementation(() => {})
|
||||
const response = await handleEnterpriseRequest(
|
||||
new Request("http://localhost/api/enterprise", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
email: "fake-visitor@example.test",
|
||||
message: "Help",
|
||||
reason: "account",
|
||||
captcha: "token",
|
||||
}),
|
||||
}),
|
||||
{
|
||||
captcha: { secret: "test-secret", fetch: async () => Response.json({ success: true }) },
|
||||
spam: { apiKey: "test-key", fetch: async () => new Response(null, { status: 503 }) },
|
||||
send: async () => {
|
||||
throw new Error("SES down")
|
||||
},
|
||||
},
|
||||
)
|
||||
expect(response.status).toBe(500)
|
||||
})
|
||||
|
||||
test("an inquiry with a malformed country or requirement is rejected before anything is sent", async () => {
|
||||
expect(await contact({ name: "Ada", role: "CTO", country: "x".repeat(81) })).toEqual({
|
||||
status: 400,
|
||||
sent: [],
|
||||
leads: [],
|
||||
})
|
||||
expect(await contact({ name: "Ada", role: "CTO", dpa: "yes" })).toEqual({
|
||||
status: 400,
|
||||
sent: [],
|
||||
leads: [],
|
||||
})
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import { i18n } from "~/i18n"
|
||||
import { localeFromRequest } from "~/lib/language"
|
||||
import { createLead } from "~/lib/salesforce"
|
||||
import { inferenceSpendOptions } from "~/lib/inference-spend"
|
||||
import { contactReasons } from "~/lib/contact-reason"
|
||||
import { ENTERPRISE_SPAM_THRESHOLD, enterpriseSpamProbability } from "~/lib/enterprise-spam"
|
||||
import { verifyHcaptcha } from "~/lib/hcaptcha"
|
||||
|
||||
@@ -13,13 +14,22 @@ interface EnterpriseFormData {
|
||||
company?: string
|
||||
email: string
|
||||
phone?: string
|
||||
/** A country name; the Lead's Country is a text field. */
|
||||
country?: string
|
||||
inferenceSpend?: string
|
||||
inRegionRouting?: boolean
|
||||
dpa?: boolean
|
||||
/** Set only for an inquiry that isn't for sales; its `contactReasons` value. */
|
||||
reason?: string
|
||||
alias?: string
|
||||
message: string
|
||||
captcha?: string
|
||||
}
|
||||
|
||||
const EMAIL_OCTOPUS_LIST_ID = "1b381e5e-39bd-11f1-ba4a-cdd4791f0c43"
|
||||
// An inquiry that isn't for sales skips Salesforce and EmailOctopus. It's emailed here, titled with its reason, to be
|
||||
// forwarded to the team that handles it.
|
||||
const CONTACT_INBOX_EMAIL = "stefan@anoma.ly"
|
||||
|
||||
function splitFullName(fullName: string) {
|
||||
const parts = fullName
|
||||
@@ -42,10 +52,19 @@ function escapeHtml(value: unknown) {
|
||||
|
||||
export function enterpriseEmailContent(body: EnterpriseFormData, inferenceSpend?: string) {
|
||||
const field = (value?: unknown) => (value ? `${escapeHtml(value)}<br>` : "")
|
||||
const requirements = [
|
||||
body.inRegionRouting && "In-region routing (EU or US)",
|
||||
body.dpa && "Data Processing Agreement (DPA)",
|
||||
].filter((item) => typeof item === "string")
|
||||
const details = [
|
||||
inferenceSpend && `Current monthly inference spend: ${inferenceSpend}`,
|
||||
body.country && `Country: ${body.country}`,
|
||||
requirements.length > 0 && `Compliance requirements: ${requirements.join(", ")}`,
|
||||
].filter((item) => typeof item === "string")
|
||||
return {
|
||||
html: `
|
||||
${escapeHtml(body.message)}<br><br>
|
||||
${inferenceSpend ? `Current monthly inference spend: ${escapeHtml(inferenceSpend)}<br><br>` : ""}
|
||||
${details.length > 0 ? `${details.map(escapeHtml).join("<br>")}<br><br>` : ""}
|
||||
--<br>
|
||||
${field(body.name)}
|
||||
${field(body.role)}
|
||||
@@ -54,7 +73,7 @@ ${field(body.phone)}`.trim(),
|
||||
text: [
|
||||
body.message,
|
||||
"",
|
||||
...(inferenceSpend ? [`Current monthly inference spend: ${inferenceSpend}`, ""] : []),
|
||||
...(details.length > 0 ? [...details, ""] : []),
|
||||
"--",
|
||||
...[body.name, body.role, body.company, body.email, body.phone].filter(Boolean),
|
||||
].join("\n"),
|
||||
@@ -97,6 +116,7 @@ export async function handleEnterpriseRequest(
|
||||
options: {
|
||||
salesforceSubmit?: (payload: Record<string, unknown>) => Promise<boolean>
|
||||
notifications?: boolean
|
||||
send?: typeof sendEmail
|
||||
spam?: Parameters<typeof enterpriseSpamProbability>[1]
|
||||
captcha?: Parameters<typeof verifyHcaptcha>[1]
|
||||
} = {},
|
||||
@@ -110,7 +130,10 @@ export async function handleEnterpriseRequest(
|
||||
return Response.json({ success: true, message: dict["enterprise.form.success.submitted"] }, { status: 200 })
|
||||
}
|
||||
|
||||
if (!body.name || !body.role || !body.email || !body.message) {
|
||||
// A sales inquiry names its sender and their role; any other inquiry only needs its reason.
|
||||
const reason = body.reason === undefined ? undefined : contactReasons.find((item) => item.value === body.reason)
|
||||
const complete = body.reason === undefined ? Boolean(body.name && body.role) : reason !== undefined
|
||||
if (!body.email || !body.message || !complete) {
|
||||
return Response.json({ error: dict["enterprise.form.error.allFieldsRequired"] }, { status: 400 })
|
||||
}
|
||||
|
||||
@@ -119,6 +142,16 @@ export async function handleEnterpriseRequest(
|
||||
return Response.json({ error: dict["enterprise.form.error.invalidEmailFormat"] }, { status: 400 })
|
||||
}
|
||||
|
||||
// The Lead's Country holds up to 80 characters.
|
||||
const structured =
|
||||
(body.country === undefined || (typeof body.country === "string" && body.country.trim().length <= 80)) &&
|
||||
(body.inRegionRouting === undefined || typeof body.inRegionRouting === "boolean") &&
|
||||
(body.dpa === undefined || typeof body.dpa === "boolean")
|
||||
if (!structured) {
|
||||
return Response.json({ error: dict["enterprise.form.error.allFieldsRequired"] }, { status: 400 })
|
||||
}
|
||||
const country = body.country?.trim() || undefined
|
||||
|
||||
const inferenceSpend = inferenceSpendOptions.find((option) => option.value === body.inferenceSpend)
|
||||
if (body.inferenceSpend && !inferenceSpend) {
|
||||
return Response.json({ error: dict["enterprise.form.error.invalidInferenceSpend"] }, { status: 400 })
|
||||
@@ -145,7 +178,27 @@ export async function handleEnterpriseRequest(
|
||||
return Response.json({ success: true, message: dict["enterprise.form.success.submitted"] }, { status: 200 })
|
||||
}
|
||||
|
||||
const emailContent = enterpriseEmailContent(body, inferenceSpend?.salesforceValue)
|
||||
const send = options.send ?? sendEmail
|
||||
|
||||
if (reason) {
|
||||
const sent = await send({
|
||||
to: CONTACT_INBOX_EMAIL,
|
||||
subject: reason.label,
|
||||
body: `${escapeHtml(body.message)}<br><br>--<br>${escapeHtml(body.email)}`,
|
||||
text: [body.message, "", "--", body.email].join("\n"),
|
||||
replyTo: body.email,
|
||||
}).then(
|
||||
() => true,
|
||||
(err) => {
|
||||
console.error("Failed to send contact email:", err)
|
||||
return false
|
||||
},
|
||||
)
|
||||
if (!sent) return Response.json({ error: dict["enterprise.form.error.internalServer"] }, { status: 500 })
|
||||
return Response.json({ success: true, message: dict["enterprise.form.success.submitted"] }, { status: 200 })
|
||||
}
|
||||
|
||||
const emailContent = enterpriseEmailContent({ ...body, country }, inferenceSpend?.salesforceValue)
|
||||
|
||||
const [lead, mail, octopus] = await Promise.all([
|
||||
createLead(
|
||||
@@ -155,7 +208,10 @@ export async function handleEnterpriseRequest(
|
||||
company: body.company,
|
||||
email: body.email,
|
||||
phone: body.phone,
|
||||
country,
|
||||
inferenceSpend: inferenceSpend?.salesforceValue,
|
||||
inRegionRouting: body.inRegionRouting === true,
|
||||
dpa: body.dpa === true,
|
||||
message: body.message,
|
||||
},
|
||||
options.salesforceSubmit,
|
||||
@@ -165,7 +221,7 @@ export async function handleEnterpriseRequest(
|
||||
}),
|
||||
options.notifications === false
|
||||
? Promise.resolve(false)
|
||||
: sendEmail({
|
||||
: send({
|
||||
to: Resource.ENTERPRISE_SALES_INBOX_EMAIL.value,
|
||||
subject: `Enterprise Inquiry from ${body.name}`,
|
||||
body: emailContent.html,
|
||||
|
||||
@@ -59,6 +59,8 @@ test("a fake entry keeps the visitor message, spend range, and enterprise lead s
|
||||
Title: "Engineering lead",
|
||||
Description: " We need SSO for our developers.\nPlease share the enterprise options. ",
|
||||
Current_Monthly_Inference_Spend__c: "$10K–$50K",
|
||||
In_Region_Routing__c: false,
|
||||
DPA_Required__c: false,
|
||||
LeadSource: "enterprise website form",
|
||||
})
|
||||
})
|
||||
@@ -74,7 +76,10 @@ test("the local Enterprise request handler applies the updated mapping to a form
|
||||
role: "Engineering lead",
|
||||
company: "Example Test Company",
|
||||
email: "fake-local-visitor@example.test",
|
||||
country: " Germany ",
|
||||
inferenceSpend: "50k-100k",
|
||||
inRegionRouting: true,
|
||||
dpa: false,
|
||||
message: " How are you bro!\nThis is a local form test. ",
|
||||
alias: "",
|
||||
captcha: "test-token",
|
||||
@@ -99,7 +104,10 @@ test("the local Enterprise request handler applies the updated mapping to a form
|
||||
expect(leads.at(-1)).toMatchObject({
|
||||
LastName: "Fake Local Form Visitor",
|
||||
Description: " How are you bro!\nThis is a local form test. ",
|
||||
Country: "Germany",
|
||||
Current_Monthly_Inference_Spend__c: "$50K–$100K",
|
||||
In_Region_Routing__c: true,
|
||||
DPA_Required__c: false,
|
||||
LeadSource: "enterprise website form",
|
||||
})
|
||||
})
|
||||
|
||||
@@ -44,7 +44,10 @@ export interface SalesforceLeadInput {
|
||||
company?: string
|
||||
email: string
|
||||
phone?: string
|
||||
country?: string
|
||||
inferenceSpend?: string
|
||||
inRegionRouting?: boolean
|
||||
dpa?: boolean
|
||||
message: string
|
||||
}
|
||||
|
||||
@@ -58,8 +61,11 @@ export async function createLead(
|
||||
Email: input.email,
|
||||
Phone: input.phone ?? null,
|
||||
Title: input.role,
|
||||
Country: input.country,
|
||||
Description: input.message,
|
||||
Current_Monthly_Inference_Spend__c: input.inferenceSpend,
|
||||
In_Region_Routing__c: input.inRegionRouting ?? false,
|
||||
DPA_Required__c: input.dpa ?? false,
|
||||
LeadSource: "enterprise website form",
|
||||
}
|
||||
if (submit) return submit(payload)
|
||||
|
||||
Reference in New Issue
Block a user