From a6326861da050f75be96e6fbbdbcfe5b3f78ddb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Paulillo?= Date: Wed, 4 Sep 2024 12:48:17 -0300 Subject: [PATCH] Fixes Typebot.start, fetchSessions and adds typebot composite key migration --- .../migration.sql | 8 ++++++++ prisma/postgresql-schema.prisma | 2 ++ .../chatbot/typebot/controllers/typebot.controller.ts | 6 ++++-- .../integrations/chatbot/typebot/routes/typebot.router.ts | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 prisma/postgresql-migrations/20240904141414_add_typebot_composite_key/migration.sql diff --git a/prisma/postgresql-migrations/20240904141414_add_typebot_composite_key/migration.sql b/prisma/postgresql-migrations/20240904141414_add_typebot_composite_key/migration.sql new file mode 100644 index 00000000..4114901c --- /dev/null +++ b/prisma/postgresql-migrations/20240904141414_add_typebot_composite_key/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - A unique constraint covering the columns `[url,typebot,instanceId]` on the table `Typebot` will be added. If there are existing duplicate values, this will fail. + +*/ +-- CreateIndex +CREATE UNIQUE INDEX "Typebot_url_typebot_instanceId_key" ON "Typebot"("url", "typebot", "instanceId"); diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index 931d67bd..1729f6e6 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -311,6 +311,8 @@ model Typebot { Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) instanceId String TypebotSetting TypebotSetting[] + + @@unique([url, typebot, instanceId], name: "url_typebot_instanceId") } model TypebotSetting { diff --git a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts index b933341e..6e86650a 100644 --- a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts +++ b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts @@ -682,7 +682,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr stopBotFromMe: stopBotFromMe, keepOpen: keepOpen, prefilledVariables: prefilledVariables, - typebotId: findBot.id, + botId: findBot.id }); if (response.sessionId) { @@ -889,7 +889,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr throw new Error('Typebot not found'); } - return await this.sessionRepository.findMany({ + const sessions = await this.sessionRepository.findMany({ where: { instanceId: instanceId, remoteJid, @@ -897,6 +897,8 @@ export class TypebotController extends ChatbotController implements ChatbotContr type: 'typebot', }, }); + + return sessions; } catch (error) { this.logger.error(error); throw new Error('Error fetching sessions'); diff --git a/src/api/integrations/chatbot/typebot/routes/typebot.router.ts b/src/api/integrations/chatbot/typebot/routes/typebot.router.ts index f556f94f..05e54d9a 100644 --- a/src/api/integrations/chatbot/typebot/routes/typebot.router.ts +++ b/src/api/integrations/chatbot/typebot/routes/typebot.router.ts @@ -113,7 +113,7 @@ export class TypebotRouter extends RouterBroker { request: req, schema: instanceSchema, ClassRef: InstanceDto, - execute: (instance) => typebotController.fetchSessions(instance, req.params.typebotId), + execute: (instance) => typebotController.fetchSessions(instance, req.params.typebotId, req.query.remoteJid as string | undefined) }); res.status(HttpStatus.OK).json(response);