108 lines
3.1 KiB
TypeScript
108 lines
3.1 KiB
TypeScript
"use server";
|
|
|
|
import AdminNotification from "@/components/email-templates/admin-notification";
|
|
import { render } from "@react-email/components";
|
|
import UserConfirmation from "@/components/email-templates/user-confirmation";
|
|
import { createClient } from "@/utils/supabase/server";
|
|
import { useResend } from "@/hooks/use-resend";
|
|
import { typeMessageMap } from "@/src/applications/entities/models/contact-us.model";
|
|
|
|
export async function sendContactEmail(formData: {
|
|
name: string;
|
|
email: string;
|
|
phone: string;
|
|
typeMessage: string;
|
|
message: string;
|
|
}) {
|
|
try {
|
|
// Initialize Supabase
|
|
const supabase = await createClient();
|
|
const { resend } = useResend();
|
|
|
|
// Get message type label
|
|
const messageTypeLabel =
|
|
typeMessageMap.get(formData.typeMessage) || "Unknown";
|
|
|
|
// Save to Supabase
|
|
const { data: contactData, error: contactError } = await supabase
|
|
.from("contact_messages")
|
|
.insert([
|
|
{
|
|
name: formData.name,
|
|
email: formData.email,
|
|
phone: formData.phone,
|
|
message_type: formData.typeMessage,
|
|
message_type_label: messageTypeLabel,
|
|
message: formData.message,
|
|
status: "new",
|
|
},
|
|
])
|
|
.select();
|
|
|
|
if (contactError) {
|
|
console.error("Error saving contact message to Supabase:", contactError);
|
|
return {
|
|
success: false,
|
|
error: "Failed to save your message. Please try again later.",
|
|
};
|
|
}
|
|
|
|
// Render admin email template
|
|
const adminEmailHtml = await render(
|
|
AdminNotification({
|
|
name: formData.name,
|
|
email: formData.email,
|
|
phone: formData.phone,
|
|
messageType: messageTypeLabel,
|
|
message: formData.message,
|
|
})
|
|
);
|
|
|
|
// Send email to admin
|
|
const { data: emailData, error: emailError } = await resend.emails.send({
|
|
from: "Contact Form <contact@backspacex.tech>",
|
|
to: ["xdamazon17@gmail.com"],
|
|
subject: `New Contact Form Submission: ${messageTypeLabel}`,
|
|
html: adminEmailHtml,
|
|
});
|
|
|
|
if (emailError) {
|
|
console.error("Error sending email via Resend:", emailError);
|
|
// Note: We don't return error here since the data is already saved to Supabase
|
|
}
|
|
|
|
const userEmailHtml = await render(
|
|
UserConfirmation({
|
|
name: formData.name,
|
|
messageType: messageTypeLabel,
|
|
message: formData.message,
|
|
})
|
|
);
|
|
|
|
// Send confirmation email to user
|
|
const { data: confirmationData, error: confirmationError } =
|
|
await resend.emails.send({
|
|
from: "Your Company <support@backspacex.tech>",
|
|
to: [formData.email],
|
|
subject: "Thank you for contacting us",
|
|
html: userEmailHtml,
|
|
});
|
|
|
|
if (confirmationError) {
|
|
console.error("Error sending confirmation email:", confirmationError);
|
|
// Note: We don't return error here either
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: "Your message has been sent successfully!",
|
|
};
|
|
} catch (error) {
|
|
console.error("Unexpected error in sendContactEmail:", error);
|
|
return {
|
|
success: false,
|
|
error: "An unexpected error occurred. Please try again later.",
|
|
};
|
|
}
|
|
}
|