Refactor: Continue implementation plan

This commit is contained in:
gpt-engineer-app[bot]
2025-10-21 12:56:53 +00:00
parent 74860c6774
commit 0d247d9fdd
10 changed files with 254 additions and 65 deletions

View File

@@ -1,10 +1,11 @@
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4";
import { Novu } from "npm:@novu/api@1.6.0";
import { startRequest, endRequest } from "../_shared/logger.ts";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-request-id',
};
serve(async (req) => {
@@ -12,6 +13,8 @@ serve(async (req) => {
return new Response(null, { headers: corsHeaders });
}
const tracking = startRequest('migrate-novu-users');
try {
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
@@ -53,14 +56,21 @@ serve(async (req) => {
if (profilesError) throw profilesError;
if (!profiles || profiles.length === 0) {
endRequest(tracking, 200);
return new Response(
JSON.stringify({
success: true,
message: 'No users to migrate',
results: [],
requestId: tracking.requestId
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
},
status: 200,
}
);
@@ -127,27 +137,41 @@ serve(async (req) => {
await new Promise(resolve => setTimeout(resolve, 100));
}
endRequest(tracking, 200);
return new Response(
JSON.stringify({
success: true,
total: profiles.length,
results,
requestId: tracking.requestId
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
},
status: 200,
}
);
} catch (error: any) {
console.error('Error migrating Novu users:', error);
endRequest(tracking, 500, error.message);
return new Response(
JSON.stringify({
success: false,
error: error.message,
requestId: tracking.requestId
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
},
status: 500,
}
);