@@ -13,6 +13,8 @@ import {
1313 SendPresenceDto ,
1414 UpdateMessageDto ,
1515 WhatsAppNumberDto ,
16+ getCatalogDto ,
17+ getCollectionsDto ,
1618} from '@api/dto/chat.dto' ;
1719import {
1820 AcceptGroupInvite ,
@@ -121,6 +123,9 @@ import makeWASocket, {
121123 WAMessageUpdate ,
122124 WAPresence ,
123125 WASocket ,
126+ Product ,
127+ GetCatalogOptions ,
128+ CatalogCollection ,
124129} from 'baileys' ;
125130import { Label } from 'baileys/lib/Types/Label' ;
126131import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation' ;
@@ -4534,4 +4539,118 @@ export class BaileysStartupService extends ChannelStartupService {
45344539
45354540 return response ;
45364541 }
4542+
4543+ //Catalogs and collections
4544+ public async fetchCatalog ( instanceName : string , data : getCatalogDto ) {
4545+ const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4546+ const limit = data . limit || 10 ;
4547+ const cursor = data . cursor || null ;
4548+
4549+ const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4550+
4551+ if ( ! onWhatsapp . exists ) {
4552+ throw new BadRequestException ( onWhatsapp ) ;
4553+ }
4554+
4555+ try {
4556+ const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4557+ const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4558+ const catalog = await this . getCatalog ( { jid : info ?. jid , limit, cursor } ) ;
4559+
4560+ return {
4561+ wuid : info ?. jid || jid ,
4562+ name : info ?. name ,
4563+ numberExists : info ?. exists ,
4564+ isBusiness : business . isBusiness ,
4565+ catalogLength : catalog ?. products . length ,
4566+ catalog : catalog ?. products ,
4567+ } ;
4568+ } catch ( error ) {
4569+ console . log ( error ) ;
4570+ return {
4571+ wuid : jid ,
4572+ name : null ,
4573+ isBusiness : false ,
4574+ } ;
4575+ }
4576+ }
4577+
4578+ public async getCatalog ( {
4579+ jid,
4580+ limit,
4581+ cursor,
4582+ } : GetCatalogOptions ) : Promise < { products : Product [ ] ; nextPageCursor : string | undefined } > {
4583+ try {
4584+ jid = jid ? createJid ( jid ) : this . instance . wuid ;
4585+
4586+ const catalog = await this . client . getCatalog ( { jid, limit : limit , cursor : cursor } ) ;
4587+
4588+ if ( ! catalog ) {
4589+ return {
4590+ products : undefined ,
4591+ nextPageCursor : undefined ,
4592+ } ;
4593+ }
4594+
4595+ return catalog ;
4596+ } catch ( error ) {
4597+ throw new InternalServerErrorException ( 'Error getCatalog' , error . toString ( ) ) ;
4598+ }
4599+ }
4600+
4601+ public async fetchCatalogCollections ( instanceName : string , data : getCollectionsDto ) {
4602+ const jid = data . number ? createJid ( data . number ) : this . client ?. user ?. id ;
4603+ const limit = data . limit || 10 ;
4604+
4605+ const onWhatsapp = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4606+
4607+ if ( ! onWhatsapp . exists ) {
4608+ throw new BadRequestException ( onWhatsapp ) ;
4609+ }
4610+
4611+ try {
4612+ const info = ( await this . whatsappNumber ( { numbers : [ jid ] } ) ) ?. shift ( ) ;
4613+ const business = await this . fetchBusinessProfile ( info ?. jid ) ;
4614+ const catalogCollections = await this . getCollections ( info ?. jid , limit ) ;
4615+
4616+ return {
4617+ wuid : info ?. jid || jid ,
4618+ name : info ?. name ,
4619+ numberExists : info ?. exists ,
4620+ isBusiness : business . isBusiness ,
4621+ catalogLength : catalogCollections ?. length ,
4622+ catalogCollections : catalogCollections ,
4623+ } ;
4624+ } catch ( error ) {
4625+ console . log ( error ) ;
4626+ return {
4627+ wuid : jid ,
4628+ name : null ,
4629+ isBusiness : false ,
4630+ } ;
4631+ }
4632+ }
4633+
4634+ public async getCollections ( jid ?: string | undefined , limit ?: number ) : Promise < CatalogCollection [ ] > {
4635+ try {
4636+ jid = jid ? createJid ( jid ) : this . instance . wuid ;
4637+
4638+ const result = await this . client . getCollections ( jid , limit ) ;
4639+
4640+ if ( ! result ) {
4641+ return [
4642+ {
4643+ id : undefined ,
4644+ name : undefined ,
4645+ products : [ ] ,
4646+ status : undefined ,
4647+ } ,
4648+ ] ;
4649+ }
4650+
4651+ return result . collections ;
4652+ } catch ( error ) {
4653+ throw new InternalServerErrorException ( 'Error getCatalog' , error . toString ( ) ) ;
4654+ }
4655+ }
45374656}
0 commit comments