Files
thrilltrack-explorer/supabase/functions/manage-moderator-topic/index.ts
gpt-engineer-app[bot] bf3da6414a Centralize CORS configuration
Consolidate CORS handling by introducing a shared supabase/functions/_shared/cors.ts and migrate edge functions to import from it. Remove inline cors.ts usage across functions, standardize headers (including traceparent and x-request-id), and prepare for environment-aware origins.
2025-11-10 21:28:46 +00:00

127 lines
4.1 KiB
TypeScript

import { serve } from "https://deno.land/std@0.190.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 { corsHeadersWithTracing as corsHeaders } from '../_shared/cors.ts';
import { edgeLogger, startRequest, endRequest } from "../_shared/logger.ts";
import { withEdgeRetry } from '../_shared/retryHelper.ts';
const TOPICS = {
MODERATION_SUBMISSIONS: 'moderation-submissions',
MODERATION_REPORTS: 'moderation-reports',
} as const;
serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
const tracking = startRequest('manage-moderator-topic');
try {
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"');
}
edgeLogger.info(`${action === 'add' ? 'Adding' : 'Removing'} user ${userId} ${action === 'add' ? 'to' : 'from'} moderator topics`, { action: 'manage_moderator_topic', requestId: tracking.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: tracking.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: tracking.requestId, userId, topicKey });
}
},
{ maxAttempts: 3, baseDelay: 1000 },
tracking.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: tracking.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);
endRequest(tracking, allSuccess ? 200 : 207);
return new Response(
JSON.stringify({
success: allSuccess,
userId,
action,
results,
requestId: tracking.requestId
}),
{
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
},
status: allSuccess ? 200 : 207, // 207 = Multi-Status (partial success)
}
);
} catch (error: any) {
edgeLogger.error('Error managing moderator topic', { action: 'manage_moderator_topic', requestId: tracking.requestId, error: error.message });
endRequest(tracking, 500, 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,
}
);
}
});