/* eslint-disable no-undef */ // Edge Function: send-order-confirmation-email // Self-host Supabase (Docker) import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; import { createClient } from "https://esm.sh/@supabase/supabase-js@2"; import Stripe from "https://esm.sh/stripe@12.0.0"; const RESEND_API_KEY = Deno.env.get("RESEND_API_KEY"); const STRIPE_SECRET_KEY = Deno.env.get("STRIPE_SECRET_KEY") || ""; const SUPABASE_URL = Deno.env.get("SUPABASE_URL") || ""; const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") || ""; const ADMIN_EMAIL = Deno.env.get("ADMIN_EMAIL") || ""; const stripe = new Stripe(STRIPE_SECRET_KEY, { apiVersion: "2022-11-15", httpClient: Stripe.createFetchHttpClient(), }); const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type", }; serve(async (req) => { if (req.method === "OPTIONS") { return new Response("ok", { headers: corsHeaders }); } try { const { orderData, sessionId } = await req.json(); // 1️⃣ Récupérer l'email vérifié depuis la session Stripe let customerEmail = null; if (sessionId && STRIPE_SECRET_KEY) { try { const session = await stripe.checkout.sessions.retrieve(sessionId); // Priorité : customer_details.email puis customer_email customerEmail = session?.customer_details?.email || session?.customer_email; } catch (stripeError) { console.error("Error retrieving Stripe session:", stripeError); // Fallback : email envoyé par le front customerEmail = orderData?.email; } } else { // Sans sessionId (tests par ex.) customerEmail = orderData?.email; } if (!customerEmail) { console.warn("No email found in Stripe session or order data."); // On continue quand même : la commande sera enregistrée mais sans email } // 2️⃣ Client Supabase (SERVICE ROLE pour pouvoir écrire dans la table) const supabaseClient = createClient( SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, { global: { headers: { Authorization: req.headers.get("Authorization") ?? "", }, }, }, ); // 3️⃣ Enregistrer la commande dans Supabase const finalOrderData = { ...orderData, customer_email: customerEmail, session_id: sessionId, status: "En attente de traitement", created_at: new Date().toISOString(), }; const { data: order, error: orderError } = await supabaseClient .from("orders") .insert(finalOrderData) .select() .single(); if (orderError) { console.error("Error saving order to Supabase:", orderError); // on continue quand même pour tenter les mails } // 4️⃣ Email client (joli template violet) let customerEmailError = null; let customerEmailResult = null; if (RESEND_API_KEY && customerEmail) { const res = await fetch("https://api.resend.com/emails", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${RESEND_API_KEY}`, }, body: JSON.stringify({ from: "Dites le en chanson ", to: [customerEmail], subject: `Votre commande Dites le en chanson est confirmée ! (ID: ${order?.id?.slice(0, 8) ?? ""})`, html: ` Confirmation de votre commande - Dites le en chanson
Dites-le en chanson Logo

Merci pour votre commande !

Bonjour ${orderData?.recipient_name ?? "cher client"},

Nous sommes ravis de vous confirmer que votre commande a bien été enregistrée. Notre équipe de créateurs est déjà prête à composer votre chanson personnalisée !

Récapitulatif de votre commande (ID: ${order?.id?.slice(0,8) ?? ""}) :

Produit: ${orderData?.product_name ?? "Chanson personnalisée"}

Prix Payé: ${orderData?.price ? orderData.price + " €" : "—"}

Pour: ${orderData?.recipient_name ?? "—"}

Langue: ${orderData?.language ?? "—"}

Voix: ${orderData?.voice_gender ?? "—"}

Style: ${orderData?.musical_style ?? "—"}

Ambiance: ${orderData?.mood ?? "—"}

${orderData?.anecdote1 ? `

Anecdote 1: ${orderData.anecdote1}

` : ""} ${orderData?.anecdote2 ? `

Anecdote 2: ${orderData.anecdote2}

` : ""} ${orderData?.anecdote3 ? `

Anecdote 3: ${orderData.anecdote3}

` : ""}

Délai de création : Votre chanson unique sera prête et vous sera livrée par email dans les 48 heures.

Nous mettons tout notre cœur pour transformer vos histoires en mélodies inoubliables.

Si vous avez la moindre question, n'hésitez pas à nous contacter.

Visiter notre site

`, }), }); const data = await res.json(); if (!res.ok) { console.error("Resend API Error (Customer):", data); customerEmailError = data.message || "Unknown Resend Error"; } else { customerEmailResult = data; } } else { if (!RESEND_API_KEY) console.error("RESEND_API_KEY is missing"); if (!customerEmail) console.error("No customer email available to send confirmation."); } // 5️⃣ Email admin "Nouvelle Commande Reçue" let adminEmailError = null; let adminEmailResult = null; if (RESEND_API_KEY && ADMIN_EMAIL) { const adminRes = await fetch("https://api.resend.com/emails", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${RESEND_API_KEY}`, }, body: JSON.stringify({ from: "Dites le en chanson ", to: [ADMIN_EMAIL], subject: `Nouvelle commande reçue (ID: ${order?.id?.slice(0,8) ?? "N/A"})`, html: ` Nouvelle Commande Reçue

Nouvelle Commande Reçue !

Une nouvelle commande a été passée sur votre site.

ID de Commande : ${order?.id ?? "N/A"}

ID de Session Stripe : ${sessionId ?? "N/A"}

Email du Client : ${customerEmail ?? "N/A"}

Statut : ${finalOrderData.status}

Détails de la commande :

`, }), }); const adminData = await adminRes.json(); if (!adminRes.ok) { console.error("Resend API Error (Admin):", adminData); adminEmailError = adminData.message || "Unknown Resend Error"; } else { adminEmailResult = adminData; } } else { if (!ADMIN_EMAIL) console.error("ADMIN_EMAIL is missing"); } return new Response( JSON.stringify({ success: true, orderId: order?.id || "N/A", customerEmail, emailSent: !!customerEmailResult, customerEmailError, adminEmailSent: !!adminEmailResult, adminEmailError, dbError: orderError ? orderError.message : null, }), { status: 200, headers: { ...corsHeaders, "Content-Type": "application/json" }, }, ); } catch (error) { console.error("Function Critical Error:", error); return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" }, }); } });