Skip to content

Commit 6c1355b

Browse files
committed
feat: Criado um novo grupo de rotas (business) para tratar dos catalogos de produtos e Coleções evitando alterações desnecessárias em arquivos do repositório
1 parent 9a72b90 commit 6c1355b

File tree

12 files changed

+121
-79
lines changed

12 files changed

+121
-79
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getCatalogDto, getCollectionsDto } from '@api/dto/business.dto';
2+
import { InstanceDto } from '@api/dto/instance.dto';
3+
import { WAMonitoringService } from '@api/services/monitor.service';
4+
5+
export class BusinessController {
6+
constructor(private readonly waMonitor: WAMonitoringService) {}
7+
8+
public async fetchCatalog({ instanceName }: InstanceDto, data: getCatalogDto) {
9+
return await this.waMonitor.waInstances[instanceName].fetchCatalog(instanceName, data);
10+
}
11+
12+
public async fetchCollections({ instanceName }: InstanceDto, data: getCollectionsDto) {
13+
return await this.waMonitor.waInstances[instanceName].fetchCollections(instanceName, data);
14+
}
15+
}

src/api/controllers/chat.controller.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {
33
BlockUserDto,
44
DeleteMessage,
55
getBase64FromMediaMessageDto,
6-
getCatalogDto,
7-
getCollectionsDto,
86
MarkChatUnreadDto,
97
NumberDto,
108
PrivacySettingDto,
@@ -111,12 +109,4 @@ export class ChatController {
111109
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
112110
return await this.waMonitor.waInstances[instanceName].blockUser(data);
113111
}
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-
}
122112
}

src/api/dto/business.dto.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export class NumberDto {
2+
number: string;
3+
}
4+
5+
export class getCatalogDto {
6+
number?: string;
7+
limit?: number;
8+
cursor?: string;
9+
}
10+
11+
export class getCollectionsDto {
12+
number?: string;
13+
limit?: number;
14+
}

src/api/dto/chat.dto.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,3 @@ 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: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { getCollectionsDto } from '@api/dto/business.dto';
12
import { OfferCallDto } from '@api/dto/call.dto';
23
import {
34
ArchiveChatDto,
45
BlockUserDto,
56
DeleteMessage,
67
getBase64FromMediaMessageDto,
7-
getCatalogDto,
8-
getCollectionsDto,
98
LastMessage,
109
MarkChatUnreadDto,
1110
NumberBusiness,
@@ -4634,11 +4633,11 @@ export class BaileysStartupService extends ChannelStartupService {
46344633
return response;
46354634
}
46364635

4637-
//Catalogs and collections
4638-
public async fetchCatalog(instanceName: string, data: getCatalogDto) {
4636+
//Business Controller
4637+
public async fetchCatalog(instanceName: string, data: getCollectionsDto) {
46394638
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
46404639
const limit = data.limit || 10;
4641-
const cursor = data.cursor || null;
4640+
const cursor = null;
46424641

46434642
const onWhatsapp = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
46444643

@@ -4649,15 +4648,35 @@ export class BaileysStartupService extends ChannelStartupService {
46494648
try {
46504649
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
46514650
const business = await this.fetchBusinessProfile(info?.jid);
4652-
const catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });
4651+
4652+
let catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });
4653+
let nextPageCursor = catalog.nextPageCursor;
4654+
let nextPageCursorJson = nextPageCursor ? JSON.parse(atob(nextPageCursor)) : null;
4655+
let pagination = nextPageCursorJson?.pagination_cursor
4656+
? JSON.parse(atob(nextPageCursorJson.pagination_cursor))
4657+
: null;
4658+
let fetcherHasMore = pagination?.fetcher_has_more === true ? true : false;
4659+
4660+
let productsCatalog = catalog.products || [];
4661+
let countLoops = 0;
4662+
while (fetcherHasMore && countLoops < 4) {
4663+
catalog = await this.getCatalog({ jid: info?.jid, limit, cursor: nextPageCursor });
4664+
nextPageCursor = catalog.nextPageCursor;
4665+
nextPageCursorJson = nextPageCursor ? JSON.parse(atob(nextPageCursor)) : null;
4666+
pagination = nextPageCursorJson?.pagination_cursor
4667+
? JSON.parse(atob(nextPageCursorJson.pagination_cursor))
4668+
: null;
4669+
fetcherHasMore = pagination?.fetcher_has_more === true ? true : false;
4670+
productsCatalog = [...productsCatalog, ...catalog.products];
4671+
countLoops++;
4672+
}
46534673

46544674
return {
46554675
wuid: info?.jid || jid,
4656-
name: info?.name,
46574676
numberExists: info?.exists,
46584677
isBusiness: business.isBusiness,
4659-
catalogLength: catalog?.products.length,
4660-
catalog: catalog?.products,
4678+
catalogLength: productsCatalog.length,
4679+
catalog: productsCatalog,
46614680
};
46624681
} catch (error) {
46634682
console.log(error);
@@ -4692,9 +4711,9 @@ export class BaileysStartupService extends ChannelStartupService {
46924711
}
46934712
}
46944713

4695-
public async fetchCatalogCollections(instanceName: string, data: getCollectionsDto) {
4714+
public async fetchCollections(instanceName: string, data: getCollectionsDto) {
46964715
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
4697-
const limit = data.limit || 10;
4716+
const limit = data.limit <= 20 ? data.limit : 20; //(tem esse limite, não sei porque)
46984717

46994718
const onWhatsapp = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
47004719

@@ -4705,18 +4724,17 @@ export class BaileysStartupService extends ChannelStartupService {
47054724
try {
47064725
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
47074726
const business = await this.fetchBusinessProfile(info?.jid);
4708-
const catalogCollections = await this.getCollections(info?.jid, limit);
4727+
const collections = await this.getCollections(info?.jid, limit);
47094728

47104729
return {
47114730
wuid: info?.jid || jid,
47124731
name: info?.name,
47134732
numberExists: info?.exists,
47144733
isBusiness: business.isBusiness,
4715-
catalogLength: catalogCollections?.length,
4716-
catalogCollections: catalogCollections,
4734+
collectionsLength: collections?.length,
4735+
collections: collections,
47174736
};
47184737
} catch (error) {
4719-
console.log(error);
47204738
return {
47214739
wuid: jid,
47224740
name: null,

src/api/routes/business.router.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { RouterBroker } from '@api/abstract/abstract.router';
2+
import { NumberDto } from '@api/dto/chat.dto';
3+
import { businessController } from '@api/server.module';
4+
import { catalogSchema, collectionsSchema } from '@validate/validate.schema';
5+
import { RequestHandler, Router } from 'express';
6+
7+
import { HttpStatus } from './index.router';
8+
9+
export class BusinessRouter extends RouterBroker {
10+
constructor(...guards: RequestHandler[]) {
11+
super();
12+
this.router
13+
.post(this.routerPath('getCatalog'), ...guards, async (req, res) => {
14+
const response = await this.dataValidate<NumberDto>({
15+
request: req,
16+
schema: catalogSchema,
17+
ClassRef: NumberDto,
18+
execute: (instance, data) => businessController.fetchCatalog(instance, data),
19+
});
20+
21+
return res.status(HttpStatus.OK).json(response);
22+
})
23+
24+
.post(this.routerPath('getCollections'), ...guards, async (req, res) => {
25+
const response = await this.dataValidate<NumberDto>({
26+
request: req,
27+
schema: collectionsSchema,
28+
ClassRef: NumberDto,
29+
execute: (instance, data) => businessController.fetchCollections(instance, data),
30+
});
31+
32+
return res.status(HttpStatus.OK).json(response);
33+
});
34+
}
35+
36+
public readonly router: Router = Router();
37+
}

src/api/routes/chat.router.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import { Contact, Message, MessageUpdate } from '@prisma/client';
2222
import {
2323
archiveChatSchema,
2424
blockUserSchema,
25-
catalogSchema,
26-
collectionsSchema,
2725
contactValidateSchema,
2826
deleteMessageSchema,
2927
markChatUnreadSchema,
@@ -209,7 +207,6 @@ export class ChatRouter extends RouterBroker {
209207

210208
return res.status(HttpStatus.OK).json(response);
211209
})
212-
213210
.post(this.routerPath('updateProfileName'), ...guards, async (req, res) => {
214211
const response = await this.dataValidate<ProfileNameDto>({
215212
request: req,
@@ -269,28 +266,6 @@ export class ChatRouter extends RouterBroker {
269266
});
270267

271268
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);
294269
});
295270
}
296271

src/api/routes/index.router.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import fs from 'fs';
1111
import mimeTypes from 'mime-types';
1212
import path from 'path';
1313

14+
import { BusinessRouter } from './business.router';
1415
import { CallRouter } from './call.router';
1516
import { ChatRouter } from './chat.router';
1617
import { GroupRouter } from './group.router';
@@ -82,6 +83,7 @@ router
8283
.use('/message', new MessageRouter(...guards).router)
8384
.use('/call', new CallRouter(...guards).router)
8485
.use('/chat', new ChatRouter(...guards).router)
86+
.use('/business', new BusinessRouter(...guards).router)
8587
.use('/group', new GroupRouter(...guards).router)
8688
.use('/template', new TemplateRouter(configService, ...guards).router)
8789
.use('/settings', new SettingsRouter(...guards).router)

src/api/server.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Chatwoot, configService, ProviderSession } from '@config/env.config';
33
import { eventEmitter } from '@config/event.config';
44
import { Logger } from '@config/logger.config';
55

6+
import { BusinessController } from './controllers/business.controller';
67
import { CallController } from './controllers/call.controller';
78
import { ChatController } from './controllers/chat.controller';
89
import { GroupController } from './controllers/group.controller';
@@ -98,6 +99,7 @@ export const instanceController = new InstanceController(
9899
export const sendMessageController = new SendMessageController(waMonitor);
99100
export const callController = new CallController(waMonitor);
100101
export const chatController = new ChatController(waMonitor);
102+
export const businessController = new BusinessController(waMonitor);
101103
export const groupController = new GroupController(waMonitor);
102104
export const labelController = new LabelController(waMonitor);
103105

src/validate/business.schema.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { JSONSchema7 } from 'json-schema';
2+
3+
export const catalogSchema: JSONSchema7 = {
4+
type: 'object',
5+
properties: {
6+
number: { type: 'string' },
7+
limit: { type: 'number' },
8+
},
9+
};
10+
11+
export const collectionsSchema: JSONSchema7 = {
12+
type: 'object',
13+
properties: {
14+
number: { type: 'string' },
15+
limit: { type: 'number' },
16+
},
17+
};

0 commit comments

Comments
 (0)