mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:31:12 -05:00
89 lines
2.4 KiB
TypeScript
89 lines
2.4 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
|
|
const channelPrefs = preferences.channelPreferences;
|
|
|
|
await novu.subscribers.updatePreference(
|
|
subscriberId,
|
|
{
|
|
enabled: true,
|
|
channels: {
|
|
email: channelPrefs.email,
|
|
sms: channelPrefs.sms,
|
|
in_app: channelPrefs.in_app,
|
|
push: channelPrefs.push,
|
|
},
|
|
}
|
|
);
|
|
|
|
console.log('Preferences updated successfully');
|
|
|
|
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,
|
|
}
|
|
);
|
|
}
|
|
});
|