mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-18 19:32:21 -06:00
Unification of integrations, Typebot & OpenAI & Dify
This commit is contained in:
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 } });
|
||||
|
||||
Reference in New Issue
Block a user