From 8f1be98d826f86dce1166ac350606362cc2791a2 Mon Sep 17 00:00:00 2001 From: Oriol Raventos Date: Mon, 4 Nov 2024 13:55:48 +0100 Subject: [PATCH] refactor(botonic-react): replace bot action id for payload with params in pre function --- .../botonic-plugin-flow-builder/src/index.ts | 16 +++++++++- .../tests/bot-action.test.ts | 32 ++++++++++++------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/botonic-plugin-flow-builder/src/index.ts b/packages/botonic-plugin-flow-builder/src/index.ts index ad5360ea80..edbeedf549 100644 --- a/packages/botonic-plugin-flow-builder/src/index.ts +++ b/packages/botonic-plugin-flow-builder/src/index.ts @@ -21,6 +21,7 @@ import { FlowWhatsappCtaUrlButtonNode, } from './content-fields' import { + HtBotActionNode, HtFlowBuilderData, HtFunctionArgument, HtFunctionArguments, @@ -108,16 +109,29 @@ export default class BotonicPluginFlowBuilder implements Plugin { this.updateRequestBeforeRoutes(request) } - private updateRequestBeforeRoutes(request: PluginPreRequest) { + private updateRequestBeforeRoutes(request: PluginPreRequest): void { if (request.input.payload) { request.input.payload = this.removeSourceSufix(request.input.payload) } + + if (request.input.payload && this.isBotAction(request.input.payload)) { + const cmsBotAction = this.cmsApi.getNodeById( + request.input.payload + ) + + request.input.payload = this.cmsApi.createPayloadWithParams(cmsBotAction) + } } private removeSourceSufix(payload: string): string { return payload.split(SOURCE_INFO_SEPARATOR)[0] } + private isBotAction(payload: string): boolean { + const botActionNode = this.cmsApi.getNodeById(payload) + return botActionNode?.type === HtNodeWithContentType.BOT_ACTION + } + async getContentsByContentID( contentID: string, locale: string, diff --git a/packages/botonic-plugin-flow-builder/tests/bot-action.test.ts b/packages/botonic-plugin-flow-builder/tests/bot-action.test.ts index 06a19508b6..3b841ab6ec 100644 --- a/packages/botonic-plugin-flow-builder/tests/bot-action.test.ts +++ b/packages/botonic-plugin-flow-builder/tests/bot-action.test.ts @@ -1,4 +1,4 @@ -import { BotonicAction, INPUT } from '@botonic/core' +import { INPUT } from '@botonic/core' import { describe, test } from '@jest/globals' import { FlowText } from '../src/index' @@ -7,6 +7,7 @@ import { basicFlow } from './helpers/flows/basic' import { createFlowBuilderPlugin, createFlowBuilderPluginAndGetContents, + createRequest, } from './helpers/utils' describe('The user clicks on a button that is connected to a BotActionNode', () => { @@ -34,20 +35,27 @@ describe('The user clicks on a button that is connected to a BotActionNode', () }) test('The request.session._botonic_action have redirect:nextPayload', async () => { - const { contents, request } = await createFlowBuilderPluginAndGetContents({ - flowBuilderOptions: { flow: basicFlow }, - requestArgs: { - input: { - type: INPUT.POSTBACK, - payload: botActionUuid, - }, + const flowBuilderPlugin = createFlowBuilderPlugin({ + flow: basicFlow, + }) + + const requestArgs = { + input: { + type: INPUT.POSTBACK, + payload: botActionUuid, + }, + } + + const request = createRequest({ + ...requestArgs, + plugins: { + // @ts-ignore + flowBuilderPlugin, }, }) + await flowBuilderPlugin.pre(request) - expect(contents.length).toBe(1) - expect(request.session._botonic_action).toBe( - `${BotonicAction.Redirect}:${ratingPayloadWithParams}` - ) + expect(request.input.payload).toBe(ratingPayloadWithParams) }) test('In the custom action the payloadParmas defined in the BotActionNode are obtained', async () => {