@@ -13,6 +13,8 @@ import {
1313 SendPresenceDto ,
1414 UpdateMessageDto ,
1515 WhatsAppNumberDto ,
16+ getCatalogDto ,
17+ getCollectionsDto ,
1618} from '@api/dto/chat.dto' ;
1719import {
1820 AcceptGroupInvite ,
@@ -117,6 +119,9 @@ import makeWASocket, {
117119 WAMessageUpdate ,
118120 WAPresence ,
119121 WASocket ,
122+ Product ,
123+ GetCatalogOptions ,
124+ CatalogCollection ,
120125} from 'baileys' ;
121126import { Label } from 'baileys/lib/Types/Label' ;
122127import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation' ;
@@ -4017,4 +4022,118 @@ export class BaileysStartupService extends ChannelStartupService {
40174022
40184023 return response ;
40194024 }
4025+
4026+ //Catalogs and collections
4027+ public async fetchCatalog ( instanceName : string , data : getCatalogDto ) {
4028+ const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4029+ const limit = data . limit || 10 ;
4030+ const cursor = data . cursor || null ;
4031+
4032+ const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4033+
4034+ if ( ! onWhatsapp . exists ) {
4035+ throw new BadRequestException ( onWhatsapp ) ;
4036+ }
4037+
4038+ try {
4039+ const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4040+ const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4041+ const catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor } ) ;
4042+
4043+ return {
4044+ wuid : info ?. jid || jid ,
4045+ name : info ?. name ,
4046+ numberExists : info ?. exists ,
4047+ isBusiness : business . isBusiness ,
4048+ catalogLength : catalog ?. products . length ,
4049+ catalog : catalog ?. products ,
4050+ } ;
4051+ } catch ( error ) {
4052+ console . log ( error ) ;
4053+ return {
4054+ wuid : jid ,
4055+ name : null ,
4056+ isBusiness : false ,
4057+ } ;
4058+ }
4059+ }
4060+
4061+ public async getCatalog ( {
4062+ jid,
4063+ limit,
4064+ cursor,
4065+ } : GetCatalogOptions ) : Promise < { products : Product [ ] ; nextPageCursor : string | undefined } > {
4066+ try {
4067+ jid = jid ? createJid ( jid ) : this . instance . wuid ;
4068+
4069+ const catalog = await this . client . getCatalog ( { jid, limit : limit , cursor : cursor } ) ;
4070+
4071+ if ( ! catalog ) {
4072+ return {
4073+ products : undefined ,
4074+ nextPageCursor : undefined ,
4075+ } ;
4076+ }
4077+
4078+ return catalog ;
4079+ } catch ( error ) {
4080+ throw new InternalServerErrorException ( 'Error getCatalog' , error . toString ( ) ) ;
4081+ }
4082+ }
4083+
4084+ public async fetchCatalogCollections ( instanceName : string , data : getCollectionsDto ) {
4085+ const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4086+ const limit = data . limit || 10 ;
4087+
4088+ const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4089+
4090+ if ( ! onWhatsapp . exists ) {
4091+ throw new BadRequestException ( onWhatsapp ) ;
4092+ }
4093+
4094+ try {
4095+ const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4096+ const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4097+ const catalogCollections = await this . getCollections ( info ?. jid , limit ) ;
4098+
4099+ return {
4100+ wuid : info ?. jid || jid ,
4101+ name : info ?. name ,
4102+ numberExists : info ?. exists ,
4103+ isBusiness : business . isBusiness ,
4104+ catalogLength : catalogCollections ?. length ,
4105+ catalogCollections : catalogCollections ,
4106+ } ;
4107+ } catch ( error ) {
4108+ console . log ( error ) ;
4109+ return {
4110+ wuid : jid ,
4111+ name : null ,
4112+ isBusiness : false ,
4113+ } ;
4114+ }
4115+ }
4116+
4117+ public async getCollections ( jid ?: string | undefined , limit ?: number ) : Promise < CatalogCollection [ ] > {
4118+ try {
4119+ jid = jid ? createJid ( jid ) : this . instance . wuid ;
4120+
4121+ const result = await this . client . getCollections ( jid , limit ) ;
4122+
4123+ if ( ! result ) {
4124+ return [
4125+ {
4126+ id : undefined ,
4127+ name : undefined ,
4128+ products : [ ] ,
4129+ status : undefined ,
4130+ } ,
4131+ ] ;
4132+ }
4133+
4134+ return result . collections ;
4135+ } catch ( error ) {
4136+ throw new InternalServerErrorException ( 'Error getCatalog' , error . toString ( ) ) ;
4137+ }
4138+ }
40204139}
0 commit comments