mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 17:11:12 -05:00
Fix park submission locations
Implement Phase 1 of the JSONB violation fix by creating the `park_submission_locations` table. This includes migrating existing data from `park_submissions.temp_location_data` and updating relevant code to read and write to the new relational table. The `temp_location_data` column will be dropped after data migration.
This commit is contained in:
@@ -81,12 +81,21 @@ export async function fetchSubmissionItems(submissionId: string): Promise<Submis
|
||||
switch (item.item_type) {
|
||||
case 'park': {
|
||||
const parkSub = (item as any).park_submission;
|
||||
// Fetch location from park_submission_locations if available
|
||||
let locationData = null;
|
||||
if (parkSub?.id) {
|
||||
const { data } = await supabase
|
||||
.from('park_submission_locations')
|
||||
.select('*')
|
||||
.eq('park_submission_id', parkSub.id)
|
||||
.maybeSingle();
|
||||
locationData = data;
|
||||
}
|
||||
|
||||
item_data = {
|
||||
...parkSub,
|
||||
// Transform temp_location_data → location for form compatibility
|
||||
location: parkSub.temp_location_data || undefined,
|
||||
// Remove temp_location_data to avoid confusion
|
||||
temp_location_data: undefined
|
||||
// Transform park_submission_location → location for form compatibility
|
||||
location: locationData || undefined
|
||||
};
|
||||
break;
|
||||
}
|
||||
@@ -273,8 +282,6 @@ export async function updateSubmissionItem(
|
||||
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()
|
||||
};
|
||||
|
||||
@@ -289,34 +296,57 @@ export async function updateSubmissionItem(
|
||||
console.info('[Submission Flow] Saving park data', {
|
||||
itemId,
|
||||
parkSubmissionId: item.park_submission_id,
|
||||
hasLocation: !!updateData.temp_location_data,
|
||||
locationData: updateData.temp_location_data,
|
||||
hasLocation: !!parkData.location,
|
||||
fields: Object.keys(updateData),
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
|
||||
const { error: updateError } = await supabase
|
||||
.from('park_submissions')
|
||||
// Update park_submissions
|
||||
const { error: parkError } = await supabase
|
||||
.from('park_submissions' as any)
|
||||
.update(updateData)
|
||||
.eq('id', item.park_submission_id);
|
||||
|
||||
if (updateError) {
|
||||
handleError(updateError, {
|
||||
action: 'Update Park Submission Data',
|
||||
metadata: {
|
||||
itemId,
|
||||
parkSubmissionId: item.park_submission_id,
|
||||
updateFields: Object.keys(updateData)
|
||||
}
|
||||
});
|
||||
throw updateError;
|
||||
if (parkError) {
|
||||
console.error('[Submission Flow] Park update failed:', parkError);
|
||||
throw parkError;
|
||||
}
|
||||
|
||||
console.info('[Submission Flow] Park data saved successfully', {
|
||||
itemId,
|
||||
parkSubmissionId: item.park_submission_id,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
|
||||
// Update or insert location if provided
|
||||
if (parkData.location) {
|
||||
const locationData = {
|
||||
park_submission_id: item.park_submission_id,
|
||||
name: parkData.location.name,
|
||||
street_address: parkData.location.street_address || null,
|
||||
city: parkData.location.city || null,
|
||||
state_province: parkData.location.state_province || null,
|
||||
country: parkData.location.country,
|
||||
postal_code: parkData.location.postal_code || null,
|
||||
latitude: parkData.location.latitude,
|
||||
longitude: parkData.location.longitude,
|
||||
timezone: parkData.location.timezone || null,
|
||||
display_name: parkData.location.display_name || null
|
||||
};
|
||||
|
||||
// Try to update first, if no rows affected, insert
|
||||
const { error: locationError } = await supabase
|
||||
.from('park_submission_locations' as any)
|
||||
.upsert(locationData, {
|
||||
onConflict: 'park_submission_id'
|
||||
});
|
||||
|
||||
if (locationError) {
|
||||
console.error('[Submission Flow] Location upsert failed:', locationError);
|
||||
throw locationError;
|
||||
}
|
||||
|
||||
console.info('[Submission Flow] Location saved', {
|
||||
parkSubmissionId: item.park_submission_id,
|
||||
locationName: locationData.name
|
||||
});
|
||||
}
|
||||
|
||||
console.info('[Submission Flow] Park data saved successfully');
|
||||
break;
|
||||
}
|
||||
case 'ride': {
|
||||
|
||||
Reference in New Issue
Block a user