mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 13:51:13 -05:00
Fix: Implement location data storage fix
This commit is contained in:
@@ -294,6 +294,12 @@ async function createPark(data: any, dependencyMap: Map<string, string>): Promis
|
||||
// Handle park edit
|
||||
const resolvedData = resolveDependencies(data, dependencyMap);
|
||||
|
||||
// Resolve location_id if location data is provided
|
||||
let locationId = resolvedData.location_id;
|
||||
if (resolvedData.location && !locationId) {
|
||||
locationId = await resolveLocationId(resolvedData.location);
|
||||
}
|
||||
|
||||
// Extract image assignments from ImageAssignments structure
|
||||
const imageData = extractImageAssignments(resolvedData.images);
|
||||
|
||||
@@ -311,7 +317,7 @@ async function createPark(data: any, dependencyMap: Map<string, string>): Promis
|
||||
email: resolvedData.email || null,
|
||||
operator_id: resolvedData.operator_id || null,
|
||||
property_owner_id: resolvedData.property_owner_id || null,
|
||||
location_id: resolvedData.location_id || null,
|
||||
location_id: locationId || null,
|
||||
...imageData,
|
||||
updated_at: new Date().toISOString()
|
||||
};
|
||||
@@ -333,6 +339,12 @@ async function createPark(data: any, dependencyMap: Map<string, string>): Promis
|
||||
validateSubmissionData(data, 'Park');
|
||||
const resolvedData = resolveDependencies(data, dependencyMap);
|
||||
|
||||
// Resolve location_id if location data is provided
|
||||
let locationId = resolvedData.location_id;
|
||||
if (resolvedData.location && !locationId) {
|
||||
locationId = await resolveLocationId(resolvedData.location);
|
||||
}
|
||||
|
||||
// Ensure unique slug
|
||||
const uniqueSlug = await ensureUniqueSlug(resolvedData.slug, 'parks');
|
||||
resolvedData.slug = uniqueSlug;
|
||||
@@ -341,7 +353,11 @@ async function createPark(data: any, dependencyMap: Map<string, string>): Promis
|
||||
const imageData = extractImageAssignments(resolvedData.images);
|
||||
|
||||
// Transform to database format
|
||||
const parkData = { ...transformParkData(resolvedData), ...imageData };
|
||||
const parkData = {
|
||||
...transformParkData(resolvedData),
|
||||
...imageData,
|
||||
location_id: locationId || null,
|
||||
};
|
||||
|
||||
// Insert into database
|
||||
const { data: park, error } = await supabase
|
||||
@@ -358,6 +374,51 @@ async function createPark(data: any, dependencyMap: Map<string, string>): Promis
|
||||
return park.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve location data to a location_id
|
||||
* Checks for existing locations by coordinates, creates new ones if needed
|
||||
*/
|
||||
async function resolveLocationId(locationData: any): Promise<string | null> {
|
||||
if (!locationData || !locationData.latitude || !locationData.longitude) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if location already exists by coordinates
|
||||
const { data: existingLocation } = await supabase
|
||||
.from('locations')
|
||||
.select('id')
|
||||
.eq('latitude', locationData.latitude)
|
||||
.eq('longitude', locationData.longitude)
|
||||
.maybeSingle();
|
||||
|
||||
if (existingLocation) {
|
||||
return existingLocation.id;
|
||||
}
|
||||
|
||||
// Create new location (moderator has permission via RLS)
|
||||
const { data: newLocation, error } = await supabase
|
||||
.from('locations')
|
||||
.insert({
|
||||
name: locationData.name,
|
||||
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,
|
||||
})
|
||||
.select('id')
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Error creating location:', error);
|
||||
throw new Error(`Failed to create location: ${error.message}`);
|
||||
}
|
||||
|
||||
return newLocation.id;
|
||||
}
|
||||
|
||||
async function createRide(data: any, dependencyMap: Map<string, string>): Promise<string> {
|
||||
const { transformRideData, validateSubmissionData } = await import('./entityTransformers');
|
||||
const { ensureUniqueSlug } = await import('./slugUtils');
|
||||
|
||||
Reference in New Issue
Block a user