Refactor park submission location handling

This commit is contained in:
gpt-engineer-app[bot]
2025-11-05 20:46:02 +00:00
parent f9c11cb064
commit dfd17e8244
4 changed files with 66 additions and 1 deletions

View File

@@ -94,8 +94,9 @@ function validateEntityDataStrict(
result.blockingErrors.push('Status is required');
}
const hasLocation = data.location_id !== null && data.location_id !== undefined;
const hasTempLocation = data.temp_location_data !== null && data.temp_location_data !== undefined;
const hadLocation = originalData?.location_id !== null && originalData?.location_id !== undefined;
if (!hasLocation && !hadLocation) {
if (!hasLocation && !hasTempLocation && !hadLocation) {
result.blockingErrors.push('Location is required for parks');
}
if (hadLocation && data.location_id === null) {
@@ -1505,6 +1506,44 @@ async function createPark(supabase: any, data: any): Promise<string> {
const submitterId = data._submitter_id;
let uploadedPhotos: any[] = [];
// Create location if temp_location_data exists and location_id is missing
if (data.temp_location_data && !data.location_id) {
edgeLogger.info('Creating location from temp data', {
action: 'approval_create_location',
locationName: data.temp_location_data.name
});
const { data: newLocation, error: locationError } = await supabase
.from('locations')
.insert({
name: data.temp_location_data.name,
city: data.temp_location_data.city,
state_province: data.temp_location_data.state_province,
country: data.temp_location_data.country,
latitude: data.temp_location_data.latitude,
longitude: data.temp_location_data.longitude,
timezone: data.temp_location_data.timezone,
postal_code: data.temp_location_data.postal_code
})
.select('id')
.single();
if (locationError) {
throw new Error(`Failed to create location: ${locationError.message}`);
}
data.location_id = newLocation.id;
edgeLogger.info('Location created successfully', {
action: 'approval_location_created',
locationId: newLocation.id,
locationName: data.temp_location_data.name
});
}
// Clean up temp data
delete data.temp_location_data;
// Transform images object if present
if (data.images) {
const { uploaded, banner_assignment, card_assignment } = data.images;