From a2663b392ab2cfb40db12d9119302a2fb60fd91d Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Tue, 11 Nov 2025 15:37:38 +0000 Subject: [PATCH] Backfill Park Locations Add a script to backfill missing location data for already-approved parks from their submission data, and make it executable from the admin settings interface. Implement a backfill function (SQL migration) and an edge function to trigger it, plus UI integration in Admin Settings to initiate the process. Ensure only parks with approved submissions and location data are linked to new location records, and expose results in admin. --- .../backfill-park-locations/index.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 supabase/functions/backfill-park-locations/index.ts diff --git a/supabase/functions/backfill-park-locations/index.ts b/supabase/functions/backfill-park-locations/index.ts new file mode 100644 index 00000000..2ddf61cc --- /dev/null +++ b/supabase/functions/backfill-park-locations/index.ts @@ -0,0 +1,54 @@ +import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts'; +import { edgeLogger } from '../_shared/logger.ts'; + +export default createEdgeFunction( + { + name: 'backfill-park-locations', + requireAuth: true, + }, + async (req, context, supabase) => { + edgeLogger.info('Starting park location backfill', { requestId: context.requestId }); + + // Check if user is superuser + const { data: profile, error: profileError } = await supabase + .from('user_profiles') + .select('role') + .eq('id', context.user.id) + .single(); + + if (profileError || profile?.role !== 'superuser') { + edgeLogger.warn('Unauthorized backfill attempt', { + userId: context.user.id, + requestId: context.requestId + }); + return new Response( + JSON.stringify({ error: 'Unauthorized: Superuser access required' }), + { status: 403, headers: { 'Content-Type': 'application/json' } } + ); + } + + // Execute the backfill function + const { data, error } = await supabase.rpc('backfill_park_locations'); + + if (error) { + edgeLogger.error('Error running park location backfill', { + error, + requestId: context.requestId + }); + throw error; + } + + edgeLogger.info('Park location backfill completed', { + results: data, + requestId: context.requestId + }); + + return new Response( + JSON.stringify({ + success: true, + ...data, + }), + { headers: { 'Content-Type': 'application/json' } } + ); + } +);