diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index 59c85e8f..61af39fe 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -2026,6 +2026,7 @@ export type Database = { slug: string status: string submission_id: string + temp_location_data: Json | null updated_at: string website_url: string | null } @@ -2051,6 +2052,7 @@ export type Database = { slug: string status?: string submission_id: string + temp_location_data?: Json | null updated_at?: string website_url?: string | null } @@ -2076,6 +2078,7 @@ export type Database = { slug?: string status?: string submission_id?: string + temp_location_data?: Json | null updated_at?: string website_url?: string | null } diff --git a/src/lib/entitySubmissionHelpers.ts b/src/lib/entitySubmissionHelpers.ts index 57ce80b8..f539d8b7 100644 --- a/src/lib/entitySubmissionHelpers.ts +++ b/src/lib/entitySubmissionHelpers.ts @@ -680,6 +680,17 @@ export async function submitParkCreation( operator_id: data.operator_id || null, property_owner_id: data.property_owner_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_id: bannerImage?.cloudflare_id || data.banner_image_id || null, card_image_url: cardImage?.url || data.card_image_url || null, diff --git a/supabase/functions/process-selective-approval/index.ts b/supabase/functions/process-selective-approval/index.ts index 5abb68d7..69a5d752 100644 --- a/supabase/functions/process-selective-approval/index.ts +++ b/supabase/functions/process-selective-approval/index.ts @@ -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 { 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; diff --git a/supabase/migrations/20251105204521_9d709e71-6555-40b1-acf5-b1b34c0c44ed.sql b/supabase/migrations/20251105204521_9d709e71-6555-40b1-acf5-b1b34c0c44ed.sql new file mode 100644 index 00000000..bca5b1c8 --- /dev/null +++ b/supabase/migrations/20251105204521_9d709e71-6555-40b1-acf5-b1b34c0c44ed.sql @@ -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; \ No newline at end of file