mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 15:11:13 -05:00
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.
102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4";
|
|
import { Novu } from "npm:@novu/api@1.6.0";
|
|
import { corsHeadersWithTracing as corsHeaders } from '../_shared/cors.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;
|
|
|
|
export default createEdgeFunction(
|
|
{
|
|
name: 'manage-moderator-topic',
|
|
requireAuth: false,
|
|
corsHeaders: corsHeaders
|
|
},
|
|
async (req, context) => {
|
|
const novuApiKey = Deno.env.get('NOVU_API_KEY');
|
|
if (!novuApiKey) {
|
|
throw new Error('NOVU_API_KEY is not configured');
|
|
}
|
|
|
|
const novu = new Novu({ secretKey: novuApiKey });
|
|
|
|
const { userId, action } = await req.json();
|
|
|
|
if (!userId || !action) {
|
|
throw new Error('Missing required fields: userId, action');
|
|
}
|
|
|
|
if (action !== 'add' && action !== 'remove') {
|
|
throw new Error('Action must be either "add" or "remove"');
|
|
}
|
|
|
|
context.span.setAttribute('action', 'manage_moderator_topic');
|
|
edgeLogger.info(`${action === 'add' ? 'Adding' : 'Removing'} user ${userId} ${action === 'add' ? 'to' : 'from'} moderator topics`, { action: 'manage_moderator_topic', requestId: context.requestId, userId, operation: action });
|
|
|
|
const topics = [TOPICS.MODERATION_SUBMISSIONS, TOPICS.MODERATION_REPORTS];
|
|
const results = [];
|
|
|
|
for (const topicKey of topics) {
|
|
try {
|
|
await withEdgeRetry(
|
|
async () => {
|
|
if (action === 'add') {
|
|
// Add subscriber to topic
|
|
await novu.topics.addSubscribers(topicKey, {
|
|
subscribers: [userId],
|
|
});
|
|
edgeLogger.info('Added user to topic', { action: 'manage_moderator_topic', requestId: context.requestId, userId, topicKey });
|
|
} else {
|
|
// Remove subscriber from topic
|
|
await novu.topics.removeSubscribers(topicKey, {
|
|
subscribers: [userId],
|
|
});
|
|
edgeLogger.info('Removed user from topic', { action: 'manage_moderator_topic', requestId: context.requestId, userId, topicKey });
|
|
}
|
|
},
|
|
{ maxAttempts: 3, baseDelay: 1000 },
|
|
context.requestId,
|
|
`${action}-topic-${topicKey}`
|
|
);
|
|
|
|
results.push({ topic: topicKey, action: action === 'add' ? 'added' : 'removed', success: true });
|
|
} catch (error: any) {
|
|
edgeLogger.error(`Error ${action}ing user ${userId} ${action === 'add' ? 'to' : 'from'} topic ${topicKey}`, {
|
|
action: 'manage_moderator_topic',
|
|
requestId: context.requestId,
|
|
userId,
|
|
topicKey,
|
|
error: error.message
|
|
});
|
|
results.push({
|
|
topic: topicKey,
|
|
action: action === 'add' ? 'added' : 'removed',
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
}
|
|
|
|
const allSuccess = results.every(r => r.success);
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: allSuccess,
|
|
userId,
|
|
action,
|
|
results,
|
|
}),
|
|
{
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
status: allSuccess ? 200 : 207, // 207 = Multi-Status (partial success)
|
|
}
|
|
);
|
|
}
|
|
);
|