mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 00:31:13 -05:00
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
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
|
import { Novu } from "npm:@novu/node@2.0.2";
|
|
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4";
|
|
|
|
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');
|
|
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(novuApiKey, {
|
|
backendUrl: Deno.env.get('VITE_NOVU_API_URL') || 'https://api.novu.co',
|
|
});
|
|
|
|
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
|
|
|
const { userId, preferences } = await req.json();
|
|
|
|
console.log('Updating preferences for user:', userId);
|
|
|
|
// 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';
|
|
|
|
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');
|
|
} 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({
|
|
success: true,
|
|
}),
|
|
{
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
status: 200,
|
|
}
|
|
);
|
|
} catch (error: any) {
|
|
console.error('Error updating Novu preferences:', error);
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
error: error.message,
|
|
}),
|
|
{
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
status: 500,
|
|
}
|
|
);
|
|
}
|
|
});
|