UNPKG

@jss-rule-engine/workflow

Version:

470 lines 24.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowService = void 0; var workflowQuery_1 = require("./graphql/workflowQuery"); var helper_1 = require("./lib/helper"); var WorkflowService = /** @class */ (function () { function WorkflowService(options) { this.workflows = {}; this.options = options; this.databaseService = options.databaseService; } WorkflowService.prototype.findRelevantEmbeddings = function (data, indexId, topN, thresold) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.databaseService.findRelevantEmbeddings(data, indexId, topN, thresold)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; WorkflowService.prototype.addScheduledTask = function (params) { return __awaiter(this, void 0, void 0, function () { var dbParams; return __generator(this, function (_a) { switch (_a.label) { case 0: dbParams = { id: (0, helper_1.cleanId)(params.taskId), visitorId: (0, helper_1.cleanId)(params.visitorId), workflowId: (0, helper_1.cleanId)(params.workflowId), taskType: params.triggerType, scheduledTime: params.scheduledTime, payload: params.triggerParameters }; return [4 /*yield*/, this.databaseService.addScheduledTask(dbParams)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; WorkflowService.prototype.getWorkflow = function (workflowId) { var id = (0, helper_1.cleanId)(workflowId); return this.workflows[id] || null; }; WorkflowService.prototype.init = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.databaseService.init()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; WorkflowService.prototype.load = function (workflowConfig) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this.workflows[(0, helper_1.cleanId)(workflowConfig.id)] = workflowConfig; console.log('Loaded ', workflowConfig.id); return [2 /*return*/]; }); }); }; WorkflowService.prototype.addVisitorToState = function (workflowId, stateId, visitorId) { return __awaiter(this, void 0, void 0, function () { var workflow, state; return __generator(this, function (_a) { switch (_a.label) { case 0: workflow = this.getWorkflow(workflowId); if (!workflow) throw new Error('Workflow not found'); state = workflow.states[(0, helper_1.cleanId)(stateId)]; if (!state) throw new Error('State not found'); return [4 /*yield*/, this.databaseService.addVisitor((0, helper_1.cleanId)(visitorId), (0, helper_1.cleanId)(stateId), (0, helper_1.cleanId)(workflowId))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; WorkflowService.prototype.executeTriggers = function (options) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var currentStateId, cleanWorkflowId, workflow, visitorObject, workflowContext, ruleEngineContext, result, state, _i, _e, trigger, _f, ex_1; return __generator(this, function (_g) { switch (_g.label) { case 0: return [4 /*yield*/, this.databaseService.getVisitorState((0, helper_1.cleanId)(options.visitorId), (0, helper_1.cleanId)(options.workflowId))]; case 1: currentStateId = _g.sent(); if (!!currentStateId) return [3 /*break*/, 3]; //get default state currentStateId = (_a = options.defaultStateId) !== null && _a !== void 0 ? _a : null; if (!currentStateId) { return [2 /*return*/, { success: true, visitorId: options.visitorId, clientCommands: [], workflowId: options.workflowId, newStateId: currentStateId }]; } return [4 /*yield*/, this.databaseService.addVisitor((0, helper_1.cleanId)(options.visitorId), (0, helper_1.cleanId)(currentStateId), (0, helper_1.cleanId)(options.workflowId))]; case 2: _g.sent(); _g.label = 3; case 3: ; if (!currentStateId) { throw new Error('Current state ID is null or undefined.'); } currentStateId = (0, helper_1.cleanId)(currentStateId); cleanWorkflowId = (0, helper_1.cleanId)(options.workflowId); console.log('Workflows - ', this.workflows); workflow = this.workflows[cleanWorkflowId]; if (!workflow) throw new Error('Workflow not found for the state ' + currentStateId + ' ' + options.workflowId); visitorObject = { id: options.visitorId, }; workflowContext = { ruleEngine: this.options.ruleEngine, visitor: visitorObject, workflowService: this, workflow: workflow, clientCommands: [], trigger: options.eventName, triggerParameters: options.eventParameters, metadata: { newStateId: currentStateId }, ruleEngineContext: this.options.ruleEngineContext }; ruleEngineContext = this.options.ruleEngineContext ? this.options.ruleEngineContext : (_b = this.options.ruleEngine) === null || _b === void 0 ? void 0 : _b.getRuleEngineContext(); (_c = ruleEngineContext === null || ruleEngineContext === void 0 ? void 0 : ruleEngineContext.sessionContext) === null || _c === void 0 ? void 0 : _c.set('workflowContext', workflowContext); result = { success: true, visitorId: options.visitorId, clientCommands: [], workflowId: (0, helper_1.cleanId)(workflow.id), prevStateId: currentStateId, newStateId: currentStateId, }; _g.label = 4; case 4: _g.trys.push([4, 12, , 13]); state = workflow.states[currentStateId]; if (!state) { console.warn("Can't find the state in workflow " + currentStateId); if (workflow.defaultStateId) { state = workflow.states[workflow.defaultStateId]; } } console.log("Checking ".concat(state.id, " triggers - ").concat((_d = state.triggers) === null || _d === void 0 ? void 0 : _d.length)); _i = 0, _e = state.triggers; _g.label = 5; case 5: if (!(_i < _e.length)) return [3 /*break*/, 11]; trigger = _e[_i]; console.log("Processing ".concat(trigger.id, ". Has condition - ").concat(trigger.condition != null)); console.log(trigger.condition); _f = !trigger.condition; if (_f) return [3 /*break*/, 7]; return [4 /*yield*/, evaluateCondition(trigger.condition, workflowContext)]; case 6: _f = (_g.sent()); _g.label = 7; case 7: if (!_f) return [3 /*break*/, 9]; console.log("Trigger ".concat(trigger.id, " condition is true. Executing actions.")); return [4 /*yield*/, this.executeActions(options.visitorId, workflowContext, state)]; case 8: _g.sent(); return [3 /*break*/, 10]; case 9: console.log('Trigger condition is false - skipping trigger'); _g.label = 10; case 10: _i++; return [3 /*break*/, 5]; case 11: result.newStateId = workflowContext.metadata.newStateId; result.clientCommands = workflowContext.clientCommands; return [2 /*return*/, result]; case 12: ex_1 = _g.sent(); console.log('Error - ', ex_1); result.success = false; return [3 /*break*/, 13]; case 13: return [2 /*return*/, result]; } }); }); }; WorkflowService.prototype.executeActions = function (visitorId, workflowExecutionContext, state) { var _a; return __awaiter(this, void 0, void 0, function () { var _i, _b, action, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: console.log("Executing actions - ".concat((_a = state === null || state === void 0 ? void 0 : state.actions) === null || _a === void 0 ? void 0 : _a.length)); if (!state) { return [2 /*return*/]; } _i = 0, _b = state.actions; _d.label = 1; case 1: if (!(_i < _b.length)) return [3 /*break*/, 6]; action = _b[_i]; _c = !action.condition; if (_c) return [3 /*break*/, 3]; return [4 /*yield*/, evaluateCondition(action.condition, workflowExecutionContext)]; case 2: _c = (_d.sent()); _d.label = 3; case 3: if (!_c) return [3 /*break*/, 5]; return [4 /*yield*/, this.executeAction(visitorId, action, workflowExecutionContext)]; case 4: _d.sent(); _d.label = 5; case 5: _i++; return [3 /*break*/, 1]; case 6: return [2 /*return*/]; } }); }); }; WorkflowService.prototype.executeAction = function (visitorId, action, workflowExecutionContext) { var _a; return __awaiter(this, void 0, void 0, function () { var actionCommand; return __generator(this, function (_b) { switch (_b.label) { case 0: console.log("Executing action ".concat(action.id, " ").concat(action.templateId)); actionCommand = this.options.actionFactory.getAction(action.templateId); if (!actionCommand) return [3 /*break*/, 2]; return [4 /*yield*/, actionCommand.execute(action, workflowExecutionContext)]; case 1: _b.sent(); return [3 /*break*/, 3]; case 2: console.warn('Action command not found', action.templateId); _b.label = 3; case 3: if (!action.nextStateId) return [3 /*break*/, 5]; return [4 /*yield*/, this.changeVisitorState((0, helper_1.cleanId)(visitorId), (0, helper_1.cleanId)((_a = workflowExecutionContext.workflow) === null || _a === void 0 ? void 0 : _a.id), (0, helper_1.cleanId)(action.nextStateId), workflowExecutionContext)]; case 4: _b.sent(); _b.label = 5; case 5: return [2 /*return*/]; } }); }); }; WorkflowService.prototype.removeVisitorFromWorkflow = function (visitorId, workflowId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.databaseService.removeVisitor((0, helper_1.cleanId)(visitorId), (0, helper_1.cleanId)(workflowId))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; WorkflowService.prototype.changeVisitorState = function (visitorId, workflowId, nextStateId, context) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: context.metadata.newStateId = (0, helper_1.cleanId)(nextStateId); return [4 /*yield*/, this.databaseService.updateVisitorState((0, helper_1.cleanId)(visitorId), (0, helper_1.cleanId)(nextStateId), (0, helper_1.cleanId)(workflowId))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; WorkflowService.prototype.getStateVisitors = function (workflowId, stateId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.databaseService.getStateVisitors(workflowId, stateId)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; WorkflowService.prototype.parseGraphQLResponse = function (response) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var item, workflow; var _this = this; return __generator(this, function (_c) { item = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.item; if (!item) { console.warn("Can't find valid GraphQL response"); throw new Error("Invalid GraphQL response"); } workflow = { id: (0, helper_1.cleanId)(item.id), name: item.name, states: {}, defaultStateId: (0, helper_1.cleanId)((_b = item.field) === null || _b === void 0 ? void 0 : _b.value) // Remove curly braces from GUID }; // Process each state item.stateItems.results.forEach(function (stateItem) { console.log("Parsing state - ", stateItem === null || stateItem === void 0 ? void 0 : stateItem.id); var state = { id: (0, helper_1.cleanId)(stateItem.id), name: stateItem.name, triggers: [], actions: [] }; // Process children (triggers and actions) stateItem.children.results.forEach(function (child) { var parsedItem = _this.parseWorkflowItem(child); if (parsedItem) { if (child.template.name === 'Trigger') { state.triggers.push(parsedItem); } else { state.actions.push(parsedItem); } } }); workflow.states[state.id] = state; }); console.log("Parsed workflow - ", workflow.id); return [2 /*return*/, workflow]; }); }); }; WorkflowService.prototype.parseWorkflowItem = function (child) { var fields = child.fields.reduce(function (acc, field) { acc[field.name] = field.value; return acc; }, {}); var id = (0, helper_1.cleanId)(child.id); var name = child.name; var templateId = child.template.id; if (child.template.name === 'Trigger') { var trigger = { id: id, name: name, type: 'trigger', templateId: templateId, condition: fields.Condition || '', fields: fields }; return trigger; } else { // Map children items to actionObj.subitems with id, name, template (id, name), and fields (id, name, value) var subitems = []; if (child.children && child.children.results && Array.isArray(child.children.results)) { subitems = child.children.results.map(function (subitem) { var _a; return ({ id: (0, helper_1.cleanId)(subitem.id), name: subitem.name, templateId: (_a = subitem === null || subitem === void 0 ? void 0 : subitem.template) === null || _a === void 0 ? void 0 : _a.id, fields: Array.isArray(subitem.fields) ? subitem.fields.map(function (field) { return ({ id: field.id, name: field.name, value: field.value }); }) : [] }); }); } var action = { id: id, name: name, templateId: templateId, condition: fields.Condition || '', nextStateId: (0, helper_1.cleanId)(fields.NextState) || undefined, fields: fields, subitems: subitems }; return action; } }; WorkflowService.prototype.getSitecoreQuery = function (path, language) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, workflowQuery_1.sitecoreQuery)(path, language)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; return WorkflowService; }()); exports.WorkflowService = WorkflowService; function evaluateCondition(condition, context) { var _a; return __awaiter(this, void 0, void 0, function () { var result, error_1; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); console.log('Evaluating condition.'); return [4 /*yield*/, ((_a = context.ruleEngine) === null || _a === void 0 ? void 0 : _a.parseAndRunRule(condition, context.ruleEngineContext))]; case 1: result = _b.sent(); console.log('Result - ', result); return [2 /*return*/, result ? result : false]; case 2: error_1 = _b.sent(); return [2 /*return*/, false]; case 3: return [2 /*return*/]; } }); }); } exports.default = WorkflowService; //# sourceMappingURL=workflowService.js.map