import { serve } from "https://deno.land/std@0.190.0/http/server.ts"; import { createClient } from "https://esm.sh/@supabase/supabase-js@2"; const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type", }; interface DeleteOrderRequest { order_id: string; } serve(async (req: Request): Promise => { if (req.method === "OPTIONS") { return new Response(null, { headers: corsHeaders }); } try { const body: DeleteOrderRequest = await req.json(); const { order_id } = body; if (!order_id) { return new Response( JSON.stringify({ success: false, error: "order_id is required" }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } console.log("[DELETE-ORDER] Deleting order:", order_id); const supabaseUrl = Deno.env.get("SUPABASE_URL")!; const supabaseServiceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!; const supabase = createClient(supabaseUrl, supabaseServiceKey); // Call the database function to delete the order const { data, error } = await supabase .rpc("delete_order", { order_uuid: order_id }); if (error) { console.error("[DELETE-ORDER] Error:", error); return new Response( JSON.stringify({ success: false, error: error.message }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } console.log("[DELETE-ORDER] Success:", data); return new Response( JSON.stringify(data), { headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } catch (error: any) { console.error("[DELETE-ORDER] Unexpected error:", error); return new Response( JSON.stringify({ success: false, error: error.message || "Internal server error" }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } });