mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 00:31:12 -05:00
Refactor: Continue implementation plan
This commit is contained in:
@@ -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',
|
||||
};
|
||||
|
||||
const TOPICS = {
|
||||
@@ -17,6 +18,8 @@ serve(async (req) => {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
}
|
||||
|
||||
const tracking = startRequest('manage-moderator-topic');
|
||||
|
||||
try {
|
||||
const novuApiKey = Deno.env.get('NOVU_API_KEY');
|
||||
if (!novuApiKey) {
|
||||
@@ -35,7 +38,7 @@ serve(async (req) => {
|
||||
throw new Error('Action must be either "add" or "remove"');
|
||||
}
|
||||
|
||||
console.log(`${action === 'add' ? 'Adding' : 'Removing'} user ${userId} ${action === 'add' ? 'to' : 'from'} moderator topics`);
|
||||
console.log(`${action === 'add' ? 'Adding' : 'Removing'} user ${userId} ${action === 'add' ? 'to' : 'from'} moderator topics`, { requestId: tracking.requestId });
|
||||
|
||||
const topics = [TOPICS.MODERATION_SUBMISSIONS, TOPICS.MODERATION_REPORTS];
|
||||
const results = [];
|
||||
@@ -70,28 +73,42 @@ serve(async (req) => {
|
||||
|
||||
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' },
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
},
|
||||
status: allSuccess ? 200 : 207, // 207 = Multi-Status (partial success)
|
||||
}
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.error('Error managing moderator topic:', 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,
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user