@botonic/plugin-flow-builder
Version:
Use Flow Builder to show your contents
58 lines • 2.53 kB
JavaScript
import { __awaiter } from "tslib";
import { storeCaseRating } from '@botonic/core';
import { v7 as uuid } from 'uuid';
import { AGENT_RATING_PAYLOAD, SEPARATOR } from '../constants';
import { EventAction, trackEvent } from '../tracking';
export function getContentsByPayload(context) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const { cmsApi, flowBuilderPlugin, request, contentID } = context;
if ((_a = request.input.payload) === null || _a === void 0 ? void 0 : _a.startsWith(AGENT_RATING_PAYLOAD)) {
return yield resolveRatingPayload(context);
}
const id = contentID
? (_b = cmsApi.getNodeByContentID(contentID)) === null || _b === void 0 ? void 0 : _b.id
: request.input.payload;
const targetNode = id ? cmsApi.getNodeById(id) : undefined;
if (targetNode) {
return yield flowBuilderPlugin.getContentsByNode(targetNode);
}
return [];
});
}
function resolveRatingPayload(context) {
return __awaiter(this, void 0, void 0, function* () {
const { cmsApi, flowBuilderPlugin, request } = context;
if (!request.input.payload) {
return [];
}
const id = request.input.payload;
const buttonId = id === null || id === void 0 ? void 0 : id.split(SEPARATOR)[1];
const ratingNode = cmsApi.getRatingNodeByButtonId(buttonId);
const ratingButton = cmsApi.getRatingButtonById(ratingNode, buttonId);
const { target, text, value } = ratingButton;
const possibleOptions = ratingNode.content.buttons.map(button => button.text);
const possibleValues = ratingNode.content.buttons.map(button => button.value);
if (request.session._hubtype_case_id) {
const event = {
action: EventAction.FeedbackCase,
feedbackTargetId: request.session._hubtype_case_id,
feedbackGroupId: uuid().toString(),
possibleOptions,
possibleValues,
option: text,
value,
};
yield storeCaseRating(request.session, value);
yield trackEvent(request, EventAction.FeedbackCase, event);
}
const targetNode = target
? cmsApi.getNodeById(target.id)
: undefined;
if (targetNode) {
return yield flowBuilderPlugin.getContentsByNode(targetNode);
}
return [];
});
}
//# sourceMappingURL=payload.js.map