@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
103 lines • 5.16 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { EventAction } from '@botonic/shared';
import { getArgumentsByLocale } from '../functions';
import { HubtypeQueuesClient } from '../services/hubtype-queues-client';
import { getCommonFlowContentEventArgsForContentId, trackEvent, } from '../tracking';
import { ContentFieldsBase } from './content-fields-base';
var QueueStatusResult;
(function (QueueStatusResult) {
QueueStatusResult["OPEN"] = "open";
QueueStatusResult["CLOSED"] = "closed";
QueueStatusResult["OPEN_WITHOUT_AGENTS"] = "open-without-agents";
})(QueueStatusResult || (QueueStatusResult = {}));
export class FlowQueueStatusConditional extends ContentFieldsBase {
constructor() {
super(...arguments);
this.arguments = [];
this.queueId = '';
this.queueName = '';
this.isQueueOpen = false;
this.isAvailableAgent = false;
}
static fromHubtypeCMS(component, locale, botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
const newChannelConditional = new FlowQueueStatusConditional(component.id);
newChannelConditional.code = component.code;
newChannelConditional.arguments = component.content.arguments;
newChannelConditional.resultMapping = component.content
.result_mapping;
yield newChannelConditional.setConditionalResult(locale, botonicContext);
return newChannelConditional;
});
}
setConditionalResult(locale, botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const args = getArgumentsByLocale(this.arguments, locale);
const queueId = ((_a = args.find(arg => arg.name === 'queue_id')) === null || _a === void 0 ? void 0 : _a.value) || '';
const queueName = ((_b = args.find(arg => arg.name === 'queue_name')) === null || _b === void 0 ? void 0 : _b.value) || '';
const checkAvailableAgents = ((_c = args.find(arg => arg.name === 'check_available_agents')) === null || _c === void 0 ? void 0 : _c.value) || false;
const queueStatus = yield this.conditionalQueueStatus(botonicContext, {
queueId,
queueName,
checkAvailableAgents,
});
const conditionalResult = (_d = this.resultMapping) === null || _d === void 0 ? void 0 : _d.find(rMap => rMap.result === queueStatus);
if (!conditionalResult) {
throw new Error(`No conditional result found for node ${this.code} with queue: ${queueName}`);
}
this.conditionalResult = conditionalResult;
this.followUp = conditionalResult.target;
});
}
conditionalQueueStatus(botonicContext_1, _a) {
return __awaiter(this, arguments, void 0, function* (botonicContext, { queueId, checkAvailableAgents, queueName }) {
const hubtypeQueuesClient = new HubtypeQueuesClient(botonicContext, queueId, checkAvailableAgents);
const data = yield hubtypeQueuesClient.getAvailability();
this.queueId = queueId;
this.queueName = queueName;
this.isQueueOpen = data.open;
this.isAvailableAgent = data.available_agents > 0;
if (checkAvailableAgents && data.open && data.available_agents === 0) {
return QueueStatusResult.OPEN_WITHOUT_AGENTS;
}
return data.open ? QueueStatusResult.OPEN : QueueStatusResult.CLOSED;
});
}
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 eventQueueStatusConditional = {
action: EventAction.ConditionalQueueStatus,
flowThreadId,
flowId,
flowName,
flowNodeId,
flowNodeContentId,
flowNodeIsMeaningful: false,
queueId: this.queueId,
queueName: this.queueName,
isQueueOpen: this.isQueueOpen,
isAvailableAgent: this.isAvailableAgent,
};
const { action } = eventQueueStatusConditional, eventArgs = __rest(eventQueueStatusConditional, ["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-queue-status-conditional.js.map