Skip to content

Commit

Permalink
refactor(botonic-react): replace bot action id for payload with param…
Browse files Browse the repository at this point in the history
…s in pre function
  • Loading branch information
Iru89 committed Nov 4, 2024
1 parent f9d4f76 commit 8f1be98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
16 changes: 15 additions & 1 deletion packages/botonic-plugin-flow-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
FlowWhatsappCtaUrlButtonNode,
} from './content-fields'
import {
HtBotActionNode,
HtFlowBuilderData,
HtFunctionArgument,
HtFunctionArguments,
Expand Down Expand Up @@ -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<HtBotActionNode>(
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,
Expand Down
32 changes: 20 additions & 12 deletions packages/botonic-plugin-flow-builder/tests/bot-action.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 8f1be98

Please sign in to comment.