@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
87 lines • 4.43 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { EventAction } from '@botonic/shared';
import { getCommonFlowContentEventArgsForContentId, trackEvent, } from '../../tracking';
import { ContentFieldsBase } from '../content-fields-base';
import { VariableFormat, } from '../hubtype-fields';
import { evaluateBooleanCondition, evaluateNumberCondition, evaluateStringCondition, } from './custom-conditional-v2-evaluators';
import { findLastMatchingCondition, resolveWithDefaultTarget, } from './custom-conditional-v2-resolver';
export class FlowCustomConditionalV2 extends ContentFieldsBase {
constructor() {
super(...arguments);
this.variableFormat = VariableFormat.String;
this.keyPath = '';
this.conditions = [];
this.customResult = '';
this.resolvedOperator = '';
}
static fromHubtypeCMS(component, botonicContext) {
const newCustomConditionalV2 = new FlowCustomConditionalV2(component.id);
newCustomConditionalV2.code = component.code;
newCustomConditionalV2.variableFormat = component.content.type;
newCustomConditionalV2.keyPath = component.content.key_path;
newCustomConditionalV2.conditions = component.content.conditions;
newCustomConditionalV2.defaultTarget = component.content.default_target;
newCustomConditionalV2.setFollowUp(botonicContext);
return newCustomConditionalV2;
}
setFollowUp(botonicContext) {
const botVariable = this.getValueFromKeyPath(botonicContext, this.keyPath);
const resolved = this.evaluateConditions(botVariable);
this.customResult = resolved.customResult;
this.resolvedOperator = resolved.operator;
this.followUp = resolved.target;
}
evaluateConditions(botVariable) {
if (!this.defaultTarget) {
throw new Error(`Default target not found for custom condition node ${this.code}`);
}
switch (this.variableFormat) {
case VariableFormat.String: {
const match = findLastMatchingCondition(this.conditions, botVariable, this.variableFormat, (variable, condition) => evaluateStringCondition(variable, condition));
return resolveWithDefaultTarget(match, this.defaultTarget, this.code);
}
case VariableFormat.Number: {
const match = findLastMatchingCondition(this.conditions, botVariable, this.variableFormat, (variable, condition) => evaluateNumberCondition(variable, condition));
return resolveWithDefaultTarget(match, this.defaultTarget, this.code);
}
case VariableFormat.Boolean: {
const match = findLastMatchingCondition(this.conditions, botVariable, this.variableFormat, (variable, condition) => evaluateBooleanCondition(variable, condition));
return resolveWithDefaultTarget(match, this.defaultTarget, this.code, 'false');
}
default:
throw new Error(`Invalid variable format ${this.variableFormat} for custom condition node ${this.code}`);
}
}
trackFlow(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
const { flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId } = yield getCommonFlowContentEventArgsForContentId(botonicContext, this.id);
const eventCustomConditional = {
action: EventAction.ConditionalCustom,
flowThreadId,
flowId,
flowName,
flowNodeId,
flowNodeContentId,
flowNodeIsMeaningful: false,
conditionalVariable: this.customResult,
variableFormat: this.variableFormat,
operator: this.resolvedOperator,
};
const { action } = eventCustomConditional, eventArgs = __rest(eventCustomConditional, ["action"]);
yield trackEvent(botonicContext, action, eventArgs);
});
}
processContent(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
yield this.filterContent(botonicContext, this);
yield this.trackFlow(botonicContext);
return;
});
}
toBotonic() {
return __awaiter(this, void 0, void 0, function* () {
return;
});
}
}
//# sourceMappingURL=flow-custom-condition-v2.js.map