|
@@ -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,
|