Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 10s
Build & Deploy / 🧪 QA (push) Successful in 2m20s
Build & Deploy / 🏗️ Build (push) Successful in 3m46s
Build & Deploy / 🚀 Deploy (push) Successful in 22s
Build & Deploy / 🧪 Post-Deploy Verification (push) Failing after 4m26s
Build & Deploy / 🔔 Notify (push) Successful in 2s
124 lines
4.0 KiB
TypeScript
124 lines
4.0 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';
|
|
|
|
// Anti-spam Honeypot Check
|
|
const honeypot = formData.get('company_website') as string;
|
|
if (honeypot) {
|
|
logger.warn('Spam detected via honeypot in brochure request', { email });
|
|
// Silently succeed to fool the bot without doing actual work
|
|
return { success: true };
|
|
}
|
|
|
|
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,
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
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: 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 };
|
|
}
|