mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
Fix updateItemDataWithHistory
Refactor to store park location data relationally: - Retrieve park_submission_id from park_submissions - Remove embedded temp_location_data usage - Update park_submissions without location, then upsert location into park_submission_locations - Align behavior with updateSubmissionItems pattern to fix JSON storage in Item history
This commit is contained in:
@@ -1509,27 +1509,21 @@ export async function editSubmissionItem(
|
||||
|
||||
// Update relational table with new data based on item type
|
||||
if (currentItem.item_type === 'park') {
|
||||
// For parks, store location in temp_location_data if provided
|
||||
// First, get the park_submission_id
|
||||
const { data: parkSub, error: parkSubError } = await supabase
|
||||
.from('park_submissions')
|
||||
.select('id')
|
||||
.eq('submission_id', currentItem.submission_id)
|
||||
.single();
|
||||
|
||||
if (parkSubError) throw parkSubError;
|
||||
|
||||
// Prepare update data (remove location from main update)
|
||||
const updateData: any = { ...newData };
|
||||
const locationData = updateData.location;
|
||||
delete updateData.location; // Remove nested object before updating park_submissions
|
||||
|
||||
// If location object is provided, store it in temp_location_data
|
||||
if (newData.location) {
|
||||
updateData.temp_location_data = {
|
||||
name: newData.location.name,
|
||||
street_address: newData.location.street_address || null,
|
||||
city: newData.location.city || null,
|
||||
state_province: newData.location.state_province || null,
|
||||
country: newData.location.country,
|
||||
latitude: newData.location.latitude,
|
||||
longitude: newData.location.longitude,
|
||||
timezone: newData.location.timezone || null,
|
||||
postal_code: newData.location.postal_code || null,
|
||||
display_name: newData.location.display_name
|
||||
};
|
||||
delete updateData.location; // Remove the nested object
|
||||
}
|
||||
|
||||
// Update park_submissions table
|
||||
// Update park_submissions table (without temp_location_data!)
|
||||
const { error: parkUpdateError } = await supabase
|
||||
.from('park_submissions')
|
||||
.update(updateData)
|
||||
@@ -1537,6 +1531,29 @@ export async function editSubmissionItem(
|
||||
|
||||
if (parkUpdateError) throw parkUpdateError;
|
||||
|
||||
// Handle location separately in relational table
|
||||
if (locationData) {
|
||||
const { error: locationError } = await supabase
|
||||
.from('park_submission_locations' as any)
|
||||
.upsert({
|
||||
park_submission_id: parkSub.id,
|
||||
name: locationData.name,
|
||||
street_address: locationData.street_address || null,
|
||||
city: locationData.city || null,
|
||||
state_province: locationData.state_province || null,
|
||||
country: locationData.country,
|
||||
postal_code: locationData.postal_code || null,
|
||||
latitude: locationData.latitude,
|
||||
longitude: locationData.longitude,
|
||||
timezone: locationData.timezone || null,
|
||||
display_name: locationData.display_name || null
|
||||
}, {
|
||||
onConflict: 'park_submission_id'
|
||||
});
|
||||
|
||||
if (locationError) throw locationError;
|
||||
}
|
||||
|
||||
} else if (currentItem.item_type === 'ride') {
|
||||
const { error: rideUpdateError } = await supabase
|
||||
.from('ride_submissions')
|
||||
|
||||
Reference in New Issue
Block a user