mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 10:31:13 -05:00
Fix submission update logic
This commit is contained in:
@@ -224,22 +224,109 @@ export async function detectDependencyConflicts(
|
||||
}
|
||||
|
||||
/**
|
||||
* Update individual submission item status
|
||||
* Note: item_data and original_data are read-only (managed via relational tables)
|
||||
* Update individual submission item status and data
|
||||
*/
|
||||
export async function updateSubmissionItem(
|
||||
itemId: string,
|
||||
updates: Partial<SubmissionItemWithDeps>
|
||||
): Promise<void> {
|
||||
// Remove item_data and original_data from updates (managed via relational tables)
|
||||
const { item_data, original_data, ...cleanUpdates } = updates;
|
||||
|
||||
// Update submission_items table
|
||||
const { error } = await supabase
|
||||
.from('submission_items')
|
||||
.update(cleanUpdates)
|
||||
.eq('id', itemId);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
// If item_data is provided, update the relational table
|
||||
if (item_data !== undefined) {
|
||||
// Fetch the item to get its type and foreign keys
|
||||
const { data: item, error: fetchError } = await supabase
|
||||
.from('submission_items')
|
||||
.select('item_type, park_submission_id, ride_submission_id, company_submission_id, ride_model_submission_id, timeline_event_submission_id, photo_submission_id')
|
||||
.eq('id', itemId)
|
||||
.single();
|
||||
|
||||
if (fetchError) throw fetchError;
|
||||
if (!item) throw new Error(`Submission item ${itemId} not found`);
|
||||
|
||||
// Update the appropriate relational table
|
||||
switch (item.item_type) {
|
||||
case 'park': {
|
||||
if (!item.park_submission_id) break;
|
||||
const parkData = item_data as any;
|
||||
const updateData: any = {
|
||||
...parkData,
|
||||
// Transform location → temp_location_data for storage
|
||||
temp_location_data: parkData.location || null,
|
||||
updated_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Remove fields that shouldn't be in park_submissions
|
||||
delete updateData.location;
|
||||
|
||||
// Remove undefined fields
|
||||
Object.keys(updateData).forEach(key => {
|
||||
if (updateData[key] === undefined) delete updateData[key];
|
||||
});
|
||||
|
||||
const { error: updateError } = await supabase
|
||||
.from('park_submissions')
|
||||
.update(updateData)
|
||||
.eq('id', item.park_submission_id);
|
||||
|
||||
if (updateError) throw updateError;
|
||||
break;
|
||||
}
|
||||
case 'ride': {
|
||||
if (!item.ride_submission_id) break;
|
||||
const { error: updateError } = await supabase
|
||||
.from('ride_submissions')
|
||||
.update({ ...(item_data as any), updated_at: new Date().toISOString() })
|
||||
.eq('id', item.ride_submission_id);
|
||||
|
||||
if (updateError) throw updateError;
|
||||
break;
|
||||
}
|
||||
case 'operator':
|
||||
case 'manufacturer':
|
||||
case 'designer':
|
||||
case 'property_owner': {
|
||||
if (!item.company_submission_id) break;
|
||||
const { error: updateError } = await supabase
|
||||
.from('company_submissions')
|
||||
.update({ ...(item_data as any), updated_at: new Date().toISOString() })
|
||||
.eq('id', item.company_submission_id);
|
||||
|
||||
if (updateError) throw updateError;
|
||||
break;
|
||||
}
|
||||
case 'ride_model': {
|
||||
if (!item.ride_model_submission_id) break;
|
||||
const { error: updateError } = await supabase
|
||||
.from('ride_model_submissions')
|
||||
.update({ ...(item_data as any), updated_at: new Date().toISOString() })
|
||||
.eq('id', item.ride_model_submission_id);
|
||||
|
||||
if (updateError) throw updateError;
|
||||
break;
|
||||
}
|
||||
case 'milestone':
|
||||
case 'timeline_event': {
|
||||
if (!item.timeline_event_submission_id) break;
|
||||
const { error: updateError } = await supabase
|
||||
.from('timeline_event_submissions')
|
||||
.update({ ...(item_data as any), updated_at: new Date().toISOString() })
|
||||
.eq('id', item.timeline_event_submission_id);
|
||||
|
||||
if (updateError) throw updateError;
|
||||
break;
|
||||
}
|
||||
// Photo submissions handled separately due to complex structure
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user