Fix edge function logging and types

This commit is contained in:
gpt-engineer-app[bot]
2025-11-03 19:57:27 +00:00
parent 99ceacfe0c
commit 6fbaf0c606
9 changed files with 175 additions and 62 deletions

View File

@@ -1,6 +1,7 @@
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 { edgeLogger, startRequest, endRequest } from "../_shared/logger.ts";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
@@ -8,6 +9,8 @@ const corsHeaders = {
};
serve(async (req) => {
const tracking = startRequest('update-novu-preferences');
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
@@ -29,7 +32,7 @@ serve(async (req) => {
const { userId, preferences } = await req.json();
console.log('Updating preferences for user:', userId);
edgeLogger.info('Updating preferences for user', { userId, requestId: tracking.requestId });
// Validate input
if (!userId) {
@@ -94,7 +97,11 @@ serve(async (req) => {
);
results.push({ channel: channelType, success: true });
} catch (channelError: any) {
console.error(`Failed to update ${channelType} preference:`, channelError.message);
edgeLogger.error('Failed to update channel preference', {
channel: channelType,
error: channelError.message,
requestId: tracking.requestId
});
results.push({
channel: channelType,
success: false,
@@ -109,7 +116,10 @@ serve(async (req) => {
const allSucceeded = failedChannels.length === 0;
if (!allSucceeded) {
console.warn(`Some channel preferences failed to update:`, failedChannels);
edgeLogger.warn('Some channel preferences failed to update', {
failedChannels: failedChannels.map(c => c.channel),
requestId: tracking.requestId
});
return new Response(
JSON.stringify({
success: false,
@@ -124,7 +134,11 @@ serve(async (req) => {
);
}
console.log('All preferences updated successfully');
const duration = endRequest(tracking);
edgeLogger.info('All preferences updated successfully', {
requestId: tracking.requestId,
duration
});
return new Response(
JSON.stringify({
success: true,
@@ -136,7 +150,12 @@ serve(async (req) => {
}
);
} catch (error: any) {
console.error('Error updating Novu preferences:', error);
const duration = endRequest(tracking);
edgeLogger.error('Error updating Novu preferences', {
error: error.message,
requestId: tracking.requestId,
duration
});
return new Response(
JSON.stringify({