Browse Source

fix: change ShopRenter API pagination to zero-based (page=0) #79

- ShopRenter API uses zero-based pagination (page=0, not page=1)
- Fixed pagination in shoprenter-sync for products, orders, and customers
- Fixed pagination in shoprenter-scheduled-sync for products
- Fixed pagination in mcp-shoprenter for order fetching
- Updated pagination logic: last page is now totalPages - 1 (zero-based)

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

Co-Authored-By: Claude <noreply@anthropic.com>
Claude 5 months ago
parent
commit
9cad341786

+ 2 - 2
supabase/functions/mcp-shoprenter/index.ts

@@ -196,10 +196,10 @@ async function fetchAllOrdersPages(
   filters?: ShopRenterOrderFilters
 ): Promise<any[]> {
   const allOrders: any[] = [];
-  let page = 1;
+  let page = 0;  // ShopRenter API uses zero-based pagination
   let hasMore = true;
 
-  while (hasMore && page <= maxPages) {
+  while (hasMore && page < maxPages) {
     const response = await fetchOrders(shop_id, page, 100, filters);
     const orders = Array.isArray(response) ? response : (response.data || response.orders || []);
 

+ 2 - 2
supabase/functions/shoprenter-scheduled-sync/index.ts

@@ -136,7 +136,7 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
         if (config?.sync_products !== false) {
           try {
             console.log(`[ShopRenter Scheduled Sync] Syncing products for store ${storeId}`)
-            let page = 1
+            let page = 0  // ShopRenter API uses zero-based pagination
             let hasMore = true
             const limit = 50
 
@@ -174,7 +174,7 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
                 // Check if there are more pages
                 if (productsData.pagination && productsData.pagination.total) {
                   const totalPages = Math.ceil(productsData.pagination.total / limit)
-                  hasMore = page < totalPages
+                  hasMore = page < totalPages - 1  // Zero-based, so last page is totalPages - 1
                 } else {
                   hasMore = productsData.items.length === limit
                 }

+ 6 - 6
supabase/functions/shoprenter-sync/index.ts

@@ -364,7 +364,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
         console.log('[ShopRenter] Product sync disabled by store permissions')
       } else {
         console.log('[ShopRenter] Syncing products...')
-        let page = 1
+        let page = 0  // ShopRenter API uses zero-based pagination
         let hasMore = true
         const limit = 50
 
@@ -405,7 +405,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
             // Check if there are more pages
             if (productsData.pagination && productsData.pagination.total) {
               const totalPages = Math.ceil(productsData.pagination.total / limit)
-              hasMore = page < totalPages
+              hasMore = page < totalPages - 1  // Zero-based, so last page is totalPages - 1
             } else {
               hasMore = productsData.items.length === limit
             }
@@ -432,7 +432,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
     // Sync Orders
     try {
       console.log('[ShopRenter] Syncing orders...')
-      let page = 1
+      let page = 0  // ShopRenter API uses zero-based pagination
       let hasMore = true
       const limit = 50
 
@@ -479,7 +479,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
           // Check if there are more pages
           if (ordersData.pagination && ordersData.pagination.total) {
             const totalPages = Math.ceil(ordersData.pagination.total / limit)
-            hasMore = page < totalPages
+            hasMore = page < totalPages - 1  // Zero-based, so last page is totalPages - 1
           } else {
             hasMore = ordersData.items.length === limit
           }
@@ -499,7 +499,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
     // Sync Customers
     try {
       console.log('[ShopRenter] Syncing customers...')
-      let page = 1
+      let page = 0  // ShopRenter API uses zero-based pagination
       let hasMore = true
       const limit = 50
 
@@ -542,7 +542,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
           // Check if there are more pages
           if (customersData.pagination && customersData.pagination.total) {
             const totalPages = Math.ceil(customersData.pagination.total / limit)
-            hasMore = page < totalPages
+            hasMore = page < totalPages - 1  // Zero-based, so last page is totalPages - 1
           } else {
             hasMore = customersData.items.length === limit
           }