031f5e7480872686361c23e2e4ae80e484e214be
[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 _0x0dc1=['length','hasOwnProperty','model','hook','callback','toLowerCase','util','../../mysqldb','../logger','triggers','client','http','Chat','ChatInteraction','afterCreate','newinteraction','afterUpdate','closedinteraction','ChatMessage','newmessage','SmsInteraction','Sms','SmsMessage','Whatsapp','WhatsappInteraction','WhatsappMessage','Fax','FaxInteraction','FaxMessage','MailInteraction','Mail','MailMessage','Openchannel','OpenchannelInteraction','request','then','info','Trigger,\x20%s','EventManager','error','catch','get','channel','direction','resolve','ContactId','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','sequelize','query','Sequelize','QueryTypes','SELECT','contact','undefined','interface','format','SIP/%s','name','agent','incomingmessage','outgoingmessage','AfterCreateMessage','event','offlineMessage','motionChannel','opened','UserId','changed','closed','substatus','AfterUpdate','User','find','internal','default'];(function(_0x4eaa4c,_0x2f7422){var _0x15aec6=function(_0x23efc9){while(--_0x23efc9){_0x4eaa4c['push'](_0x4eaa4c['shift']());}};_0x15aec6(++_0x2f7422);}(_0x0dc1,0x17d));var _0x10dc=function(_0x56267e,_0x10eb7f){_0x56267e=_0x56267e-0x0;var _0x3a1038=_0x0dc1[_0x56267e];return _0x3a1038;};'use strict';var util=require(_0x10dc('0x0'));var db=require(_0x10dc('0x1'))['db'];var jayson=require('jayson/promise');var logger=require(_0x10dc('0x2'))(_0x10dc('0x3'));var client=jayson[_0x10dc('0x4')][_0x10dc('0x5')]({'port':0x232a});var triggers=[{'channel':_0x10dc('0x6'),'model':_0x10dc('0x7'),'event':_0x10dc('0x8'),'condition':_0x10dc('0x9'),'callback':afterCreate},{'channel':_0x10dc('0x6'),'model':_0x10dc('0x7'),'event':_0x10dc('0xa'),'condition':_0x10dc('0xb'),'callback':afterUpdate},{'channel':_0x10dc('0x6'),'model':_0x10dc('0xc'),'event':_0x10dc('0x8'),'condition':_0x10dc('0xd'),'callback':afterCreateMessage},{'channel':_0x10dc('0x6'),'model':'ChatOfflineMessage','event':_0x10dc('0x8'),'condition':_0x10dc('0xd'),'callback':afterCreateOfflineMessage},{'channel':'Sms','model':_0x10dc('0xe'),'event':'afterCreate','condition':_0x10dc('0x9'),'callback':afterCreate},{'channel':_0x10dc('0xf'),'model':'SmsInteraction','event':_0x10dc('0xa'),'condition':_0x10dc('0xb'),'callback':afterUpdate},{'channel':_0x10dc('0xf'),'model':_0x10dc('0x10'),'event':_0x10dc('0x8'),'condition':_0x10dc('0xd'),'callback':afterCreateMessage},{'channel':_0x10dc('0x11'),'model':_0x10dc('0x12'),'event':_0x10dc('0x8'),'condition':_0x10dc('0x9'),'callback':afterCreate},{'channel':'Whatsapp','model':'WhatsappInteraction','event':'afterUpdate','condition':'closedinteraction','callback':afterUpdate},{'channel':_0x10dc('0x11'),'model':_0x10dc('0x13'),'event':_0x10dc('0x8'),'condition':_0x10dc('0xd'),'callback':afterCreateMessage},{'channel':_0x10dc('0x14'),'model':_0x10dc('0x15'),'event':_0x10dc('0x8'),'condition':_0x10dc('0x9'),'callback':afterCreate},{'channel':_0x10dc('0x14'),'model':_0x10dc('0x15'),'event':'afterUpdate','condition':'closedinteraction','callback':afterUpdate},{'channel':_0x10dc('0x14'),'model':_0x10dc('0x16'),'event':_0x10dc('0x8'),'condition':'newmessage','callback':afterCreateMessage},{'channel':'Mail','model':_0x10dc('0x17'),'event':_0x10dc('0x8'),'condition':_0x10dc('0x9'),'callback':afterCreate},{'channel':_0x10dc('0x18'),'model':_0x10dc('0x17'),'event':'afterUpdate','condition':_0x10dc('0xb'),'callback':afterUpdate},{'channel':_0x10dc('0x18'),'model':_0x10dc('0x19'),'event':_0x10dc('0x8'),'condition':_0x10dc('0xd'),'callback':afterCreateMessage},{'channel':_0x10dc('0x1a'),'model':_0x10dc('0x1b'),'event':_0x10dc('0x8'),'condition':_0x10dc('0x9'),'callback':afterCreate},{'channel':_0x10dc('0x1a'),'model':_0x10dc('0x1b'),'event':_0x10dc('0xa'),'condition':_0x10dc('0xb'),'callback':afterUpdate},{'channel':_0x10dc('0x1a'),'model':'OpenchannelMessage','event':_0x10dc('0x8'),'condition':_0x10dc('0xd'),'callback':afterCreateMessage}];function request(_0x256b9f){return client[_0x10dc('0x1c')]('EventManager',_0x256b9f)[_0x10dc('0x1d')](function(_0x176377){logger[_0x10dc('0x1e')](_0x10dc('0x1f'),_0x10dc('0x20'),_0x176377);if(_0x176377[_0x10dc('0x21')]){if(_0x176377[_0x10dc('0x21')]['code']===0x1f4){logger['error'](_0x10dc('0x1f'),_0x10dc('0x20'),_0x176377['error']);}}})[_0x10dc('0x22')](function(_0x1b9d2a){logger[_0x10dc('0x21')](_0x10dc('0x1f'),_0x10dc('0x20'),_0x1b9d2a);});}function afterCreateMessage(_0x1ab7e8){return function(_0x4b9ca9){_0x4b9ca9=_0x4b9ca9[_0x10dc('0x23')]({'plain':!![]});_0x4b9ca9[_0x10dc('0x24')]=_0x1ab7e8;_0x4b9ca9['motionChannel']=_0x1ab7e8;_0x4b9ca9['event']=_0x4b9ca9[_0x10dc('0x25')];Promise[_0x10dc('0x26')]()[_0x10dc('0x1d')](function(){if(typeof _0x4b9ca9[_0x10dc('0x27')]==='undefined')return null;var _0x34f96c=_0x10dc('0x28')+_0x4b9ca9['ContactId'];return db[_0x10dc('0x29')][_0x10dc('0x2a')](_0x34f96c,{'type':db[_0x10dc('0x2b')][_0x10dc('0x2c')][_0x10dc('0x2d')],'raw':!![]});})['then'](function(_0xd5c664){if(_0xd5c664){_0x4b9ca9[_0x10dc('0x2e')]=_0xd5c664[0x0];}if(typeof _0x4b9ca9['UserId']===_0x10dc('0x2f'))return;return getAgent(_0x4b9ca9['UserId']);})[_0x10dc('0x1d')](function(_0x52c492){if(_0x52c492){_0x4b9ca9[_0x10dc('0x30')]=util[_0x10dc('0x31')](_0x10dc('0x32'),_0x52c492[_0x10dc('0x33')]);_0x4b9ca9[_0x10dc('0x34')]=_0x52c492;_0x4b9ca9[_0x10dc('0x24')]=_0x1ab7e8;return request({'event':_0x10dc('0x35'),'message':_0x4b9ca9});}else{return request({'event':_0x10dc('0x36'),'message':_0x4b9ca9});}})[_0x10dc('0x22')](function(_0x305048){logger['error'](_0x10dc('0x1f'),_0x10dc('0x37'),_0x305048);});};}function afterCreateOfflineMessage(_0x41acce){return function(_0x5e1386){_0x5e1386=_0x5e1386[_0x10dc('0x23')]({'plain':!![]});_0x5e1386[_0x10dc('0x24')]=_0x41acce;_0x5e1386['motionChannel']=_0x41acce;_0x5e1386[_0x10dc('0x38')]=_0x10dc('0x39');return request({'event':_0x5e1386[_0x10dc('0x38')],'message':_0x5e1386});};}function afterCreate(_0x15ad95,_0x2ae4b9){return function(_0x79855f){_0x79855f=_0x79855f[_0x10dc('0x23')]({'plain':!![]});_0x79855f[_0x10dc('0x24')]=_0x15ad95;_0x79855f[_0x10dc('0x3a')]=_0x15ad95;_0x79855f[_0x10dc('0x38')]=_0x10dc('0x3b');Promise[_0x10dc('0x26')]()[_0x10dc('0x1d')](function(){var _0x23827f=_0x10dc('0x28')+_0x79855f[_0x10dc('0x27')];return db['sequelize'][_0x10dc('0x2a')](_0x23827f,{'type':db[_0x10dc('0x2b')][_0x10dc('0x2c')][_0x10dc('0x2d')],'raw':!![]});})[_0x10dc('0x1d')](function(_0x358a7a){if(_0x358a7a){_0x79855f[_0x10dc('0x2e')]=_0x358a7a[0x0];}if(typeof _0x79855f['UserId']==='undefined')return;return getAgent(_0x79855f[_0x10dc('0x3c')]);})[_0x10dc('0x1d')](function(_0x13ee42){if(_0x13ee42){_0x79855f[_0x10dc('0x30')]=util['format'](_0x10dc('0x32'),_0x13ee42[_0x10dc('0x33')]);_0x79855f['agent']=_0x13ee42;}return request({'event':_0x2ae4b9,'message':_0x79855f});})[_0x10dc('0x22')](function(_0x418ef8){logger['error'](_0x10dc('0x1f'),'AfterCreate',_0x418ef8);});};}function afterUpdate(_0xc44992){return function(_0x2b790c){var _0x3db878=_0x2b790c[_0x10dc('0x3d')](_0x10dc('0x3e'))&&_0x2b790c[_0x10dc('0x3e')];var _0x20c1c1=_0x2b790c[_0x10dc('0x3d')]('substatus');if(_0x3db878||_0x20c1c1){var _0x5a8ea9=_0x3db878?'closedinteraction':_0x10dc('0x3f');var _0xf251b3=_0x2b790c[_0x10dc('0x23')]({'plain':!![]});_0xf251b3[_0x10dc('0x24')]=_0xc44992;_0xf251b3[_0x10dc('0x3a')]=_0xc44992;_0xf251b3[_0x10dc('0x38')]=_0x3db878?'closed':_0x10dc('0x3f');var _0x2a36e8=_0x10dc('0x28')+_0xf251b3['ContactId'];return db[_0x10dc('0x29')]['query'](_0x2a36e8,{'type':db[_0x10dc('0x2b')][_0x10dc('0x2c')][_0x10dc('0x2d')],'raw':!![]})['then'](function(_0xf2f43f){if(_0xf2f43f){_0xf251b3[_0x10dc('0x2e')]=_0xf2f43f[0x0];}if(typeof _0xf251b3[_0x10dc('0x3c')]==='undefined')return;return getAgent(_0xf251b3[_0x10dc('0x3c')]);})[_0x10dc('0x1d')](function(_0x4e3351){if(_0x4e3351){_0xf251b3[_0x10dc('0x30')]=util[_0x10dc('0x31')](_0x10dc('0x32'),_0x4e3351[_0x10dc('0x33')]);_0xf251b3[_0x10dc('0x34')]=_0x4e3351;}return request({'event':_0x5a8ea9,'message':_0xf251b3});})[_0x10dc('0x22')](function(_0x87fc22){logger[_0x10dc('0x21')]('Trigger,\x20%s',_0x10dc('0x40'),_0x87fc22);});}};}function getAgent(_0x191999){return db[_0x10dc('0x41')][_0x10dc('0x42')]({'where':{'id':_0x191999},'attributes':['id',_0x10dc('0x33'),'fullname','email',_0x10dc('0x43')],'raw':!![]})[_0x10dc('0x1d')](function(_0x180d0c){return _0x180d0c;});}exports[_0x10dc('0x44')]=function(){for(var _0x42606a=0x0;_0x42606a<triggers[_0x10dc('0x45')];_0x42606a+=0x1){if(db[_0x10dc('0x46')](triggers[_0x42606a][_0x10dc('0x47')])){db[triggers[_0x42606a]['model']][_0x10dc('0x48')](triggers[_0x42606a][_0x10dc('0x38')],triggers[_0x42606a][_0x10dc('0x49')](triggers[_0x42606a]['channel'][_0x10dc('0x4a')](),triggers[_0x42606a]['condition']));}}};