diff options
| author | Ryan Vogel <[email protected]> | 2026-03-17 22:43:43 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-03-17 22:43:43 -0400 |
| commit | a849a17e9329f0b4b0dbb85abf1366b51f935f8c (patch) | |
| tree | 2f2958b68a99962f193ea0410bec27d70d0d25ad /packages/console/app/src/lib | |
| parent | 0292f1b5596db954e3811f91a9fafcfad650ead1 (diff) | |
| download | opencode-a849a17e9329f0b4b0dbb85abf1366b51f935f8c.tar.gz opencode-a849a17e9329f0b4b0dbb85abf1366b51f935f8c.zip | |
feat(enterprise): contact form now pushes to salesforce 🙄 (#17964)
Co-authored-by: slickstef11 <[email protected]>
Co-authored-by: Frank <[email protected]>
Diffstat (limited to 'packages/console/app/src/lib')
| -rw-r--r-- | packages/console/app/src/lib/salesforce.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/packages/console/app/src/lib/salesforce.ts b/packages/console/app/src/lib/salesforce.ts new file mode 100644 index 000000000..48e0caee7 --- /dev/null +++ b/packages/console/app/src/lib/salesforce.ts @@ -0,0 +1,81 @@ +import { Resource } from "@opencode-ai/console-resource" + +async function login() { + const url = Resource.SALESFORCE_INSTANCE_URL.value.replace(/\/$/, "") + const clientId = Resource.SALESFORCE_CLIENT_ID.value + const clientSecret = Resource.SALESFORCE_CLIENT_SECRET.value + + const params = new URLSearchParams({ + grant_type: "client_credentials", + client_id: clientId, + client_secret: clientSecret, + }) + + const res = await fetch(`${url}/services/oauth2/token`, { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + body: params.toString(), + }).catch((err) => { + console.error("Failed to fetch Salesforce access token:", err) + }) + + if (!res) return + + if (!res.ok) { + console.error("Failed to fetch Salesforce access token:", res.status, await res.text()) + return + } + + const data = (await res.json()) as { access_token?: string; instance_url?: string } + if (!data.access_token) { + console.error("Salesforce auth response did not include an access token") + return + } + + return { + token: data.access_token, + url: data.instance_url ?? url, + } +} + +export interface SalesforceLeadInput { + name: string + role: string + company?: string + email: string + phone?: string + message: string +} + +export async function createLead(input: SalesforceLeadInput): Promise<boolean> { + const auth = await login() + if (!auth) return false + + const res = await fetch(`${auth.url}/services/data/v59.0/sobjects/Lead`, { + method: "POST", + headers: { + Authorization: `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + LastName: input.name, + Company: input.company?.trim() || "Website", + Email: input.email, + Phone: input.phone ?? null, + Title: input.role, + Description: input.message, + LeadSource: "Website", + }), + }).catch((err) => { + console.error("Failed to create Salesforce lead:", err) + }) + + if (!res) return false + + if (!res.ok) { + console.error("Failed to create Salesforce lead:", res.status, await res.text()) + return false + } + + return true +} |
