Next.js website for Rocky Mountain Vending company featuring: - Product catalog with Stripe integration - Service areas and parts pages - Admin dashboard with Clerk authentication - SEO optimized pages with JSON-LD structured data Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
No EOL
2.9 KiB
TypeScript
100 lines
No EOL
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
import { WebhookClient, RequestMachineFormWebhookData } from "@/lib/webhook-client"
|
|
|
|
interface RequestMachineFormData extends RequestMachineFormWebhookData {}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const formData: RequestMachineFormData = body
|
|
|
|
// Validate required fields
|
|
const requiredFields = ['firstName', 'lastName', 'email', 'phone', 'company', 'employeeCount', 'machineType', 'machineCount']
|
|
const missingFields = requiredFields.filter(field => !formData[field])
|
|
|
|
if (missingFields.length > 0) {
|
|
return NextResponse.json(
|
|
{ error: `Missing required fields: ${missingFields.join(', ')}` },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Basic email validation
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
if (!emailRegex.test(formData.email)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid email address' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Basic phone validation (remove all non-digit characters)
|
|
const phoneDigits = formData.phone.replace(/\D/g, '')
|
|
if (phoneDigits.length < 10) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid phone number' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Validate employee count
|
|
const employeeCount = parseInt(formData.employeeCount)
|
|
if (isNaN(employeeCount) || employeeCount < 1 || employeeCount > 10000) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid number of employees/people' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Validate machine count
|
|
const machineCount = parseInt(formData.machineCount)
|
|
if (isNaN(machineCount) || machineCount < 1 || machineCount > 100) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid number of machines' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Validate consent and agreement
|
|
if (!formData.marketingConsent) {
|
|
return NextResponse.json(
|
|
{ error: 'Marketing consent is required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (!formData.termsAgreement) {
|
|
return NextResponse.json(
|
|
{ error: 'Terms agreement is required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Submit to webhook
|
|
const webhookResult = await WebhookClient.submitRequestMachineForm(formData)
|
|
|
|
if (!webhookResult.success) {
|
|
console.error('Machine request webhook submission failed:', webhookResult.error)
|
|
return NextResponse.json(
|
|
{ error: webhookResult.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
console.log('Successfully submitted machine request form to GHL:', formData)
|
|
|
|
// Return success response
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: webhookResult.message
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('Machine request form submission error:', error)
|
|
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |