Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 8s
Build & Deploy / 🧪 QA (push) Failing after 1m53s
Build & Deploy / 🏗️ Build (push) Has been skipped
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
- implemented BrochureDeliveryEmail template - created AutoBrochureModal wrapper with 5s delay - updated layout.tsx and BrochureCTA to use new success state - added tests/brochure-modal.test.ts e2e test
115 lines
3.7 KiB
TypeScript
115 lines
3.7 KiB
TypeScript
'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. Send Brochure via Email
|
|
const brochureUrl = `https://klz-cables.com/brochure/klz-product-catalog-${locale}.pdf`;
|
|
|
|
try {
|
|
const { sendEmail } = await import('@/lib/mail/mailer');
|
|
const { render } = await import('@mintel/mail');
|
|
const React = await import('react');
|
|
const { BrochureDeliveryEmail } = await import('@/components/emails/BrochureDeliveryEmail');
|
|
|
|
const html = await render(
|
|
React.createElement(BrochureDeliveryEmail, {
|
|
email,
|
|
brochureUrl,
|
|
locale: locale as 'en' | 'de',
|
|
}),
|
|
);
|
|
|
|
const emailResult = await sendEmail({
|
|
to: email,
|
|
subject: locale === 'de' ? 'Ihr KLZ Kabelkatalog' : 'Your KLZ Cable Catalog',
|
|
html,
|
|
});
|
|
|
|
if (emailResult.success) {
|
|
logger.info('Brochure email sent successfully', { email });
|
|
} else {
|
|
logger.error('Failed to send brochure email', { error: emailResult.error, email });
|
|
services.errors.captureException(new Error(`Brochure email failed: ${emailResult.error}`), {
|
|
action: 'requestBrochureAction_email',
|
|
email,
|
|
});
|
|
return { success: false, error: 'Failed to send email. Please try again later.' };
|
|
}
|
|
} catch (error) {
|
|
logger.error('Exception while sending brochure email', { error });
|
|
return { success: false, error: 'Failed to send email. Please try again later.' };
|
|
}
|
|
|
|
// 4. Track success
|
|
services.analytics.track('brochure-request-success', {
|
|
locale,
|
|
delivery_method: 'email',
|
|
});
|
|
|
|
return { success: true };
|
|
}
|