UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

171 lines 7.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SET_SECURE_FORMS_TILE = void 0; // @ts-ignore const axios_1 = require("axios"); /* Custom modules */ const createNodeDescriptor_1 = require("../../createNodeDescriptor"); const buildAppUrl_1 = require("../apps/utils/buildAppUrl"); const stage0_1 = require("./htmlTemplates/secureForms/stage0"); const stage1_1 = require("./htmlTemplates/secureForms/stage1"); const stage2_1 = require("./htmlTemplates/secureForms/stage2"); const stageError_1 = require("./htmlTemplates/secureForms/stageError"); // Doubled to make sure autoformat does not put caret return breaking ts-ignore hint // @ts-ignore const config_1 = require("../../../../config"); // @ts-ignore const config_2 = require("../../../../config"); function buildSecureFormsAppTemplateData(jwtToken) { if (!jwtToken || typeof jwtToken !== "string") { throw new TypeError("Unexpected parameter. Can't build template data"); } const submitUrl = (0, config_2.getServiceSecureFormsUrl)() + "/submit"; return { submitUrl, jwtToken }; } function buildSecureFormsTileTemplateData(jwtReadToken, formId) { return { retrieveUrl: (0, config_2.getServiceSecureFormsUrl)() + "/" + formId, jwtReadToken }; } function agentAssistTile(params) { return function (stage, options = {}) { const stages = [stage0_1.default, stage1_1.default, stage2_1.default, stageError_1.default]; const { cognigy, config } = params; cognigy.api.sendTileUpdateToAgentAssistWorkspace(Object.assign(Object.assign({}, params), { tile: { id: config.tileId, type: "html", data: { html: stages[stage](options) } } })); }; } exports.SET_SECURE_FORMS_TILE = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "setSecureFormsTile", defaultLabel: "Copilot: SecureForms Tile", appearance: { showIcon: false }, tags: ["agentAssist"], fields: [ { key: "tileId", label: "UI__NODE_EDITOR__SET_HTML_TILE__TILE_ID__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__SET_HTML_TILE__TILE_ID__DESCRIPTION", defaultValue: "secureForms", params: { required: true } } ], sections: [], form: [ { type: "field", key: "tileId" } ], function: async (params) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; const { cognigy, config } = params; const { api, input } = cognigy; if (!config.tileId) { api.logDebugError("Missing required parameter: 'tileId'", "Copilot: SecureForms Tile"); return; } const updateAgentTile = agentAssistTile(params); const contextKey = "secureForms"; let contextVal = api.getSystemContext(contextKey); function handleError(error) { api.logDebugError(error, "SecureForms Tile Error", "Copilot: SecureForms Tile Error"); updateAgentTile(3, { error }); } /** * Trigger: Init Process (0 -> 1) */ if (((_c = (_b = (_a = input === null || input === void 0 ? void 0 : input.data) === null || _a === void 0 ? void 0 : _a._cognigy) === null || _b === void 0 ? void 0 : _b._agentAssist) === null || _c === void 0 ? void 0 : _c.payload) === "__secureForms_init__") { try { const apiKey = (0, config_1.getServiceSecureFormsApiKey)(); const config = { url: (0, config_2.getServiceSecureFormsUrl)(), method: "post", headers: { "x-api-key": apiKey } }; const response = await axios_1.default.request(config); const { formId, jwtToken, jwtReadToken } = response.data; contextVal = { appLinkSent: false, isFinished: false, isSubmitted: false, formId, jwtReadToken, jwtToken }; api.setSystemContext(contextKey, contextVal); // Prepare xApp if (!input.apps.session.token) { const appSessionToken = await api.initAppSession({ styleConfig: {} }); api.addToInput("apps.session.token", appSessionToken); } const appTemplateURL = `${input.apps.baseUrl}/apps/secureforms/`; api.setAppState(appTemplateURL, buildSecureFormsAppTemplateData(jwtToken)); } catch (error) { handleError(`Request to SecureForms server failed, error was: ${error.message}`); return; } } /** * Trigger: Card data submitted (1 -> 2) */ if (((_g = (_f = (_e = (_d = input === null || input === void 0 ? void 0 : input.data) === null || _d === void 0 ? void 0 : _d._cognigy) === null || _e === void 0 ? void 0 : _e._app) === null || _f === void 0 ? void 0 : _f.payload) === null || _g === void 0 ? void 0 : _g.success) === true) { contextVal = Object.assign(Object.assign({}, contextVal), { isSubmitted: true }); api.setSystemContext(contextKey, contextVal); } /** * Trigger: xApp ERROR Handler (1 -> 3) */ if ((_l = (_k = (_j = (_h = input === null || input === void 0 ? void 0 : input.data) === null || _h === void 0 ? void 0 : _h._cognigy) === null || _j === void 0 ? void 0 : _j._app) === null || _k === void 0 ? void 0 : _k.payload) === null || _l === void 0 ? void 0 : _l.error) { handleError("User was not able to submit the card data."); return; } /** * Stage 0. Waiting for the human agent to start the process */ if (!contextVal) { updateAgentTile(0); return; } /** * Stage 1. Waiting for the card data submit */ if ((contextVal === null || contextVal === void 0 ? void 0 : contextVal.isSubmitted) === false) { const appUrl = (0, buildAppUrl_1.buildAppUrl)(input.apps.baseUrl, (_m = input.apps.session) === null || _m === void 0 ? void 0 : _m.token); const { appLinkSent } = contextVal; updateAgentTile(1, { appUrl, appLinkSent }); contextVal = Object.assign(Object.assign({}, contextVal), { appLinkSent: true }); api.setSystemContext(contextKey, contextVal); return; } /** * Stage 2. Card data submitted. * Send this only once by isFinished flag because Tile is a statefull app */ if ((contextVal === null || contextVal === void 0 ? void 0 : contextVal.isSubmitted) === true && (contextVal === null || contextVal === void 0 ? void 0 : contextVal.isFinished) === false) { const { formId, jwtReadToken } = contextVal; const templateData = buildSecureFormsTileTemplateData(jwtReadToken, formId); updateAgentTile(2, templateData); contextVal = Object.assign(Object.assign({}, contextVal), { isFinished: true }); api.setSystemContext(contextKey, contextVal); return; } } }); //# sourceMappingURL=setSecureFormsTile.js.map