Browse Source

fix(scraper): improve custom-urls handler with shop verification and PATCH support

- Add shop existence verification before custom URL operations
- Support PATCH without /toggle suffix for enable/disable
- Add debug logging for troubleshooting
- Use consistent store_id variable in PATCH and DELETE handlers

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

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh 4 months ago
parent
commit
e391077e3e
1 changed files with 52 additions and 3 deletions
  1. 52 3
      supabase/functions/scraper-management/index.ts

+ 52 - 3
supabase/functions/scraper-management/index.ts

@@ -403,6 +403,29 @@ Deno.serve(async (req) => {
             scraper_api_secret: store.scraper_api_secret,
           });
 
+          // Log the shop ID being used for debugging
+          console.log(`[scraper-management] custom-urls: Using shop ID: ${store.id} for store: ${store.store_url}`);
+
+          // Verify shop exists in scraper before proceeding
+          try {
+            const shopData = await scraperClient.getShop(store.id);
+            console.log(`[scraper-management] custom-urls: Shop found in scraper:`, {
+              scraperShopId: shopData.shop?.id,
+              customId: shopData.shop?.custom_id,
+              storeId: store.id
+            });
+          } catch (shopError) {
+            console.error(`[scraper-management] custom-urls: Shop not found in scraper for store ${store.id}:`, shopError);
+            return new Response(
+              JSON.stringify({
+                error: 'Shop not found in scraper',
+                message: 'The store may not be properly registered with the scraper. Try re-registering the shop.',
+                store_id: store.id
+              }),
+              { status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+            );
+          }
+
           if (req.method === 'GET') {
             // List custom URLs
             const customUrls = await scraperClient.listCustomUrls(store.id);
@@ -484,8 +507,32 @@ Deno.serve(async (req) => {
             scraper_api_secret: store.scraper_api_secret,
           });
 
-          if (req.method === 'PATCH' && subAction === 'toggle') {
+          // Log the shop ID being used for debugging
+          console.log(`[scraper-management] custom-urls PATCH/DELETE: Using shop ID: ${store_id}, customUrlId: ${customUrlId}`);
+
+          // Verify shop exists in scraper before proceeding
+          try {
+            const shopData = await scraperClient.getShop(store_id);
+            console.log(`[scraper-management] custom-urls PATCH/DELETE: Shop found in scraper:`, {
+              scraperShopId: shopData.shop?.id,
+              customId: shopData.shop?.custom_id,
+              storeId: store_id
+            });
+          } catch (shopError) {
+            console.error(`[scraper-management] custom-urls PATCH/DELETE: Shop not found in scraper for store ${store_id}:`, shopError);
+            return new Response(
+              JSON.stringify({
+                error: 'Shop not found in scraper',
+                message: 'The store may not be properly registered with the scraper. Try re-registering the shop.',
+                store_id: store_id
+              }),
+              { status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+            );
+          }
+
+          if (req.method === 'PATCH') {
             // Toggle custom URL enabled status
+            // Support both PATCH /custom-urls/:id and PATCH /custom-urls/:id/toggle
             if (typeof enabled !== 'boolean') {
               return new Response(
                 JSON.stringify({ error: 'enabled (boolean) is required' }),
@@ -493,7 +540,8 @@ Deno.serve(async (req) => {
               );
             }
 
-            await scraperClient.setCustomUrlEnabled(store.id, customUrlId, enabled);
+            console.log(`[scraper-management] Toggling custom URL ${customUrlId} for store ${store_id} to enabled=${enabled}`);
+            await scraperClient.setCustomUrlEnabled(store_id, customUrlId, enabled);
 
             return new Response(
               JSON.stringify({ success: true, message: `Custom URL ${enabled ? 'enabled' : 'disabled'}` }),
@@ -502,7 +550,8 @@ Deno.serve(async (req) => {
 
           } else if (req.method === 'DELETE') {
             // Delete custom URL
-            await scraperClient.deleteCustomUrl(store.id, customUrlId);
+            console.log(`[scraper-management] Deleting custom URL ${customUrlId} for store ${store_id}`);
+            await scraperClient.deleteCustomUrl(store_id, customUrlId);
 
             return new Response(
               JSON.stringify({ success: true, message: 'Custom URL deleted successfully' }),