Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin-flow-builder: replace bot action id for payload with params in pre funtion #BLT1187 #2929

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "botonic",
"version": "0.27.0",
"version": "0.30.4",
"scripts": {
"clean": "rimraf packages/**/lib"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-plugin-flow-builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botonic/plugin-flow-builder",
"version": "0.30.3",
"version": "0.30.4-alpha.0",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"description": "Use Flow Builder to show your contents",
Expand Down
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)
}
Comment on lines 113 to +123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: You can unify both if (request.input.payload) conditions

}

private removeSourceSufix(payload: string): string {
return payload.split(SOURCE_INFO_SEPARATOR)[0]
}

private isBotAction(payload: string): boolean {
const node = this.cmsApi.getNodeById(payload)
return node?.type === HtNodeWithContentType.BOT_ACTION
}

async getContentsByContentID(
contentID: string,
locale: string,
Expand Down
34 changes: 21 additions & 13 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 All @@ -33,21 +34,28 @@ describe('The user clicks on a button that is connected to a BotActionNode', ()
expect(nextPaylod).toBe(botActionUuid)
})

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,
},
test('When the request.input.payload is a UUID of a bot action node it is replaced by the payload with parameters defined in the node', async () => {
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
Loading