mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 09:31:13 -05:00
Continue Phase 2 Batch 2 and Batch 3
Migrate 6 background jobs to use wrapEdgeFunction: cleanup-old-versions, process-scheduled-deletions, data-retention-cleanup, run-cleanup-jobs, scheduled-maintenance, process-expired-bans. Replace old server routines with edgeFunction wrapper, add centralized logging, tracing, and standardized error handling, and adjust for batch-wise deployment.
This commit is contained in:
@@ -1,30 +1,21 @@
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
||||
import { corsHeaders } from '../_shared/cors.ts';
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
// Handle CORS preflight
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
// Create admin client
|
||||
const supabaseAdmin = createClient(
|
||||
Deno.env.get('SUPABASE_URL')!,
|
||||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
|
||||
{
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false
|
||||
}
|
||||
}
|
||||
);
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
name: 'process-expired-bans',
|
||||
requireAuth: false,
|
||||
},
|
||||
async (req, context, supabase) => {
|
||||
edgeLogger.info('Processing expired bans', {
|
||||
requestId: context.requestId
|
||||
});
|
||||
|
||||
const now = new Date().toISOString();
|
||||
|
||||
// Find expired bans
|
||||
const { data: expiredBans, error: fetchError } = await supabaseAdmin
|
||||
const { data: expiredBans, error: fetchError } = await supabase
|
||||
.from('profiles')
|
||||
.select('user_id, username, ban_reason, ban_expires_at')
|
||||
.eq('banned', true)
|
||||
@@ -32,18 +23,28 @@ Deno.serve(async (req) => {
|
||||
.lte('ban_expires_at', now);
|
||||
|
||||
if (fetchError) {
|
||||
edgeLogger.error('Error fetching expired bans', { action: 'process_expired_bans', error: fetchError });
|
||||
edgeLogger.error('Error fetching expired bans', {
|
||||
error: fetchError,
|
||||
requestId: context.requestId
|
||||
});
|
||||
throw fetchError;
|
||||
}
|
||||
|
||||
edgeLogger.info(`Found ${expiredBans?.length || 0} expired bans to process`, { action: 'process_expired_bans', count: expiredBans?.length || 0 });
|
||||
edgeLogger.info('Found expired bans to process', {
|
||||
count: expiredBans?.length || 0,
|
||||
requestId: context.requestId
|
||||
});
|
||||
|
||||
// Unban users with expired bans
|
||||
const unbannedUsers: string[] = [];
|
||||
for (const profile of expiredBans || []) {
|
||||
edgeLogger.info('Unbanning user', { action: 'process_expired_bans', username: profile.username, userId: profile.user_id });
|
||||
edgeLogger.info('Unbanning user', {
|
||||
username: profile.username,
|
||||
userId: profile.user_id,
|
||||
requestId: context.requestId
|
||||
});
|
||||
|
||||
const { error: unbanError } = await supabaseAdmin
|
||||
const { error: unbanError } = await supabase
|
||||
.from('profiles')
|
||||
.update({
|
||||
banned: false,
|
||||
@@ -53,14 +54,18 @@ Deno.serve(async (req) => {
|
||||
.eq('user_id', profile.user_id);
|
||||
|
||||
if (unbanError) {
|
||||
edgeLogger.error('Failed to unban user', { action: 'process_expired_bans', username: profile.username, error: unbanError });
|
||||
edgeLogger.error('Failed to unban user', {
|
||||
username: profile.username,
|
||||
error: unbanError,
|
||||
requestId: context.requestId
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// Log the automatic unban
|
||||
const { error: logError } = await supabaseAdmin
|
||||
const { error: logError } = await supabase
|
||||
.rpc('log_admin_action', {
|
||||
_admin_user_id: '00000000-0000-0000-0000-000000000000', // System user ID
|
||||
_admin_user_id: '00000000-0000-0000-0000-000000000000',
|
||||
_target_user_id: profile.user_id,
|
||||
_action: 'auto_unban',
|
||||
_details: {
|
||||
@@ -71,13 +76,20 @@ Deno.serve(async (req) => {
|
||||
});
|
||||
|
||||
if (logError) {
|
||||
edgeLogger.error('Failed to log auto-unban', { action: 'process_expired_bans', username: profile.username, error: logError });
|
||||
edgeLogger.error('Failed to log auto-unban', {
|
||||
username: profile.username,
|
||||
error: logError,
|
||||
requestId: context.requestId
|
||||
});
|
||||
}
|
||||
|
||||
unbannedUsers.push(profile.username);
|
||||
}
|
||||
|
||||
edgeLogger.info('Successfully unbanned users', { action: 'process_expired_bans', count: unbannedUsers.length });
|
||||
edgeLogger.info('Successfully unbanned users', {
|
||||
count: unbannedUsers.length,
|
||||
requestId: context.requestId
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
@@ -86,23 +98,7 @@ Deno.serve(async (req) => {
|
||||
unbanned_users: unbannedUsers,
|
||||
processed_at: now
|
||||
}),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
}
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
edgeLogger.error('Error in process-expired-bans', { action: 'process_expired_bans', error });
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
success: false
|
||||
}),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 500
|
||||
}
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
});
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user