Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/api/controllers/business.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getCatalogDto, getCollectionsDto } from '@api/dto/business.dto';
import { InstanceDto } from '@api/dto/instance.dto';
import { WAMonitoringService } from '@api/services/monitor.service';

export class BusinessController {
constructor(private readonly waMonitor: WAMonitoringService) {}

public async fetchCatalog({ instanceName }: InstanceDto, data: getCatalogDto) {
return await this.waMonitor.waInstances[instanceName].fetchCatalog(instanceName, data);
}

public async fetchCollections({ instanceName }: InstanceDto, data: getCollectionsDto) {
return await this.waMonitor.waInstances[instanceName].fetchCollections(instanceName, data);
}
}
10 changes: 0 additions & 10 deletions src/api/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
BlockUserDto,
DeleteMessage,
getBase64FromMediaMessageDto,
getCatalogDto,
getCollectionsDto,
MarkChatUnreadDto,
NumberDto,
PrivacySettingDto,
Expand Down Expand Up @@ -111,12 +109,4 @@ export class ChatController {
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
return await this.waMonitor.waInstances[instanceName].blockUser(data);
}

public async fetchCatalog({ instanceName }: InstanceDto, data: getCatalogDto) {
return await this.waMonitor.waInstances[instanceName].fetchCatalog(instanceName, data);
}

public async fetchCatalogCollections({ instanceName }: InstanceDto, data: getCollectionsDto) {
return await this.waMonitor.waInstances[instanceName].fetchCatalogCollections(instanceName, data);
}
}
14 changes: 14 additions & 0 deletions src/api/dto/business.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export class NumberDto {
number: string;
}

export class getCatalogDto {
number?: string;
limit?: number;
cursor?: string;
}

export class getCollectionsDto {
number?: string;
limit?: number;
}
11 changes: 0 additions & 11 deletions src/api/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,3 @@ export class BlockUserDto {
number: string;
status: 'block' | 'unblock';
}

export class getCatalogDto {
number?: string;
limit?: number;
cursor?: string;
}

export class getCollectionsDto {
number?: string;
limit?: number;
}
48 changes: 33 additions & 15 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { getCollectionsDto } from '@api/dto/business.dto';
import { OfferCallDto } from '@api/dto/call.dto';
import {
ArchiveChatDto,
BlockUserDto,
DeleteMessage,
getBase64FromMediaMessageDto,
getCatalogDto,
getCollectionsDto,
LastMessage,
MarkChatUnreadDto,
NumberBusiness,
Expand Down Expand Up @@ -4634,11 +4633,11 @@ export class BaileysStartupService extends ChannelStartupService {
return response;
}

//Catalogs and collections
public async fetchCatalog(instanceName: string, data: getCatalogDto) {
//Business Controller
public async fetchCatalog(instanceName: string, data: getCollectionsDto) {
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
const limit = data.limit || 10;
const cursor = data.cursor || null;
const cursor = null;

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

Expand All @@ -4649,15 +4648,35 @@ export class BaileysStartupService extends ChannelStartupService {
try {
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
const business = await this.fetchBusinessProfile(info?.jid);
const catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });

let catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });
let nextPageCursor = catalog.nextPageCursor;
let nextPageCursorJson = nextPageCursor ? JSON.parse(atob(nextPageCursor)) : null;
let pagination = nextPageCursorJson?.pagination_cursor
? JSON.parse(atob(nextPageCursorJson.pagination_cursor))
: null;
let fetcherHasMore = pagination?.fetcher_has_more === true ? true : false;

let productsCatalog = catalog.products || [];
let countLoops = 0;
while (fetcherHasMore && countLoops < 4) {
catalog = await this.getCatalog({ jid: info?.jid, limit, cursor: nextPageCursor });
nextPageCursor = catalog.nextPageCursor;
nextPageCursorJson = nextPageCursor ? JSON.parse(atob(nextPageCursor)) : null;
pagination = nextPageCursorJson?.pagination_cursor
? JSON.parse(atob(nextPageCursorJson.pagination_cursor))
: null;
fetcherHasMore = pagination?.fetcher_has_more === true ? true : false;
productsCatalog = [...productsCatalog, ...catalog.products];
countLoops++;
}

return {
wuid: info?.jid || jid,
name: info?.name,
numberExists: info?.exists,
isBusiness: business.isBusiness,
catalogLength: catalog?.products.length,
catalog: catalog?.products,
catalogLength: productsCatalog.length,
catalog: productsCatalog,
};
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -4692,9 +4711,9 @@ export class BaileysStartupService extends ChannelStartupService {
}
}

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

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

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

return {
wuid: info?.jid || jid,
name: info?.name,
numberExists: info?.exists,
isBusiness: business.isBusiness,
catalogLength: catalogCollections?.length,
catalogCollections: catalogCollections,
collectionsLength: collections?.length,
collections: collections,
};
} catch (error) {
console.log(error);
return {
wuid: jid,
name: null,
Expand Down
37 changes: 37 additions & 0 deletions src/api/routes/business.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { NumberDto } from '@api/dto/chat.dto';
import { businessController } from '@api/server.module';
import { catalogSchema, collectionsSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';

import { HttpStatus } from './index.router';

export class BusinessRouter extends RouterBroker {
constructor(...guards: RequestHandler[]) {
super();
this.router
.post(this.routerPath('getCatalog'), ...guards, async (req, res) => {
const response = await this.dataValidate<NumberDto>({
request: req,
schema: catalogSchema,
ClassRef: NumberDto,
execute: (instance, data) => businessController.fetchCatalog(instance, data),
});

return res.status(HttpStatus.OK).json(response);
})

.post(this.routerPath('getCollections'), ...guards, async (req, res) => {
const response = await this.dataValidate<NumberDto>({
request: req,
schema: collectionsSchema,
ClassRef: NumberDto,
execute: (instance, data) => businessController.fetchCollections(instance, data),
});

return res.status(HttpStatus.OK).json(response);
});
}

public readonly router: Router = Router();
}
25 changes: 0 additions & 25 deletions src/api/routes/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { Contact, Message, MessageUpdate } from '@prisma/client';
import {
archiveChatSchema,
blockUserSchema,
catalogSchema,
collectionsSchema,
contactValidateSchema,
deleteMessageSchema,
markChatUnreadSchema,
Expand Down Expand Up @@ -209,7 +207,6 @@ export class ChatRouter extends RouterBroker {

return res.status(HttpStatus.OK).json(response);
})

.post(this.routerPath('updateProfileName'), ...guards, async (req, res) => {
const response = await this.dataValidate<ProfileNameDto>({
request: req,
Expand Down Expand Up @@ -269,28 +266,6 @@ export class ChatRouter extends RouterBroker {
});

return res.status(HttpStatus.CREATED).json(response);
})

.post(this.routerPath('fetchCatalog'), ...guards, async (req, res) => {
const response = await this.dataValidate<NumberDto>({
request: req,
schema: catalogSchema,
ClassRef: NumberDto,
execute: (instance, data) => chatController.fetchCatalog(instance, data),
});

return res.status(HttpStatus.OK).json(response);
})

.post(this.routerPath('fetchCollections'), ...guards, async (req, res) => {
const response = await this.dataValidate<NumberDto>({
request: req,
schema: collectionsSchema,
ClassRef: NumberDto,
execute: (instance, data) => chatController.fetchCatalogCollections(instance, data),
});

return res.status(HttpStatus.OK).json(response);
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/api/routes/index.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fs from 'fs';
import mimeTypes from 'mime-types';
import path from 'path';

import { BusinessRouter } from './business.router';
import { CallRouter } from './call.router';
import { ChatRouter } from './chat.router';
import { GroupRouter } from './group.router';
Expand Down Expand Up @@ -82,6 +83,7 @@ router
.use('/message', new MessageRouter(...guards).router)
.use('/call', new CallRouter(...guards).router)
.use('/chat', new ChatRouter(...guards).router)
.use('/business', new BusinessRouter(...guards).router)
.use('/group', new GroupRouter(...guards).router)
.use('/template', new TemplateRouter(configService, ...guards).router)
.use('/settings', new SettingsRouter(...guards).router)
Expand Down
2 changes: 2 additions & 0 deletions src/api/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Chatwoot, configService, ProviderSession } from '@config/env.config';
import { eventEmitter } from '@config/event.config';
import { Logger } from '@config/logger.config';

import { BusinessController } from './controllers/business.controller';
import { CallController } from './controllers/call.controller';
import { ChatController } from './controllers/chat.controller';
import { GroupController } from './controllers/group.controller';
Expand Down Expand Up @@ -98,6 +99,7 @@ export const instanceController = new InstanceController(
export const sendMessageController = new SendMessageController(waMonitor);
export const callController = new CallController(waMonitor);
export const chatController = new ChatController(waMonitor);
export const businessController = new BusinessController(waMonitor);
export const groupController = new GroupController(waMonitor);
export const labelController = new LabelController(waMonitor);

Expand Down
17 changes: 17 additions & 0 deletions src/validate/business.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { JSONSchema7 } from 'json-schema';

export const catalogSchema: JSONSchema7 = {
type: 'object',
properties: {
number: { type: 'string' },
limit: { type: 'number' },
},
};

export const collectionsSchema: JSONSchema7 = {
type: 'object',
properties: {
number: { type: 'string' },
limit: { type: 'number' },
},
};
18 changes: 0 additions & 18 deletions src/validate/chat.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,3 @@ export const profileSchema: JSONSchema7 = {
isBusiness: { type: 'boolean' },
},
};

export const catalogSchema: JSONSchema7 = {
type: 'object',
properties: {
number: { type: 'string' },
limit: { type: 'number' },
cursor: { type: 'string' },
},
};

export const collectionsSchema: JSONSchema7 = {
type: 'object',
properties: {
number: { type: 'string' },
limit: { type: 'number' },
cursor: { type: 'string' },
},
};
1 change: 1 addition & 0 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Integrations Schema
export * from './business.schema';
export * from './chat.schema';
export * from './group.schema';
export * from './instance.schema';
Expand Down