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,36 @@ 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+ console . log ( catalog ) ;
4654+ let nextPageCursor = catalog . nextPageCursor ;
4655+ let nextPageCursorJson = nextPageCursor ? JSON . parse ( atob ( nextPageCursor ) ) : null ;
4656+ let pagination = nextPageCursorJson ?. pagination_cursor
4657+ ? JSON . parse ( atob ( nextPageCursorJson . pagination_cursor ) )
4658+ : null ;
4659+ let fetcherHasMore = pagination ?. fetcher_has_more === true ? true : false ;
4660+
4661+ let productsCatalog = catalog . products || [ ] ;
4662+ let countLoops = 0 ;
4663+ while ( fetcherHasMore && countLoops < 4 ) {
4664+ catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor : nextPageCursor } ) ;
4665+ nextPageCursor = catalog . nextPageCursor ;
4666+ nextPageCursorJson = nextPageCursor ? JSON . parse ( atob ( nextPageCursor ) ) : null ;
4667+ pagination = nextPageCursorJson ?. pagination_cursor
4668+ ? JSON . parse ( atob ( nextPageCursorJson . pagination_cursor ) )
4669+ : null ;
4670+ fetcherHasMore = pagination ?. fetcher_has_more === true ? true : false ;
4671+ productsCatalog = [ ...productsCatalog , ...catalog . products ] ;
4672+ countLoops ++ ;
4673+ }
46534674
46544675 return {
46554676 wuid : info ?. jid || jid ,
4656- name : info ?. name ,
46574677 numberExists : info ?. exists ,
46584678 isBusiness : business . isBusiness ,
4659- catalogLength : catalog ?. products . length ,
4660- catalog : catalog ?. products ,
4679+ catalogLength : productsCatalog . length ,
4680+ catalog : productsCatalog ,
46614681 } ;
46624682 } catch ( error ) {
46634683 console . log ( error ) ;
@@ -4692,9 +4712,9 @@ export class BaileysStartupService extends ChannelStartupService {
46924712 }
46934713 }
46944714
4695- public async fetchCatalogCollections ( instanceName : string , data : getCollectionsDto ) {
4715+ public async fetchCollections ( instanceName : string , data : getCollectionsDto ) {
46964716 const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4697- const limit = data . limit || 10 ;
4717+ const limit = data . limit <= 20 ? data . limit : 20 ; //(tem esse limite, não sei porque)
46984718
46994719 const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
47004720
@@ -4705,18 +4725,17 @@ export class BaileysStartupService extends ChannelStartupService {
47054725 try {
47064726 const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
47074727 const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4708- const catalogCollections = await this . getCollections ( info ?. jid , limit ) ;
4728+ const collections = await this . getCollections ( info ?. jid , limit ) ;
47094729
47104730 return {
47114731 wuid : info ?. jid || jid ,
47124732 name : info ?. name ,
47134733 numberExists : info ?. exists ,
47144734 isBusiness : business . isBusiness ,
4715- catalogLength : catalogCollections ?. length ,
4716- catalogCollections : catalogCollections ,
4735+ collectionsLength : collections ?. length ,
4736+ collections : collections ,
47174737 } ;
47184738 } catch ( error ) {
4719- console . log ( error ) ;
47204739 return {
47214740 wuid : jid ,
47224741 name : null ,
0 commit comments