mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 10:11:13 -05:00
Migrate remaining edge functions to wrapper
Refactor process-expired-bans, detect-location, detect-anomalies, rate-limit-metrics, and collect-metrics to use createEdgeFunction wrapper with standardized error handling, tracing, and reduced boilerplate. Update signatures to receive { supabase, span, requestId } (and user where applicable), replace manual logging with span events, remove per-function boilerplate, and ensure consistent wrapper configuration (cors, auth, rate limits, and tracing).
This commit is contained in:
@@ -1,16 +1,10 @@
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
import { serve } from 'https://deno.land/std@0.190.0/http/server.ts';
|
||||
import { createEdgeFunction, type EdgeFunctionContext } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { addSpanEvent } from '../_shared/logger.ts';
|
||||
import { corsHeaders } from '../_shared/cors.ts';
|
||||
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
name: 'process-expired-bans',
|
||||
requireAuth: false,
|
||||
},
|
||||
async (req, context, supabase) => {
|
||||
edgeLogger.info('Processing expired bans', {
|
||||
requestId: context.requestId
|
||||
});
|
||||
const handler = async (req: Request, { supabase, span, requestId }: EdgeFunctionContext) => {
|
||||
addSpanEvent(span, 'processing_expired_bans', { requestId });
|
||||
|
||||
const now = new Date().toISOString();
|
||||
|
||||
@@ -23,25 +17,18 @@ export default createEdgeFunction(
|
||||
.lte('ban_expires_at', now);
|
||||
|
||||
if (fetchError) {
|
||||
edgeLogger.error('Error fetching expired bans', {
|
||||
error: fetchError,
|
||||
requestId: context.requestId
|
||||
});
|
||||
addSpanEvent(span, 'error_fetching_expired_bans', { error: fetchError.message });
|
||||
throw fetchError;
|
||||
}
|
||||
|
||||
edgeLogger.info('Found expired bans to process', {
|
||||
count: expiredBans?.length || 0,
|
||||
requestId: context.requestId
|
||||
});
|
||||
addSpanEvent(span, 'found_expired_bans', { count: expiredBans?.length || 0 });
|
||||
|
||||
// Unban users with expired bans
|
||||
const unbannedUsers: string[] = [];
|
||||
for (const profile of expiredBans || []) {
|
||||
edgeLogger.info('Unbanning user', {
|
||||
addSpanEvent(span, 'unbanning_user', {
|
||||
username: profile.username,
|
||||
userId: profile.user_id,
|
||||
requestId: context.requestId
|
||||
userId: profile.user_id
|
||||
});
|
||||
|
||||
const { error: unbanError } = await supabase
|
||||
@@ -54,10 +41,9 @@ export default createEdgeFunction(
|
||||
.eq('user_id', profile.user_id);
|
||||
|
||||
if (unbanError) {
|
||||
edgeLogger.error('Failed to unban user', {
|
||||
addSpanEvent(span, 'failed_to_unban_user', {
|
||||
username: profile.username,
|
||||
error: unbanError,
|
||||
requestId: context.requestId
|
||||
error: unbanError.message
|
||||
});
|
||||
continue;
|
||||
}
|
||||
@@ -76,29 +62,28 @@ export default createEdgeFunction(
|
||||
});
|
||||
|
||||
if (logError) {
|
||||
edgeLogger.error('Failed to log auto-unban', {
|
||||
addSpanEvent(span, 'failed_to_log_auto_unban', {
|
||||
username: profile.username,
|
||||
error: logError,
|
||||
requestId: context.requestId
|
||||
error: logError.message
|
||||
});
|
||||
}
|
||||
|
||||
unbannedUsers.push(profile.username);
|
||||
}
|
||||
|
||||
edgeLogger.info('Successfully unbanned users', {
|
||||
count: unbannedUsers.length,
|
||||
requestId: context.requestId
|
||||
});
|
||||
addSpanEvent(span, 'successfully_unbanned_users', { count: unbannedUsers.length });
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
unbanned_count: unbannedUsers.length,
|
||||
unbanned_users: unbannedUsers,
|
||||
processed_at: now
|
||||
}),
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
unbanned_count: unbannedUsers.length,
|
||||
unbanned_users: unbannedUsers,
|
||||
processed_at: now
|
||||
};
|
||||
};
|
||||
|
||||
serve(createEdgeFunction({
|
||||
name: 'process-expired-bans',
|
||||
requireAuth: false,
|
||||
corsHeaders,
|
||||
enableTracing: true,
|
||||
}, handler));
|
||||
|
||||
Reference in New Issue
Block a user