All files / src/app/api/contact route.ts

0% Statements 0/62
100% Branches 0/0
0% Functions 0/1
0% Lines 0/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                                                                                             
import { NextRequest } from 'next/server';
import { z } from 'zod';
import { withErrorHandling, successResponse, ApiError } from '@/lib/api';
import { logger } from '@/lib/logging';

// Contact form validation schema
const ContactFormSchema = z.object({
  firstName: z.string().min(1, 'First name is required'),
  lastName: z.string().min(1, 'Last name is required'),
  email: z.string().email('Invalid email address'),
  phone: z.string().optional(),
  subject: z.string().min(1, 'Subject is required'),
  message: z.string().min(10, 'Message must be at least 10 characters'),
});

/**
 * POST /api/contact
 * Handle contact form submissions
 */
async function handlePost(request: NextRequest) {
  const body = await request.json();

  // Validate input
  const validationResult = ContactFormSchema.safeParse(body);
  if (!validationResult.success) {
    throw ApiError.badRequest(validationResult.error.issues[0].message);
  }

  const { firstName, lastName, email, phone, subject, message } = validationResult.data;

  // Log the contact form submission
  logger.info('Contact form submitted', {
    category: 'CONTACT',
    name: `${firstName} ${lastName}`,
    email,
    phone: phone || 'Not provided',
    subject: subject || 'No subject',
    messageLength: message.length,
  });

  // TODO: In production, you would:
  // 1. Send an email notification to the support team
  // 2. Store the message in the database
  // 3. Send a confirmation email to the user

  // For now, we just log and return success
  // You can integrate with email services like:
  // - SendGrid
  // - AWS SES
  // - Resend
  // - Nodemailer

  return successResponse({
    message: 'Your message has been sent successfully. We will get back to you soon!',
    data: {
      name: `${firstName} ${lastName}`,
      email,
    },
  });
}

export const POST = withErrorHandling(handlePost);