cb385377bb0c877485abea89dc84ad932473c456
[motion2.git] / server / config / triggers / index.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 _0xbf5b=['SmsInteraction','SmsMessage','Whatsapp','WhatsappInteraction','WhatsappMessage','Fax','FaxInteraction','FaxMessage','MailInteraction','Mail','MailMessage','OpenchannelInteraction','Openchannel','OpenchannelMessage','request','EventManager','info','error','code','Trigger,\x20%s','catch','channel','event','direction','resolve','then','ContactId','undefined','query','Sequelize','QueryTypes','SELECT','contact','UserId','SIP/%s','name','agent','incomingmessage','get','motionChannel','opened','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','sequelize','interface','AfterCreate','changed','closed','substatus','format','User','find','fullname','default','length','hasOwnProperty','model','hook','callback','toLowerCase','condition','util','../../mysqldb','jayson/promise','../logger','triggers','http','Chat','ChatInteraction','afterCreate','newinteraction','afterUpdate','closedinteraction','ChatMessage','newmessage','ChatOfflineMessage','Sms'];(function(_0xc902d8,_0x206194){var _0x5e64fb=function(_0x218dd3){while(--_0x218dd3){_0xc902d8['push'](_0xc902d8['shift']());}};_0x5e64fb(++_0x206194);}(_0xbf5b,0x16c));var _0xbbf5=function(_0x12fc6d,_0x1b2dc1){_0x12fc6d=_0x12fc6d-0x0;var _0x2929f0=_0xbf5b[_0x12fc6d];return _0x2929f0;};'use strict';var util=require(_0xbbf5('0x0'));var db=require(_0xbbf5('0x1'))['db'];var jayson=require(_0xbbf5('0x2'));var logger=require(_0xbbf5('0x3'))(_0xbbf5('0x4'));var client=jayson['client'][_0xbbf5('0x5')]({'port':0x232a});var triggers=[{'channel':_0xbbf5('0x6'),'model':_0xbbf5('0x7'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0x9'),'callback':afterCreate},{'channel':_0xbbf5('0x6'),'model':'ChatInteraction','event':_0xbbf5('0xa'),'condition':_0xbbf5('0xb'),'callback':afterUpdate},{'channel':_0xbbf5('0x6'),'model':_0xbbf5('0xc'),'event':'afterCreate','condition':_0xbbf5('0xd'),'callback':afterCreateMessage},{'channel':'Chat','model':_0xbbf5('0xe'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0xd'),'callback':afterCreateOfflineMessage},{'channel':_0xbbf5('0xf'),'model':_0xbbf5('0x10'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0x9'),'callback':afterCreate},{'channel':_0xbbf5('0xf'),'model':'SmsInteraction','event':'afterUpdate','condition':_0xbbf5('0xb'),'callback':afterUpdate},{'channel':_0xbbf5('0xf'),'model':_0xbbf5('0x11'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0xd'),'callback':afterCreateMessage},{'channel':_0xbbf5('0x12'),'model':_0xbbf5('0x13'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0x9'),'callback':afterCreate},{'channel':_0xbbf5('0x12'),'model':'WhatsappInteraction','event':_0xbbf5('0xa'),'condition':_0xbbf5('0xb'),'callback':afterUpdate},{'channel':_0xbbf5('0x12'),'model':_0xbbf5('0x14'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0xd'),'callback':afterCreateMessage},{'channel':_0xbbf5('0x15'),'model':_0xbbf5('0x16'),'event':_0xbbf5('0x8'),'condition':'newinteraction','callback':afterCreate},{'channel':_0xbbf5('0x15'),'model':'FaxInteraction','event':'afterUpdate','condition':_0xbbf5('0xb'),'callback':afterUpdate},{'channel':_0xbbf5('0x15'),'model':_0xbbf5('0x17'),'event':_0xbbf5('0x8'),'condition':'newmessage','callback':afterCreateMessage},{'channel':'Mail','model':_0xbbf5('0x18'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0x9'),'callback':afterCreate},{'channel':_0xbbf5('0x19'),'model':'MailInteraction','event':_0xbbf5('0xa'),'condition':'closedinteraction','callback':afterUpdate},{'channel':'Mail','model':_0xbbf5('0x1a'),'event':_0xbbf5('0x8'),'condition':'newmessage','callback':afterCreateMessage},{'channel':'Openchannel','model':_0xbbf5('0x1b'),'event':_0xbbf5('0x8'),'condition':_0xbbf5('0x9'),'callback':afterCreate},{'channel':_0xbbf5('0x1c'),'model':_0xbbf5('0x1b'),'event':_0xbbf5('0xa'),'condition':_0xbbf5('0xb'),'callback':afterUpdate},{'channel':_0xbbf5('0x1c'),'model':_0xbbf5('0x1d'),'event':_0xbbf5('0x8'),'condition':'newmessage','callback':afterCreateMessage}];function request(_0xd0afb9){return client[_0xbbf5('0x1e')](_0xbbf5('0x1f'),_0xd0afb9)['then'](function(_0x211b91){logger[_0xbbf5('0x20')]('Trigger,\x20%s',_0xbbf5('0x1f'),_0x211b91);if(_0x211b91[_0xbbf5('0x21')]){if(_0x211b91[_0xbbf5('0x21')][_0xbbf5('0x22')]===0x1f4){logger['error'](_0xbbf5('0x23'),'EventManager',_0x211b91[_0xbbf5('0x21')]);}}})[_0xbbf5('0x24')](function(_0x7c5dbc){logger[_0xbbf5('0x21')](_0xbbf5('0x23'),'EventManager',_0x7c5dbc);});}function afterCreateMessage(_0x1c2178){return function(_0x3d3533){_0x3d3533=_0x3d3533['get']({'plain':!![]});_0x3d3533[_0xbbf5('0x25')]=_0x1c2178;_0x3d3533['motionChannel']=_0x1c2178;_0x3d3533[_0xbbf5('0x26')]=_0x3d3533[_0xbbf5('0x27')];Promise[_0xbbf5('0x28')]()[_0xbbf5('0x29')](function(){if(typeof _0x3d3533[_0xbbf5('0x2a')]===_0xbbf5('0x2b'))return null;var _0x9f4570='SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20'+_0x3d3533[_0xbbf5('0x2a')];return db['sequelize'][_0xbbf5('0x2c')](_0x9f4570,{'type':db[_0xbbf5('0x2d')][_0xbbf5('0x2e')][_0xbbf5('0x2f')],'raw':!![]});})[_0xbbf5('0x29')](function(_0x102861){if(_0x102861){_0x3d3533[_0xbbf5('0x30')]=_0x102861[0x0];}if(typeof _0x3d3533[_0xbbf5('0x31')]===_0xbbf5('0x2b'))return;return getAgent(_0x3d3533[_0xbbf5('0x31')]);})[_0xbbf5('0x29')](function(_0x3cad52){if(_0x3cad52){_0x3d3533['interface']=util['format'](_0xbbf5('0x32'),_0x3cad52[_0xbbf5('0x33')]);_0x3d3533[_0xbbf5('0x34')]=_0x3cad52;_0x3d3533[_0xbbf5('0x25')]=_0x1c2178;return request({'event':_0xbbf5('0x35'),'message':_0x3d3533});}else{return request({'event':'outgoingmessage','message':_0x3d3533});}})[_0xbbf5('0x24')](function(_0x45ca6d){logger[_0xbbf5('0x21')](_0xbbf5('0x23'),'AfterCreateMessage',_0x45ca6d);});};}function afterCreateOfflineMessage(_0x3c99d7){return function(_0x57539f){_0x57539f=_0x57539f[_0xbbf5('0x36')]({'plain':!![]});_0x57539f[_0xbbf5('0x25')]=_0x3c99d7;_0x57539f[_0xbbf5('0x37')]=_0x3c99d7;_0x57539f[_0xbbf5('0x26')]='offlineMessage';return request({'event':_0x57539f['event'],'message':_0x57539f});};}function afterCreate(_0x5c9197,_0x3f6f75){return function(_0x10d8dd){_0x10d8dd=_0x10d8dd[_0xbbf5('0x36')]({'plain':!![]});_0x10d8dd[_0xbbf5('0x25')]=_0x5c9197;_0x10d8dd[_0xbbf5('0x37')]=_0x5c9197;_0x10d8dd[_0xbbf5('0x26')]=_0xbbf5('0x38');Promise[_0xbbf5('0x28')]()[_0xbbf5('0x29')](function(){var _0x5977d4=_0xbbf5('0x39')+_0x10d8dd['ContactId'];return db[_0xbbf5('0x3a')][_0xbbf5('0x2c')](_0x5977d4,{'type':db[_0xbbf5('0x2d')][_0xbbf5('0x2e')]['SELECT'],'raw':!![]});})[_0xbbf5('0x29')](function(_0x3a3bb3){if(_0x3a3bb3){_0x10d8dd['contact']=_0x3a3bb3[0x0];}if(typeof _0x10d8dd[_0xbbf5('0x31')]===_0xbbf5('0x2b'))return;return getAgent(_0x10d8dd[_0xbbf5('0x31')]);})[_0xbbf5('0x29')](function(_0x5e71ed){if(_0x5e71ed){_0x10d8dd[_0xbbf5('0x3b')]=util['format'](_0xbbf5('0x32'),_0x5e71ed['name']);_0x10d8dd[_0xbbf5('0x34')]=_0x5e71ed;}return request({'event':_0x3f6f75,'message':_0x10d8dd});})[_0xbbf5('0x24')](function(_0x3e4a53){logger['error']('Trigger,\x20%s',_0xbbf5('0x3c'),_0x3e4a53);});};}function afterUpdate(_0x7ef327){return function(_0xbb6881){var _0x57e026=_0xbb6881[_0xbbf5('0x3d')]('closed')&&_0xbb6881[_0xbbf5('0x3e')];var _0x3a4828=_0xbb6881[_0xbbf5('0x3d')](_0xbbf5('0x3f'));if(_0x57e026||_0x3a4828){var _0x13abff=_0x57e026?_0xbbf5('0xb'):_0xbbf5('0x3f');var _0x4d4edb=_0xbb6881[_0xbbf5('0x36')]({'plain':!![]});_0x4d4edb[_0xbbf5('0x25')]=_0x7ef327;_0x4d4edb['motionChannel']=_0x7ef327;_0x4d4edb[_0xbbf5('0x26')]=_0x57e026?_0xbbf5('0x3e'):'substatus';var _0x40a835=_0xbbf5('0x39')+_0x4d4edb[_0xbbf5('0x2a')];return db['sequelize'][_0xbbf5('0x2c')](_0x40a835,{'type':db['Sequelize'][_0xbbf5('0x2e')]['SELECT'],'raw':!![]})[_0xbbf5('0x29')](function(_0x44378e){if(_0x44378e){_0x4d4edb[_0xbbf5('0x30')]=_0x44378e[0x0];}if(typeof _0x4d4edb['UserId']===_0xbbf5('0x2b'))return;return getAgent(_0x4d4edb[_0xbbf5('0x31')]);})['then'](function(_0x1c4d36){if(_0x1c4d36){_0x4d4edb['interface']=util[_0xbbf5('0x40')](_0xbbf5('0x32'),_0x1c4d36[_0xbbf5('0x33')]);_0x4d4edb[_0xbbf5('0x34')]=_0x1c4d36;}return request({'event':_0x13abff,'message':_0x4d4edb});})[_0xbbf5('0x24')](function(_0x5484dc){logger[_0xbbf5('0x21')](_0xbbf5('0x23'),'AfterUpdate',_0x5484dc);});}};}function getAgent(_0x50d34d){return db[_0xbbf5('0x41')][_0xbbf5('0x42')]({'where':{'id':_0x50d34d},'attributes':['id',_0xbbf5('0x33'),_0xbbf5('0x43'),'email','internal'],'raw':!![]})['then'](function(_0x3c4044){return _0x3c4044;});}exports[_0xbbf5('0x44')]=function(){for(var _0x38f252=0x0;_0x38f252<triggers[_0xbbf5('0x45')];_0x38f252+=0x1){if(db[_0xbbf5('0x46')](triggers[_0x38f252][_0xbbf5('0x47')])){db[triggers[_0x38f252]['model']][_0xbbf5('0x48')](triggers[_0x38f252][_0xbbf5('0x26')],triggers[_0x38f252][_0xbbf5('0x49')](triggers[_0x38f252]['channel'][_0xbbf5('0x4a')](),triggers[_0x38f252][_0xbbf5('0x4b')]));}}};