From dd23402238ae514436269b441134c7ed0d196aff Mon Sep 17 00:00:00 2001 From: Rafael Lopes Date: Fri, 3 Jul 2026 11:06:38 -0300 Subject: [PATCH] =?UTF-8?q?FIX:=20corre=C3=A7=C3=B5es=20encontradas=20na?= =?UTF-8?q?=20vers=C3=A3o=20demo=20whatsapp-web.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 2 +- .../attendance/attendance-assignment.service.ts | 8 +++++--- src/modules/whatsapp/dto/whatsapp.dto.ts | 5 +++-- src/modules/whatsapp/whatsapp.controller.ts | 2 +- src/modules/whatsapp/whatsapp.service.ts | 13 +++++++++++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.ts b/src/main.ts index a9ebdfa..faee86c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -48,7 +48,7 @@ async function bootstrap() { const swaggerConfig = new DocumentBuilder() .setTitle('Omnichannel Backend API') - .setDescription('Documentacao das rotas da API do Omnichannel Sothis.') + .setDescription('Documentacao das rotas da API do Sothis Connect.') .setVersion('1.0.0') .addBearerAuth() .build(); diff --git a/src/modules/attendance/attendance-assignment.service.ts b/src/modules/attendance/attendance-assignment.service.ts index e30bfa5..4972d75 100644 --- a/src/modules/attendance/attendance-assignment.service.ts +++ b/src/modules/attendance/attendance-assignment.service.ts @@ -124,9 +124,11 @@ export class AttendanceAssignmentService { private readonly agentPresenceService: AgentPresenceService, ) {} - async assignChat(chatId: string, userId: string | number, areaId?: string | number | null) { + async assignChat(chatId: string, userId: string | number, areaId?: string | number | null, skipPresenceCheck = false) { this.logger.log(`Atribuindo chat ${chatId} ao usuario ${userId}`); - await this.assertUserCanReceiveAssignment(Number(userId)); + if (!skipPresenceCheck) { + await this.assertUserCanReceiveAssignment(Number(userId)); + } const result = await this.attendanceAssignmentRepository.assignChat(chatId, Number(userId), areaId ? Number(areaId) : null); return this.enrichAssignment(result.rows[0]); } @@ -996,7 +998,7 @@ export class AttendanceAssignmentService { if (!lastBotSentAt) return false; const normalized = this.normalize(message).replace(/[!?.,\s]+/g, ' ').trim(); - const greetingOnlyMessages = ['oi', 'ola', 'olá', 'bom dia', 'boa tarde', 'boa noite']; + const greetingOnlyMessages = ['oi', 'ola', 'ol�', 'bom dia', 'boa tarde', 'boa noite']; if (!greetingOnlyMessages.includes(normalized)) return false; const lastBotTime = new Date(lastBotSentAt).getTime(); diff --git a/src/modules/whatsapp/dto/whatsapp.dto.ts b/src/modules/whatsapp/dto/whatsapp.dto.ts index a62e79b..4319e6a 100644 --- a/src/modules/whatsapp/dto/whatsapp.dto.ts +++ b/src/modules/whatsapp/dto/whatsapp.dto.ts @@ -77,8 +77,9 @@ export class AssignChatDto { userId: string; @IsOptional() - @IsString() - areaId?: string; + @Type(() => Number) + @IsInt() + areaId?: number | null; } export class TransferChatDto { diff --git a/src/modules/whatsapp/whatsapp.controller.ts b/src/modules/whatsapp/whatsapp.controller.ts index 4d6ed89..0514323 100644 --- a/src/modules/whatsapp/whatsapp.controller.ts +++ b/src/modules/whatsapp/whatsapp.controller.ts @@ -138,7 +138,7 @@ export class WhatsappController { }) @Post('assign') async assignChat(@Body() body: AssignChatDto) { - return this.assignmentService.assignChat(body.chatId, body.userId, body.areaId); + return this.assignmentService.assignChat(body.chatId, body.userId, body.areaId, true); } @ApiOperation({ diff --git a/src/modules/whatsapp/whatsapp.service.ts b/src/modules/whatsapp/whatsapp.service.ts index f5277ad..9eebda0 100644 --- a/src/modules/whatsapp/whatsapp.service.ts +++ b/src/modules/whatsapp/whatsapp.service.ts @@ -522,14 +522,23 @@ export class WhatsappService implements OnModuleInit { areaId?: number | null, variables?: Record, ) { + if (this.status !== 'CONNECTED') throw new Error('WhatsApp não está conectado'); + + const phone = to.replace(/@c\.us$/i, ''); + const numberId = await this.client.getNumberId(phone).catch(() => null); + if (!numberId) { + throw new Error(`O número ${phone} não está registrado no WhatsApp.`); + } + const resolvedTo = numberId._serialized; + const template = await this.whatsappTemplateService.getTemplateById(templateId); if (!template) { throw new Error('Template de WhatsApp nao encontrado'); } const renderedContent = this.renderTemplateContent(template.content, variables); - const sentMessage = await this.sendMessage(to, renderedContent); - const chatId = this.getSentMessageChatId(sentMessage, to); + const sentMessage = await this.sendMessage(resolvedTo, renderedContent); + const chatId = this.getSentMessageChatId(sentMessage, resolvedTo); const assignment = await this.assignmentService.assignChat(chatId, userId, areaId || null); const lockedAssignment = await this.assignmentService.markAwaitingCustomerReply(chatId);