diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index f0d0441c..457bd8d6 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -579,7 +579,13 @@ export class TypebotService { const remoteJid = data.remoteJid; const status = data.status; - if (status === 'closed') { + const defaultSettingCheck = await this.prismaRepository.typebotSetting.findFirst({ + where: { + instanceId, + }, + }); + + if (status === 'delete') { await this.prismaRepository.typebotSession.deleteMany({ where: { remoteJid: remoteJid, @@ -588,27 +594,50 @@ export class TypebotService { }); return { typebot: { ...instance, typebot: { remoteJid: remoteJid, status: status } } }; - } else { - const session = await this.prismaRepository.typebotSession.updateMany({ - where: { - instanceId: instanceId, - remoteJid: remoteJid, - }, - data: { - status: status, - }, - }); - - const typebotData = { - remoteJid: remoteJid, - status: status, - session, - }; - - this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData); - - return { typebot: { ...instance, typebot: typebotData } }; } + + if (status === 'closed') { + if (defaultSettingCheck?.keepOpen) { + await this.prismaRepository.typebotSession.updateMany({ + where: { + instanceId: instanceId, + remoteJid: remoteJid, + }, + data: { + status: status, + }, + }); + } else { + await this.prismaRepository.typebotSession.deleteMany({ + where: { + remoteJid: remoteJid, + instanceId: instanceId, + }, + }); + } + + return { typebot: { ...instance, typebot: { remoteJid: remoteJid, status: status } } }; + } + + const session = await this.prismaRepository.typebotSession.updateMany({ + where: { + instanceId: instanceId, + remoteJid: remoteJid, + }, + data: { + status: status, + }, + }); + + const typebotData = { + remoteJid: remoteJid, + status: status, + session, + }; + + this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData); + + return { typebot: { ...instance, typebot: typebotData } }; } catch (error) { this.logger.error(error); throw new Error('Error changing status'); @@ -719,7 +748,6 @@ export class TypebotService { } if (startSession) { - console.log('startSession', startSession); let findTypebot: any = await this.prismaRepository.typebot.findFirst({ where: { url: url, @@ -728,8 +756,6 @@ export class TypebotService { }, }); - console.log('findTypebot', findTypebot); - if (!findTypebot) { findTypebot = await this.prismaRepository.typebot.create({ data: { @@ -749,8 +775,6 @@ export class TypebotService { }); } - console.log('findTypebot2', findTypebot); - await this.prismaRepository.typebotSession.deleteMany({ where: { remoteJid: remoteJid, @@ -1500,17 +1524,28 @@ export class TypebotService { participant: string; }; - if (!listeningFromMe && key.fromMe) { + if (stopBotFromMe && key.fromMe && session) { + if (keepOpen) { + await this.prismaRepository.typebotSession.update({ + where: { + id: session.id, + }, + data: { + status: 'closed', + }, + }); + } else { + await this.prismaRepository.typebotSession.deleteMany({ + where: { + typebotId: findTypebot.id, + remoteJid: remoteJid, + }, + }); + } return; } - if (stopBotFromMe && listeningFromMe && key.fromMe && session) { - await this.prismaRepository.typebotSession.deleteMany({ - where: { - typebotId: findTypebot.id, - remoteJid: remoteJid, - }, - }); + if (!listeningFromMe && key.fromMe) { return; } @@ -1554,24 +1589,6 @@ export class TypebotService { ); } - // await this.processTypebot( - // instance, - // remoteJid, - // msg, - // session, - // findTypebot, - // url, - // expire, - // typebot, - // keywordFinish, - // delayMessage, - // unknownMessage, - // listeningFromMe, - // stopBotFromMe, - // keepOpen, - // content, - // ); - if (session && !session.awaitUser) return; } catch (error) { this.logger.error(error); @@ -1606,12 +1623,23 @@ export class TypebotService { const diffInMinutes = Math.floor(diff / 1000 / 60); if (diffInMinutes > expire) { - await this.prismaRepository.typebotSession.deleteMany({ - where: { - typebotId: findTypebot.id, - remoteJid: remoteJid, - }, - }); + if (keepOpen) { + await this.prismaRepository.typebotSession.update({ + where: { + id: session.id, + }, + data: { + status: 'closed', + }, + }); + } else { + await this.prismaRepository.typebotSession.deleteMany({ + where: { + typebotId: findTypebot.id, + remoteJid: remoteJid, + }, + }); + } const data = await this.createNewSession(instance, { enabled: findTypebot.enabled, @@ -1669,12 +1697,23 @@ export class TypebotService { } if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { - await this.prismaRepository.typebotSession.deleteMany({ - where: { - typebotId: findTypebot.id, - remoteJid: remoteJid, - }, - }); + if (keepOpen) { + await this.prismaRepository.typebotSession.update({ + where: { + id: session.id, + }, + data: { + status: 'closed', + }, + }); + } else { + await this.prismaRepository.typebotSession.deleteMany({ + where: { + typebotId: findTypebot.id, + remoteJid: remoteJid, + }, + }); + } return; } @@ -1783,12 +1822,23 @@ export class TypebotService { } if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { - await this.prismaRepository.typebotSession.deleteMany({ - where: { - typebotId: findTypebot.id, - remoteJid: remoteJid, - }, - }); + if (keepOpen) { + await this.prismaRepository.typebotSession.update({ + where: { + id: session.id, + }, + data: { + status: 'closed', + }, + }); + } else { + await this.prismaRepository.typebotSession.deleteMany({ + where: { + typebotId: findTypebot.id, + remoteJid: remoteJid, + }, + }); + } return; } @@ -1864,12 +1914,23 @@ export class TypebotService { } if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { - await this.prismaRepository.typebotSession.deleteMany({ - where: { - typebotId: findTypebot.id, - remoteJid: remoteJid, - }, - }); + if (keepOpen) { + await this.prismaRepository.typebotSession.update({ + where: { + id: session.id, + }, + data: { + status: 'closed', + }, + }); + } else { + await this.prismaRepository.typebotSession.deleteMany({ + where: { + typebotId: findTypebot.id, + remoteJid: remoteJid, + }, + }); + } return; } diff --git a/src/api/integrations/typebot/validate/typebot.schema.ts b/src/api/integrations/typebot/validate/typebot.schema.ts index dca16e60..32c54f4b 100644 --- a/src/api/integrations/typebot/validate/typebot.schema.ts +++ b/src/api/integrations/typebot/validate/typebot.schema.ts @@ -47,7 +47,7 @@ export const typebotStatusSchema: JSONSchema7 = { type: 'object', properties: { remoteJid: { type: 'string' }, - status: { type: 'string', enum: ['opened', 'closed', 'paused'] }, + status: { type: 'string', enum: ['opened', 'closed', 'paused', 'delete'] }, }, required: ['remoteJid', 'status'], ...isNotEmpty('remoteJid', 'status'), diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index e5a834b5..946ae1ba 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -1951,15 +1951,6 @@ export class BaileysStartupService extends ChannelStartupService { ); } - if (this.configService.get('TYPEBOT').ENABLED && !isIntegration) { - if (messageRaw.messageType !== 'reactionMessage') - await this.typebotService.sendTypebot( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw, - ); - } - if (this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) await this.prismaRepository.message.create({ data: messageRaw, diff --git a/src/api/services/channels/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts index 673a2761..d1f36e71 100644 --- a/src/api/services/channels/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -850,15 +850,6 @@ export class BusinessStartupService extends ChannelStartupService { ); } - if (this.configService.get('TYPEBOT').ENABLED && !isIntegration) { - if (messageRaw.messageType !== 'reactionMessage') - await this.typebotService.sendTypebot( - { instanceName: this.instance.name, instanceId: this.instanceId }, - messageRaw.key.remoteJid, - messageRaw, - ); - } - await this.prismaRepository.message.create({ data: messageRaw, });