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

@@ -2026,6 +2026,7 @@ export type Database = {
slug: string slug: string
status: string status: string
submission_id: string submission_id: string
temp_location_data: Json | null
updated_at: string updated_at: string
website_url: string | null website_url: string | null
} }
@@ -2051,6 +2052,7 @@ export type Database = {
slug: string slug: string
status?: string status?: string
submission_id: string submission_id: string
temp_location_data?: Json | null
updated_at?: string updated_at?: string
website_url?: string | null website_url?: string | null
} }
@@ -2076,6 +2078,7 @@ export type Database = {
slug?: string slug?: string
status?: string status?: string
submission_id?: string submission_id?: string
temp_location_data?: Json | null
updated_at?: string updated_at?: string
website_url?: string | null website_url?: string | null
} }

View File

@@ -680,6 +680,17 @@ export async function submitParkCreation(
operator_id: data.operator_id || null, operator_id: data.operator_id || null,
property_owner_id: data.property_owner_id || null, property_owner_id: data.property_owner_id || null,
location_id: data.location_id || null, location_id: data.location_id || null,
temp_location_data: data.location ? {
name: data.location.name,
city: data.location.city || null,
state_province: data.location.state_province || null,
country: data.location.country,
latitude: data.location.latitude,
longitude: data.location.longitude,
timezone: data.location.timezone || null,
postal_code: data.location.postal_code || null,
display_name: data.location.display_name
} : null,
banner_image_url: bannerImage?.url || data.banner_image_url || null, banner_image_url: bannerImage?.url || data.banner_image_url || null,
banner_image_id: bannerImage?.cloudflare_id || data.banner_image_id || null, banner_image_id: bannerImage?.cloudflare_id || data.banner_image_id || null,
card_image_url: cardImage?.url || data.card_image_url || null, card_image_url: cardImage?.url || data.card_image_url || null,

View File

@@ -94,8 +94,9 @@ function validateEntityDataStrict(
result.blockingErrors.push('Status is required'); result.blockingErrors.push('Status is required');
} }
const hasLocation = data.location_id !== null && data.location_id !== undefined; 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; 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'); result.blockingErrors.push('Location is required for parks');
} }
if (hadLocation && data.location_id === null) { if (hadLocation && data.location_id === null) {
@@ -1505,6 +1506,44 @@ async function createPark(supabase: any, data: any): Promise<string> {
const submitterId = data._submitter_id; const submitterId = data._submitter_id;
let uploadedPhotos: any[] = []; 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 // Transform images object if present
if (data.images) { if (data.images) {
const { uploaded, banner_assignment, card_assignment } = data.images; const { uploaded, banner_assignment, card_assignment } = data.images;

View File

@@ -0,0 +1,12 @@
-- Add temporary location storage to park_submissions
-- This stores location data until approval, when it will be created in locations table
ALTER TABLE park_submissions
ADD COLUMN temp_location_data JSONB;
COMMENT ON COLUMN park_submissions.temp_location_data IS
'Temporary storage for location data before approval. Contains: name, city, state_province, country, latitude, longitude, timezone, postal_code, display_name';
-- Add index for querying submissions with temp location data
CREATE INDEX idx_park_submissions_temp_location ON park_submissions (submission_id)
WHERE temp_location_data IS NOT NULL;