Files
thrilltrack-explorer/supabase/functions/notify-moderators-submission/index.ts
2025-10-12 18:22:25 +00:00

117 lines
3.4 KiB
TypeScript

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";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
};
interface NotificationPayload {
submission_id: string;
submission_type: string;
submitter_name: string;
action: string;
}
serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
try {
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
const supabase = createClient(supabaseUrl, supabaseServiceKey);
const payload: NotificationPayload = await req.json();
const { submission_id, submission_type, submitter_name, action } = payload;
console.log('Notifying moderators about submission via topic:', { submission_id, submission_type });
// Get the moderation-alert workflow
const { data: workflow, error: workflowError } = await supabase
.from('notification_templates')
.select('workflow_id')
.eq('category', 'moderation')
.eq('is_active', true)
.single();
if (workflowError || !workflow) {
console.error('Error fetching workflow:', workflowError);
return new Response(
JSON.stringify({
success: false,
error: 'Workflow not found or not active'
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 500,
}
);
}
// Prepare notification payload
const notificationPayload = {
itemType: submission_type,
submitterName: submitter_name,
submissionId: submission_id,
action: action || 'create',
moderationUrl: `https://ydvtmnrszybqnbcqbdcy.supabase.co/admin/moderation`,
};
// Send ONE notification to the moderation-submissions topic
// All subscribers (moderators) will receive it automatically
const { data, error } = await supabase.functions.invoke('trigger-notification', {
body: {
workflowId: workflow.workflow_id,
topicKey: 'moderation-submissions',
payload: notificationPayload,
},
});
if (error) {
console.error('Failed to notify moderators via topic:', error);
return new Response(
JSON.stringify({
success: false,
error: 'Failed to send notification to topic',
details: error.message
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 500,
}
);
}
console.log('Successfully notified all moderators via topic:', data);
return new Response(
JSON.stringify({
success: true,
message: 'Moderator notifications sent via topic',
topicKey: 'moderation-submissions',
transactionId: data?.transactionId,
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
}
);
} catch (error: any) {
console.error('Error in notify-moderators-submission:', error);
return new Response(
JSON.stringify({
success: false,
error: error.message,
}),
{
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 500,
}
);
}
});