Files
thrilltrack-explorer/supabase/functions/trigger-notification/index.ts
gpt-engineer-app[bot] 1333a5e8c8 Fix Novu API import path
2025-10-12 15:41:36 +00:00

63 lines
1.6 KiB
TypeScript

import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { Novu } from "npm:@novu/api@0.0.7";
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({
apiKey: novuApiKey,
baseURL: Deno.env.get('VITE_NOVU_API_URL') || 'https://api.novu.co',
});
const { workflowId, subscriberId, payload, overrides } = await req.json();
console.log('Triggering notification:', { workflowId, subscriberId });
const result = await novu.events.trigger(workflowId, {
to: subscriberId,
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,
}
);
}
});