Unification of integrations, Typebot & OpenAI & Dify

This commit is contained in:
Judson Cairo 2024-08-14 17:47:31 -03:00
parent b68814795d
commit 1059c92583
7 changed files with 401 additions and 384 deletions

View File

@ -27,7 +27,7 @@ enum DeviceMessage {
desktop desktop
} }
enum TypebotSessionStatus { enum SessionStatus {
opened opened
closed closed
paused paused
@ -90,17 +90,15 @@ model Instance {
Typebot Typebot[] Typebot Typebot[]
Session Session? Session Session?
MessageUpdate MessageUpdate[] MessageUpdate MessageUpdate[]
TypebotSession TypebotSession[]
TypebotSetting TypebotSetting? TypebotSetting TypebotSetting?
Media Media[] Media Media[]
OpenaiCreds OpenaiCreds[] OpenaiCreds OpenaiCreds[]
OpenaiBot OpenaiBot[] OpenaiBot OpenaiBot[]
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting? OpenaiSetting OpenaiSetting?
Template Template[] Template Template[]
Dify Dify[] Dify Dify[]
DifySession DifySession[]
DifySetting DifySetting? DifySetting DifySetting?
integrationSessions IntegrationSession[]
} }
model Session { model Session {
@ -154,13 +152,11 @@ model Message {
instanceId String instanceId String
typebotSessionId String? typebotSessionId String?
MessageUpdate MessageUpdate[] MessageUpdate MessageUpdate[]
TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id])
Media Media? Media Media?
OpenaiSession OpenaiSession? @relation(fields: [openaiSessionId], references: [id])
openaiSessionId String?
webhookUrl String? @db.VarChar(500) webhookUrl String? @db.VarChar(500)
DifySession DifySession? @relation(fields: [difySessionId], references: [id])
difySessionId String? sessionId String?
sessions IntegrationSession? @relation(fields: [sessionId], references: [id])
} }
model MessageUpdate { model MessageUpdate {
@ -308,25 +304,8 @@ model Typebot {
triggerValue String? triggerValue String?
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
sessions TypebotSession[]
TypebotSetting TypebotSetting[] TypebotSetting TypebotSetting[]
} sessions IntegrationSession[]
model TypebotSession {
id String @id @default(cuid())
remoteJid String @db.VarChar(100)
pushName String? @db.VarChar(100)
sessionId String @db.VarChar(100)
status TypebotSessionStatus
prefilledVariables Json? @db.Json
awaitUser Boolean @default(false)
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
typebotId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
} }
model TypebotSetting { model TypebotSetting {
@ -348,6 +327,30 @@ model TypebotSetting {
instanceId String @unique instanceId String @unique
} }
model IntegrationSession {
id String @id @default(cuid())
sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100)
pushName String?
status SessionStatus
awaitUser Boolean @default(false)
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
parameters Json?
OpenaiBot OpenaiBot? @relation(fields: [openaiBotId], references: [id], onDelete: Cascade)
openaiBotId String?
DifyBot Dify? @relation(fields: [difyId], references: [id], onDelete: Cascade)
difyId String?
Typebot Typebot? @relation(fields: [typebotId], references: [id], onDelete: Cascade)
typebotId String?
}
model Media { model Media {
id String @id @default(cuid()) id String @id @default(cuid())
fileName String @unique @db.VarChar(500) fileName String @unique @db.VarChar(500)
@ -402,23 +405,8 @@ model OpenaiBot {
openaiCredsId String openaiCredsId String
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting[] OpenaiSetting OpenaiSetting[]
} sessions IntegrationSession[]
model OpenaiSession {
id String @id @default(cuid())
sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100)
status TypebotSessionStatus
awaitUser Boolean @default(false)
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
OpenaiBot OpenaiBot @relation(fields: [openaiBotId], references: [id], onDelete: Cascade)
openaiBotId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
} }
model OpenaiSetting { model OpenaiSetting {
@ -478,23 +466,8 @@ model Dify {
updatedAt DateTime @updatedAt @db.Timestamp updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
DifySession DifySession[]
DifySetting DifySetting[] DifySetting DifySetting[]
} sessions IntegrationSession[]
model DifySession {
id String @id @default(cuid())
sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100)
status TypebotSessionStatus
awaitUser Boolean @default(false)
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Dify Dify @relation(fields: [difyId], references: [id], onDelete: Cascade)
difyId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
} }
model DifySetting { model DifySetting {

View File

@ -0,0 +1,92 @@
/*
Warnings:
- You are about to drop the column `difySessionId` on the `Message` table. All the data in the column will be lost.
- You are about to drop the column `openaiSessionId` on the `Message` table. All the data in the column will be lost.
- You are about to drop the column `typebotSessionId` on the `Message` table. All the data in the column will be lost.
- You are about to drop the `DifySession` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `OpenaiSession` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `TypebotSession` table. If the table is not empty, all the data it contains will be lost.
*/
-- CreateEnum
CREATE TYPE "SessionStatus" AS ENUM ('opened', 'closed', 'paused');
-- DropForeignKey
ALTER TABLE "DifySession" DROP CONSTRAINT "DifySession_difyId_fkey";
-- DropForeignKey
ALTER TABLE "DifySession" DROP CONSTRAINT "DifySession_instanceId_fkey";
-- DropForeignKey
ALTER TABLE "Message" DROP CONSTRAINT "Message_difySessionId_fkey";
-- DropForeignKey
ALTER TABLE "Message" DROP CONSTRAINT "Message_openaiSessionId_fkey";
-- DropForeignKey
ALTER TABLE "Message" DROP CONSTRAINT "Message_typebotSessionId_fkey";
-- DropForeignKey
ALTER TABLE "OpenaiSession" DROP CONSTRAINT "OpenaiSession_instanceId_fkey";
-- DropForeignKey
ALTER TABLE "OpenaiSession" DROP CONSTRAINT "OpenaiSession_openaiBotId_fkey";
-- DropForeignKey
ALTER TABLE "TypebotSession" DROP CONSTRAINT "TypebotSession_instanceId_fkey";
-- DropForeignKey
ALTER TABLE "TypebotSession" DROP CONSTRAINT "TypebotSession_typebotId_fkey";
-- AlterTable
ALTER TABLE "Message" DROP COLUMN "difySessionId",
DROP COLUMN "openaiSessionId",
DROP COLUMN "typebotSessionId",
ADD COLUMN "sessionId" TEXT;
-- DropTable
DROP TABLE "DifySession";
-- DropTable
DROP TABLE "OpenaiSession";
-- DropTable
DROP TABLE "TypebotSession";
-- DropEnum
DROP TYPE "TypebotSessionStatus";
-- CreateTable
CREATE TABLE "IntegrationSession" (
"id" TEXT NOT NULL,
"sessionId" VARCHAR(255) NOT NULL,
"remoteJid" VARCHAR(100) NOT NULL,
"pushName" TEXT,
"status" "SessionStatus" NOT NULL,
"awaitUser" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP NOT NULL,
"instanceId" TEXT NOT NULL,
"parameters" JSONB,
"openaiBotId" TEXT,
"difyId" TEXT,
"typebotId" TEXT,
CONSTRAINT "IntegrationSession_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Message" ADD CONSTRAINT "Message_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "IntegrationSession"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "IntegrationSession" ADD CONSTRAINT "IntegrationSession_instanceId_fkey" FOREIGN KEY ("instanceId") REFERENCES "Instance"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "IntegrationSession" ADD CONSTRAINT "IntegrationSession_openaiBotId_fkey" FOREIGN KEY ("openaiBotId") REFERENCES "OpenaiBot"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "IntegrationSession" ADD CONSTRAINT "IntegrationSession_difyId_fkey" FOREIGN KEY ("difyId") REFERENCES "Dify"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "IntegrationSession" ADD CONSTRAINT "IntegrationSession_typebotId_fkey" FOREIGN KEY ("typebotId") REFERENCES "Typebot"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -27,7 +27,7 @@ enum DeviceMessage {
desktop desktop
} }
enum TypebotSessionStatus { enum SessionStatus {
opened opened
closed closed
paused paused
@ -88,19 +88,17 @@ model Instance {
Sqs Sqs? Sqs Sqs?
Websocket Websocket? Websocket Websocket?
Typebot Typebot[] Typebot Typebot[]
Session Session? sessions Session?
MessageUpdate MessageUpdate[] MessageUpdate MessageUpdate[]
TypebotSession TypebotSession[]
TypebotSetting TypebotSetting? TypebotSetting TypebotSetting?
Media Media[] Media Media[]
OpenaiCreds OpenaiCreds[] OpenaiCreds OpenaiCreds[]
OpenaiBot OpenaiBot[] OpenaiBot OpenaiBot[]
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting? OpenaiSetting OpenaiSetting?
Template Template[] Template Template[]
Dify Dify[] Dify Dify[]
DifySession DifySession[]
DifySetting DifySetting? DifySetting DifySetting?
integrationSessions IntegrationSession[]
} }
model Session { model Session {
@ -152,15 +150,12 @@ model Message {
chatwootIsRead Boolean? @db.Boolean chatwootIsRead Boolean? @db.Boolean
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
typebotSessionId String?
MessageUpdate MessageUpdate[] MessageUpdate MessageUpdate[]
TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id])
Media Media? Media Media?
OpenaiSession OpenaiSession? @relation(fields: [openaiSessionId], references: [id])
openaiSessionId String?
webhookUrl String? @db.VarChar(500) webhookUrl String? @db.VarChar(500)
DifySession DifySession? @relation(fields: [difySessionId], references: [id])
difySessionId String? sessionId String?
sessions IntegrationSession? @relation(fields: [sessionId], references: [id])
} }
model MessageUpdate { model MessageUpdate {
@ -310,25 +305,8 @@ model Typebot {
triggerValue String? triggerValue String?
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
sessions TypebotSession[]
TypebotSetting TypebotSetting[] TypebotSetting TypebotSetting[]
} sessions IntegrationSession[]
model TypebotSession {
id String @id @default(cuid())
remoteJid String @db.VarChar(100)
pushName String? @db.VarChar(100)
sessionId String @db.VarChar(100)
status TypebotSessionStatus
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)
typebotId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
} }
model TypebotSetting { model TypebotSetting {
@ -404,23 +382,32 @@ model OpenaiBot {
openaiCredsId String openaiCredsId String
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting[] OpenaiSetting OpenaiSetting[]
sessions IntegrationSession[]
} }
model OpenaiSession { model IntegrationSession {
id String @id @default(cuid()) id String @id @default(cuid())
sessionId String @db.VarChar(255) sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100) remoteJid String @db.VarChar(100)
status TypebotSessionStatus pushName String?
status SessionStatus
awaitUser Boolean @default(false) @db.Boolean awaitUser Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Timestamp createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp updatedAt DateTime @updatedAt @db.Timestamp
OpenaiBot OpenaiBot @relation(fields: [openaiBotId], references: [id], onDelete: Cascade)
openaiBotId String
Message Message[] Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
parameters Json? @db.JsonB
OpenaiBot OpenaiBot? @relation(fields: [openaiBotId], references: [id], onDelete: Cascade)
openaiBotId String?
DifyBot Dify? @relation(fields: [difyId], references: [id], onDelete: Cascade)
difyId String?
Typebot Typebot? @relation(fields: [typebotId], references: [id], onDelete: Cascade)
typebotId String?
} }
model OpenaiSetting { model OpenaiSetting {
@ -480,23 +467,8 @@ model Dify {
updatedAt DateTime @updatedAt @db.Timestamp updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
DifySession DifySession[]
DifySetting DifySetting[] DifySetting DifySetting[]
} sessions IntegrationSession[]
model DifySession {
id String @id @default(cuid())
sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100)
status TypebotSessionStatus
awaitUser Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Dify Dify @relation(fields: [difyId], references: [id], onDelete: Cascade)
difyId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
} }
model DifySetting { model DifySetting {

View File

@ -4,7 +4,7 @@ import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service'; import { WAMonitoringService } from '@api/services/monitor.service';
import { Auth, ConfigService, HttpServer, S3 } from '@config/env.config'; import { Auth, ConfigService, HttpServer, S3 } from '@config/env.config';
import { Logger } from '@config/logger.config'; import { Logger } from '@config/logger.config';
import { Dify, DifySession, DifySetting, Message } from '@prisma/client'; import { Dify, DifySetting, IntegrationSession, Message } from '@prisma/client';
import { sendTelemetry } from '@utils/sendTelemetry'; import { sendTelemetry } from '@utils/sendTelemetry';
import axios from 'axios'; import axios from 'axios';
import { Readable } from 'stream'; import { Readable } from 'stream';
@ -159,7 +159,7 @@ export class DifyService {
id: difyId, id: difyId,
}, },
include: { include: {
DifySession: true, sessions: true,
}, },
}); });
@ -298,7 +298,7 @@ export class DifyService {
instanceId: instanceId, instanceId: instanceId,
}, },
include: { include: {
DifySession: true, sessions: true,
}, },
}); });
@ -332,7 +332,7 @@ export class DifyService {
throw new Error('Dify not found'); throw new Error('Dify not found');
} }
try { try {
await this.prismaRepository.difySession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
difyId: difyId, difyId: difyId,
}, },
@ -554,35 +554,14 @@ export class DifyService {
throw new Error('Dify not found'); throw new Error('Dify not found');
} }
if (dify) { return await this.prismaRepository.integrationSession.findMany({
return await this.prismaRepository.difySession.findMany({
where: {
difyId: difyId,
},
include: {
Dify: true,
},
});
}
if (remoteJid) {
return await this.prismaRepository.difySession.findMany({
where: {
remoteJid: remoteJid,
difyId: difyId,
},
include: {
Dify: true,
},
});
}
return await this.prismaRepository.difySession.findMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid,
difyId: dify ? difyId : { not: null },
}, },
include: { include: {
Dify: true, DifyBot: true,
}, },
}); });
} catch (error) { } catch (error) {
@ -611,9 +590,10 @@ export class DifyService {
const status = data.status; const status = data.status;
if (status === 'delete') { if (status === 'delete') {
await this.prismaRepository.difySession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
difyId: { not: null },
}, },
}); });
@ -622,28 +602,31 @@ export class DifyService {
if (status === 'closed') { if (status === 'closed') {
if (defaultSettingCheck?.keepOpen) { if (defaultSettingCheck?.keepOpen) {
await this.prismaRepository.difySession.updateMany({ await this.prismaRepository.integrationSession.updateMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
difyId: { not: null },
}, },
data: { data: {
status: 'closed', status: 'closed',
}, },
}); });
} else { } else {
await this.prismaRepository.difySession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
difyId: { not: null },
}, },
}); });
} }
return { dify: { ...instance, dify: { remoteJid: remoteJid, status: status } } }; return { dify: { ...instance, dify: { remoteJid: remoteJid, status: status } } };
} else { } else {
const session = await this.prismaRepository.difySession.updateMany({ const session = await this.prismaRepository.integrationSession.updateMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid: remoteJid, remoteJid: remoteJid,
difyId: { not: null },
}, },
data: { data: {
status: status, status: status,
@ -905,13 +888,23 @@ export class DifyService {
} }
} }
const session = await this.prismaRepository.difySession.findFirst({ let session = await this.prismaRepository.integrationSession.findFirst({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId: instance.instanceId, instanceId: instance.instanceId,
}, },
orderBy: { createdAt: 'desc' },
}); });
if (session) {
if (session.status !== 'closed' && !session.difyId) {
this.logger.warn('Session is already opened in another integration');
return;
} else if (!session.difyId) {
session = null;
}
}
const content = this.getConversationMessage(msg); const content = this.getConversationMessage(msg);
let findDify = null; let findDify = null;
@ -976,7 +969,7 @@ export class DifyService {
}; };
if (stopBotFromMe && key.fromMe && session) { if (stopBotFromMe && key.fromMe && session) {
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1024,7 +1017,7 @@ export class DifyService {
public async createNewSession(instance: InstanceDto, data: any) { public async createNewSession(instance: InstanceDto, data: any) {
try { try {
const session = await this.prismaRepository.difySession.create({ const session = await this.prismaRepository.integrationSession.create({
data: { data: {
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
sessionId: data.remoteJid, sessionId: data.remoteJid,
@ -1047,7 +1040,7 @@ export class DifyService {
remoteJid: string, remoteJid: string,
dify: Dify, dify: Dify,
settings: DifySetting, settings: DifySetting,
session: DifySession, session: IntegrationSession,
content: string, content: string,
pushName?: string, pushName?: string,
) { ) {
@ -1101,7 +1094,7 @@ export class DifyService {
false, false,
); );
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1156,7 +1149,7 @@ export class DifyService {
false, false,
); );
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1231,7 +1224,7 @@ export class DifyService {
false, false,
); );
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1291,7 +1284,7 @@ export class DifyService {
); );
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1300,7 +1293,7 @@ export class DifyService {
}, },
}); });
} else { } else {
await this.prismaRepository.difySession.delete({ await this.prismaRepository.integrationSession.delete({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1319,7 +1312,7 @@ export class DifyService {
instance: any, instance: any,
remoteJid: string, remoteJid: string,
dify: Dify, dify: Dify,
session: DifySession, session: IntegrationSession,
settings: DifySetting, settings: DifySetting,
content: string, content: string,
pushName?: string, pushName?: string,
@ -1339,7 +1332,7 @@ export class DifyService {
if (diffInMinutes > settings.expire) { if (diffInMinutes > settings.expire) {
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1348,7 +1341,7 @@ export class DifyService {
}, },
}); });
} else { } else {
await this.prismaRepository.difySession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
difyId: dify.id, difyId: dify.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1366,7 +1359,7 @@ export class DifyService {
return; return;
} }
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1394,7 +1387,7 @@ export class DifyService {
if (settings.keywordFinish && content.toLowerCase() === settings.keywordFinish.toLowerCase()) { if (settings.keywordFinish && content.toLowerCase() === settings.keywordFinish.toLowerCase()) {
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1403,7 +1396,7 @@ export class DifyService {
}, },
}); });
} else { } else {
await this.prismaRepository.difySession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
difyId: dify.id, difyId: dify.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1454,7 +1447,7 @@ export class DifyService {
false, false,
); );
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1509,7 +1502,7 @@ export class DifyService {
false, false,
); );
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1590,7 +1583,7 @@ export class DifyService {
false, false,
); );
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1651,7 +1644,7 @@ export class DifyService {
); );
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.difySession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1660,7 +1653,7 @@ export class DifyService {
}, },
}); });
} else { } else {
await this.prismaRepository.difySession.delete({ await this.prismaRepository.integrationSession.delete({
where: { where: {
id: session.id, id: session.id,
}, },

View File

@ -9,7 +9,7 @@ import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service'; import { WAMonitoringService } from '@api/services/monitor.service';
import { ConfigService, Language, S3 } from '@config/env.config'; import { ConfigService, Language, S3 } from '@config/env.config';
import { Logger } from '@config/logger.config'; import { Logger } from '@config/logger.config';
import { Message, OpenaiBot, OpenaiCreds, OpenaiSession, OpenaiSetting } from '@prisma/client'; import { IntegrationSession, Message, OpenaiBot, OpenaiCreds, OpenaiSetting } from '@prisma/client';
import { sendTelemetry } from '@utils/sendTelemetry'; import { sendTelemetry } from '@utils/sendTelemetry';
import axios from 'axios'; import axios from 'axios';
import { downloadMediaMessage } from 'baileys'; import { downloadMediaMessage } from 'baileys';
@ -289,7 +289,7 @@ export class OpenaiService {
id: openaiBotId, id: openaiBotId,
}, },
include: { include: {
OpenaiSession: true, sessions: true,
}, },
}); });
@ -455,7 +455,7 @@ export class OpenaiService {
instanceId: instanceId, instanceId: instanceId,
}, },
include: { include: {
OpenaiSession: true, sessions: true,
}, },
}); });
@ -489,7 +489,7 @@ export class OpenaiService {
throw new Error('Openai bot not found'); throw new Error('Openai bot not found');
} }
try { try {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
openaiBotId: openaiBotId, openaiBotId: openaiBotId,
}, },
@ -758,32 +758,11 @@ export class OpenaiService {
throw new Error('Openai Bot not found'); throw new Error('Openai Bot not found');
} }
if (openaiBot) { return await this.prismaRepository.integrationSession.findMany({
return await this.prismaRepository.openaiSession.findMany({
where: {
openaiBotId: openaiBotId,
},
include: {
OpenaiBot: true,
},
});
}
if (remoteJid) {
return await this.prismaRepository.openaiSession.findMany({
where: {
remoteJid: remoteJid,
openaiBotId: openaiBotId,
},
include: {
OpenaiBot: true,
},
});
}
return await this.prismaRepository.openaiSession.findMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid,
openaiBotId: openaiBot ? openaiBotId : { not: null },
}, },
include: { include: {
OpenaiBot: true, OpenaiBot: true,
@ -815,9 +794,10 @@ export class OpenaiService {
const status = data.status; const status = data.status;
if (status === 'delete') { if (status === 'delete') {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
openaiBotId: { not: null },
}, },
}); });
@ -826,16 +806,18 @@ export class OpenaiService {
if (status === 'closed') { if (status === 'closed') {
if (defaultSettingCheck?.keepOpen) { if (defaultSettingCheck?.keepOpen) {
await this.prismaRepository.openaiSession.updateMany({ await this.prismaRepository.integrationSession.updateMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
openaiBotId: { not: null },
status: { not: 'closed' },
}, },
data: { data: {
status: 'closed', status: 'closed',
}, },
}); });
} else { } else {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
}, },
@ -844,10 +826,11 @@ export class OpenaiService {
return { openai: { ...instance, openai: { remoteJid: remoteJid, status: status } } }; return { openai: { ...instance, openai: { remoteJid: remoteJid, status: status } } };
} else { } else {
const session = await this.prismaRepository.openaiSession.updateMany({ const session = await this.prismaRepository.integrationSession.updateMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid: remoteJid, remoteJid: remoteJid,
openaiBotId: { not: null },
}, },
data: { data: {
status: status, status: status,
@ -1109,13 +1092,23 @@ export class OpenaiService {
} }
} }
const session = await this.prismaRepository.openaiSession.findFirst({ let session = await this.prismaRepository.integrationSession.findFirst({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId: instance.instanceId, instanceId: instance.instanceId,
}, },
orderBy: { createdAt: 'desc' },
}); });
if (session) {
if (session.status !== 'closed' && !session.openaiBotId) {
this.logger.warn('Session is already opened in another integration');
return;
} else if (!session.openaiBotId) {
session = null;
}
}
const content = this.getConversationMessage(msg); const content = this.getConversationMessage(msg);
let findOpenai = null; let findOpenai = null;
@ -1184,7 +1177,7 @@ export class OpenaiService {
}; };
if (stopBotFromMe && key.fromMe && session) { if (stopBotFromMe && key.fromMe && session) {
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1274,7 +1267,7 @@ export class OpenaiService {
let session = null; let session = null;
if (threadId) { if (threadId) {
session = await this.prismaRepository.openaiSession.create({ session = await this.prismaRepository.integrationSession.create({
data: { data: {
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
sessionId: threadId, sessionId: threadId,
@ -1297,7 +1290,7 @@ export class OpenaiService {
remoteJid: string, remoteJid: string,
openaiBot: OpenaiBot, openaiBot: OpenaiBot,
settings: OpenaiSetting, settings: OpenaiSetting,
session: OpenaiSession, session: IntegrationSession,
content: string, content: string,
) { ) {
const data = await this.createAssistantNewSession(instance, { const data = await this.createAssistantNewSession(instance, {
@ -1338,7 +1331,7 @@ export class OpenaiService {
false, false,
); );
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1431,7 +1424,7 @@ export class OpenaiService {
instance: any, instance: any,
remoteJid: string, remoteJid: string,
openaiBot: OpenaiBot, openaiBot: OpenaiBot,
session: OpenaiSession, session: IntegrationSession,
settings: OpenaiSetting, settings: OpenaiSetting,
content: string, content: string,
) { ) {
@ -1450,7 +1443,7 @@ export class OpenaiService {
if (diffInMinutes > settings.expire) { if (diffInMinutes > settings.expire) {
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1459,7 +1452,7 @@ export class OpenaiService {
}, },
}); });
} else { } else {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
openaiBotId: openaiBot.id, openaiBotId: openaiBot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1477,7 +1470,7 @@ export class OpenaiService {
return; return;
} }
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1505,7 +1498,7 @@ export class OpenaiService {
if (settings.keywordFinish && content.toLowerCase() === settings.keywordFinish.toLowerCase()) { if (settings.keywordFinish && content.toLowerCase() === settings.keywordFinish.toLowerCase()) {
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1514,7 +1507,7 @@ export class OpenaiService {
}, },
}); });
} else { } else {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
openaiBotId: openaiBot.id, openaiBotId: openaiBot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1566,7 +1559,7 @@ export class OpenaiService {
false, false,
); );
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1595,7 +1588,7 @@ export class OpenaiService {
if (!creds) throw new Error('Openai Creds not found'); if (!creds) throw new Error('Openai Creds not found');
try { try {
const session = await this.prismaRepository.openaiSession.create({ const session = await this.prismaRepository.integrationSession.create({
data: { data: {
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
sessionId: id, sessionId: id,
@ -1618,7 +1611,7 @@ export class OpenaiService {
remoteJid: string, remoteJid: string,
openaiBot: OpenaiBot, openaiBot: OpenaiBot,
settings: OpenaiSetting, settings: OpenaiSetting,
session: OpenaiSession, session: IntegrationSession,
content: string, content: string,
) { ) {
const data = await this.createChatCompletionNewSession(instance, { const data = await this.createChatCompletionNewSession(instance, {
@ -1694,7 +1687,7 @@ export class OpenaiService {
false, false,
); );
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1713,7 +1706,7 @@ export class OpenaiService {
instance: any, instance: any,
remoteJid: string, remoteJid: string,
openaiBot: OpenaiBot, openaiBot: OpenaiBot,
session: OpenaiSession, session: IntegrationSession,
settings: OpenaiSetting, settings: OpenaiSetting,
content: string, content: string,
) { ) {
@ -1732,7 +1725,7 @@ export class OpenaiService {
if (diffInMinutes > settings.expire) { if (diffInMinutes > settings.expire) {
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1741,7 +1734,7 @@ export class OpenaiService {
}, },
}); });
} else { } else {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
openaiBotId: openaiBot.id, openaiBotId: openaiBot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1759,7 +1752,7 @@ export class OpenaiService {
return; return;
} }
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1787,7 +1780,7 @@ export class OpenaiService {
if (settings.keywordFinish && content.toLowerCase() === settings.keywordFinish.toLowerCase()) { if (settings.keywordFinish && content.toLowerCase() === settings.keywordFinish.toLowerCase()) {
if (settings.keepOpen) { if (settings.keepOpen) {
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1796,7 +1789,7 @@ export class OpenaiService {
}, },
}); });
} else { } else {
await this.prismaRepository.openaiSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
openaiBotId: openaiBot.id, openaiBotId: openaiBot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1878,7 +1871,7 @@ export class OpenaiService {
false, false,
); );
await this.prismaRepository.openaiSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },

View File

@ -5,7 +5,7 @@ import { WAMonitoringService } from '@api/services/monitor.service';
import { Events } from '@api/types/wa.types'; import { Events } from '@api/types/wa.types';
import { Auth, ConfigService, HttpServer, S3, Typebot } from '@config/env.config'; import { Auth, ConfigService, HttpServer, S3, Typebot } from '@config/env.config';
import { Logger } from '@config/logger.config'; import { Logger } from '@config/logger.config';
import { Instance, Message, Typebot as TypebotModel, TypebotSession } from '@prisma/client'; import { Instance, IntegrationSession, Message, Typebot as TypebotModel } from '@prisma/client';
import { sendTelemetry } from '@utils/sendTelemetry'; import { sendTelemetry } from '@utils/sendTelemetry';
import axios from 'axios'; import axios from 'axios';
@ -329,7 +329,7 @@ export class TypebotService {
throw new Error('Typebot not found'); throw new Error('Typebot not found');
} }
try { try {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
typebotId: typebotId, typebotId: typebotId,
}, },
@ -551,32 +551,11 @@ export class TypebotService {
throw new Error('Typebot not found'); throw new Error('Typebot not found');
} }
if (typebotId) { return await this.prismaRepository.integrationSession.findMany({
return await this.prismaRepository.typebotSession.findMany({
where: {
typebotId: typebotId,
},
include: {
Typebot: true,
},
});
}
if (remoteJid) {
return await this.prismaRepository.typebotSession.findMany({
where: {
remoteJid: remoteJid,
instanceId: instanceId,
},
include: {
Typebot: true,
},
});
}
return await this.prismaRepository.typebotSession.findMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid,
typebotId: typebotId ?? { not: null },
}, },
include: { include: {
Typebot: true, Typebot: true,
@ -608,10 +587,11 @@ export class TypebotService {
}); });
if (status === 'delete') { if (status === 'delete') {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId: instanceId, instanceId: instanceId,
typebotId: { not: null },
}, },
}); });
@ -620,20 +600,22 @@ export class TypebotService {
if (status === 'closed') { if (status === 'closed') {
if (defaultSettingCheck?.keepOpen) { if (defaultSettingCheck?.keepOpen) {
await this.prismaRepository.typebotSession.updateMany({ await this.prismaRepository.integrationSession.updateMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid: remoteJid, remoteJid: remoteJid,
typebotId: { not: null },
}, },
data: { data: {
status: status, status: status,
}, },
}); });
} else { } else {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId: instanceId, instanceId: instanceId,
typebotId: { not: null },
}, },
}); });
} }
@ -641,10 +623,11 @@ export class TypebotService {
return { typebot: { ...instance, typebot: { remoteJid: remoteJid, status: status } } }; return { typebot: { ...instance, typebot: { remoteJid: remoteJid, status: status } } };
} }
const session = await this.prismaRepository.typebotSession.updateMany({ const session = await this.prismaRepository.integrationSession.updateMany({
where: { where: {
instanceId: instanceId, instanceId: instanceId,
remoteJid: remoteJid, remoteJid: remoteJid,
typebotId: { not: null },
}, },
data: { data: {
status: status, status: status,
@ -798,10 +781,11 @@ export class TypebotService {
}); });
} }
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId: instanceData.id, instanceId: instanceData.id,
typebotId: { not: null },
}, },
}); });
@ -1015,13 +999,13 @@ export class TypebotService {
let session = null; let session = null;
if (request?.data?.sessionId) { if (request?.data?.sessionId) {
session = await this.prismaRepository.typebotSession.create({ session = await this.prismaRepository.integrationSession.create({
data: { data: {
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
pushName: data.pushName || '', pushName: data.pushName || '',
sessionId: `${id}-${request.data.sessionId}`, sessionId: `${id}-${request.data.sessionId}`,
status: 'opened', status: 'opened',
prefilledVariables: { parameters: {
...data.prefilledVariables, ...data.prefilledVariables,
remoteJid: data.remoteJid, remoteJid: data.remoteJid,
pushName: data.pushName || '', pushName: data.pushName || '',
@ -1045,7 +1029,7 @@ export class TypebotService {
public async sendWAMessage( public async sendWAMessage(
instance: Instance, instance: Instance,
session: TypebotSession, session: IntegrationSession,
settings: { settings: {
expire: number; expire: number;
keywordFinish: string; keywordFinish: string;
@ -1146,7 +1130,7 @@ export class TypebotService {
async function processMessages( async function processMessages(
instance: any, instance: any,
session: TypebotSession, session: IntegrationSession,
settings: { settings: {
expire: number; expire: number;
keywordFinish: string; keywordFinish: string;
@ -1262,7 +1246,7 @@ export class TypebotService {
sendTelemetry('/message/sendText'); sendTelemetry('/message/sendText');
} }
await prismaRepository.typebotSession.update({ await prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1272,13 +1256,13 @@ export class TypebotService {
}); });
} else { } else {
if (!settings?.keepOpen) { if (!settings?.keepOpen) {
await prismaRepository.typebotSession.deleteMany({ await prismaRepository.integrationSession.deleteMany({
where: { where: {
id: session.id, id: session.id,
}, },
}); });
} else { } else {
await prismaRepository.typebotSession.update({ await prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1488,13 +1472,23 @@ export class TypebotService {
} }
} }
const session = await this.prismaRepository.typebotSession.findFirst({ let session = await this.prismaRepository.integrationSession.findFirst({
where: { where: {
remoteJid: remoteJid, remoteJid: remoteJid,
instanceId: instance.instanceId, instanceId: instance.instanceId,
}, },
orderBy: { createdAt: 'desc' },
}); });
if (session) {
if (session.status !== 'closed' && !session.typebotId) {
this.logger.warn('Session is already opened in another integration');
return;
} else if (!session.typebotId) {
session = null;
}
}
const content = this.getConversationMessage(msg); const content = this.getConversationMessage(msg);
let findTypebot = null; let findTypebot = null;
@ -1558,7 +1552,7 @@ export class TypebotService {
}; };
if (stopBotFromMe && key.fromMe && session) { if (stopBotFromMe && key.fromMe && session) {
await this.prismaRepository.typebotSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1624,7 +1618,7 @@ export class TypebotService {
instance: Instance, instance: Instance,
remoteJid: string, remoteJid: string,
msg: Message, msg: Message,
session: TypebotSession, session: IntegrationSession,
findTypebot: TypebotModel, findTypebot: TypebotModel,
url: string, url: string,
expire: number, expire: number,
@ -1648,7 +1642,7 @@ export class TypebotService {
if (diffInMinutes > expire) { if (diffInMinutes > expire) {
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.typebotSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1657,7 +1651,7 @@ export class TypebotService {
}, },
}); });
} else { } else {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
typebotId: findTypebot.id, typebotId: findTypebot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1722,7 +1716,7 @@ export class TypebotService {
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.typebotSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1731,7 +1725,7 @@ export class TypebotService {
}, },
}); });
} else { } else {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
typebotId: findTypebot.id, typebotId: findTypebot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1847,7 +1841,7 @@ export class TypebotService {
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.typebotSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1856,7 +1850,7 @@ export class TypebotService {
}, },
}); });
} else { } else {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
typebotId: findTypebot.id, typebotId: findTypebot.id,
remoteJid: remoteJid, remoteJid: remoteJid,
@ -1911,7 +1905,7 @@ export class TypebotService {
return; return;
} }
await this.prismaRepository.typebotSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1939,7 +1933,7 @@ export class TypebotService {
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.typebotSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
id: session.id, id: session.id,
}, },
@ -1948,7 +1942,7 @@ export class TypebotService {
}, },
}); });
} else { } else {
await this.prismaRepository.typebotSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
typebotId: findTypebot.id, typebotId: findTypebot.id,
remoteJid: remoteJid, remoteJid: remoteJid,

View File

@ -168,7 +168,7 @@ export class WAMonitoringService {
await this.prismaRepository.proxy.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.proxy.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.rabbitmq.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.rabbitmq.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.sqs.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.sqs.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.typebotSession.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.integrationSession.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.typebot.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.typebot.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.websocket.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.websocket.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.setting.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.setting.deleteMany({ where: { instanceId: instance.id } });