|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | + |
| 3 | +type ContactPayload = { |
| 4 | + fullName?: string; |
| 5 | + companyEmail?: string; |
| 6 | + message?: string; |
| 7 | +}; |
| 8 | + |
| 9 | +const LOOPS_CONTACTS_API_URL = "https://app.loops.so/api/v1/contacts/update"; |
| 10 | +const LOOPS_EVENTS_API_URL = "https://app.loops.so/api/v1/events/send"; |
| 11 | +const ENTERPRISE_EVENT_NAME = "enterpriseContactSubmitted"; |
| 12 | +const ENTERPRISE_SOURCE = "OpenWork Enterprise Contact Form"; |
| 13 | +const ENTERPRISE_GROUP = "Enterprise Leads"; |
| 14 | + |
| 15 | +function splitName(fullName: string) { |
| 16 | + const parts = fullName.trim().split(/\s+/).filter(Boolean); |
| 17 | + |
| 18 | + if (parts.length === 0) { |
| 19 | + return { firstName: "", lastName: "" }; |
| 20 | + } |
| 21 | + |
| 22 | + return { |
| 23 | + firstName: parts[0] ?? "", |
| 24 | + lastName: parts.slice(1).join(" ") |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +function validatePayload(payload: ContactPayload) { |
| 29 | + const fullName = payload.fullName?.trim() ?? ""; |
| 30 | + const companyEmail = payload.companyEmail?.trim() ?? ""; |
| 31 | + const message = payload.message?.trim() ?? ""; |
| 32 | + |
| 33 | + if (!fullName || !companyEmail || !message) { |
| 34 | + return { error: "Please fill out all fields." }; |
| 35 | + } |
| 36 | + |
| 37 | + const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 38 | + if (!emailPattern.test(companyEmail)) { |
| 39 | + return { error: "Please enter a valid work email." }; |
| 40 | + } |
| 41 | + |
| 42 | + return { |
| 43 | + fullName, |
| 44 | + companyEmail, |
| 45 | + message |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function deriveCompanyFromEmail(email: string) { |
| 50 | + const domain = email.split("@")[1]?.toLowerCase() ?? ""; |
| 51 | + if (!domain) { |
| 52 | + return ""; |
| 53 | + } |
| 54 | + |
| 55 | + const parts = domain.split(".").filter(Boolean); |
| 56 | + if (parts.length === 0) { |
| 57 | + return ""; |
| 58 | + } |
| 59 | + |
| 60 | + const compoundSuffixes = new Set([ |
| 61 | + "co.uk", |
| 62 | + "com.au", |
| 63 | + "co.jp", |
| 64 | + "com.br", |
| 65 | + "co.in", |
| 66 | + "com.sg", |
| 67 | + "com.mx", |
| 68 | + "com.tr" |
| 69 | + ]); |
| 70 | + |
| 71 | + const suffix = parts.slice(-2).join("."); |
| 72 | + const baseIndex = |
| 73 | + parts.length >= 3 && compoundSuffixes.has(suffix) |
| 74 | + ? parts.length - 3 |
| 75 | + : parts.length - 2; |
| 76 | + const companySlug = parts[Math.max(baseIndex, 0)] ?? ""; |
| 77 | + |
| 78 | + return companySlug |
| 79 | + .split(/[-_]+/) |
| 80 | + .filter(Boolean) |
| 81 | + .map(part => part.charAt(0).toUpperCase() + part.slice(1)) |
| 82 | + .join(" "); |
| 83 | +} |
| 84 | + |
| 85 | +export async function POST(request: Request) { |
| 86 | + const apiKey = process.env.LOOPS_API_KEY?.trim(); |
| 87 | + if (!apiKey) { |
| 88 | + return NextResponse.json( |
| 89 | + { error: "Loops is not configured on this deployment." }, |
| 90 | + { status: 500 } |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + let payload: ContactPayload; |
| 95 | + try { |
| 96 | + payload = (await request.json()) as ContactPayload; |
| 97 | + } catch { |
| 98 | + return NextResponse.json( |
| 99 | + { error: "Invalid request payload." }, |
| 100 | + { status: 400 } |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + const validated = validatePayload(payload); |
| 105 | + if ("error" in validated) { |
| 106 | + return NextResponse.json({ error: validated.error }, { status: 400 }); |
| 107 | + } |
| 108 | + |
| 109 | + const { firstName, lastName } = splitName(validated.fullName); |
| 110 | + const company = deriveCompanyFromEmail(validated.companyEmail); |
| 111 | + const submittedAt = new Date().toISOString(); |
| 112 | + |
| 113 | + const contactResponse = await fetch(LOOPS_CONTACTS_API_URL, { |
| 114 | + method: "PUT", |
| 115 | + headers: { |
| 116 | + Authorization: `Bearer ${apiKey}`, |
| 117 | + "Content-Type": "application/json" |
| 118 | + }, |
| 119 | + body: JSON.stringify({ |
| 120 | + email: validated.companyEmail, |
| 121 | + firstName, |
| 122 | + lastName: lastName || undefined, |
| 123 | + company: company || undefined, |
| 124 | + notes: validated.message, |
| 125 | + source: ENTERPRISE_SOURCE, |
| 126 | + userGroup: ENTERPRISE_GROUP |
| 127 | + }), |
| 128 | + cache: "no-store" |
| 129 | + }); |
| 130 | + |
| 131 | + if (!contactResponse.ok) { |
| 132 | + let detail = "Failed to submit contact request."; |
| 133 | + |
| 134 | + try { |
| 135 | + const errorData = (await contactResponse.json()) as { message?: string }; |
| 136 | + if (errorData.message?.trim()) { |
| 137 | + detail = errorData.message; |
| 138 | + } |
| 139 | + } catch { |
| 140 | + // Ignore invalid error payloads from upstream and return a generic message. |
| 141 | + } |
| 142 | + |
| 143 | + return NextResponse.json({ error: detail }, { status: 502 }); |
| 144 | + } |
| 145 | + |
| 146 | + const eventResponse = await fetch(LOOPS_EVENTS_API_URL, { |
| 147 | + method: "POST", |
| 148 | + headers: { |
| 149 | + Authorization: `Bearer ${apiKey}`, |
| 150 | + "Content-Type": "application/json" |
| 151 | + }, |
| 152 | + body: JSON.stringify({ |
| 153 | + email: validated.companyEmail, |
| 154 | + eventName: ENTERPRISE_EVENT_NAME, |
| 155 | + firstName, |
| 156 | + lastName: lastName || undefined, |
| 157 | + source: ENTERPRISE_SOURCE, |
| 158 | + userGroup: ENTERPRISE_GROUP, |
| 159 | + eventProperties: { |
| 160 | + message: validated.message, |
| 161 | + fullName: validated.fullName, |
| 162 | + submittedAt, |
| 163 | + page: "enterprise", |
| 164 | + note: validated.message |
| 165 | + } |
| 166 | + }), |
| 167 | + cache: "no-store" |
| 168 | + }); |
| 169 | + |
| 170 | + if (!eventResponse.ok) { |
| 171 | + let detail = "Failed to record contact event."; |
| 172 | + |
| 173 | + try { |
| 174 | + const errorData = (await eventResponse.json()) as { message?: string }; |
| 175 | + if (errorData.message?.trim()) { |
| 176 | + detail = errorData.message; |
| 177 | + } |
| 178 | + } catch { |
| 179 | + // Ignore invalid error payloads from upstream and return a generic message. |
| 180 | + } |
| 181 | + |
| 182 | + return NextResponse.json({ error: detail }, { status: 502 }); |
| 183 | + } |
| 184 | + |
| 185 | + return NextResponse.json({ ok: true }); |
| 186 | +} |
0 commit comments