Migrate Novu functions to wrapEdgeFunction

Refactor Phase 3 Batch 2–4 Novu-related functions to use the createEdgeFunction wrapper, replacing explicit HTTP servers with edge wrapper, adding standardized logging, tracing, and error handling across subscriber management, topic/notification, and migration/sync functions.
This commit is contained in:
gpt-engineer-app[bot]
2025-11-11 03:55:02 +00:00
parent 19804ea9bd
commit a1280ddd05
9 changed files with 166 additions and 569 deletions

View File

@@ -1,17 +1,16 @@
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";
import { edgeLogger } from "../_shared/logger.ts";
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
serve(async (req) => {
const tracking = startRequest('update-novu-preferences');
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
try {
export default createEdgeFunction(
{
name: 'update-novu-preferences',
requireAuth: false,
corsHeaders: corsHeaders
},
async (req, context) => {
const novuApiKey = Deno.env.get('NOVU_API_KEY');
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
@@ -28,33 +27,16 @@ serve(async (req) => {
const { userId, preferences } = await req.json();
edgeLogger.info('Updating preferences for user', { userId, requestId: tracking.requestId });
context.span.setAttribute('action', 'update_novu_preferences');
edgeLogger.info('Updating preferences for user', { userId, requestId: context.requestId });
// Validate input
if (!userId) {
return new Response(
JSON.stringify({
success: false,
error: 'userId is required',
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,
}
);
throw new Error('userId is required');
}
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,
}
);
throw new Error('channelPreferences is required in preferences object');
}
// Get Novu subscriber ID from database
@@ -96,7 +78,7 @@ serve(async (req) => {
edgeLogger.error('Failed to update channel preference', {
channel: channelType,
error: channelError.message,
requestId: tracking.requestId
requestId: context.requestId
});
results.push({
channel: channelType,
@@ -114,54 +96,24 @@ serve(async (req) => {
if (!allSucceeded) {
edgeLogger.warn('Some channel preferences failed to update', {
failedChannels: failedChannels.map(c => c.channel),
requestId: tracking.requestId
requestId: context.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
}
);
throw new Error('Some channel preferences failed to update: ' + failedChannels.map(c => c.channel).join(', '));
}
const duration = endRequest(tracking);
edgeLogger.info('All preferences updated successfully', {
requestId: tracking.requestId,
duration
requestId: context.requestId
});
return new Response(
JSON.stringify({
success: true,
results,
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: { '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,
}
);
}
});
);