diff --git a/supabase/functions/process-selective-approval/index.ts b/supabase/functions/process-selective-approval/index.ts index f360fbd6..4220d81f 100644 --- a/supabase/functions/process-selective-approval/index.ts +++ b/supabase/functions/process-selective-approval/index.ts @@ -359,6 +359,40 @@ serve(withRateLimit(async (req) => { throw new Error(`Failed to fetch items: ${fetchError.message}`); } + // Query temporary references for all submission items + const { data: tempRefs, error: tempRefsError } = await supabase + .from('submission_item_temp_refs') + .select('submission_item_id, ref_type, ref_order_index') + .in('submission_item_id', itemIds); + + if (tempRefsError) { + edgeLogger.warn('Failed to fetch temp refs', { + action: 'approval_fetch_temp_refs', + submissionId, + error: tempRefsError.message, + requestId: tracking.requestId + }); + // Don't throw - continue with empty temp refs (backwards compatibility) + } + + // Build a map: itemId -> { _temp_operator_ref: 0, _temp_park_ref: 1, ... } + const tempRefsByItemId = new Map>(); + for (const ref of tempRefs || []) { + if (!tempRefsByItemId.has(ref.submission_item_id)) { + tempRefsByItemId.set(ref.submission_item_id, {}); + } + const fieldName = `_temp_${ref.ref_type}_ref`; + tempRefsByItemId.get(ref.submission_item_id)![fieldName] = ref.ref_order_index; + } + + edgeLogger.info('Loaded temp refs', { + action: 'approval_temp_refs_loaded', + submissionId, + itemsWithTempRefs: tempRefsByItemId.size, + totalTempRefs: tempRefs?.length || 0, + requestId: tracking.requestId + }); + // Get the submitter's user_id from the submission const { data: submission, error: submissionError } = await supabase .from('content_submissions') @@ -423,25 +457,39 @@ serve(withRateLimit(async (req) => { let itemData: any; switch (item.item_type) { case 'park': - itemData = (item as any).park_submission; + itemData = { + ...(item as any).park_submission, + // Merge temp refs for this item + ...(tempRefsByItemId.get(item.id) || {}) + }; break; case 'ride': - itemData = (item as any).ride_submission; + itemData = { + ...(item as any).ride_submission, + ...(tempRefsByItemId.get(item.id) || {}) + }; break; case 'manufacturer': case 'operator': case 'property_owner': case 'designer': - itemData = (item as any).company_submission; + itemData = { + ...(item as any).company_submission, + ...(tempRefsByItemId.get(item.id) || {}) + }; break; case 'ride_model': - itemData = (item as any).ride_model_submission; + itemData = { + ...(item as any).ride_model_submission, + ...(tempRefsByItemId.get(item.id) || {}) + }; break; case 'photo': // Combine photo_submission with its photo_items array itemData = { ...(item as any).photo_submission, - photos: (item as any).photo_submission?.photo_items || [] + photos: (item as any).photo_submission?.photo_items || [], + ...(tempRefsByItemId.get(item.id) || {}) }; break; default: @@ -453,6 +501,17 @@ serve(withRateLimit(async (req) => { // Fallback to item_data if relational data not found (for backwards compatibility) itemData = item.item_data; } + + // Log if temp refs were found for this item + if (tempRefsByItemId.has(item.id)) { + edgeLogger.info('Item has temp refs', { + action: 'approval_item_temp_refs', + itemId: item.id, + itemType: item.item_type, + tempRefs: tempRefsByItemId.get(item.id), + requestId: tracking.requestId + }); + } // Validate entity data with strict validation, passing original_data for edits const validation = validateEntityDataStrict(item.item_type, itemData, item.original_data);