e460c081e0efca38b4b262e32932afd515a55d3b
[motion2.git] / server / services / routing / lib / rule-engine.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x26b3=['init','rules','activeRules','isArray','concat','isObject','push','sync','filter','orderBy','asc','clone','consequence','name','nextTick','ruleRef','call','next','isEqual','restart','length','matchPath','noop','findRules','turn','prioritize','toJSON','map','condition','toString','parse','object','register','lodash','isNil','ignoreFactChanges','inherits','prototype'];(function(_0x433122,_0x41ac4b){var _0x36d9ae=function(_0x26dd4b){while(--_0x26dd4b){_0x433122['push'](_0x433122['shift']());}};_0x36d9ae(++_0x41ac4b);}(_0x26b3,0x1e9));var _0x326b=function(_0x20ebde,_0xc1c3a1){_0x20ebde=_0x20ebde-0x0;var _0x37455d=_0x26b3[_0x20ebde];return _0x37455d;};'use strict';var _=require(_0x326b('0x0'));var util=require('util');var EventEmitter=require('events')['EventEmitter'];function RuleEngine(_0x3436ae,_0xd733bc){this['init']();if(!_[_0x326b('0x1')](_0x3436ae)){this['register'](_0x3436ae);}if(_0xd733bc){this[_0x326b('0x2')]=_0xd733bc[_0x326b('0x2')];}return this;}util[_0x326b('0x3')](RuleEngine,EventEmitter);RuleEngine[_0x326b('0x4')][_0x326b('0x5')]=function(_0x3ca272){this[_0x326b('0x6')]=[];this[_0x326b('0x7')]=[];};RuleEngine['prototype']['register']=function(_0x55dbf4){if(Array[_0x326b('0x8')](_0x55dbf4)){this['rules']=this[_0x326b('0x6')][_0x326b('0x9')](_0x55dbf4);}else if(_0x55dbf4!==null&&_[_0x326b('0xa')](_0x55dbf4)){this['rules'][_0x326b('0xb')](_0x55dbf4);}this['sync']();};RuleEngine[_0x326b('0x4')][_0x326b('0xc')]=function(){this['activeRules']=this[_0x326b('0x6')][_0x326b('0xd')](function(_0x5678dc){if(_[_0x326b('0x1')](_0x5678dc['on'])){_0x5678dc['on']=!![];}if(_0x5678dc['on']===!![]){return _0x5678dc;}});this['activeRules']=_[_0x326b('0xe')](this['activeRules'],['p'],[_0x326b('0xf')]);};RuleEngine['prototype']['execute']=function(_0x55d949,_0x4f6c41){var _0x1e19f5=![];_0x55d949['result']=!![];var _0x1e5563=_[_0x326b('0x10')](_0x55d949);var _0x8d56ca=_[_0x326b('0x10')](_0x55d949);var _0x163517=this[_0x326b('0x7')];var _0xf76bff=[];var _0x4bbd90=this[_0x326b('0x2')];(function FnRuleLoop(_0x2353d9){var _0x437bc3={'rules':function(){return _0x163517;},'rule':function(){return _0x163517[_0x2353d9];},'when':function(_0x2a9211){if(_0x2a9211){var _0x422b23=_0x163517[_0x2353d9][_0x326b('0x11')];_0x422b23['ruleRef']=_0x163517[_0x2353d9]['id']||_0x163517[_0x2353d9][_0x326b('0x12')]||'index_'+_0x2353d9;process[_0x326b('0x13')](function(){_0xf76bff['push'](_0x422b23[_0x326b('0x14')]);_0x422b23[_0x326b('0x15')](_0x1e5563,_0x437bc3);});}else{process[_0x326b('0x13')](function(){_0x437bc3[_0x326b('0x16')]();});}},'restart':function(){return FnRuleLoop(0x0);},'stop':function(){_0x1e19f5=!![];return FnRuleLoop(0x0);},'next':function(_0x1b9890){if(!_0x4bbd90&&!_[_0x326b('0x17')](_0x8d56ca,_0x1e5563)){_0x8d56ca=_['clone'](_0x1e5563);process[_0x326b('0x13')](function(){_0x437bc3[_0x326b('0x18')]();});}else{process[_0x326b('0x13')](function(){if(_[_0x326b('0x1')](_0x1b9890)){return FnRuleLoop(_0x2353d9+0x1);}else{return FnRuleLoop(_0x1b9890);}});}}};if(_0x2353d9<_0x163517[_0x326b('0x19')]&&_0x1e19f5===![]){var _0x14020c=_0x163517[_0x2353d9]['condition'];_0x14020c[_0x326b('0x15')](_0x1e5563,_0x437bc3);}else{process[_0x326b('0x13')](function(){_0x1e5563[_0x326b('0x1a')]=_0xf76bff;return _0x4f6c41?_0x4f6c41(_0x1e5563):_[_0x326b('0x1b')]();});}}(0x0));};RuleEngine['prototype'][_0x326b('0x1c')]=function(_0x487d8d){if(_[_0x326b('0x1')](_0x487d8d)){return this[_0x326b('0x6')];}else{var _0x52a9a0=_['matches'](_0x487d8d);return _[_0x326b('0xd')](this['rules'],_0x52a9a0);}};RuleEngine[_0x326b('0x4')][_0x326b('0x1d')]=function(_0x577aed,_0x532304){var _0x48324d=_0x577aed==='on'||_0x577aed==='ON'?!![]:![];var _0x10cc5c=this[_0x326b('0x1c')](_0x532304);for(var _0x3e9f56=0x0,_0x5730ee=_0x10cc5c['length'];_0x3e9f56<_0x5730ee;_0x3e9f56+=0x1){_0x10cc5c[_0x3e9f56]['on']=_0x48324d;}this[_0x326b('0xc')]();};RuleEngine[_0x326b('0x4')][_0x326b('0x1e')]=function(_0x3ca936,_0x964e5b){_0x3ca936=parseInt(_0x3ca936,0xa);var _0x49236c=this[_0x326b('0x1c')](_0x964e5b);for(var _0x3c410e=0x0,_0x3411f0=_0x49236c[_0x326b('0x19')];_0x3c410e<_0x3411f0;_0x3c410e+=0x1){_0x49236c[_0x3c410e]['priority']=_0x3ca936;}this[_0x326b('0xc')]();};RuleEngine[_0x326b('0x4')][_0x326b('0x1f')]=function(){var _0x1b68e5=this[_0x326b('0x6')];if(_0x1b68e5 instanceof Array){_0x1b68e5=_0x1b68e5[_0x326b('0x20')](function(_0x4691ee){_0x4691ee[_0x326b('0x21')]=_0x4691ee[_0x326b('0x21')][_0x326b('0x22')]();_0x4691ee[_0x326b('0x11')]=_0x4691ee[_0x326b('0x11')]['toString']();return _0x4691ee;});}else if(!_[_0x326b('0x1')](_0x1b68e5)){_0x1b68e5[_0x326b('0x21')]=_0x1b68e5['condition'][_0x326b('0x22')]();_0x1b68e5[_0x326b('0x11')]=_0x1b68e5[_0x326b('0x11')][_0x326b('0x22')]();}return _0x1b68e5;};RuleEngine['prototype']['fromJSON']=function(_0x101af8){this[_0x326b('0x5')]();if(_['isString'](_0x101af8)){_0x101af8=JSON[_0x326b('0x23')](_0x101af8);}if(_0x101af8 instanceof Array){_0x101af8=_0x101af8[_0x326b('0x20')](function(_0x299725){_0x299725[_0x326b('0x21')]=eval('('+_0x299725[_0x326b('0x21')]+')');_0x299725['consequence']=eval('('+_0x299725[_0x326b('0x11')]+')');return _0x299725;});}else if(_0x101af8!==null&&typeof _0x101af8==_0x326b('0x24')){_0x101af8[_0x326b('0x21')]=eval('('+_0x101af8['condition']+')');_0x101af8['consequence']=eval('('+_0x101af8[_0x326b('0x11')]+')');}this[_0x326b('0x25')](_0x101af8);};module['exports']=RuleEngine;