mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 01:11:13 -05:00
feat: Implement Novu topics for moderator notifications
This commit is contained in:
99
supabase/functions/manage-moderator-topic/index.ts
Normal file
99
supabase/functions/manage-moderator-topic/index.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
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";
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
};
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
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"');
|
||||
}
|
||||
|
||||
console.log(`${action === 'add' ? 'Adding' : 'Removing'} user ${userId} ${action === 'add' ? 'to' : 'from'} moderator topics`);
|
||||
|
||||
const topics = [TOPICS.MODERATION_SUBMISSIONS, TOPICS.MODERATION_REPORTS];
|
||||
const results = [];
|
||||
|
||||
for (const topicKey of topics) {
|
||||
try {
|
||||
if (action === 'add') {
|
||||
// Add subscriber to topic
|
||||
await novu.topics.addSubscribers(topicKey, {
|
||||
subscribers: [userId],
|
||||
});
|
||||
console.log(`Added ${userId} to topic ${topicKey}`);
|
||||
results.push({ topic: topicKey, action: 'added', success: true });
|
||||
} else {
|
||||
// Remove subscriber from topic
|
||||
await novu.topics.removeSubscribers(topicKey, {
|
||||
subscribers: [userId],
|
||||
});
|
||||
console.log(`Removed ${userId} from topic ${topicKey}`);
|
||||
results.push({ topic: topicKey, action: 'removed', success: true });
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error(`Error ${action}ing user ${userId} ${action === 'add' ? 'to' : 'from'} topic ${topicKey}:`, error);
|
||||
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: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: allSuccess ? 200 : 207, // 207 = Multi-Status (partial success)
|
||||
}
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.error('Error managing moderator topic:', error);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
error: error.message,
|
||||
}),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user