Fix seed-test-data edge function

This commit is contained in:
gpt-engineer-app[bot]
2025-11-04 16:04:04 +00:00
parent abb9761a77
commit 83e20bfd56

View File

@@ -135,9 +135,23 @@ async function getPendingSubmissionItems(
supabase: any, supabase: any,
itemType: string itemType: string
): Promise<Array<{ id: string; item_data_id: string }>> { ): Promise<Array<{ id: string; item_data_id: string }>> {
// Determine which FK column to select based on itemType
let fkColumn: string;
if (itemType === 'park') {
fkColumn = 'park_submission_id';
} else if (itemType === 'ride') {
fkColumn = 'ride_submission_id';
} else if (['manufacturer', 'operator', 'designer', 'property_owner'].includes(itemType)) {
fkColumn = 'company_submission_id';
} else if (itemType === 'ride_model') {
fkColumn = 'ride_model_submission_id';
} else {
fkColumn = 'photo_submission_id';
}
const { data, error } = await supabase const { data, error } = await supabase
.from('submission_items') .from('submission_items')
.select('id, item_data_id') .select(`id, ${fkColumn}`)
.eq('item_type', itemType) .eq('item_type', itemType)
.eq('status', 'pending'); .eq('status', 'pending');
@@ -146,7 +160,11 @@ async function getPendingSubmissionItems(
return []; return [];
} }
return data || []; // Map the response to maintain backward compatibility
return (data || []).map(item => ({
id: item.id,
item_data_id: item[fkColumn]
}));
} }
// Get slug from relational submission table // Get slug from relational submission table
@@ -482,16 +500,30 @@ Deno.serve(async (req) => {
typeDataId = insertedData?.id; typeDataId = insertedData?.id;
} }
// Create submission_item (WITHOUT item_data, using item_data_id) // Create submission_item using type-specific foreign keys
const { error: itemError } = await supabase.from('submission_items').insert({ const itemData: any = {
id: itemId, id: itemId,
submission_id: submissionId, submission_id: submissionId,
item_type: type, item_type: type,
item_data_id: typeDataId,
status: 'pending', status: 'pending',
order_index: 0, order_index: 0,
depends_on: options.dependsOn || null depends_on: options.dependsOn || null
}); };
// Add the appropriate foreign key based on type
if (type === 'park') {
itemData.park_submission_id = typeDataId;
} else if (type === 'ride') {
itemData.ride_submission_id = typeDataId;
} else if (['manufacturer', 'operator', 'designer', 'property_owner'].includes(type)) {
itemData.company_submission_id = typeDataId;
} else if (type === 'ride_model') {
itemData.ride_model_submission_id = typeDataId;
} else if (type === 'photo') {
itemData.photo_submission_id = typeDataId;
}
const { error: itemError } = await supabase.from('submission_items').insert(itemData);
if (itemError) { if (itemError) {
edgeLogger.error('Error inserting submission_item', { type, error: itemError.message }); edgeLogger.error('Error inserting submission_item', { type, error: itemError.message });