diff --git a/supabase/functions/update-novu-preferences/index.ts b/supabase/functions/update-novu-preferences/index.ts index dcc1cd82..3a45bf8e 100644 --- a/supabase/functions/update-novu-preferences/index.ts +++ b/supabase/functions/update-novu-preferences/index.ts @@ -45,22 +45,38 @@ serve(async (req) => { 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'; - await novu.subscribers.updatePreference( - subscriberId, - { - enabled: true, - channels: { - email: channelPrefs.email, - sms: channelPrefs.sms, - in_app: channelPrefs.in_app, - push: channelPrefs.push, - }, + try { + // Update each channel preference separately + const channelTypes = ['email', 'sms', 'in_app', 'push'] as const; + + 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] + }, + } + ); + } catch (channelError: any) { + console.warn(`Failed to update ${channelType} preference:`, channelError.message); + // Continue with other channels even if one fails + } + } } - ); - - console.log('Preferences updated successfully'); + console.log('Preferences updated successfully'); + } catch (novuError: any) { + console.error('Novu API error:', novuError); + throw new Error(`Failed to update Novu preferences: ${novuError.message || 'Unknown error'}`); + } return new Response( JSON.stringify({