Explorar el Código

fix: handle phone_numbers as object for many-to-one relationship #99

The Supabase client returns phone_numbers as a single object (not array)
for many-to-one foreign key relationships. Updated the API to handle
both object and array formats for backward compatibility.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Claude hace 5 meses
padre
commit
43d470eabe
Se han modificado 1 ficheros con 14 adiciones y 6 borrados
  1. 14 6
      supabase/functions/api/index.ts

+ 14 - 6
supabase/functions/api/index.ts

@@ -93,12 +93,20 @@ serve(async (req) => {
         )
       }
 
-      // Transform phone_numbers array to phone_number string for backward compatibility
-      const transformedStores = stores?.map(store => ({
-        ...store,
-        phone_number: store.phone_numbers?.[0]?.phone_number || null,
-        phone_number_details: store.phone_numbers?.[0] || null
-      })) || []
+      // Transform phone_numbers to phone_number string for backward compatibility
+      // Note: For many-to-one relationships, Supabase returns an object, not an array
+      const transformedStores = stores?.map(store => {
+        // Handle both object (many-to-one) and array (one-to-many) formats
+        const phoneData = Array.isArray(store.phone_numbers)
+          ? store.phone_numbers[0]
+          : store.phone_numbers;
+
+        return {
+          ...store,
+          phone_number: phoneData?.phone_number || null,
+          phone_number_details: phoneData || null
+        };
+      }) || []
 
       return new Response(
         JSON.stringify({