@cognigy/rest-api-client
Version:
Cognigy REST-Client
169 lines • 7.39 kB
JavaScript
import { __awaiter } from "tslib";
// @ts-ignore
import Axios from "axios";
/* Custom modules */
import { createNodeDescriptor } from "../../createNodeDescriptor";
import { buildAppUrl } from "../apps/utils/buildAppUrl";
import stage0 from "./htmlTemplates/secureForms/stage0";
import stage1 from "./htmlTemplates/secureForms/stage1";
import stage2 from "./htmlTemplates/secureForms/stage2";
import stageError from "./htmlTemplates/secureForms/stageError";
// Doubled to make sure autoformat does not put caret return breaking ts-ignore hint
// @ts-ignore
import { getServiceSecureFormsApiKey } from "../../../../config";
// @ts-ignore
import { getServiceSecureFormsUrl } from "../../../../config";
function buildSecureFormsAppTemplateData(jwtToken) {
if (!jwtToken || typeof jwtToken !== "string") {
throw new TypeError("Unexpected parameter. Can't build template data");
}
const submitUrl = getServiceSecureFormsUrl() + "/submit";
return {
submitUrl,
jwtToken
};
}
function buildSecureFormsTileTemplateData(jwtReadToken, formId) {
return {
retrieveUrl: getServiceSecureFormsUrl() + "/" + formId,
jwtReadToken
};
}
function agentAssistTile(params) {
return function (stage, options = {}) {
const stages = [stage0, stage1, stage2, stageError];
const { cognigy, config } = params;
cognigy.api.sendTileUpdateToAgentAssistWorkspace(Object.assign(Object.assign({}, params), { tile: {
id: config.tileId,
type: "html",
data: {
html: stages[stage](options)
}
} }));
};
}
export const SET_SECURE_FORMS_TILE = 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: (params) => __awaiter(void 0, void 0, void 0, function* () {
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 = getServiceSecureFormsApiKey();
const config = {
url: getServiceSecureFormsUrl(),
method: "post",
headers: {
"x-api-key": apiKey
}
};
const response = yield Axios.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 = yield 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 = 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