1+ import { getCollectionsDto } from '@api/dto/business.dto' ;
12import { OfferCallDto } from '@api/dto/call.dto' ;
23import {
34 ArchiveChatDto ,
45 BlockUserDto ,
56 DeleteMessage ,
67 getBase64FromMediaMessageDto ,
7- getCatalogDto ,
8- getCollectionsDto ,
98 LastMessage ,
109 MarkChatUnreadDto ,
1110 NumberBusiness ,
@@ -4634,11 +4633,11 @@ export class BaileysStartupService extends ChannelStartupService {
46344633 return response ;
46354634 }
46364635
4637- //Catalogs and collections
4638- public async fetchCatalog ( instanceName : string , data : getCatalogDto ) {
4636+ //Business Controller
4637+ public async fetchCatalog ( instanceName : string , data : getCollectionsDto ) {
46394638 const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
46404639 const limit = data . limit || 10 ;
4641- const cursor = data . cursor || null ;
4640+ const cursor = null ;
46424641
46434642 const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
46444643
@@ -4649,15 +4648,35 @@ export class BaileysStartupService extends ChannelStartupService {
46494648 try {
46504649 const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
46514650 const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4652- const catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor } ) ;
4651+
4652+ let catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor } ) ;
4653+ let nextPageCursor = catalog . nextPageCursor ;
4654+ let nextPageCursorJson = nextPageCursor ? JSON . parse ( atob ( nextPageCursor ) ) : null ;
4655+ let pagination = nextPageCursorJson ?. pagination_cursor
4656+ ? JSON . parse ( atob ( nextPageCursorJson . pagination_cursor ) )
4657+ : null ;
4658+ let fetcherHasMore = pagination ?. fetcher_has_more === true ? true : false ;
4659+
4660+ let productsCatalog = catalog . products || [ ] ;
4661+ let countLoops = 0 ;
4662+ while ( fetcherHasMore && countLoops < 4 ) {
4663+ catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor : nextPageCursor } ) ;
4664+ nextPageCursor = catalog . nextPageCursor ;
4665+ nextPageCursorJson = nextPageCursor ? JSON . parse ( atob ( nextPageCursor ) ) : null ;
4666+ pagination = nextPageCursorJson ?. pagination_cursor
4667+ ? JSON . parse ( atob ( nextPageCursorJson . pagination_cursor ) )
4668+ : null ;
4669+ fetcherHasMore = pagination ?. fetcher_has_more === true ? true : false ;
4670+ productsCatalog = [ ...productsCatalog , ...catalog . products ] ;
4671+ countLoops ++ ;
4672+ }
46534673
46544674 return {
46554675 wuid : info ?. jid || jid ,
4656- name : info ?. name ,
46574676 numberExists : info ?. exists ,
46584677 isBusiness : business . isBusiness ,
4659- catalogLength : catalog ?. products . length ,
4660- catalog : catalog ?. products ,
4678+ catalogLength : productsCatalog . length ,
4679+ catalog : productsCatalog ,
46614680 } ;
46624681 } catch ( error ) {
46634682 console . log ( error ) ;
@@ -4692,9 +4711,9 @@ export class BaileysStartupService extends ChannelStartupService {
46924711 }
46934712 }
46944713
4695- public async fetchCatalogCollections ( instanceName : string , data : getCollectionsDto ) {
4714+ public async fetchCollections ( instanceName : string , data : getCollectionsDto ) {
46964715 const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4697- const limit = data . limit || 10 ;
4716+ const limit = data . limit <= 20 ? data . limit : 20 ; //(tem esse limite, não sei porque)
46984717
46994718 const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
47004719
@@ -4705,18 +4724,17 @@ export class BaileysStartupService extends ChannelStartupService {
47054724 try {
47064725 const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
47074726 const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4708- const catalogCollections = await this . getCollections ( info ?. jid , limit ) ;
4727+ const collections = await this . getCollections ( info ?. jid , limit ) ;
47094728
47104729 return {
47114730 wuid : info ?. jid || jid ,
47124731 name : info ?. name ,
47134732 numberExists : info ?. exists ,
47144733 isBusiness : business . isBusiness ,
4715- catalogLength : catalogCollections ?. length ,
4716- catalogCollections : catalogCollections ,
4734+ collectionsLength : collections ?. length ,
4735+ collections : collections ,
47174736 } ;
47184737 } catch ( error ) {
4719- console . log ( error ) ;
47204738 return {
47214739 wuid : jid ,
47224740 name : null ,
0 commit comments