mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-05 11:38:30 +01:00
* fix(contact): require saved requests and add real browser rollout journeys * test(browser): preserve project viewport in no-script journey * test(browser): use published long article for contents navigation * test(browser): exercise production runtime and retain failure diagnostics * test(browser): target the contact alert and real standalone entrypoint
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { type NextRequest } from 'next/server';
|
|
import { z } from 'zod';
|
|
import {
|
|
jsonSuccess,
|
|
jsonError,
|
|
validateBody,
|
|
hasValidationError,
|
|
handleError,
|
|
HTTP_STATUS,
|
|
} from '@/lib/api';
|
|
import { DOMAIN_ERRORS } from '@/lib/constants';
|
|
import { isSupabaseConfigured, getServiceClient } from '@/lib/supabase';
|
|
import { logger } from '@/lib/logger';
|
|
import { enforceRateLimit } from '@/lib/rate-limit';
|
|
|
|
const ContactSchema = z.object({
|
|
name: z.string().min(1, 'Name is required'),
|
|
email: z.string().email('Invalid email address'),
|
|
expertise: z.string().min(1, 'Please select an area of expertise'),
|
|
message: z.string().min(1, 'Message is required'),
|
|
});
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
// Rate limit by IP (5 per 10 minutes)
|
|
const limited = enforceRateLimit(req, 'contact');
|
|
if (limited) return limited;
|
|
// Validate input
|
|
const validation = await validateBody(req, ContactSchema);
|
|
if (hasValidationError(validation)) {
|
|
return validation.error;
|
|
}
|
|
|
|
const { name, email, expertise: _expertise, message } = validation.data;
|
|
|
|
if (isSupabaseConfigured()) {
|
|
try {
|
|
const supabase = getServiceClient();
|
|
const { data, error } = await supabase
|
|
.from('consultations')
|
|
.insert({ name, email, message, status: 'new' })
|
|
.select('id')
|
|
.single();
|
|
if (error) {
|
|
logger.error('Supabase consultations insert error:', error);
|
|
return jsonError(
|
|
'Failed to save contact request',
|
|
'DATABASE_ERROR',
|
|
HTTP_STATUS.INTERNAL_ERROR,
|
|
);
|
|
}
|
|
return jsonSuccess({ id: data.id }, "Thank you! We'll be in touch soon.");
|
|
} catch (dbErr) {
|
|
logger.error('Supabase client error (contact):', dbErr);
|
|
// Never report success when the request was not saved.
|
|
}
|
|
}
|
|
|
|
return jsonError(
|
|
'Contact delivery is temporarily unavailable. Please use the email link instead.',
|
|
'SERVICE_UNAVAILABLE',
|
|
HTTP_STATUS.SERVICE_UNAVAILABLE,
|
|
);
|
|
} catch (error) {
|
|
return handleError(error, DOMAIN_ERRORS.FAILED_SUBMIT_CONTACT);
|
|
}
|
|
}
|