Add Company Backfill Tool

Implement edge function and UI to backfill missing company data from submissions, including admin trigger and result reporting, and wire into admin settings.
This commit is contained in:
gpt-engineer-app[bot]
2025-11-11 15:48:49 +00:00
parent 314db65591
commit 664c894bb1
5 changed files with 297 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
import { edgeLogger } from '../_shared/logger.ts';
export default createEdgeFunction(
{
name: 'backfill-company-data',
requireAuth: true,
},
async (req, context, supabase) => {
edgeLogger.info('Starting company data 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_company_data');
if (error) {
edgeLogger.error('Error running company data backfill', {
error,
requestId: context.requestId
});
throw error;
}
edgeLogger.info('Company data backfill completed', {
results: data,
requestId: context.requestId
});
return new Response(
JSON.stringify({
success: true,
...data,
}),
{ headers: { 'Content-Type': 'application/json' } }
);
}
);