From 2a7f9698d2ad0af94eef9d7c00cad68f0be16441 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Mon, 29 Jul 2024 15:09:40 -0300 Subject: [PATCH] fix: adjusts for new manager --- .../migration.sql | 20 +++++++++++++++ prisma/mysql-schema.prisma | 1 + prisma/postgresql-schema.prisma | 4 +-- .../openai/services/openai.service.ts | 25 +++++++++---------- .../openai/validate/openai.schema.ts | 4 +-- .../typebot/services/typebot.service.ts | 3 +-- .../typebot/validate/typebot.schema.ts | 2 +- 7 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 prisma/migrations/20240729180347_modify_typebot_session_status_openai_typebot_table/migration.sql diff --git a/prisma/migrations/20240729180347_modify_typebot_session_status_openai_typebot_table/migration.sql b/prisma/migrations/20240729180347_modify_typebot_session_status_openai_typebot_table/migration.sql new file mode 100644 index 00000000..2fad828c --- /dev/null +++ b/prisma/migrations/20240729180347_modify_typebot_session_status_openai_typebot_table/migration.sql @@ -0,0 +1,20 @@ +/* + Warnings: + + - The values [open] on the enum `TypebotSessionStatus` will be removed. If these variants are still used in the database, this will fail. + - Changed the type of `status` on the `TypebotSession` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + +*/ +-- AlterEnum +BEGIN; +CREATE TYPE "TypebotSessionStatus_new" AS ENUM ('opened', 'closed', 'paused'); +ALTER TABLE "TypebotSession" ALTER COLUMN "status" TYPE "TypebotSessionStatus_new" USING ("status"::text::"TypebotSessionStatus_new"); +ALTER TABLE "OpenaiSession" ALTER COLUMN "status" TYPE "TypebotSessionStatus_new" USING ("status"::text::"TypebotSessionStatus_new"); +ALTER TYPE "TypebotSessionStatus" RENAME TO "TypebotSessionStatus_old"; +ALTER TYPE "TypebotSessionStatus_new" RENAME TO "TypebotSessionStatus"; +DROP TYPE "TypebotSessionStatus_old"; +COMMIT; + +-- AlterTable +ALTER TABLE "TypebotSession" DROP COLUMN "status", +ADD COLUMN "status" "TypebotSessionStatus" NOT NULL; diff --git a/prisma/mysql-schema.prisma b/prisma/mysql-schema.prisma index 55fdaba3..533907fa 100644 --- a/prisma/mysql-schema.prisma +++ b/prisma/mysql-schema.prisma @@ -36,6 +36,7 @@ enum TypebotSessionStatus { enum TriggerType { all keyword + none } enum TriggerOperator { diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index 00050b8a..da924b5f 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -28,7 +28,7 @@ enum DeviceMessage { } enum TypebotSessionStatus { - open + opened closed paused } @@ -292,7 +292,7 @@ model TypebotSession { remoteJid String @db.VarChar(100) pushName String? @db.VarChar(100) sessionId String @db.VarChar(100) - status String @db.VarChar(100) + status TypebotSessionStatus prefilledVariables Json? @db.JsonB awaitUser Boolean @default(false) @db.Boolean createdAt DateTime? @default(now()) @db.Timestamp diff --git a/src/api/integrations/openai/services/openai.service.ts b/src/api/integrations/openai/services/openai.service.ts index a5bea18b..ad71e06d 100644 --- a/src/api/integrations/openai/services/openai.service.ts +++ b/src/api/integrations/openai/services/openai.service.ts @@ -371,7 +371,7 @@ export class OpenaiService { throw new Error('Openai Bot already exists'); } - if (data.triggerType !== 'all') { + if (data.triggerType === 'keyword') { if (!data.triggerOperator || !data.triggerValue) { throw new Error('Trigger operator and value are required'); } @@ -449,7 +449,6 @@ export class OpenaiService { }); if (!openaiBots.length) { - this.logger.error('Openai bot not found'); return null; } @@ -618,7 +617,7 @@ export class OpenaiService { stopBotFromMe: settings.stopBotFromMe, keepOpen: settings.keepOpen, ignoreJids: settings.ignoreJids, - openaiIdFallback: settings.Fallback, + openaiIdFallback: settings.openaiIdFallback, fallback: settings.Fallback, }; } catch (error) { @@ -1152,7 +1151,7 @@ export class OpenaiService { data: { remoteJid: data.remoteJid, sessionId: threadId, - status: 'open', + status: 'opened', awaitUser: false, openaiBotId: data.openaiBotId, instanceId: instance.instanceId, @@ -1217,7 +1216,7 @@ export class OpenaiService { id: session.id, }, data: { - status: 'open', + status: 'opened', awaitUser: true, }, }); @@ -1255,7 +1254,7 @@ export class OpenaiService { settings: OpenaiSetting, content: string, ) { - if (session && session.status !== 'open') { + if (session && session.status !== 'opened') { return; } @@ -1291,7 +1290,7 @@ export class OpenaiService { id: session.id, }, data: { - status: 'open', + status: 'opened', awaitUser: false, }, }); @@ -1369,7 +1368,7 @@ export class OpenaiService { id: session.id, }, data: { - status: 'open', + status: 'opened', awaitUser: true, }, }); @@ -1397,7 +1396,7 @@ export class OpenaiService { data: { remoteJid: data.remoteJid, sessionId: id, - status: 'open', + status: 'opened', awaitUser: false, openaiBotId: data.openaiBotId, instanceId: instance.instanceId, @@ -1497,7 +1496,7 @@ export class OpenaiService { id: session.id, }, data: { - status: 'open', + status: 'opened', awaitUser: true, }, }); @@ -1515,7 +1514,7 @@ export class OpenaiService { settings: OpenaiSetting, content: string, ) { - if (session && session.status !== 'open') { + if (session && session.status !== 'opened') { return; } @@ -1551,7 +1550,7 @@ export class OpenaiService { id: session.id, }, data: { - status: 'open', + status: 'opened', awaitUser: false, }, }); @@ -1659,7 +1658,7 @@ export class OpenaiService { id: session.id, }, data: { - status: 'open', + status: 'opened', awaitUser: true, }, }); diff --git a/src/api/integrations/openai/validate/openai.schema.ts b/src/api/integrations/openai/validate/openai.schema.ts index f7ee30fe..bb8e280c 100644 --- a/src/api/integrations/openai/validate/openai.schema.ts +++ b/src/api/integrations/openai/validate/openai.schema.ts @@ -33,8 +33,8 @@ export const openaiSchema: JSONSchema7 = { assistantMessages: { type: 'array', items: { type: 'string' } }, userMessages: { type: 'array', items: { type: 'string' } }, maxTokens: { type: 'integer' }, - triggerType: { type: 'string', enum: ['all', 'keyword'] }, - triggerOperator: { type: 'string', enum: ['equals', 'contains', 'startsWith', 'endsWith', 'regex', 'none'] }, + triggerType: { type: 'string', enum: ['all', 'keyword', 'none'] }, + triggerOperator: { type: 'string', enum: ['equals', 'contains', 'startsWith', 'endsWith', 'regex'] }, triggerValue: { type: 'string' }, expire: { type: 'integer' }, keywordFinish: { type: 'string' }, diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index 3f3b4b36..547d9061 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -229,7 +229,7 @@ export class TypebotService { throw new Error('Typebot already exists'); } - if (data.triggerType !== 'all') { + if (data.triggerType === 'keyword') { if (!data.triggerOperator || !data.triggerValue) { throw new Error('Trigger operator and value are required'); } @@ -300,7 +300,6 @@ export class TypebotService { }); if (!typebots.length) { - this.logger.error('Typebot not found'); return null; } diff --git a/src/api/integrations/typebot/validate/typebot.schema.ts b/src/api/integrations/typebot/validate/typebot.schema.ts index 838d2da5..dca16e60 100644 --- a/src/api/integrations/typebot/validate/typebot.schema.ts +++ b/src/api/integrations/typebot/validate/typebot.schema.ts @@ -27,7 +27,7 @@ export const typebotSchema: JSONSchema7 = { enabled: { type: 'boolean' }, url: { type: 'string' }, typebot: { type: 'string' }, - triggerType: { type: 'string', enum: ['all', 'keyword'] }, + triggerType: { type: 'string', enum: ['all', 'keyword', 'none'] }, triggerOperator: { type: 'string', enum: ['equals', 'contains', 'startsWith', 'endsWith', 'regex'] }, triggerValue: { type: 'string' }, expire: { type: 'integer' },