From 5a138688bc18952609c81358e603449f153a162a Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sun, 19 Oct 2025 17:37:47 +0000 Subject: [PATCH] Fix validation for edits --- .../functions/process-selective-approval/index.ts | 4 ++-- .../process-selective-approval/validation.ts | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/supabase/functions/process-selective-approval/index.ts b/supabase/functions/process-selective-approval/index.ts index 1ee2e77c..8816d805 100644 --- a/supabase/functions/process-selective-approval/index.ts +++ b/supabase/functions/process-selective-approval/index.ts @@ -286,8 +286,8 @@ serve(async (req) => { try { console.log(`Processing item ${item.id} of type ${item.item_type}`); - // Validate entity data with strict validation - const validation = validateEntityDataStrict(item.item_type, item.item_data); + // Validate entity data with strict validation, passing original_data for edits + const validation = validateEntityDataStrict(item.item_type, item.item_data, item.original_data); if (validation.blockingErrors.length > 0) { console.error(`❌ Blocking errors for item ${item.id}:`, validation.blockingErrors); diff --git a/supabase/functions/process-selective-approval/validation.ts b/supabase/functions/process-selective-approval/validation.ts index f8e6e3da..63176a32 100644 --- a/supabase/functions/process-selective-approval/validation.ts +++ b/supabase/functions/process-selective-approval/validation.ts @@ -20,7 +20,8 @@ export interface StrictValidationResult { */ export function validateEntityDataStrict( entityType: string, - data: any + data: any, + originalData?: any ): StrictValidationResult { const result: StrictValidationResult = { valid: true, @@ -78,7 +79,10 @@ export function validateEntityDataStrict( if (!data.status) { result.blockingErrors.push('Status is required'); } - if (data.location_id === null || data.location_id === undefined) { + // For edits, check if location exists in either new or original data + const hasLocation = data.location_id !== null && data.location_id !== undefined; + const hadLocation = originalData?.location_id !== null && originalData?.location_id !== undefined; + if (!hasLocation && !hadLocation) { result.blockingErrors.push('Location is required for parks'); } if (data.opening_date && data.closing_date) { @@ -97,7 +101,10 @@ export function validateEntityDataStrict( if (!data.status) { result.blockingErrors.push('Status is required'); } - if (data.park_id === null || data.park_id === undefined) { + // For edits, check if park exists in either new or original data + const hasPark = data.park_id !== null && data.park_id !== undefined; + const hadPark = originalData?.park_id !== null && originalData?.park_id !== undefined; + if (!hasPark && !hadPark) { result.blockingErrors.push('Park is required for rides'); } if (data.max_speed_kmh && (data.max_speed_kmh < 0 || data.max_speed_kmh > 300)) {