mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
feat: Implement temp ref cleanup
This commit is contained in:
@@ -359,6 +359,40 @@ serve(withRateLimit(async (req) => {
|
|||||||
throw new Error(`Failed to fetch items: ${fetchError.message}`);
|
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
|
// Get the submitter's user_id from the submission
|
||||||
const { data: submission, error: submissionError } = await supabase
|
const { data: submission, error: submissionError } = await supabase
|
||||||
.from('content_submissions')
|
.from('content_submissions')
|
||||||
@@ -423,25 +457,39 @@ serve(withRateLimit(async (req) => {
|
|||||||
let itemData: any;
|
let itemData: any;
|
||||||
switch (item.item_type) {
|
switch (item.item_type) {
|
||||||
case 'park':
|
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;
|
break;
|
||||||
case 'ride':
|
case 'ride':
|
||||||
itemData = (item as any).ride_submission;
|
itemData = {
|
||||||
|
...(item as any).ride_submission,
|
||||||
|
...(tempRefsByItemId.get(item.id) || {})
|
||||||
|
};
|
||||||
break;
|
break;
|
||||||
case 'manufacturer':
|
case 'manufacturer':
|
||||||
case 'operator':
|
case 'operator':
|
||||||
case 'property_owner':
|
case 'property_owner':
|
||||||
case 'designer':
|
case 'designer':
|
||||||
itemData = (item as any).company_submission;
|
itemData = {
|
||||||
|
...(item as any).company_submission,
|
||||||
|
...(tempRefsByItemId.get(item.id) || {})
|
||||||
|
};
|
||||||
break;
|
break;
|
||||||
case 'ride_model':
|
case 'ride_model':
|
||||||
itemData = (item as any).ride_model_submission;
|
itemData = {
|
||||||
|
...(item as any).ride_model_submission,
|
||||||
|
...(tempRefsByItemId.get(item.id) || {})
|
||||||
|
};
|
||||||
break;
|
break;
|
||||||
case 'photo':
|
case 'photo':
|
||||||
// Combine photo_submission with its photo_items array
|
// Combine photo_submission with its photo_items array
|
||||||
itemData = {
|
itemData = {
|
||||||
...(item as any).photo_submission,
|
...(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;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -453,6 +501,17 @@ serve(withRateLimit(async (req) => {
|
|||||||
// Fallback to item_data if relational data not found (for backwards compatibility)
|
// Fallback to item_data if relational data not found (for backwards compatibility)
|
||||||
itemData = item.item_data;
|
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
|
// Validate entity data with strict validation, passing original_data for edits
|
||||||
const validation = validateEntityDataStrict(item.item_type, itemData, item.original_data);
|
const validation = validateEntityDataStrict(item.item_type, itemData, item.original_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user