feat: Implement temp ref cleanup

This commit is contained in:
gpt-engineer-app[bot]
2025-11-05 18:15:21 +00:00
parent 303853ff94
commit d62b3c2412

View File

@@ -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<string, Record<string, number>>();
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);