Skip to content

Commit fa67b93

Browse files
matheusmartinsInspermatheusmartinsInsper
authored andcommitted
feat: Add support to get Catalogs and Collections with new routes: '{{baseUrl}}/chat/fetchCatalogs' and '{{baseUrl}}/chat/fetchCollections'
1 parent d58c6de commit fa67b93

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

src/api/controllers/chat.controller.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
SendPresenceDto,
1414
UpdateMessageDto,
1515
WhatsAppNumberDto,
16+
getCatalogDto,
17+
getCollectionsDto,
1618
} from '@api/dto/chat.dto';
1719
import { InstanceDto } from '@api/dto/instance.dto';
1820
import { Query } from '@api/repository/repository.service';
@@ -109,4 +111,12 @@ export class ChatController {
109111
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
110112
return await this.waMonitor.waInstances[instanceName].blockUser(data);
111113
}
114+
115+
public async fetchCatalog({ instanceName }: InstanceDto, data: getCatalogDto) {
116+
return await this.waMonitor.waInstances[instanceName].fetchCatalog(instanceName, data);
117+
}
118+
119+
public async fetchCatalogCollections({ instanceName }: InstanceDto, data: getCollectionsDto) {
120+
return await this.waMonitor.waInstances[instanceName].fetchCatalogCollections(instanceName, data);
121+
}
112122
}

src/api/dto/chat.dto.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,14 @@ export class BlockUserDto {
126126
number: string;
127127
status: 'block' | 'unblock';
128128
}
129+
130+
export class getCatalogDto {
131+
number?: string;
132+
limit?: number;
133+
cursor?: string;
134+
}
135+
136+
export class getCollectionsDto {
137+
number?: string;
138+
limit?: number;
139+
}

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
SendPresenceDto,
1414
UpdateMessageDto,
1515
WhatsAppNumberDto,
16+
getCatalogDto,
17+
getCollectionsDto,
1618
} from '@api/dto/chat.dto';
1719
import {
1820
AcceptGroupInvite,
@@ -117,6 +119,9 @@ import makeWASocket, {
117119
WAMessageUpdate,
118120
WAPresence,
119121
WASocket,
122+
Product,
123+
GetCatalogOptions,
124+
CatalogCollection,
120125
} from 'baileys';
121126
import { Label } from 'baileys/lib/Types/Label';
122127
import { 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
}

src/api/routes/chat.router.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
readMessageSchema,
3737
updateMessageSchema,
3838
whatsappNumberSchema,
39+
catalogSchema,
40+
collectionsSchema,
3941
} from '@validate/validate.schema';
4042
import { RequestHandler, Router } from 'express';
4143

@@ -267,7 +269,30 @@ export class ChatRouter extends RouterBroker {
267269
});
268270

269271
return res.status(HttpStatus.CREATED).json(response);
270-
});
272+
})
273+
274+
.post(this.routerPath('fetchCatalog'), ...guards, async (req, res) => {
275+
const response = await this.dataValidate<NumberDto>({
276+
request: req,
277+
schema: catalogSchema,
278+
ClassRef: NumberDto,
279+
execute: (instance, data) => chatController.fetchCatalog(instance, data),
280+
});
281+
282+
return res.status(HttpStatus.OK).json(response);
283+
})
284+
285+
.post(this.routerPath('fetchCollections'), ...guards, async (req, res) => {
286+
const response = await this.dataValidate<NumberDto>({
287+
request: req,
288+
schema: collectionsSchema,
289+
ClassRef: NumberDto,
290+
execute: (instance, data) => chatController.fetchCatalogCollections(instance, data),
291+
});
292+
293+
return res.status(HttpStatus.OK).json(response);
294+
})
295+
271296
}
272297

273298
public readonly router: Router = Router();

src/validate/chat.schema.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,21 @@ export const profileSchema: JSONSchema7 = {
315315
isBusiness: { type: 'boolean' },
316316
},
317317
};
318+
319+
export const catalogSchema: JSONSchema7 = {
320+
type: 'object',
321+
properties: {
322+
number: { type: 'string' },
323+
limit: { type: 'number' },
324+
cursor: { type: 'string' },
325+
},
326+
};
327+
328+
export const collectionsSchema: JSONSchema7 = {
329+
type: 'object',
330+
properties: {
331+
number: { type: 'string' },
332+
limit: { type: 'number' },
333+
cursor: { type: 'string' },
334+
},
335+
};

0 commit comments

Comments
 (0)