Skip to content

Commit b143363

Browse files
Merge pull request #1201 from wayre/catalogProducts
Feat: Adicionei suporte para obter o Catálogos de Produtos e as Coleções de Produtos para a versão 2.2.3
2 parents 023e030 + 56a8e09 commit b143363

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

src/api/controllers/chat.controller.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
BlockUserDto,
44
DeleteMessage,
55
getBase64FromMediaMessageDto,
6+
getCatalogDto,
7+
getCollectionsDto,
68
MarkChatUnreadDto,
79
NumberDto,
810
PrivacySettingDto,
@@ -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
@@ -4,6 +4,8 @@ import {
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
}

src/api/routes/chat.router.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { Contact, Message, MessageUpdate } from '@prisma/client';
2222
import {
2323
archiveChatSchema,
2424
blockUserSchema,
25+
catalogSchema,
26+
collectionsSchema,
2527
contactValidateSchema,
2628
deleteMessageSchema,
2729
markChatUnreadSchema,
@@ -267,6 +269,28 @@ export class ChatRouter extends RouterBroker {
267269
});
268270

269271
return res.status(HttpStatus.CREATED).json(response);
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);
270294
});
271295
}
272296

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)