Преглед изворни кода

feat: integrate scraper registration into all store connection flows

- Add automatic scraper registration to Shopify OAuth flow
- Add automatic scraper registration to WooCommerce OAuth flow
- Add automatic scraper registration to ShopRenter installation completion
- All new stores are now automatically registered with scraper service
- Include webhook setup and scheduling enablement
- Background registration to not block OAuth flows
- Comprehensive error handling with non-blocking scraper failures
Fszontagh пре 4 месеци
родитељ
комит
83fcd19f93

+ 60 - 0
supabase/functions/complete-shoprenter-install/index.ts

@@ -1,12 +1,67 @@
 import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
 import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
 import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
 import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
 import { wrapHandler } from '../_shared/error-handler.ts'
 import { wrapHandler } from '../_shared/error-handler.ts'
+import { createScraperClient } from '../_shared/scraper-client.ts'
 
 
 const corsHeaders = {
 const corsHeaders = {
   'Access-Control-Allow-Origin': '*',
   'Access-Control-Allow-Origin': '*',
   'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
   'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
 }
 }
 
 
+// Register store with scraper service
+async function registerStoreWithScraper(
+  storeId: string,
+  storeUrl: string,
+  supabase: any
+): Promise<void> {
+  try {
+    console.log(`[ShopRenter] Registering store ${storeId} with scraper service`)
+
+    // Create scraper client with global configuration
+    const scraperClient = await createScraperClient()
+
+    // Register shop with scraper using store_id as custom_id
+    const job = await scraperClient.registerShop(storeUrl, storeId)
+    console.log(`[ShopRenter] Scraper registration job created: ${job.id}`)
+
+    // Set up webhook to receive scraper status updates
+    const webhookUrl = `${Deno.env.get('SUPABASE_URL')}/functions/v1/scraper-webhook`
+    try {
+      await scraperClient.setWebhook(storeId, webhookUrl)
+      console.log(`[ShopRenter] Scraper webhook configured for store ${storeId}`)
+    } catch (webhookError) {
+      console.warn(`[ShopRenter] Failed to configure scraper webhook:`, webhookError)
+      // Continue anyway - webhook is optional
+    }
+
+    // Enable scheduled scraping
+    try {
+      await scraperClient.setScheduling(storeId, true)
+      console.log(`[ShopRenter] Scraper scheduling enabled for store ${storeId}`)
+    } catch (scheduleError) {
+      console.warn(`[ShopRenter] Failed to enable scraper scheduling:`, scheduleError)
+      // Continue anyway
+    }
+
+    // Update database to mark as registered
+    const { error: updateError } = await supabase
+      .from('stores')
+      .update({ scraper_registered: true })
+      .eq('id', storeId)
+
+    if (updateError) {
+      console.error(`[ShopRenter] Failed to update scraper registration status:`, updateError)
+      // Continue anyway - this is not critical for the OAuth flow
+    } else {
+      console.log(`[ShopRenter] Store ${storeId} successfully registered with scraper`)
+    }
+
+  } catch (error) {
+    console.error(`[ShopRenter] Failed to register store with scraper:`, error)
+    // Don't throw error - scraper registration should not fail the OAuth flow
+  }
+}
+
 serve(wrapHandler('complete-shoprenter-install', async (req) => {
 serve(wrapHandler('complete-shoprenter-install', async (req) => {
   if (req.method === 'OPTIONS') {
   if (req.method === 'OPTIONS') {
     return new Response('ok', { headers: corsHeaders })
     return new Response('ok', { headers: corsHeaders })
@@ -219,6 +274,11 @@ serve(wrapHandler('complete-shoprenter-install', async (req) => {
 
 
     console.log(`[ShopRenter] Created new store ${newStore.id} for ${pendingInstall.shopname}`)
     console.log(`[ShopRenter] Created new store ${newStore.id} for ${pendingInstall.shopname}`)
 
 
+    // Register store with scraper service in background
+    registerStoreWithScraper(newStore.id, `https://${pendingInstall.shopname}.myshoprenter.hu`, supabase)
+      .then(() => console.log(`[ShopRenter] Scraper registration completed for store ${newStore.id}`))
+      .catch(err => console.error(`[ShopRenter] Scraper registration failed:`, err))
+
     return new Response(
     return new Response(
       JSON.stringify({
       JSON.stringify({
         success: true,
         success: true,

+ 60 - 0
supabase/functions/oauth-shopify/index.ts

@@ -2,6 +2,7 @@ import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
 import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
 import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
 import { createHmac } from 'https://deno.land/std@0.168.0/node/crypto.ts'
 import { createHmac } from 'https://deno.land/std@0.168.0/node/crypto.ts'
 import { wrapHandler, logError } from '../_shared/error-handler.ts'
 import { wrapHandler, logError } from '../_shared/error-handler.ts'
+import { createScraperClient } from '../_shared/scraper-client.ts'
 
 
 const corsHeaders = {
 const corsHeaders = {
   'Access-Control-Allow-Origin': '*',
   'Access-Control-Allow-Origin': '*',
@@ -102,6 +103,60 @@ async function exchangeCodeForToken(
   }
   }
 }
 }
 
 
+// Register store with scraper service
+async function registerStoreWithScraper(
+  storeId: string,
+  storeUrl: string,
+  supabase: any
+): Promise<void> {
+  try {
+    console.log(`[Shopify] Registering store ${storeId} with scraper service`)
+
+    // Create scraper client with global configuration
+    const scraperClient = await createScraperClient()
+
+    // Register shop with scraper using store_id as custom_id
+    const job = await scraperClient.registerShop(storeUrl, storeId)
+    console.log(`[Shopify] Scraper registration job created: ${job.id}`)
+
+    // Set up webhook to receive scraper status updates
+    const webhookUrl = `${Deno.env.get('SUPABASE_URL')}/functions/v1/scraper-webhook`
+    try {
+      await scraperClient.setWebhook(storeId, webhookUrl)
+      console.log(`[Shopify] Scraper webhook configured for store ${storeId}`)
+    } catch (webhookError) {
+      console.warn(`[Shopify] Failed to configure scraper webhook:`, webhookError)
+      // Continue anyway - webhook is optional
+    }
+
+    // Enable scheduled scraping
+    try {
+      await scraperClient.setScheduling(storeId, true)
+      console.log(`[Shopify] Scraper scheduling enabled for store ${storeId}`)
+    } catch (scheduleError) {
+      console.warn(`[Shopify] Failed to enable scraper scheduling:`, scheduleError)
+      // Continue anyway
+    }
+
+    // Update database to mark as registered
+    const { error: updateError } = await supabase
+      .from('stores')
+      .update({ scraper_registered: true })
+      .eq('id', storeId)
+
+    if (updateError) {
+      console.error(`[Shopify] Failed to update scraper registration status:`, updateError)
+      // Continue anyway - this is not critical for the OAuth flow
+    } else {
+      console.log(`[Shopify] Store ${storeId} successfully registered with scraper`)
+    }
+
+  } catch (error) {
+    console.error(`[Shopify] Failed to register store with scraper:`, error)
+    // Don't throw error - scraper registration should not fail the OAuth flow
+  }
+}
+
 // Test Shopify API connection
 // Test Shopify API connection
 async function testShopifyConnection(
 async function testShopifyConnection(
   shop: string,
   shop: string,
@@ -506,6 +561,11 @@ serve(wrapHandler('oauth-shopify', async (req) => {
 
 
       console.log(`[Shopify] Store connected successfully: ${storeName} (${shopDomain}) with phone number ${phoneNumberId}`)
       console.log(`[Shopify] Store connected successfully: ${storeName} (${shopDomain}) with phone number ${phoneNumberId}`)
 
 
+      // Register store with scraper service in background
+      registerStoreWithScraper(insertedStore.id, `https://${shopDomain}`, supabaseAdmin)
+        .then(() => console.log(`[Shopify] Scraper registration completed for store ${insertedStore.id}`))
+        .catch(err => console.error(`[Shopify] Scraper registration failed:`, err))
+
       // Trigger auto-sync in background
       // Trigger auto-sync in background
       const triggerSyncUrl = `${supabaseUrl}/functions/v1/trigger-sync`
       const triggerSyncUrl = `${supabaseUrl}/functions/v1/trigger-sync`
       fetch(triggerSyncUrl, {
       fetch(triggerSyncUrl, {

+ 60 - 0
supabase/functions/oauth-woocommerce/index.ts

@@ -2,6 +2,7 @@ import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
 import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
 import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
 import { createHmac } from 'https://deno.land/std@0.168.0/node/crypto.ts'
 import { createHmac } from 'https://deno.land/std@0.168.0/node/crypto.ts'
 import { wrapHandler, logError } from '../_shared/error-handler.ts'
 import { wrapHandler, logError } from '../_shared/error-handler.ts'
+import { createScraperClient } from '../_shared/scraper-client.ts'
 
 
 const corsHeaders = {
 const corsHeaders = {
   'Access-Control-Allow-Origin': '*',
   'Access-Control-Allow-Origin': '*',
@@ -65,6 +66,60 @@ function validateStoreUrl(storeUrl: string): { valid: boolean; error?: string; n
   }
   }
 }
 }
 
 
+// Register store with scraper service
+async function registerStoreWithScraper(
+  storeId: string,
+  storeUrl: string,
+  supabase: any
+): Promise<void> {
+  try {
+    console.log(`[WooCommerce] Registering store ${storeId} with scraper service`)
+
+    // Create scraper client with global configuration
+    const scraperClient = await createScraperClient()
+
+    // Register shop with scraper using store_id as custom_id
+    const job = await scraperClient.registerShop(storeUrl, storeId)
+    console.log(`[WooCommerce] Scraper registration job created: ${job.id}`)
+
+    // Set up webhook to receive scraper status updates
+    const webhookUrl = `${Deno.env.get('SUPABASE_URL')}/functions/v1/scraper-webhook`
+    try {
+      await scraperClient.setWebhook(storeId, webhookUrl)
+      console.log(`[WooCommerce] Scraper webhook configured for store ${storeId}`)
+    } catch (webhookError) {
+      console.warn(`[WooCommerce] Failed to configure scraper webhook:`, webhookError)
+      // Continue anyway - webhook is optional
+    }
+
+    // Enable scheduled scraping
+    try {
+      await scraperClient.setScheduling(storeId, true)
+      console.log(`[WooCommerce] Scraper scheduling enabled for store ${storeId}`)
+    } catch (scheduleError) {
+      console.warn(`[WooCommerce] Failed to enable scraper scheduling:`, scheduleError)
+      // Continue anyway
+    }
+
+    // Update database to mark as registered
+    const { error: updateError } = await supabase
+      .from('stores')
+      .update({ scraper_registered: true })
+      .eq('id', storeId)
+
+    if (updateError) {
+      console.error(`[WooCommerce] Failed to update scraper registration status:`, updateError)
+      // Continue anyway - this is not critical for the OAuth flow
+    } else {
+      console.log(`[WooCommerce] Store ${storeId} successfully registered with scraper`)
+    }
+
+  } catch (error) {
+    console.error(`[WooCommerce] Failed to register store with scraper:`, error)
+    // Don't throw error - scraper registration should not fail the OAuth flow
+  }
+}
+
 // Test WooCommerce API connection
 // Test WooCommerce API connection
 async function testWooCommerceConnection(
 async function testWooCommerceConnection(
   storeUrl: string,
   storeUrl: string,
@@ -277,6 +332,11 @@ serve(wrapHandler('oauth-woocommerce', async (req) => {
 
 
       console.log(`[WooCommerce] Store connected successfully (manual): ${storeName}`)
       console.log(`[WooCommerce] Store connected successfully (manual): ${storeName}`)
 
 
+      // Register store with scraper service in background
+      registerStoreWithScraper(insertedStore.id, validation.normalized, supabaseAdmin)
+        .then(() => console.log(`[WooCommerce] Scraper registration completed for store ${insertedStore.id}`))
+        .catch(err => console.error(`[WooCommerce] Scraper registration failed:`, err))
+
       // Trigger auto-sync in background
       // Trigger auto-sync in background
       const triggerSyncUrl = `${supabaseUrl}/functions/v1/trigger-sync`
       const triggerSyncUrl = `${supabaseUrl}/functions/v1/trigger-sync`
       fetch(triggerSyncUrl, {
       fetch(triggerSyncUrl, {