Merge pull request #773 from judsonjuniorr/V2-integration-unification

V2 integration unification
This commit is contained in:
Davidson Gomes 2024-08-14 18:36:27 -03:00 committed by GitHub
commit 376cafa350
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 401 additions and 384 deletions

View File

@ -105,7 +105,7 @@
"@types/express": "^4.17.17",
"@types/js-yaml": "^4.0.5",
"@types/json-schema": "^7.0.15",
"@types/mime": "^4.0.0",
"@types/mime": "3.0.0",
"@types/node": "^18.15.11",
"@types/node-windows": "^0.1.2",
"@types/qrcode": "^1.5.0",

View File

@ -27,7 +27,7 @@ enum DeviceMessage {
desktop
}
enum TypebotSessionStatus {
enum SessionStatus {
opened
closed
paused
@ -90,17 +90,15 @@ model Instance {
Typebot Typebot[]
Session Session?
MessageUpdate MessageUpdate[]
TypebotSession TypebotSession[]
TypebotSetting TypebotSetting?
Media Media[]
OpenaiCreds OpenaiCreds[]
OpenaiBot OpenaiBot[]
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting?
Template Template[]
Dify Dify[]
DifySession DifySession[]
DifySetting DifySetting?
integrationSessions IntegrationSession[]
}
model Session {
@ -154,13 +152,11 @@ model Message {
instanceId String
typebotSessionId String?
MessageUpdate MessageUpdate[]
TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id])
Media Media?
OpenaiSession OpenaiSession? @relation(fields: [openaiSessionId], references: [id])
openaiSessionId String?
webhookUrl String? @db.VarChar(500)
DifySession DifySession? @relation(fields: [difySessionId], references: [id])
difySessionId String?
sessionId String?
session IntegrationSession? @relation(fields: [sessionId], references: [id])
}
model MessageUpdate {
@ -308,25 +304,8 @@ model Typebot {
triggerValue String?
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
sessions TypebotSession[]
TypebotSetting TypebotSetting[]
}
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
sessions IntegrationSession[]
}
model TypebotSetting {
@ -348,6 +327,30 @@ model TypebotSetting {
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 {
id String @id @default(cuid())
fileName String @unique @db.VarChar(500)
@ -402,23 +405,8 @@ model OpenaiBot {
openaiCredsId String
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting[]
}
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
sessions IntegrationSession[]
}
model OpenaiSetting {
@ -478,23 +466,8 @@ model Dify {
updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
DifySession DifySession[]
DifySetting DifySetting[]
}
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
sessions IntegrationSession[]
}
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
}
enum TypebotSessionStatus {
enum SessionStatus {
opened
closed
paused
@ -90,17 +90,15 @@ model Instance {
Typebot Typebot[]
Session Session?
MessageUpdate MessageUpdate[]
TypebotSession TypebotSession[]
TypebotSetting TypebotSetting?
Media Media[]
OpenaiCreds OpenaiCreds[]
OpenaiBot OpenaiBot[]
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting?
Template Template[]
Dify Dify[]
DifySession DifySession[]
DifySetting DifySetting?
integrationSessions IntegrationSession[]
}
model Session {
@ -152,15 +150,12 @@ model Message {
chatwootIsRead Boolean? @db.Boolean
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
typebotSessionId String?
MessageUpdate MessageUpdate[]
TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id])
Media Media?
OpenaiSession OpenaiSession? @relation(fields: [openaiSessionId], references: [id])
openaiSessionId String?
webhookUrl String? @db.VarChar(500)
DifySession DifySession? @relation(fields: [difySessionId], references: [id])
difySessionId String?
sessionId String?
session IntegrationSession? @relation(fields: [sessionId], references: [id])
}
model MessageUpdate {
@ -310,25 +305,8 @@ model Typebot {
triggerValue String?
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
sessions TypebotSession[]
TypebotSetting TypebotSetting[]
}
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
sessions IntegrationSession[]
}
model TypebotSetting {
@ -404,23 +382,32 @@ model OpenaiBot {
openaiCredsId String
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting[]
sessions IntegrationSession[]
}
model OpenaiSession {
model IntegrationSession {
id String @id @default(cuid())
sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100)
status TypebotSessionStatus
pushName String?
status SessionStatus
awaitUser Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @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
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 {
@ -480,23 +467,8 @@ model Dify {
updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
DifySession DifySession[]
DifySetting DifySetting[]
}
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
sessions IntegrationSession[]
}
model DifySetting {

View File

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

View File

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

View File

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

View File

@ -168,7 +168,7 @@ export class WAMonitoringService {
await this.prismaRepository.proxy.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.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.websocket.deleteMany({ where: { instanceId: instance.id } });
await this.prismaRepository.setting.deleteMany({ where: { instanceId: instance.id } });