'use server'; import { getServerAppServices } from '@/lib/services/create-services.server'; export async function requestBrochureAction(formData: FormData) { const services = getServerAppServices(); const logger = services.logger.child({ action: 'requestBrochureAction' }); const { headers } = await import('next/headers'); const requestHeaders = await headers(); if ('setServerContext' in services.analytics) { (services.analytics as any).setServerContext({ userAgent: requestHeaders.get('user-agent') || undefined, language: requestHeaders.get('accept-language')?.split(',')[0] || undefined, referrer: requestHeaders.get('referer') || undefined, ip: requestHeaders.get('x-forwarded-for')?.split(',')[0] || undefined, }); } services.analytics.track('brochure-request-attempt'); const email = formData.get('email') as string; const locale = (formData.get('locale') as string) || 'en'; if (!email) { logger.warn('Missing email in brochure request'); return { success: false, error: 'Missing email address' }; } // Basic email validation if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { return { success: false, error: 'Invalid email address' }; } // 1. Save to CMS try { const { getPayload } = await import('payload'); const configPromise = (await import('@payload-config')).default; const payload = await getPayload({ config: configPromise }); await payload.create({ collection: 'form-submissions', data: { name: email.split('@')[0], email, message: `Brochure download request (${locale})`, type: 'brochure_download' as any, }, }); logger.info('Successfully saved brochure request to Payload CMS', { email }); } catch (error) { logger.error('Failed to store brochure request in Payload CMS', { error }); services.errors.captureException(error, { action: 'payload_store_brochure_request' }); } // 2. Notify via Gotify try { await services.notifications.notify({ title: '📑 Brochure Download Request', message: `New brochure download request from ${email} (${locale})`, priority: 3, }); } catch (error) { logger.error('Failed to send notification', { error }); } // 3. Track success services.analytics.track('brochure-request-success', { locale, }); // Return the brochure URL const brochureUrl = `/brochure/klz-product-catalog-${locale}.pdf`; return { success: true, brochureUrl }; }