|
|
@@ -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 { createHmac } from 'https://deno.land/std@0.168.0/node/crypto.ts'
|
|
|
import { wrapHandler, logError } from '../_shared/error-handler.ts'
|
|
|
+import { createScraperClient } from '../_shared/scraper-client.ts'
|
|
|
|
|
|
const corsHeaders = {
|
|
|
'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
|
|
|
async function testWooCommerceConnection(
|
|
|
storeUrl: string,
|
|
|
@@ -277,6 +332,11 @@ serve(wrapHandler('oauth-woocommerce', async (req) => {
|
|
|
|
|
|
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
|
|
|
const triggerSyncUrl = `${supabaseUrl}/functions/v1/trigger-sync`
|
|
|
fetch(triggerSyncUrl, {
|