mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 17:51:14 -05:00
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:
@@ -1,23 +1,22 @@
|
||||
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 { corsHeaders } from '../_shared/cors.ts';
|
||||
import { edgeLogger, startRequest, endRequest } from '../_shared/logger.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
import { withEdgeRetry } from '../_shared/retryHelper.ts';
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
|
||||
const TOPICS = {
|
||||
MODERATION_SUBMISSIONS: 'moderation-submissions',
|
||||
MODERATION_REPORTS: 'moderation-reports',
|
||||
} as const;
|
||||
|
||||
serve(async (req) => {
|
||||
const tracking = startRequest();
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
name: 'sync-all-moderators-to-topic',
|
||||
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');
|
||||
@@ -29,8 +28,9 @@ serve(async (req) => {
|
||||
const novu = new Novu({ secretKey: novuApiKey });
|
||||
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
||||
|
||||
context.span.setAttribute('action', 'sync_moderators');
|
||||
edgeLogger.info('Starting moderator sync to Novu topics', {
|
||||
requestId: tracking.requestId,
|
||||
requestId: context.requestId,
|
||||
action: 'sync_moderators'
|
||||
});
|
||||
|
||||
@@ -48,7 +48,7 @@ serve(async (req) => {
|
||||
const uniqueUserIds = [...new Set(moderatorRoles.map(r => r.user_id))];
|
||||
|
||||
edgeLogger.info('Found unique moderators to sync', {
|
||||
requestId: tracking.requestId,
|
||||
requestId: context.requestId,
|
||||
count: uniqueUserIds.length
|
||||
});
|
||||
|
||||
@@ -62,12 +62,12 @@ serve(async (req) => {
|
||||
try {
|
||||
// Ensure topic exists (Novu will create it if it doesn't)
|
||||
await novu.topics.create({ key: topicKey, name: topicKey });
|
||||
edgeLogger.info('Topic ready', { requestId: tracking.requestId, topicKey });
|
||||
edgeLogger.info('Topic ready', { requestId: context.requestId, topicKey });
|
||||
} catch (error: any) {
|
||||
// Topic might already exist, which is fine
|
||||
if (!error.message?.includes('already exists')) {
|
||||
edgeLogger.warn('Note about topic', {
|
||||
requestId: tracking.requestId,
|
||||
requestId: context.requestId,
|
||||
topicKey,
|
||||
error: error.message
|
||||
});
|
||||
@@ -90,20 +90,20 @@ serve(async (req) => {
|
||||
});
|
||||
},
|
||||
{ maxAttempts: 3, baseDelay: 2000 },
|
||||
tracking.requestId,
|
||||
context.requestId,
|
||||
`sync-batch-${topicKey}-${i}`
|
||||
);
|
||||
|
||||
successCount += batch.length;
|
||||
edgeLogger.info('Added batch of users to topic', {
|
||||
requestId: tracking.requestId,
|
||||
requestId: context.requestId,
|
||||
topicKey,
|
||||
batchSize: batch.length
|
||||
});
|
||||
} catch (error: any) {
|
||||
errorCount += batch.length;
|
||||
edgeLogger.error('Error adding batch to topic', {
|
||||
requestId: tracking.requestId,
|
||||
requestId: context.requestId,
|
||||
topicKey,
|
||||
batchSize: batch.length,
|
||||
error: error.message
|
||||
@@ -118,10 +118,8 @@ serve(async (req) => {
|
||||
});
|
||||
}
|
||||
|
||||
const duration = endRequest(tracking);
|
||||
edgeLogger.info('Sync completed', {
|
||||
requestId: tracking.requestId,
|
||||
duration,
|
||||
requestId: context.requestId,
|
||||
results
|
||||
});
|
||||
|
||||
@@ -130,39 +128,13 @@ serve(async (req) => {
|
||||
success: true,
|
||||
message: 'Moderator sync completed',
|
||||
results,
|
||||
requestId: tracking.requestId
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
},
|
||||
status: 200,
|
||||
}
|
||||
);
|
||||
} catch (error: any) {
|
||||
const duration = endRequest(tracking);
|
||||
edgeLogger.error('Error syncing moderators to topics', {
|
||||
requestId: tracking.requestId,
|
||||
duration,
|
||||
error: error.message
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
error: error.message,
|
||||
requestId: tracking.requestId
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
},
|
||||
status: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user