73 lines
2.3 KiB
TypeScript
73 lines
2.3 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 });
|
|
// We continue anyway to try sending the email, but maybe we should report this
|
|
}
|
|
|
|
// 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;
|
|
}
|