89f9474f31572bba4f512e643191a0c1fcd1030f
[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 _0x91ed=['exports','lodash','util','events','EventEmitter','init','register','ignoreFactChanges','inherits','prototype','isArray','concat','isObject','push','sync','activeRules','filter','isNil','orderBy','asc','result','consequence','ruleRef','name','nextTick','isEqual','restart','matchPath','findRules','rules','matches','turn','length','priority','toJSON','map','condition','toString','isString','parse','object'];(function(_0x351a5c,_0xf2e6bd){var _0x257a3b=function(_0xb478f0){while(--_0xb478f0){_0x351a5c['push'](_0x351a5c['shift']());}};_0x257a3b(++_0xf2e6bd);}(_0x91ed,0x149));var _0xd91e=function(_0x4fbfd9,_0x3ab91a){_0x4fbfd9=_0x4fbfd9-0x0;var _0x31dc94=_0x91ed[_0x4fbfd9];return _0x31dc94;};'use strict';var _=require(_0xd91e('0x0'));var util=require(_0xd91e('0x1'));var EventEmitter=require(_0xd91e('0x2'))[_0xd91e('0x3')];function RuleEngine(_0x461cb6,_0x3ea929){this[_0xd91e('0x4')]();if(!_['isNil'](_0x461cb6)){this[_0xd91e('0x5')](_0x461cb6);}if(_0x3ea929){this[_0xd91e('0x6')]=_0x3ea929[_0xd91e('0x6')];}return this;}util[_0xd91e('0x7')](RuleEngine,EventEmitter);RuleEngine[_0xd91e('0x8')][_0xd91e('0x4')]=function(_0x29de9c){this['rules']=[];this['activeRules']=[];};RuleEngine[_0xd91e('0x8')][_0xd91e('0x5')]=function(_0x5371be){if(Array[_0xd91e('0x9')](_0x5371be)){this['rules']=this['rules'][_0xd91e('0xa')](_0x5371be);}else if(_0x5371be!==null&&_[_0xd91e('0xb')](_0x5371be)){this['rules'][_0xd91e('0xc')](_0x5371be);}this[_0xd91e('0xd')]();};RuleEngine[_0xd91e('0x8')][_0xd91e('0xd')]=function(){this[_0xd91e('0xe')]=this['rules'][_0xd91e('0xf')](function(_0x304696){if(_[_0xd91e('0x10')](_0x304696['on'])){_0x304696['on']=!![];}if(_0x304696['on']===!![]){return _0x304696;}});this['activeRules']=_[_0xd91e('0x11')](this[_0xd91e('0xe')],['p'],[_0xd91e('0x12')]);};RuleEngine['prototype']['execute']=function(_0x1441c0,_0x5716ea){var _0x24fe6d=![];_0x1441c0[_0xd91e('0x13')]=!![];var _0x2bf0fe=_['clone'](_0x1441c0);var _0x4755f8=_['clone'](_0x1441c0);var _0x4571df=this[_0xd91e('0xe')];var _0x38dd98=[];var _0x3e8f47=this[_0xd91e('0x6')];(function FnRuleLoop(_0x523d2e){var _0x5f21da={'rules':function(){return _0x4571df;},'rule':function(){return _0x4571df[_0x523d2e];},'when':function(_0x2c9fe3){if(_0x2c9fe3){var _0x33ac86=_0x4571df[_0x523d2e][_0xd91e('0x14')];_0x33ac86[_0xd91e('0x15')]=_0x4571df[_0x523d2e]['id']||_0x4571df[_0x523d2e][_0xd91e('0x16')]||'index_'+_0x523d2e;process[_0xd91e('0x17')](function(){_0x38dd98[_0xd91e('0xc')](_0x33ac86['ruleRef']);_0x33ac86['call'](_0x2bf0fe,_0x5f21da);});}else{process[_0xd91e('0x17')](function(){_0x5f21da['next']();});}},'restart':function(){return FnRuleLoop(0x0);},'stop':function(){_0x24fe6d=!![];return FnRuleLoop(0x0);},'next':function(_0x4169d9){if(!_0x3e8f47&&!_[_0xd91e('0x18')](_0x4755f8,_0x2bf0fe)){_0x4755f8=_['clone'](_0x2bf0fe);process['nextTick'](function(){_0x5f21da[_0xd91e('0x19')]();});}else{process[_0xd91e('0x17')](function(){if(_[_0xd91e('0x10')](_0x4169d9)){return FnRuleLoop(_0x523d2e+0x1);}else{return FnRuleLoop(_0x4169d9);}});}}};if(_0x523d2e<_0x4571df['length']&&_0x24fe6d===![]){var _0x22f6b5=_0x4571df[_0x523d2e]['condition'];_0x22f6b5['call'](_0x2bf0fe,_0x5f21da);}else{process[_0xd91e('0x17')](function(){_0x2bf0fe[_0xd91e('0x1a')]=_0x38dd98;return _0x5716ea?_0x5716ea(_0x2bf0fe):_['noop']();});}}(0x0));};RuleEngine[_0xd91e('0x8')][_0xd91e('0x1b')]=function(_0x393e97){if(_[_0xd91e('0x10')](_0x393e97)){return this[_0xd91e('0x1c')];}else{var _0x412361=_[_0xd91e('0x1d')](_0x393e97);return _[_0xd91e('0xf')](this['rules'],_0x412361);}};RuleEngine[_0xd91e('0x8')][_0xd91e('0x1e')]=function(_0x20de85,_0x109990){var _0x5ca81b=_0x20de85==='on'||_0x20de85==='ON'?!![]:![];var _0x335ab4=this[_0xd91e('0x1b')](_0x109990);for(var _0x34f174=0x0,_0x13752e=_0x335ab4[_0xd91e('0x1f')];_0x34f174<_0x13752e;_0x34f174+=0x1){_0x335ab4[_0x34f174]['on']=_0x5ca81b;}this[_0xd91e('0xd')]();};RuleEngine[_0xd91e('0x8')]['prioritize']=function(_0x3a37c6,_0x45bd52){_0x3a37c6=parseInt(_0x3a37c6,0xa);var _0x13bb0c=this[_0xd91e('0x1b')](_0x45bd52);for(var _0x100b1b=0x0,_0x12e2d7=_0x13bb0c[_0xd91e('0x1f')];_0x100b1b<_0x12e2d7;_0x100b1b+=0x1){_0x13bb0c[_0x100b1b][_0xd91e('0x20')]=_0x3a37c6;}this['sync']();};RuleEngine[_0xd91e('0x8')][_0xd91e('0x21')]=function(){var _0x5b5527=this[_0xd91e('0x1c')];if(_0x5b5527 instanceof Array){_0x5b5527=_0x5b5527[_0xd91e('0x22')](function(_0x213416){_0x213416['condition']=_0x213416[_0xd91e('0x23')][_0xd91e('0x24')]();_0x213416[_0xd91e('0x14')]=_0x213416[_0xd91e('0x14')]['toString']();return _0x213416;});}else if(!_[_0xd91e('0x10')](_0x5b5527)){_0x5b5527['condition']=_0x5b5527[_0xd91e('0x23')][_0xd91e('0x24')]();_0x5b5527[_0xd91e('0x14')]=_0x5b5527[_0xd91e('0x14')]['toString']();}return _0x5b5527;};RuleEngine['prototype']['fromJSON']=function(_0x15c490){this['init']();if(_[_0xd91e('0x25')](_0x15c490)){_0x15c490=JSON[_0xd91e('0x26')](_0x15c490);}if(_0x15c490 instanceof Array){_0x15c490=_0x15c490[_0xd91e('0x22')](function(_0x2fc8d6){_0x2fc8d6['condition']=eval('('+_0x2fc8d6[_0xd91e('0x23')]+')');_0x2fc8d6['consequence']=eval('('+_0x2fc8d6[_0xd91e('0x14')]+')');return _0x2fc8d6;});}else if(_0x15c490!==null&&typeof _0x15c490==_0xd91e('0x27')){_0x15c490[_0xd91e('0x23')]=eval('('+_0x15c490['condition']+')');_0x15c490[_0xd91e('0x14')]=eval('('+_0x15c490['consequence']+')');}this[_0xd91e('0x5')](_0x15c490);};module[_0xd91e('0x28')]=RuleEngine;