Skip to content

Commit e27db06

Browse files
committed
feat: Add support to get Catalogs and Collections with new routes: '{{baseUrl}}/chat/fetchCatalogs' and '{{baseUrl}}/chat/fetchCollections'
1 parent 427c994 commit e27db06

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
@@ -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,
@@ -121,6 +123,9 @@ import makeWASocket, {
121123
WAMessageUpdate,
122124
WAPresence,
123125
WASocket,
126+
Product,
127+
GetCatalogOptions,
128+
CatalogCollection,
124129
} from 'baileys';
125130
import { Label } from 'baileys/lib/Types/Label';
126131
import { 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
}

src/api/routes/chat.router.ts

Lines changed: 24 additions & 0 deletions
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,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)