95 lines
3.2 KiB
TypeScript
95 lines
3.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
|
|
// Validation schema matching sp_25 structure
|
|
const feedbackSchema = z.object({
|
|
type: z.enum(['suggestion', 'report']),
|
|
name: z.string().min(1, 'Please enter your name.').trim(),
|
|
email: z.string().email('Please enter a valid email address.').trim(),
|
|
title: z.string().min(1, 'Title is required').trim(),
|
|
details: z.string().min(1, 'Details are required').trim(),
|
|
category: z.string().default('general'),
|
|
urgency: z.string().default('low'),
|
|
siliconId: z.string().nullable().optional()
|
|
})
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
|
|
// Validate the request body
|
|
const validatedData = feedbackSchema.parse(body)
|
|
|
|
// Get client IP address and referrer
|
|
const ip_address = request.headers.get('x-forwarded-for') ||
|
|
request.headers.get('x-real-ip') ||
|
|
'unknown'
|
|
const referrer = request.headers.get('referer') || 'Direct Access'
|
|
|
|
// TODO: Database integration
|
|
// This would integrate with MongoDB/database in production
|
|
// INSERT INTO sp_feedback (type, name, email, title, details, urgency, category, referrer, siliconId, ip_address, created_at)
|
|
|
|
// TODO: Email notification
|
|
const emailData = {
|
|
to: 'contact@siliconpin.com',
|
|
subject: `New ${validatedData.type.charAt(0).toUpperCase() + validatedData.type.slice(1)} Submission`,
|
|
body: `
|
|
Type: ${validatedData.type.charAt(0).toUpperCase() + validatedData.type.slice(1)}
|
|
Name: ${validatedData.name}
|
|
Email: ${validatedData.email}
|
|
Title: ${validatedData.title}
|
|
Category: ${validatedData.category}
|
|
${validatedData.type === 'report' ? `Urgency: ${validatedData.urgency}\n` : ''}
|
|
Details:
|
|
${validatedData.details}
|
|
|
|
Referrer: ${referrer}
|
|
IP Address: ${ip_address}
|
|
${validatedData.siliconId ? `SiliconID: ${validatedData.siliconId}\n` : ''}
|
|
`.trim()
|
|
}
|
|
|
|
// Log the submission (for development)
|
|
console.log('Feedback submission:', {
|
|
...validatedData,
|
|
referrer,
|
|
ip_address,
|
|
timestamp: new Date().toISOString(),
|
|
emailData
|
|
})
|
|
|
|
// Simulate processing delay
|
|
await new Promise(resolve => setTimeout(resolve, 500))
|
|
|
|
const successMessage = validatedData.type === 'suggestion'
|
|
? 'Thank you for your suggestion! We appreciate your feedback.'
|
|
: 'Your report has been submitted. We will look into it as soon as possible.'
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: successMessage
|
|
}, { status: 200 })
|
|
|
|
} catch (error) {
|
|
console.error('Feedback submission error:', error)
|
|
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: 'Validation failed',
|
|
errors: error.issues.reduce((acc, err) => {
|
|
if (err.path.length > 0) {
|
|
acc[err.path[0] as string] = err.message
|
|
}
|
|
return acc
|
|
}, {} as Record<string, string>)
|
|
}, { status: 400 })
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: 'Internal server error. Please try again later.'
|
|
}, { status: 500 })
|
|
}
|
|
} |