Skip to content

Commit 9ca4bfa

Browse files
matheusmartinsInspermatheusmartinsInsper
authored andcommitted
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 7c3fccb commit 9ca4bfa

File tree

12 files changed

+123
-80
lines changed

12 files changed

+123
-80
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: 35 additions & 17 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,
@@ -2343,8 +2342,8 @@ export class BaileysStartupService extends ChannelStartupService {
23432342
if (file) mediaData.sticker = file.buffer.toString('base64');
23442343

23452344
const convert = data?.notConvertSticker
2346-
? Buffer.from(data.sticker, 'base64')
2347-
: await this.convertToWebP(data.sticker);
2345+
? Buffer.from(data.sticker, 'base64')
2346+
: await this.convertToWebP(data.sticker);
23482347
const gifPlayback = data.sticker.includes('.gif');
23492348
const result = await this.sendMessageWithTyping(
23502349
data.number,
@@ -4024,11 +4023,11 @@ export class BaileysStartupService extends ChannelStartupService {
40244023
return response;
40254024
}
40264025

4027-
//Catalogs and collections
4028-
public async fetchCatalog(instanceName: string, data: getCatalogDto) {
4026+
//Business Controller
4027+
public async fetchCatalog(instanceName: string, data: getCollectionsDto) {
40294028
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
40304029
const limit = data.limit || 10;
4031-
const cursor = data.cursor || null;
4030+
const cursor = null;
40324031

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

@@ -4039,15 +4038,35 @@ export class BaileysStartupService extends ChannelStartupService {
40394038
try {
40404039
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
40414040
const business = await this.fetchBusinessProfile(info?.jid);
4042-
const catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });
4041+
4042+
let catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });
4043+
let nextPageCursor = catalog.nextPageCursor;
4044+
let nextPageCursorJson = nextPageCursor ? JSON.parse(atob(nextPageCursor)) : null;
4045+
let pagination = nextPageCursorJson?.pagination_cursor
4046+
? JSON.parse(atob(nextPageCursorJson.pagination_cursor))
4047+
: null;
4048+
let fetcherHasMore = pagination?.fetcher_has_more === true ? true : false;
4049+
4050+
let productsCatalog = catalog.products || [];
4051+
let countLoops = 0;
4052+
while (fetcherHasMore && countLoops < 4) {
4053+
catalog = await this.getCatalog({ jid: info?.jid, limit, cursor: nextPageCursor });
4054+
nextPageCursor = catalog.nextPageCursor;
4055+
nextPageCursorJson = nextPageCursor ? JSON.parse(atob(nextPageCursor)) : null;
4056+
pagination = nextPageCursorJson?.pagination_cursor
4057+
? JSON.parse(atob(nextPageCursorJson.pagination_cursor))
4058+
: null;
4059+
fetcherHasMore = pagination?.fetcher_has_more === true ? true : false;
4060+
productsCatalog = [...productsCatalog, ...catalog.products];
4061+
countLoops++;
4062+
}
40434063

40444064
return {
40454065
wuid: info?.jid || jid,
4046-
name: info?.name,
40474066
numberExists: info?.exists,
40484067
isBusiness: business.isBusiness,
4049-
catalogLength: catalog?.products.length,
4050-
catalog: catalog?.products,
4068+
catalogLength: productsCatalog.length,
4069+
catalog: productsCatalog,
40514070
};
40524071
} catch (error) {
40534072
console.log(error);
@@ -4082,9 +4101,9 @@ export class BaileysStartupService extends ChannelStartupService {
40824101
}
40834102
}
40844103

4085-
public async fetchCatalogCollections(instanceName: string, data: getCollectionsDto) {
4104+
public async fetchCollections(instanceName: string, data: getCollectionsDto) {
40864105
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
4087-
const limit = data.limit || 10;
4106+
const limit = data.limit <= 20 ? data.limit : 20; //(tem esse limite, não sei porque)
40884107

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

@@ -4095,18 +4114,17 @@ export class BaileysStartupService extends ChannelStartupService {
40954114
try {
40964115
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
40974116
const business = await this.fetchBusinessProfile(info?.jid);
4098-
const catalogCollections = await this.getCollections(info?.jid, limit);
4117+
const collections = await this.getCollections(info?.jid, limit);
40994118

41004119
return {
41014120
wuid: info?.jid || jid,
41024121
name: info?.name,
41034122
numberExists: info?.exists,
41044123
isBusiness: business.isBusiness,
4105-
catalogLength: catalogCollections?.length,
4106-
catalogCollections: catalogCollections,
4124+
collectionsLength: collections?.length,
4125+
collections: collections,
41074126
};
41084127
} catch (error) {
4109-
console.log(error);
41104128
return {
41114129
wuid: jid,
41124130
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 & 24 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,
@@ -269,28 +267,6 @@ export class ChatRouter extends RouterBroker {
269267
});
270268

271269
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);
294270
});
295271
}
296272

src/api/routes/index.router.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { configService } from '@config/env.config';
88
import { Router } from 'express';
99
import fs from 'fs';
1010

11+
import { BusinessRouter } from './business.router';
1112
import { CallRouter } from './call.router';
1213
import { ChatRouter } from './chat.router';
1314
import { GroupRouter } from './group.router';
@@ -61,6 +62,7 @@ router
6162
.use('/instance', new InstanceRouter(configService, ...guards).router)
6263
.use('/message', new MessageRouter(...guards).router)
6364
.use('/call', new CallRouter(...guards).router)
65+
.use('/business', new BusinessRouter(...guards).router)
6466
.use('/chat', new ChatRouter(...guards).router)
6567
.use('/group', new GroupRouter(...guards).router)
6668
.use('/template', new TemplateRouter(configService, ...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 { 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';
@@ -74,6 +75,7 @@ export const instanceController = new InstanceController(
7475
export const sendMessageController = new SendMessageController(waMonitor);
7576
export const callController = new CallController(waMonitor);
7677
export const chatController = new ChatController(waMonitor);
78+
export const businessController = new BusinessController(waMonitor);
7779
export const groupController = new GroupController(waMonitor);
7880
export const labelController = new LabelController(waMonitor);
7981

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)