UNPKG

@botonic/plugin-flow-builder

Version:

Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.

92 lines 4.28 kB
import { __awaiter, __rest } from "tslib"; import { EventAction } from '@botonic/shared'; import { getCommonFlowContentEventArgsForContentId, trackEvent, } from '../tracking'; import { ContentFieldsBase } from './content-fields-base'; export class FlowCustomConditional extends ContentFieldsBase { constructor() { super(...arguments); this.arguments = []; this.resultMapping = []; this.conditionalResult = undefined; this.customResult = false; this.variableFormat = ''; } static fromHubtypeCMS(component, botonicContext) { const newCustomConditional = new FlowCustomConditional(component.id); newCustomConditional.code = component.code; newCustomConditional.arguments = component.content.arguments; newCustomConditional.resultMapping = component.content.result_mapping; newCustomConditional.setConditionalResult(botonicContext); newCustomConditional.variableFormat = component.content.arguments[0].type; return newCustomConditional; } setConditionalResult(botonicContext) { const functionArgument = this.arguments.find(arg => { if ('name' in arg) { return arg.name === 'keyPath'; } return false; }); let keyPath = ''; if (functionArgument && 'value' in functionArgument) { keyPath = functionArgument.value; } else { throw new Error(`Key path not found for node ${this.code}`); } const botVariable = this.getValueFromKeyPath(botonicContext, keyPath); let conditionalResult = this.resultMapping.find(rMap => rMap.result === botVariable) || this.resultMapping.find(rMap => rMap.result === 'default'); if (this.isBooleanConditional(this.resultMapping) && typeof botVariable !== 'boolean') { conditionalResult = this.resultMapping.find(rMap => rMap.result === (botVariable !== undefined && botVariable !== null)) || this.resultMapping.find(rMap => rMap.result === 'default'); } if (!conditionalResult) { throw new Error(`No conditional result found for node ${this.code} with key path: ${keyPath}`); } this.conditionalResult = conditionalResult; this.customResult = conditionalResult.result; this.followUp = conditionalResult.target; } isBooleanConditional(resultMapping) { return (resultMapping.some(rMap => rMap.result === true) && resultMapping.some(rMap => rMap.result === false) && resultMapping.some(rMap => rMap.result === 'default')); } trackFlow(botonicContext) { return __awaiter(this, void 0, void 0, function* () { var _a; const { flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId } = yield getCommonFlowContentEventArgsForContentId(botonicContext, this.id); if (!((_a = this.conditionalResult) === null || _a === void 0 ? void 0 : _a.result)) { console.warn(`Tracking event for node ${this.code} but no conditional result found`); } const eventCustomConditional = { action: EventAction.ConditionalCustom, flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId, flowNodeIsMeaningful: false, conditionalVariable: this.customResult.toString(), variableFormat: this.variableFormat, operator: 'equals to', // Custom conditional v1 only supports equals to operator }; const { action } = eventCustomConditional, eventArgs = __rest(eventCustomConditional, ["action"]); yield trackEvent(botonicContext, action, eventArgs); }); } processContent(botonicContext) { return __awaiter(this, void 0, void 0, function* () { yield this.trackFlow(botonicContext); return this.filterContent(botonicContext, this); }); } toBotonic() { return __awaiter(this, void 0, void 0, function* () { return; }); } } //# sourceMappingURL=flow-custom-conditional.js.map