From 94631fd31b91bfdad9e4ad8ef1e5b522275d57fb Mon Sep 17 00:00:00 2001 From: pac7 <47831526-pac7@users.noreply.replit.com> Date: Wed, 8 Oct 2025 14:02:03 +0000 Subject: [PATCH] Update user notification preferences in Novu system Modify the Supabase function to iterate through channel preferences and update each individually using the Novu SDK, including error handling for each channel update. Replit-Commit-Author: Agent Replit-Commit-Session-Id: a8c5cf3e-a80e-462f-b090-b081acdcf03a Replit-Commit-Checkpoint-Type: intermediate_checkpoint --- .../update-novu-preferences/index.ts | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) 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({