|
@@ -1250,19 +1250,19 @@ serve(async (req) => {
|
|
|
try {
|
|
try {
|
|
|
const platform = store.platform_name
|
|
const platform = store.platform_name
|
|
|
|
|
|
|
|
- // Use the view with computed exclusion status
|
|
|
|
|
- const viewName = platform === 'woocommerce'
|
|
|
|
|
- ? 'woocommerce_products_with_exclusion'
|
|
|
|
|
|
|
+ // Use the cache table directly
|
|
|
|
|
+ const tableName = platform === 'woocommerce'
|
|
|
|
|
+ ? 'woocommerce_products_cache'
|
|
|
: platform === 'shopify'
|
|
: platform === 'shopify'
|
|
|
- ? 'shopify_products_with_exclusion'
|
|
|
|
|
- : 'shoprenter_products_with_exclusion'
|
|
|
|
|
|
|
+ ? 'shopify_products_cache'
|
|
|
|
|
+ : 'shoprenter_products_cache'
|
|
|
|
|
|
|
|
// Build query
|
|
// Build query
|
|
|
let query = supabase
|
|
let query = supabase
|
|
|
- .from(viewName)
|
|
|
|
|
|
|
+ .from(tableName)
|
|
|
.select('*')
|
|
.select('*')
|
|
|
.eq('store_id', storeId)
|
|
.eq('store_id', storeId)
|
|
|
- .order('created_at', { ascending: false })
|
|
|
|
|
|
|
+ .order('last_synced_at', { ascending: false })
|
|
|
|
|
|
|
|
// Apply search filter
|
|
// Apply search filter
|
|
|
if (search) {
|
|
if (search) {
|
|
@@ -1298,19 +1298,42 @@ serve(async (req) => {
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Get excluded categories for this store
|
|
|
|
|
+ const { data: excludedCategories } = await supabase
|
|
|
|
|
+ .from('excluded_categories')
|
|
|
|
|
+ .select('category_id')
|
|
|
|
|
+ .eq('store_id', storeId)
|
|
|
|
|
+ .eq('platform', platform)
|
|
|
|
|
+
|
|
|
|
|
+ const excludedCategoryIds = new Set(excludedCategories?.map(ec => ec.category_id) || [])
|
|
|
|
|
+
|
|
|
|
|
+ // Check if product is excluded by category
|
|
|
|
|
+ const isExcludedByCategory = (categories: any[]): boolean => {
|
|
|
|
|
+ if (!categories || categories.length === 0) return false
|
|
|
|
|
+ return categories.some(cat => {
|
|
|
|
|
+ const catId = cat.id || cat.category_id || cat
|
|
|
|
|
+ return excludedCategoryIds.has(catId.toString())
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Transform to response format
|
|
// Transform to response format
|
|
|
- let results = products.map(p => ({
|
|
|
|
|
- id: p[platform === 'woocommerce' ? 'wc_product_id' : platform === 'shopify' ? 'shopify_product_id' : 'shoprenter_product_id'],
|
|
|
|
|
- name: p.name || '',
|
|
|
|
|
- sku: p.sku || '',
|
|
|
|
|
- price: p.price || '0',
|
|
|
|
|
- currency: p.currency || 'USD',
|
|
|
|
|
- categories: p.categories || [],
|
|
|
|
|
- enabled_in_context: !p.effectively_excluded, // Inverted logic: enabled = NOT excluded
|
|
|
|
|
- excluded_by_individual: p.excluded,
|
|
|
|
|
- excluded_by_category: p.exclusion_reason === 'category',
|
|
|
|
|
- exclusion_reason: p.exclusion_reason
|
|
|
|
|
- }))
|
|
|
|
|
|
|
+ let results = products.map(p => {
|
|
|
|
|
+ const excludedByCategory = isExcludedByCategory(p.categories || [])
|
|
|
|
|
+ const excludedByIndividual = p.excluded === true
|
|
|
|
|
+ const effectivelyExcluded = excludedByIndividual || excludedByCategory
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ id: p.product_id,
|
|
|
|
|
+ inner_id: p.inner_id, // For ShopRenter
|
|
|
|
|
+ name: p.name || '',
|
|
|
|
|
+ sku: p.sku || '',
|
|
|
|
|
+ categories: p.categories || [],
|
|
|
|
|
+ enabled_in_context: !effectivelyExcluded, // Inverted logic: enabled = NOT excluded
|
|
|
|
|
+ excluded_by_individual: excludedByIndividual,
|
|
|
|
|
+ excluded_by_category: excludedByCategory,
|
|
|
|
|
+ exclusion_reason: excludedByIndividual ? 'individual' : excludedByCategory ? 'category' : null
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
// Apply enabled filter if specified
|
|
// Apply enabled filter if specified
|
|
|
if (enabledFilter !== null) {
|
|
if (enabledFilter !== null) {
|
|
@@ -1383,22 +1406,15 @@ serve(async (req) => {
|
|
|
? 'shopify_products_cache'
|
|
? 'shopify_products_cache'
|
|
|
: 'shoprenter_products_cache'
|
|
: 'shoprenter_products_cache'
|
|
|
|
|
|
|
|
- const idColumn = platform === 'woocommerce'
|
|
|
|
|
- ? 'wc_product_id'
|
|
|
|
|
- : platform === 'shopify'
|
|
|
|
|
- ? 'shopify_product_id'
|
|
|
|
|
- : 'shoprenter_product_id'
|
|
|
|
|
-
|
|
|
|
|
// Update excluded column directly in cache table
|
|
// Update excluded column directly in cache table
|
|
|
// Note: enabled in UI = NOT excluded in DB
|
|
// Note: enabled in UI = NOT excluded in DB
|
|
|
const { error: updateError } = await supabase
|
|
const { error: updateError } = await supabase
|
|
|
.from(tableName)
|
|
.from(tableName)
|
|
|
.update({
|
|
.update({
|
|
|
- excluded: !enabled, // Inverted logic
|
|
|
|
|
- updated_at: new Date().toISOString()
|
|
|
|
|
|
|
+ excluded: !enabled // Inverted logic
|
|
|
})
|
|
})
|
|
|
.eq('store_id', store_id)
|
|
.eq('store_id', store_id)
|
|
|
- .eq(idColumn, itemId)
|
|
|
|
|
|
|
+ .eq('product_id', itemId)
|
|
|
|
|
|
|
|
if (updateError) {
|
|
if (updateError) {
|
|
|
console.error('Error updating product:', updateError)
|
|
console.error('Error updating product:', updateError)
|
|
@@ -1460,22 +1476,15 @@ serve(async (req) => {
|
|
|
? 'shopify_products_cache'
|
|
? 'shopify_products_cache'
|
|
|
: 'shoprenter_products_cache'
|
|
: 'shoprenter_products_cache'
|
|
|
|
|
|
|
|
- const idColumn = platform === 'woocommerce'
|
|
|
|
|
- ? 'wc_product_id'
|
|
|
|
|
- : platform === 'shopify'
|
|
|
|
|
- ? 'shopify_product_id'
|
|
|
|
|
- : 'shoprenter_product_id'
|
|
|
|
|
-
|
|
|
|
|
// Update all products in bulk
|
|
// Update all products in bulk
|
|
|
for (const itemId of item_ids) {
|
|
for (const itemId of item_ids) {
|
|
|
await supabase
|
|
await supabase
|
|
|
.from(tableName)
|
|
.from(tableName)
|
|
|
.update({
|
|
.update({
|
|
|
- excluded: !enabled, // Inverted logic
|
|
|
|
|
- updated_at: new Date().toISOString()
|
|
|
|
|
|
|
+ excluded: !enabled // Inverted logic
|
|
|
})
|
|
})
|
|
|
.eq('store_id', store_id)
|
|
.eq('store_id', store_id)
|
|
|
- .eq(idColumn, itemId)
|
|
|
|
|
|
|
+ .eq('product_id', itemId)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return new Response(
|
|
return new Response(
|
|
@@ -1534,8 +1543,7 @@ serve(async (req) => {
|
|
|
const { error: updateError } = await supabase
|
|
const { error: updateError } = await supabase
|
|
|
.from(tableName)
|
|
.from(tableName)
|
|
|
.update({
|
|
.update({
|
|
|
- excluded: false,
|
|
|
|
|
- updated_at: new Date().toISOString()
|
|
|
|
|
|
|
+ excluded: false
|
|
|
})
|
|
})
|
|
|
.eq('store_id', store_id)
|
|
.eq('store_id', store_id)
|
|
|
|
|
|
|
@@ -1610,8 +1618,7 @@ serve(async (req) => {
|
|
|
const { error: updateError } = await supabase
|
|
const { error: updateError } = await supabase
|
|
|
.from(tableName)
|
|
.from(tableName)
|
|
|
.update({
|
|
.update({
|
|
|
- excluded: true,
|
|
|
|
|
- updated_at: new Date().toISOString()
|
|
|
|
|
|
|
+ excluded: true
|
|
|
})
|
|
})
|
|
|
.eq('store_id', store_id)
|
|
.eq('store_id', store_id)
|
|
|
|
|
|