Rocky_Mountain_Vending/lib/webhook-client.ts

103 lines
2.3 KiB
TypeScript

export interface WebhookResponse {
success: boolean
message: string
error?: string
}
export interface ContactFormWebhookData {
firstName: string
lastName: string
email: string
phone: string
company?: string
message: string
source?: string
page?: string
timestamp: string
url: string
}
export interface RequestMachineFormWebhookData {
firstName: string
lastName: string
email: string
phone: string
company: string
employeeCount: string
machineType: string
machineCount: string
message?: string
marketingConsent: boolean
termsAgreement: boolean
source?: string
page?: string
timestamp: string
url: string
}
export class WebhookClient {
private static async sendToApi(
payload: Record<string, unknown>,
kind: "contact" | "request-machine"
): Promise<WebhookResponse> {
try {
const response = await fetch("/api/contact", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
const body = await response.json().catch(() => null)
if (!response.ok) {
return {
success: false,
message: body?.message || body?.error || "Failed to submit form",
error: body?.error || `Request failed with status ${response.status}`,
}
}
return {
success: body?.success !== false,
message:
body?.message ||
(kind === "request-machine"
? "Your consultation request was submitted successfully."
: "Your message was submitted successfully."),
}
} catch (error) {
console.error(`Lead API error for ${kind}:`, error)
return {
success: false,
message: "Failed to submit form",
error: error instanceof Error ? error.message : "Unknown error",
}
}
}
static async submitContactForm(
data: ContactFormWebhookData
): Promise<WebhookResponse> {
return this.sendToApi(
{
kind: "contact",
...data,
},
"contact"
)
}
static async submitRequestMachineForm(
data: RequestMachineFormWebhookData
): Promise<WebhookResponse> {
return this.sendToApi(
{
kind: "request-machine",
...data,
},
"request-machine"
)
}
}