44 BlockUserDto ,
55 DeleteMessage ,
66 getBase64FromMediaMessageDto ,
7+ getCatalogDto ,
8+ getCollectionsDto ,
79 LastMessage ,
810 MarkChatUnreadDto ,
911 NumberBusiness ,
@@ -91,6 +93,7 @@ import makeWASocket, {
9193 BufferedEventData ,
9294 BufferJSON ,
9395 CacheStore ,
96+ CatalogCollection ,
9497 Chat ,
9598 ConnectionState ,
9699 Contact ,
@@ -100,6 +103,7 @@ import makeWASocket, {
100103 fetchLatestBaileysVersion ,
101104 generateWAMessageFromContent ,
102105 getAggregateVotesInPollMessage ,
106+ GetCatalogOptions ,
103107 getContentType ,
104108 getDevice ,
105109 GroupMetadata ,
@@ -113,6 +117,7 @@ import makeWASocket, {
113117 MiscMessageGenerationOptions ,
114118 ParticipantAction ,
115119 prepareWAMessageMedia ,
120+ Product ,
116121 proto ,
117122 UserFacingSocketConfig ,
118123 WABrowserDescription ,
@@ -4628,4 +4633,118 @@ export class BaileysStartupService extends ChannelStartupService {
46284633
46294634 return response ;
46304635 }
4636+
4637+ //Catalogs and collections
4638+ public async fetchCatalog ( instanceName : string , data : getCatalogDto ) {
4639+ const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4640+ const limit = data . limit || 10 ;
4641+ const cursor = data . cursor || null ;
4642+
4643+ const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4644+
4645+ if ( ! onWhatsapp . exists ) {
4646+ throw new BadRequestException ( onWhatsapp ) ;
4647+ }
4648+
4649+ try {
4650+ const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4651+ const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4652+ const catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor } ) ;
4653+
4654+ return {
4655+ wuid : info ?. jid || jid ,
4656+ name : info ?. name ,
4657+ numberExists : info ?. exists ,
4658+ isBusiness : business . isBusiness ,
4659+ catalogLength : catalog ?. products . length ,
4660+ catalog : catalog ?. products ,
4661+ } ;
4662+ } catch ( error ) {
4663+ console . log ( error ) ;
4664+ return {
4665+ wuid : jid ,
4666+ name : null ,
4667+ isBusiness : false ,
4668+ } ;
4669+ }
4670+ }
4671+
4672+ public async getCatalog ( {
4673+ jid,
4674+ limit,
4675+ cursor,
4676+ } : GetCatalogOptions ) : Promise < { products : Product [ ] ; nextPageCursor : string | undefined } > {
4677+ try {
4678+ jid = jid ? createJid ( jid ) : this . instance . wuid ;
4679+
4680+ const catalog = await this . client . getCatalog ( { jid, limit : limit , cursor : cursor } ) ;
4681+
4682+ if ( ! catalog ) {
4683+ return {
4684+ products : undefined ,
4685+ nextPageCursor : undefined ,
4686+ } ;
4687+ }
4688+
4689+ return catalog ;
4690+ } catch ( error ) {
4691+ throw new InternalServerErrorException ( 'Error getCatalog' , error . toString ( ) ) ;
4692+ }
4693+ }
4694+
4695+ public async fetchCatalogCollections ( instanceName : string , data : getCollectionsDto ) {
4696+ const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4697+ const limit = data . limit || 10 ;
4698+
4699+ const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4700+
4701+ if ( ! onWhatsapp . exists ) {
4702+ throw new BadRequestException ( onWhatsapp ) ;
4703+ }
4704+
4705+ try {
4706+ const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4707+ const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4708+ const catalogCollections = await this . getCollections ( info ?. jid , limit ) ;
4709+
4710+ return {
4711+ wuid : info ?. jid || jid ,
4712+ name : info ?. name ,
4713+ numberExists : info ?. exists ,
4714+ isBusiness : business . isBusiness ,
4715+ catalogLength : catalogCollections ?. length ,
4716+ catalogCollections : catalogCollections ,
4717+ } ;
4718+ } catch ( error ) {
4719+ console . log ( error ) ;
4720+ return {
4721+ wuid : jid ,
4722+ name : null ,
4723+ isBusiness : false ,
4724+ } ;
4725+ }
4726+ }
4727+
4728+ public async getCollections ( jid ?: string | undefined , limit ?: number ) : Promise < CatalogCollection [ ] > {
4729+ try {
4730+ jid = jid ? createJid ( jid ) : this . instance . wuid ;
4731+
4732+ const result = await this . client . getCollections ( jid , limit ) ;
4733+
4734+ if ( ! result ) {
4735+ return [
4736+ {
4737+ id : undefined ,
4738+ name : undefined ,
4739+ products : [ ] ,
4740+ status : undefined ,
4741+ } ,
4742+ ] ;
4743+ }
4744+
4745+ return result . collections ;
4746+ } catch ( error ) {
4747+ throw new InternalServerErrorException ( 'Error getCatalog' , error . toString ( ) ) ;
4748+ }
4749+ }
46314750}
0 commit comments