Some checks failed
Build & Deploy KLZ Cables / 🔍 Prepare Environment (push) Failing after 56s
Build & Deploy KLZ Cables / 🧪 Quality Assurance (push) Has been skipped
Build & Deploy KLZ Cables / 🏗️ Build App (push) Has been skipped
Build & Deploy KLZ Cables / 🏗️ Build Gatekeeper (push) Has been skipped
Build & Deploy KLZ Cables / 🚀 Deploy (push) Has been skipped
Build & Deploy KLZ Cables / ⚡ PageSpeed (push) Has been skipped
Build & Deploy KLZ Cables / 🔔 Notifications (push) Successful in 2s
79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
'use server';
|
|
|
|
import client, { ensureAuthenticated } from '@/lib/directus';
|
|
import { createItem } from '@directus/sdk';
|
|
import { sendEmail } from '@/lib/mail/mailer';
|
|
import ContactEmail from '@/components/emails/ContactEmail';
|
|
import React from 'react';
|
|
import { getServerAppServices } from '@/lib/services/create-services.server';
|
|
|
|
export async function sendContactFormAction(formData: FormData) {
|
|
const services = getServerAppServices();
|
|
const logger = services.logger.child({ action: 'sendContactFormAction' });
|
|
const name = formData.get('name') as string;
|
|
const email = formData.get('email') as string;
|
|
const message = formData.get('message') as string;
|
|
const productName = formData.get('productName') as string | null;
|
|
|
|
if (!name || !email || !message) {
|
|
logger.warn('Missing required fields in contact form', {
|
|
name: !!name,
|
|
email: !!email,
|
|
message: !!message,
|
|
});
|
|
return { success: false, error: 'Missing required fields' };
|
|
}
|
|
|
|
// 1. Save to Directus
|
|
try {
|
|
await ensureAuthenticated();
|
|
if (productName) {
|
|
await client.request(
|
|
createItem('product_requests', {
|
|
product_name: productName,
|
|
email,
|
|
message,
|
|
}),
|
|
);
|
|
logger.info('Product request stored in Directus');
|
|
} else {
|
|
await client.request(
|
|
createItem('contact_submissions', {
|
|
name,
|
|
email,
|
|
message,
|
|
}),
|
|
);
|
|
logger.info('Contact submission stored in Directus');
|
|
}
|
|
} catch (error) {
|
|
logger.error('Failed to store submission in Directus', { error });
|
|
services.errors.captureException(error, { action: 'directus_store_submission' });
|
|
}
|
|
|
|
// 2. Send Email
|
|
logger.info('Sending contact form email', { email, productName });
|
|
|
|
const subject = productName ? `Product Inquiry: ${productName}` : 'New Contact Form Submission';
|
|
|
|
const result = await sendEmail({
|
|
subject,
|
|
template: React.createElement(ContactEmail, {
|
|
name,
|
|
email,
|
|
message,
|
|
productName: productName || undefined,
|
|
subject,
|
|
}),
|
|
});
|
|
|
|
if (result.success) {
|
|
logger.info('Contact form email sent successfully', { messageId: result.messageId });
|
|
} else {
|
|
logger.error('Failed to send contact form email', { error: result.error });
|
|
services.errors.captureException(result.error, { action: 'sendContactFormAction', email });
|
|
}
|
|
|
|
return result;
|
|
}
|