From c03e0cc9544eadee4a78e1afe15d2ef3e3171e3a Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Mon, 29 Jul 2024 17:29:18 -0300 Subject: [PATCH] fix: corrected typebot trigger validation Explanation: This commit fixes an issue in the validation of typebot triggers. The 'findFirst' method was used to search for triggers, but it only returns the first match found. To solve this problem, the 'findMany' method was used instead, and a loop was added to search for all matches. This change ensures that all triggers that match the validation criteria are returned. The modified file is: - typebot.service.ts: this is the main file where the changes were made. It was necessary to modify the function that validates the triggers, replacing the 'findFirst' method with the 'findMany' method and adding a loop to search for all matches. This change improves the validation of typebot triggers, ensuring that all matches are returned, and prevents errors in the trigger validation process. --- .../typebot/services/typebot.service.ts | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index 547d9061..f0d0441c 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -1292,48 +1292,66 @@ export class TypebotService { if (findTriggerRegex) return findTriggerRegex; // Check for startsWith match - const findTriggerStartsWith = await this.prismaRepository.typebot.findFirst({ + const findStartsWith = await this.prismaRepository.typebot.findMany({ where: { enabled: true, triggerType: 'keyword', triggerOperator: 'startsWith', - triggerValue: { - startsWith: content, - }, instanceId: instanceId, }, }); + let findTriggerStartsWith = null; + + for (const startsWith of findStartsWith) { + if (content.startsWith(startsWith.triggerValue)) { + findTriggerStartsWith = startsWith; + break; + } + } + if (findTriggerStartsWith) return findTriggerStartsWith; // Check for endsWith match - const findTriggerEndsWith = await this.prismaRepository.typebot.findFirst({ + const findEndsWith = await this.prismaRepository.typebot.findMany({ where: { enabled: true, triggerType: 'keyword', triggerOperator: 'endsWith', - triggerValue: { - endsWith: content, - }, instanceId: instanceId, }, }); + let findTriggerEndsWith = null; + + for (const endsWith of findEndsWith) { + if (content.endsWith(endsWith.triggerValue)) { + findTriggerEndsWith = endsWith; + break; + } + } + if (findTriggerEndsWith) return findTriggerEndsWith; // Check for contains match - const findTriggerContains = await this.prismaRepository.typebot.findFirst({ + const findContains = await this.prismaRepository.typebot.findMany({ where: { enabled: true, triggerType: 'keyword', triggerOperator: 'contains', - triggerValue: { - contains: content, - }, instanceId: instanceId, }, }); + let findTriggerContains = null; + + for (const contains of findContains) { + if (content.includes(contains.triggerValue)) { + findTriggerContains = contains; + break; + } + } + if (findTriggerContains) return findTriggerContains; const fallback = await this.prismaRepository.typebotSetting.findFirst({