import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; import { Novu } from "npm:@novu/api@1.6.0"; import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4"; import { corsHeaders } from '../_shared/cors.ts'; import { edgeLogger, startRequest, endRequest } from "../_shared/logger.ts"; serve(async (req) => { const tracking = startRequest('update-novu-preferences'); if (req.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders }); } try { const novuApiKey = Deno.env.get('NOVU_API_KEY'); const supabaseUrl = Deno.env.get('SUPABASE_URL')!; const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!; if (!novuApiKey) { throw new Error('NOVU_API_KEY is not configured'); } const novu = new Novu({ secretKey: novuApiKey }); const supabase = createClient(supabaseUrl, supabaseServiceKey); const { userId, preferences } = await req.json(); edgeLogger.info('Updating preferences for user', { userId, requestId: tracking.requestId }); // Validate input if (!userId) { return new Response( JSON.stringify({ success: false, error: 'userId is required', }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 400, } ); } if (!preferences?.channelPreferences) { return new Response( JSON.stringify({ success: false, error: 'channelPreferences is required in preferences object', }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 400, } ); } // Get Novu subscriber ID from database const { data: prefData, error: prefError } = await supabase .from('user_notification_preferences') .select('novu_subscriber_id') .eq('user_id', userId) .single(); if (prefError || !prefData?.novu_subscriber_id) { throw new Error('Novu subscriber not found'); } const subscriberId = prefData.novu_subscriber_id; // Update channel preferences in Novu // Note: Novu's updatePreference updates one channel at a time for a specific workflow const channelPrefs = preferences.channelPreferences; const workflowId = preferences.workflowId || 'default'; const channelTypes = ['email', 'sms', 'in_app', 'push'] as const; const results: { channel: string; success: boolean; error?: string }[] = []; for (const channelType of channelTypes) { if (channelPrefs[channelType] !== undefined) { try { await novu.subscribers.updatePreference( subscriberId, workflowId, { channel: { type: channelType as any, // Cast to any to handle SDK type limitations enabled: channelPrefs[channelType] }, } ); results.push({ channel: channelType, success: true }); } catch (channelError: any) { edgeLogger.error('Failed to update channel preference', { channel: channelType, error: channelError.message, requestId: tracking.requestId }); results.push({ channel: channelType, success: false, error: channelError.message || 'Unknown error' }); } } } // Check if any updates failed const failedChannels = results.filter(r => !r.success); const allSucceeded = failedChannels.length === 0; if (!allSucceeded) { edgeLogger.warn('Some channel preferences failed to update', { failedChannels: failedChannels.map(c => c.channel), requestId: tracking.requestId }); return new Response( JSON.stringify({ success: false, error: 'Some channel preferences failed to update', results, failedChannels: failedChannels.map(c => c.channel), }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 502, // Bad Gateway - external service failure } ); } const duration = endRequest(tracking); edgeLogger.info('All preferences updated successfully', { requestId: tracking.requestId, duration }); return new Response( JSON.stringify({ success: true, results, }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200, } ); } catch (error: any) { const duration = endRequest(tracking); edgeLogger.error('Error updating Novu preferences', { error: error.message, requestId: tracking.requestId, duration }); return new Response( JSON.stringify({ success: false, error: error.message, }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 500, } ); } });