UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

100 lines 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowQueueStatusConditional = void 0; const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const core_1 = require("@botonic/core"); const functions_1 = require("../functions"); const hubtype_queues_api_1 = require("../services/hubtype-queues-api"); const tracking_1 = require("../tracking"); const content_fields_base_1 = require("./content-fields-base"); var QueueStatusResult; (function (QueueStatusResult) { QueueStatusResult["OPEN"] = "open"; QueueStatusResult["CLOSED"] = "closed"; QueueStatusResult["OPEN_WITHOUT_AGENTS"] = "open-without-agents"; })(QueueStatusResult || (QueueStatusResult = {})); class FlowQueueStatusConditional extends content_fields_base_1.ContentFieldsBase { constructor() { super(...arguments); this.arguments = []; this.queueId = ''; this.queueName = ''; this.isQueueOpen = false; this.isAvailableAgent = false; } static fromHubtypeCMS(component, locale) { return tslib_1.__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); return newChannelConditional; }); } setConditionalResult(locale) { var _a, _b, _c; return tslib_1.__awaiter(this, void 0, void 0, function* () { const args = (0, functions_1.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({ queueId, queueName, checkAvailableAgents, }); const conditionalResult = this.resultMapping.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({ queueId, checkAvailableAgents, queueName, }) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const queuesApi = new hubtype_queues_api_1.HubtypeQueuesApi(queueId, checkAvailableAgents); const data = yield queuesApi.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(request) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const { flowThreadId, flowId, flowName, flowNodeId, flowNodeContentId } = (0, tracking_1.getCommonFlowContentEventArgsForContentId)(request, 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: core_1.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 = tslib_1.__rest(eventQueueStatusConditional, ["action"]); yield (0, tracking_1.trackEvent)(request, action, eventArgs); }); } toBotonic() { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}); } } exports.FlowQueueStatusConditional = FlowQueueStatusConditional; //# sourceMappingURL=flow-queue-status-conditional.js.map