From 56df0caab0bfa87dda1a8ab13f2e2f9c9e13c25d Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sat, 8 Jun 2024 15:09:55 -0300 Subject: [PATCH] fix: now in typebot we wait until the terminal block to accept the user's message, if it arrives before the block is sent, it is ignored --- CHANGELOG.md | 1 + package.json | 4 +++- prisma/postgresql-schema.prisma | 15 +++++++------ .../typebot/services/typebot.service.ts | 22 +++++++++++++------ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82b3b318..39cf5f8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Removed excessive verbose logs * Optimization in instance registration * Correction of variables breaking lines in typebot +* Now in typebot we wait until the terminal block to accept the user's message, if it arrives before the block is sent, it is ignored ### Break changes * jwt authentication removed diff --git a/package.json b/package.json index f4cde1ed..50cd7bb9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ "start:prod": "bash start.sh", "dev:server": "clear && tsnd --files --transpile-only --respawn --ignore-watch node_modules ./src/main.ts", "test": "clear && tsnd --files --transpile-only --respawn --ignore-watch node_modules ./test/all.test.ts", - "lint": "eslint --fix --ext .ts src" + "lint": "eslint --fix --ext .ts src", + "db:migrate:postgres": "npx prisma migrate dev --name init --schema ./prisma/postgresql-schema.prisma", + "db:migrate:mysql": "npx prisma migrate dev --name init --schema ./prisma/mysql-schema.prisma" }, "repository": { "type": "git", diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index b8e5335f..776d1b2d 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -70,13 +70,13 @@ model Session { } model Chat { - id Int @id @default(autoincrement()) - remoteJid String @db.VarChar(100) - labels Json? @db.JsonB - createdAt DateTime? @default(now()) @db.Timestamp - updatedAt DateTime? @updatedAt @db.Timestamp - Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) - instanceId String + id Int @id @default(autoincrement()) + remoteJid String @db.VarChar(100) + labels Json? @db.JsonB + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime? @updatedAt @db.Timestamp + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String } model Contact { @@ -256,6 +256,7 @@ model TypebotSession { sessionId String @db.VarChar(100) status String @db.VarChar(100) prefilledVariables Json? @db.JsonB + awaitUser Boolean @default(false) @db.Boolean createdAt DateTime? @default(now()) @db.Timestamp updatedAt DateTime @updatedAt @db.Timestamp Typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade) diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index b88c6af7..122076c9 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -309,6 +309,7 @@ export class TypebotService { pushName: data.pushName || '', instanceName: instance.instanceName, }, + awaitUser: false, typebotId: data.typebotId, instanceId: instance.instanceId, }, @@ -337,6 +338,7 @@ export class TypebotService { clientSideActions, this.eventEmitter, applyFormatting, + this.prismaRepository, ).catch((err) => { console.error('Erro ao processar mensagens:', err); }); @@ -420,10 +422,10 @@ export class TypebotService { clientSideActions, eventEmitter, applyFormatting, + prismaRepository, ) { for (const message of messages) { if (message.type === 'text') { - console.log('message.content.richText', message.content.richText); let formattedText = ''; for (const richText of message.content.richText) { @@ -471,7 +473,6 @@ export class TypebotService { }); } - console.log(clientSideActions); const wait = findItemAndGetSecondsToWait(clientSideActions, message.id); if (wait) { @@ -497,6 +498,15 @@ export class TypebotService { text: formattedText, }); } + + await prismaRepository.typebotSession.update({ + where: { + id: session.id, + }, + data: { + awaitUser: true, + }, + }); } else { eventEmitter.emit('typebot:end', { sessionId: session.id, @@ -522,6 +532,8 @@ export class TypebotService { }, }); + if (session && !session.awaitUser) return; + try { if (session && expire && expire > 0) { const now = Date.now(); @@ -712,13 +724,12 @@ export class TypebotService { }, data: { status: 'opened', + awaitUser: false, }, }); const content = this.getConversationMessage(msg.message); - console.log('content', content); - if (!content) { if (unknownMessage) { this.waMonitor.waInstances[instance.instanceName].textMessage({ @@ -755,11 +766,8 @@ export class TypebotService { sessionId: session.sessionId.split('-')[1], }; } - console.log('reqData', reqData); const request = await axios.post(urlTypebot, reqData); - console.log('request', request.data); - await this.sendWAMessage( instance, session,