import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; import { Novu } from "npm:@novu/api@1.6.0"; 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(null, { headers: corsHeaders }); } try { const novuApiKey = Deno.env.get('NOVU_API_KEY'); if (!novuApiKey) { throw new Error('NOVU_API_KEY is not configured'); } const novu = new Novu({ secretKey: novuApiKey }); const { workflowId, subscriberId, topicKey, payload, overrides } = await req.json(); // Support both individual subscribers and topics if (!subscriberId && !topicKey) { throw new Error('Either subscriberId or topicKey must be provided'); } const recipient = subscriberId ? { subscriberId } : { topicKey }; console.log('Triggering notification:', { workflowId, recipient }); const result = await novu.trigger({ to: recipient, workflowId, payload, overrides, }); console.log('Notification triggered successfully:', result.data); return new Response( JSON.stringify({ success: true, transactionId: result.data.transactionId, }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200, } ); } catch (error: any) { console.error('Error triggering notification:', error); return new Response( JSON.stringify({ success: false, error: error.message, }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 500, } ); } });