cf2d4aebf0fa542b5506580bfe6bf1c1e10d934d
[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 _0xb859=['call','next','isEqual','length','noop','findRules','matches','prioritize','priority','toJSON','condition','toString','consequence','fromJSON','isString','parse','map','object','exports','lodash','util','events','EventEmitter','init','isNil','register','ignoreFactChanges','inherits','rules','activeRules','isArray','concat','isObject','push','sync','prototype','filter','execute','clone','ruleRef','name','index_','nextTick'];(function(_0x3b19c9,_0x5be344){var _0x3c549=function(_0xe90207){while(--_0xe90207){_0x3b19c9['push'](_0x3b19c9['shift']());}};_0x3c549(++_0x5be344);}(_0xb859,0x16b));var _0x9b85=function(_0x819ce,_0xf25f51){_0x819ce=_0x819ce-0x0;var _0x1edcf0=_0xb859[_0x819ce];return _0x1edcf0;};'use strict';var _=require(_0x9b85('0x0'));var util=require(_0x9b85('0x1'));var EventEmitter=require(_0x9b85('0x2'))[_0x9b85('0x3')];function RuleEngine(_0x159a25,_0x17afb2){this[_0x9b85('0x4')]();if(!_[_0x9b85('0x5')](_0x159a25)){this[_0x9b85('0x6')](_0x159a25);}if(_0x17afb2){this['ignoreFactChanges']=_0x17afb2[_0x9b85('0x7')];}return this;}util[_0x9b85('0x8')](RuleEngine,EventEmitter);RuleEngine['prototype'][_0x9b85('0x4')]=function(_0x1c916a){this[_0x9b85('0x9')]=[];this[_0x9b85('0xa')]=[];};RuleEngine['prototype']['register']=function(_0x4d3b42){if(Array[_0x9b85('0xb')](_0x4d3b42)){this[_0x9b85('0x9')]=this[_0x9b85('0x9')][_0x9b85('0xc')](_0x4d3b42);}else if(_0x4d3b42!==null&&_[_0x9b85('0xd')](_0x4d3b42)){this[_0x9b85('0x9')][_0x9b85('0xe')](_0x4d3b42);}this[_0x9b85('0xf')]();};RuleEngine[_0x9b85('0x10')][_0x9b85('0xf')]=function(){this[_0x9b85('0xa')]=this[_0x9b85('0x9')][_0x9b85('0x11')](function(_0x124013){if(_[_0x9b85('0x5')](_0x124013['on'])){_0x124013['on']=!![];}if(_0x124013['on']===!![]){return _0x124013;}});this[_0x9b85('0xa')]=_['orderBy'](this[_0x9b85('0xa')],['p'],['asc']);};RuleEngine[_0x9b85('0x10')][_0x9b85('0x12')]=function(_0x2ea736,_0x3a343f){var _0x1f56de=![];_0x2ea736['result']=!![];var _0x50dd06=_[_0x9b85('0x13')](_0x2ea736);var _0x269752=_[_0x9b85('0x13')](_0x2ea736);var _0x29428b=this['activeRules'];var _0x48dac4=[];var _0x4a1294=this[_0x9b85('0x7')];(function FnRuleLoop(_0x30b0ab){var _0x4cf11d={'rules':function(){return _0x29428b;},'rule':function(){return _0x29428b[_0x30b0ab];},'when':function(_0x1ae9cc){if(_0x1ae9cc){var _0x4e962d=_0x29428b[_0x30b0ab]['consequence'];_0x4e962d[_0x9b85('0x14')]=_0x29428b[_0x30b0ab]['id']||_0x29428b[_0x30b0ab][_0x9b85('0x15')]||_0x9b85('0x16')+_0x30b0ab;process[_0x9b85('0x17')](function(){_0x48dac4[_0x9b85('0xe')](_0x4e962d[_0x9b85('0x14')]);_0x4e962d[_0x9b85('0x18')](_0x50dd06,_0x4cf11d);});}else{process[_0x9b85('0x17')](function(){_0x4cf11d[_0x9b85('0x19')]();});}},'restart':function(){return FnRuleLoop(0x0);},'stop':function(){_0x1f56de=!![];return FnRuleLoop(0x0);},'next':function(_0x26f9db){if(!_0x4a1294&&!_[_0x9b85('0x1a')](_0x269752,_0x50dd06)){_0x269752=_['clone'](_0x50dd06);process[_0x9b85('0x17')](function(){_0x4cf11d['restart']();});}else{process[_0x9b85('0x17')](function(){if(_[_0x9b85('0x5')](_0x26f9db)){return FnRuleLoop(_0x30b0ab+0x1);}else{return FnRuleLoop(_0x26f9db);}});}}};if(_0x30b0ab<_0x29428b[_0x9b85('0x1b')]&&_0x1f56de===![]){var _0x3bf8ad=_0x29428b[_0x30b0ab]['condition'];_0x3bf8ad[_0x9b85('0x18')](_0x50dd06,_0x4cf11d);}else{process[_0x9b85('0x17')](function(){_0x50dd06['matchPath']=_0x48dac4;return _0x3a343f?_0x3a343f(_0x50dd06):_[_0x9b85('0x1c')]();});}}(0x0));};RuleEngine[_0x9b85('0x10')][_0x9b85('0x1d')]=function(_0x3b23e0){if(_['isNil'](_0x3b23e0)){return this[_0x9b85('0x9')];}else{var _0xf26d99=_[_0x9b85('0x1e')](_0x3b23e0);return _[_0x9b85('0x11')](this['rules'],_0xf26d99);}};RuleEngine[_0x9b85('0x10')]['turn']=function(_0x3e5f86,_0x4c9ce7){var _0x1553b4=_0x3e5f86==='on'||_0x3e5f86==='ON'?!![]:![];var _0x3b98cf=this[_0x9b85('0x1d')](_0x4c9ce7);for(var _0x26054d=0x0,_0x49cac8=_0x3b98cf[_0x9b85('0x1b')];_0x26054d<_0x49cac8;_0x26054d+=0x1){_0x3b98cf[_0x26054d]['on']=_0x1553b4;}this[_0x9b85('0xf')]();};RuleEngine[_0x9b85('0x10')][_0x9b85('0x1f')]=function(_0x223efb,_0x4121cd){_0x223efb=parseInt(_0x223efb,0xa);var _0x9eb88d=this[_0x9b85('0x1d')](_0x4121cd);for(var _0x2c67a0=0x0,_0x26e276=_0x9eb88d[_0x9b85('0x1b')];_0x2c67a0<_0x26e276;_0x2c67a0+=0x1){_0x9eb88d[_0x2c67a0][_0x9b85('0x20')]=_0x223efb;}this['sync']();};RuleEngine['prototype'][_0x9b85('0x21')]=function(){var _0x4c62f5=this[_0x9b85('0x9')];if(_0x4c62f5 instanceof Array){_0x4c62f5=_0x4c62f5['map'](function(_0x2dedbb){_0x2dedbb[_0x9b85('0x22')]=_0x2dedbb['condition'][_0x9b85('0x23')]();_0x2dedbb[_0x9b85('0x24')]=_0x2dedbb['consequence']['toString']();return _0x2dedbb;});}else if(!_[_0x9b85('0x5')](_0x4c62f5)){_0x4c62f5[_0x9b85('0x22')]=_0x4c62f5['condition'][_0x9b85('0x23')]();_0x4c62f5[_0x9b85('0x24')]=_0x4c62f5[_0x9b85('0x24')][_0x9b85('0x23')]();}return _0x4c62f5;};RuleEngine[_0x9b85('0x10')][_0x9b85('0x25')]=function(_0x5643b6){this[_0x9b85('0x4')]();if(_[_0x9b85('0x26')](_0x5643b6)){_0x5643b6=JSON[_0x9b85('0x27')](_0x5643b6);}if(_0x5643b6 instanceof Array){_0x5643b6=_0x5643b6[_0x9b85('0x28')](function(_0x4174ca){_0x4174ca['condition']=eval('('+_0x4174ca['condition']+')');_0x4174ca[_0x9b85('0x24')]=eval('('+_0x4174ca['consequence']+')');return _0x4174ca;});}else if(_0x5643b6!==null&&typeof _0x5643b6==_0x9b85('0x29')){_0x5643b6[_0x9b85('0x22')]=eval('('+_0x5643b6['condition']+')');_0x5643b6[_0x9b85('0x24')]=eval('('+_0x5643b6[_0x9b85('0x24')]+')');}this[_0x9b85('0x6')](_0x5643b6);};module[_0x9b85('0x2a')]=RuleEngine;