b4cb1dcb9f6bd23c21a4a1822943a89a6415d955
[motion2.git] / server / api / mailAccount / mailAccount.rpc.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 _0x326f=['localhost','socket.io-emitter','request','MailAccount,\x20%s,\x20%s','debug','request\x20sent','stringify','error','code','message','info','result','catch','options','attributes','limit','include','map','model','then','GetMailAccount','ShowMailAccount','MailAccount','find','raw','setDataValue','smtpOptions','getSmtpOptions','NotifyMailAccount','body','mail','keys','CmContact','rawAttributes','createdAt','updatedAt','CompanyId','ListId','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','from','account','CmList','List','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','CloudProvider','get','applications','list','priority','asc','autoclose','push','close','*,*,*,*','length','defaults','create','List\x20not\x20found','messageId','generate','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','spread','update','interaction','created','subject','attachments','originTo','bcc','originChannel','ChatOfflineMessageId','Attachment','Attachments','User','name','UserId','unshift','waitForTheAssignedAgent','queueId','queue','waitForTheAssignedQueue','Start','lodash','randomstring','ioredis','../../mysqldb','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis'];(function(_0x34021d,_0x753aa6){var _0x16597b=function(_0x2d040a){while(--_0x2d040a){_0x34021d['push'](_0x34021d['shift']());}};_0x16597b(++_0x753aa6);}(_0x326f,0xd1));var _0xf326=function(_0x4d4b0c,_0x177a2f){_0x4d4b0c=_0x4d4b0c-0x0;var _0x1b9336=_0x326f[_0x4d4b0c];return _0x1b9336;};'use strict';var _=require(_0xf326('0x0'));var moment=require('moment');var BPromise=require('bluebird');var rs=require(_0xf326('0x1'));var Redis=require(_0xf326('0x2'));var db=require(_0xf326('0x3'))['db'];var logger=require(_0xf326('0x4'))(_0xf326('0x5'));var config=require(_0xf326('0x6'));var jayson=require(_0xf326('0x7'));var client=jayson[_0xf326('0x8')][_0xf326('0x9')]({'port':0x232c});config[_0xf326('0xa')]=_['defaults'](config[_0xf326('0xa')],{'host':_0xf326('0xb'),'port':0x18eb});var socket=require(_0xf326('0xc'))(new Redis(config[_0xf326('0xa')]));require('./mailAccount.socket')['register'](socket);function respondWithRpcPromise(_0x592975,_0x3bacf2,_0x1fc80e){return new BPromise(function(_0x163416,_0x2686e1){return client[_0xf326('0xd')](_0x592975,_0x1fc80e)['then'](function(_0x136e2d){logger['info'](_0xf326('0xe'),_0x3bacf2,'request\x20sent');logger[_0xf326('0xf')]('MailAccount,\x20%s,\x20%s,\x20%s',_0x3bacf2,_0xf326('0x10'),JSON[_0xf326('0x11')](_0x136e2d));if(_0x136e2d['error']){if(_0x136e2d[_0xf326('0x12')][_0xf326('0x13')]===0x1f4){logger[_0xf326('0x12')](_0xf326('0xe'),_0x3bacf2,_0x136e2d['error'][_0xf326('0x14')]);return _0x2686e1(_0x136e2d['error']['message']);}logger[_0xf326('0x12')](_0xf326('0xe'),_0x3bacf2,_0x136e2d[_0xf326('0x12')][_0xf326('0x14')]);return _0x163416(_0x136e2d[_0xf326('0x12')][_0xf326('0x14')]);}else{logger[_0xf326('0x15')](_0xf326('0xe'),_0x3bacf2,'request\x20sent');_0x163416(_0x136e2d[_0xf326('0x16')][_0xf326('0x14')]);}})[_0xf326('0x17')](function(_0x31ffb9){logger[_0xf326('0x12')](_0xf326('0xe'),_0x3bacf2,_0x31ffb9);_0x2686e1(_0x31ffb9);});});}exports['GetMailAccount']=function(_0x4a5b6c){var _0x56ad41=this;return new Promise(function(_0x16e605,_0x289f4b){return db['MailAccount']['findAll']({'raw':_0x4a5b6c[_0xf326('0x18')]?_0x4a5b6c['options']['raw']===undefined?!![]:![]:!![],'where':_0x4a5b6c[_0xf326('0x18')]?_0x4a5b6c[_0xf326('0x18')]['where']||null:null,'attributes':_0x4a5b6c['options']?_0x4a5b6c[_0xf326('0x18')][_0xf326('0x19')]||null:null,'limit':_0x4a5b6c['options']?_0x4a5b6c[_0xf326('0x18')][_0xf326('0x1a')]||null:null,'include':_0x4a5b6c[_0xf326('0x18')]?_0x4a5b6c[_0xf326('0x18')][_0xf326('0x1b')]?_[_0xf326('0x1c')](_0x4a5b6c['options']['include'],function(_0x4f31f6){return{'model':db[_0x4f31f6[_0xf326('0x1d')]],'as':_0x4f31f6['as'],'attributes':_0x4f31f6[_0xf326('0x19')],'include':_0x4f31f6[_0xf326('0x1b')]?_[_0xf326('0x1c')](_0x4f31f6[_0xf326('0x1b')],function(_0x410237){return{'model':db[_0x410237['model']],'as':_0x410237['as'],'attributes':_0x410237[_0xf326('0x19')],'include':_0x410237[_0xf326('0x1b')]?_[_0xf326('0x1c')](_0x410237['include'],function(_0x1617d2){return{'model':db[_0x1617d2['model']],'as':_0x1617d2['as'],'attributes':_0x1617d2['attributes']};}):[]};}):[]};}):[]:[]})[_0xf326('0x1e')](function(_0x1ae23a){logger[_0xf326('0x15')]('GetMailAccount',_0x4a5b6c);logger[_0xf326('0xf')](_0xf326('0x1f'),_0x4a5b6c,JSON['stringify'](_0x1ae23a));_0x16e605(_0x1ae23a);})[_0xf326('0x17')](function(_0x119ab8){logger[_0xf326('0x12')](_0xf326('0x1f'),_0x119ab8[_0xf326('0x14')],_0x4a5b6c);_0x289f4b(_0x56ad41[_0xf326('0x12')](0x1f4,_0x119ab8[_0xf326('0x14')]));});});};exports[_0xf326('0x20')]=function(_0x43b951){var _0x349a03=this;return new Promise(function(_0x4b52f2,_0x1035ed){return db[_0xf326('0x21')][_0xf326('0x22')]({'raw':_0x43b951['options']?_0x43b951[_0xf326('0x18')][_0xf326('0x23')]===undefined?!![]:![]:!![],'where':_0x43b951[_0xf326('0x18')]?_0x43b951['options']['where']||null:null,'attributes':_0x43b951[_0xf326('0x18')]?_0x43b951[_0xf326('0x18')][_0xf326('0x19')]||null:null,'include':_0x43b951[_0xf326('0x18')]?_0x43b951[_0xf326('0x18')][_0xf326('0x1b')]?_[_0xf326('0x1c')](_0x43b951[_0xf326('0x18')]['include'],function(_0x461e93){return{'model':db[_0x461e93[_0xf326('0x1d')]],'as':_0x461e93['as'],'attributes':_0x461e93['attributes'],'include':_0x461e93[_0xf326('0x1b')]?_['map'](_0x461e93[_0xf326('0x1b')],function(_0x481b64){return{'model':db[_0x481b64['model']],'as':_0x481b64['as'],'attributes':_0x481b64[_0xf326('0x19')],'include':_0x481b64['include']?_[_0xf326('0x1c')](_0x481b64['include'],function(_0x310dbb){return{'model':db[_0x310dbb[_0xf326('0x1d')]],'as':_0x310dbb['as'],'attributes':_0x310dbb[_0xf326('0x19')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x3dcc7e){if(_0x43b951[_0xf326('0x18')]['smtp']===!![])_0x3dcc7e[_0xf326('0x24')](_0xf326('0x25'),_0x3dcc7e[_0xf326('0x26')]());logger[_0xf326('0x15')]('ShowMailAccount',_0x43b951);logger[_0xf326('0xf')](_0xf326('0x20'),_0x43b951,JSON[_0xf326('0x11')](_0x3dcc7e));_0x4b52f2(_0x3dcc7e);})[_0xf326('0x17')](function(_0x1d88ed){logger['error'](_0xf326('0x20'),_0x1d88ed[_0xf326('0x14')],_0x43b951);_0x1035ed(_0x349a03['error'](0x1f4,_0x1d88ed['message']));});});};exports[_0xf326('0x27')]=function(_0x577634){var _0x55dfa8=this;return new Promise(function(_0x22d462,_0x5d6ae2){var _0x3e1e8c={'body':_0x577634[_0xf326('0x28')],'channel':_0xf326('0x29')};var _0x39611e=_['difference'](_[_0xf326('0x2a')](db[_0xf326('0x2b')][_0xf326('0x2c')]),[_0xf326('0x2d'),_0xf326('0x2e'),_0xf326('0x2f'),_0xf326('0x30')]);var _0x224a32=![];if(_0x577634[_0xf326('0x28')]['id']){delete _0x577634[_0xf326('0x28')]['id'];}if(_[_0xf326('0x31')](_0x577634[_0xf326('0x28')]['from'])){return _0x5d6ae2(_0x55dfa8[_0xf326('0x12')](0x1f4,_0xf326('0x32')));}if(_[_0xf326('0x31')](_0x577634[_0xf326('0x28')][_0xf326('0x28')])||_0x577634[_0xf326('0x28')]['body']===''){return _0x5d6ae2(_0x55dfa8[_0xf326('0x12')](0x1f4,_0xf326('0x33')));}if(_['isNil'](_0x577634[_0xf326('0x28')][_0xf326('0x34')])){return _0x5d6ae2(_0x55dfa8[_0xf326('0x12')](0x1f4,_0xf326('0x35')+_0x39611e));}if(!_[_0xf326('0x36')](_0x39611e,_0x577634['body']['mapKey'])){return _0x5d6ae2(_0x55dfa8[_0xf326('0x12')](0x1f4,_0xf326('0x37')+_0x39611e));}var _0x5ed62e={};_0x5ed62e[_0x577634['body'][_0xf326('0x34')]]=_0x577634[_0xf326('0x28')][_0xf326('0x38')];return db['MailAccount'][_0xf326('0x22')]({'where':{'id':_0x577634[_0xf326('0x39')]['id']},'include':[{'model':db[_0xf326('0x3a')],'as':_0xf326('0x3b'),'include':[{'model':db[_0xf326('0x2b')],'as':_0xf326('0x3c'),'where':_0x5ed62e,'limit':0x1,'order':[[_0xf326('0x2e'),_0xf326('0x3d')]]}]},{'model':db[_0xf326('0x3e')],'as':_0xf326('0x3f')},{'model':db[_0xf326('0x40')],'as':_0xf326('0x41'),'include':[{'model':db[_0xf326('0x42')],'as':_0xf326('0x42')}]}]})[_0xf326('0x1e')](function(_0x59f9a7){if(_0x59f9a7&&_0x59f9a7[_0xf326('0x3b')]){var _0x1b0e03=_0x59f9a7[_0xf326('0x43')]({'plain':!![]});_0x3e1e8c[_0xf326('0x39')]=_['omit'](_0x1b0e03,[_0xf326('0x3f'),_0xf326('0x3b')]);_0x3e1e8c[_0xf326('0x39')][_0xf326('0x25')]=_0x59f9a7['getSmtpOptions']();_0x3e1e8c[_0xf326('0x44')]=_0x1b0e03[_0xf326('0x3f')];_0x3e1e8c[_0xf326('0x45')]=_0x1b0e03[_0xf326('0x3b')];_0x3e1e8c[_0xf326('0x44')]=_['orderBy'](_0x3e1e8c['applications'],[_0xf326('0x46')],[_0xf326('0x47')]);if(_0x3e1e8c[_0xf326('0x39')]&&_0x3e1e8c['account'][_0xf326('0x48')]){_0x3e1e8c[_0xf326('0x44')][_0xf326('0x49')]({'id':0x0,'priority':_0x3e1e8c['applications']['length']+0x1,'app':_0xf326('0x4a'),'appdata':'autoclose','interval':_0xf326('0x4b')});}if(_0x3e1e8c['account'][_0xf326('0x3f')]){delete _0x3e1e8c[_0xf326('0x39')][_0xf326('0x3f')];}if(_0x3e1e8c[_0xf326('0x39')][_0xf326('0x3b')]){delete _0x3e1e8c[_0xf326('0x39')][_0xf326('0x3b')];}if(_0x1b0e03['List'][_0xf326('0x3c')][_0xf326('0x4c')]){return _0x1b0e03[_0xf326('0x3b')]['Contacts'][0x0];}var _0x208ba4=_[_0xf326('0x4d')](_0x577634[_0xf326('0x28')],{'firstName':_0x577634[_0xf326('0x28')][_0xf326('0x38')],'ListId':_0x1b0e03['ListId']});_0x208ba4[_0x577634[_0xf326('0x28')][_0xf326('0x34')]]=_0x577634[_0xf326('0x28')][_0xf326('0x38')];return db['CmContact'][_0xf326('0x4e')](_0x208ba4);}else{throw new Error(_0xf326('0x4f'));}})[_0xf326('0x1e')](function(_0x29e52e){_0x3e1e8c['contact']=_0x29e52e;if(_0x29e52e){if(!_0x577634['body'][_0xf326('0x14')][_0xf326('0x50')]){_0x577634['body'][_0xf326('0x14')][_0xf326('0x50')]=rs[_0xf326('0x51')]()+'\x20'+_0x577634[_0xf326('0x28')]['message'][_0xf326('0x38')];}var _0x5657b7={'ContactId':_0x29e52e['id'],'MailAccountId':_0x577634['account']['id'],'inReplyTo':_0x577634[_0xf326('0x28')]['message'][_0xf326('0x50')],'to':_0x577634[_0xf326('0x28')][_0xf326('0x14')]['from'],'cc':_0x577634['body'][_0xf326('0x14')]['cc'],'subject':_0x577634[_0xf326('0x28')][_0xf326('0x14')]['subject'],'attach':_0x577634[_0xf326('0x28')]['message'][_0xf326('0x52')],'lastMsgAt':moment()[_0xf326('0x53')](_0xf326('0x54')),'lastMsgDirection':'in'};return db[_0xf326('0x55')][_0xf326('0x22')]({'where':{'closed':![],'MailAccountId':_0x577634[_0xf326('0x39')]['id']},'include':[{'model':db[_0xf326('0x56')],'as':_0xf326('0x57'),'attributes':[_0xf326('0x50')],'where':{'messageId':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x58')]}}]})[_0xf326('0x1e')](function(_0x4ca1b8){if(_0x4ca1b8){return[_0x4ca1b8,![]];}return db[_0xf326('0x55')]['create'](_0x5657b7)[_0xf326('0x1e')](function(_0x32d173){return[_0x32d173,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0xf326('0x59')](function(_0x5283da,_0x3255b2){if(_0x5283da){_0x224a32=_0x3255b2;if(!_0x3255b2){return _0x5283da[_0xf326('0x5a')]({'inReplyTo':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x50')],'to':_0x577634['body']['message'][_0xf326('0x38')],'cc':_0x577634[_0xf326('0x28')][_0xf326('0x14')]['cc'],'subject':_0x577634[_0xf326('0x28')][_0xf326('0x14')]['subject'],'attach':_0x5283da[_0xf326('0x52')]?_0x5283da[_0xf326('0x52')]:_0x577634['body']['message']['attach'],'substatus':null,'substatusAt':moment()[_0xf326('0x53')](_0xf326('0x54')),'lastMsgAt':moment()[_0xf326('0x53')](_0xf326('0x54')),'lastMsgDirection':'in'});}else{return _0x5283da;}}})[_0xf326('0x1e')](function(_0x23a56e){_0x3e1e8c[_0xf326('0x5b')]=_0x23a56e['get']({'plain':!![]});_0x3e1e8c[_0xf326('0x5b')][_0xf326('0x5c')]=_0x224a32;return db[_0xf326('0x56')][_0xf326('0x4e')]({'body':_0x577634['body'][_0xf326('0x28')],'MailAccountId':_0x577634['account']['id'],'MailInteractionId':_0x23a56e['id'],'direction':'in','ContactId':_0x3e1e8c['contact']['id'],'messageId':_0x577634[_0xf326('0x28')]['message'][_0xf326('0x50')],'from':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x38')],'to':_0x577634[_0xf326('0x28')]['message']['to'],'cc':_0x577634['body']['message']['cc'],'subject':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x5d')],'attach':_0x577634[_0xf326('0x28')][_0xf326('0x14')]['attach'],'Attachments':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x5e')],'originTo':_0x577634['body']['message'][_0xf326('0x5f')],'originCc':_0x577634[_0xf326('0x28')]['message']['originCc'],'bcc':_0x577634[_0xf326('0x28')]['message'][_0xf326('0x60')],'ChatWebsiteId':_0x577634['body']['message']['ChatWebsiteId']||null,'originChannel':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x61')]||'Email','ChatOfflineMessageId':_0x577634[_0xf326('0x28')][_0xf326('0x14')][_0xf326('0x62')]||null},{'include':[{'model':db[_0xf326('0x63')],'as':_0xf326('0x64')}]});})[_0xf326('0x1e')](function(_0x7f6d0e){_0x3e1e8c[_0xf326('0x14')]=_0x7f6d0e;if(!_0x3e1e8c[_0xf326('0x5b')]['UserId']){return;}return db[_0xf326('0x65')]['find']({'attributes':['id',_0xf326('0x66')],'where':{'id':_0x3e1e8c[_0xf326('0x5b')][_0xf326('0x67')]}});})['then'](function(_0x2465f8){if(_0x2465f8){_0x3e1e8c[_0xf326('0x44')][_0xf326('0x68')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x2465f8[_0xf326('0x66')]+','+(_0x3e1e8c[_0xf326('0x39')][_0xf326('0x69')]||0xa),'interval':_0xf326('0x4b')});}if(!_0x3e1e8c['interaction'][_0xf326('0x6a')]){return;}return db['MailQueue']['find']({'attributes':['id',_0xf326('0x66')],'where':{'id':_0x3e1e8c[_0xf326('0x5b')]['queueId']}});})['then'](function(_0x4b8f8d){if(_0x4b8f8d){_0x3e1e8c[_0xf326('0x44')]['unshift']({'id':0x0,'priority':0x1,'app':_0xf326('0x6b'),'appdata':_0x4b8f8d[_0xf326('0x66')]+','+(_0x3e1e8c[_0xf326('0x39')][_0xf326('0x6c')]||0x12c),'interval':_0xf326('0x4b')});}return respondWithRpcPromise(_0xf326('0x6d'),'startRouting',_0x3e1e8c);})[_0xf326('0x1e')](function(_0x323926){_0x22d462(_0x323926);})['catch'](function(_0x497ada){_0x5d6ae2(_0x55dfa8[_0xf326('0x12')](0x1f4,_0x497ada['message']));});});};