fb0beb143ac983a31343b0f001252222cfdfc2e5
[motion-next.git] / server / api / mailAccount / mailAccount.rpc.js
1 const a397_0xa9bd=['format','unshift','NotifyMailAccount','../../config/logger','model','asc','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','List','User','Smtp','request','findOne','list','attachments','mapKey','rawAttributes','stringify','YYYY-MM-DD\x20HH:mm:ss','interaction','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','get','ShowMailAccount','bcc','applications','push','http','CmList','defaults','./mailAccount.socket','updatedAt','Attachments','Messages','GetMailAccount','ListId','Applications','message','catch','CmContact','error','update','orderBy','request\x20sent','close','result','attributes','difference','create','originTo','Start','where','MailAccount','findAll','priority','waitForTheAssignedAgent','lodash','from\x20is\x20mandatory','MailServerOut','options','attach','inReplyTo','info','register','MailAccount,\x20%s,\x20%s','then','../../mysqldb','MailApplication','UserId','limit','contact','debug','Attachment','omit','map','MailInteraction','Contacts','include','from','generate','isNil','MailAccount,\x20%s,\x20%s,\x20%s','MailMessage','length','jayson/promise','originCc','includes','autoclose','raw','body','createdAt','account','name','startRouting','*,*,*,*','moment','messageId','subject','../../rediscache','DESC'];(function(_0x3b814f,_0xa9bda6){const _0x4628ff=function(_0x481d6d){while(--_0x481d6d){_0x3b814f['push'](_0x3b814f['shift']());}};_0x4628ff(++_0xa9bda6);}(a397_0xa9bd,0x183));const a397_0x4628=function(_0x3b814f,_0xa9bda6){_0x3b814f=_0x3b814f-0x0;let _0x4628ff=a397_0xa9bd[_0x3b814f];return _0x4628ff;};const _0x206311=a397_0x4628;'use strict';const _=require(_0x206311('0x3b')),db=require(_0x206311('0x45'))['db'],logger=require(_0x206311('0x8'))('rpc'),{getSocket}=require(_0x206311('0x3')),socket=getSocket(),rs=require('randomstring'),moment=require(_0x206311('0x0')),jayson=require(_0x206311('0x57')),client=jayson['client'][_0x206311('0x1e')]({'port':0x232c});require(_0x206311('0x21'))[_0x206311('0x42')](socket);function respondWithRpcPromise(_0x481d6d,_0x55f2f5,_0x388756){return new Promise(function(_0x87adc0,_0x1f4dd5){const _0x3ccae1=a397_0x4628;return client[_0x3ccae1('0xf')](_0x481d6d,_0x388756)[_0x3ccae1('0x44')](function(_0x5f3841){const _0x51a8a4=_0x3ccae1;logger[_0x51a8a4('0x41')](_0x51a8a4('0x43'),_0x55f2f5,_0x51a8a4('0x2e')),logger[_0x51a8a4('0x4a')](_0x51a8a4('0x54'),_0x55f2f5,_0x51a8a4('0x2e'),JSON[_0x51a8a4('0x15')](_0x5f3841));if(_0x5f3841[_0x51a8a4('0x2b')]){if(_0x5f3841[_0x51a8a4('0x2b')]['code']===0x1f4)return logger[_0x51a8a4('0x2b')](_0x51a8a4('0x43'),_0x55f2f5,_0x5f3841['error'][_0x51a8a4('0x28')]),_0x1f4dd5(_0x5f3841[_0x51a8a4('0x2b')][_0x51a8a4('0x28')]);return logger[_0x51a8a4('0x2b')](_0x51a8a4('0x43'),_0x55f2f5,_0x5f3841['error'][_0x51a8a4('0x28')]),_0x87adc0(_0x5f3841[_0x51a8a4('0x2b')]['message']);}else logger[_0x51a8a4('0x41')]('MailAccount,\x20%s,\x20%s',_0x55f2f5,_0x51a8a4('0x2e')),_0x87adc0(_0x5f3841[_0x51a8a4('0x30')][_0x51a8a4('0x28')]);})[_0x3ccae1('0x29')](function(_0x590da4){const _0x306f58=_0x3ccae1;logger['error'](_0x306f58('0x43'),_0x55f2f5,_0x590da4),_0x1f4dd5(_0x590da4);});});}exports[_0x206311('0x25')]=function(_0x271ede){const _0x10a064=this;return new Promise(function(_0x2c1ab2,_0x339468){const _0x585eb2=a397_0x4628;return db[_0x585eb2('0x37')][_0x585eb2('0x38')]({'raw':_0x271ede[_0x585eb2('0x3e')]?_0x271ede[_0x585eb2('0x3e')][_0x585eb2('0x5b')]===undefined?!![]:![]:!![],'where':_0x271ede[_0x585eb2('0x3e')]?_0x271ede[_0x585eb2('0x3e')][_0x585eb2('0x36')]||null:null,'attributes':_0x271ede[_0x585eb2('0x3e')]?_0x271ede['options'][_0x585eb2('0x31')]||null:null,'limit':_0x271ede[_0x585eb2('0x3e')]?_0x271ede[_0x585eb2('0x3e')][_0x585eb2('0x48')]||null:null,'include':_0x271ede[_0x585eb2('0x3e')]?_0x271ede[_0x585eb2('0x3e')]['include']?_[_0x585eb2('0x4d')](_0x271ede[_0x585eb2('0x3e')]['include'],function(_0x1f5ed8){const _0x213e49=_0x585eb2;return{'model':db[_0x1f5ed8[_0x213e49('0x9')]],'as':_0x1f5ed8['as'],'attributes':_0x1f5ed8[_0x213e49('0x31')],'include':_0x1f5ed8[_0x213e49('0x50')]?_[_0x213e49('0x4d')](_0x1f5ed8[_0x213e49('0x50')],function(_0x431a86){const _0x48e29e=_0x213e49;return{'model':db[_0x431a86[_0x48e29e('0x9')]],'as':_0x431a86['as'],'attributes':_0x431a86[_0x48e29e('0x31')],'include':_0x431a86[_0x48e29e('0x50')]?_[_0x48e29e('0x4d')](_0x431a86['include'],function(_0x2770aa){const _0x300c9a=_0x48e29e;return{'model':db[_0x2770aa['model']],'as':_0x2770aa['as'],'attributes':_0x2770aa[_0x300c9a('0x31')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x3bdbe9){const _0x2d929f=_0x585eb2;logger[_0x2d929f('0x41')](_0x2d929f('0x25'),_0x271ede),logger[_0x2d929f('0x4a')](_0x2d929f('0x25'),_0x271ede,JSON[_0x2d929f('0x15')](_0x3bdbe9)),_0x2c1ab2(_0x3bdbe9);})[_0x585eb2('0x29')](function(_0xdc677b){const _0x4885bd=_0x585eb2;logger[_0x4885bd('0x2b')](_0x4885bd('0x25'),_0xdc677b[_0x4885bd('0x28')],_0x271ede),_0x339468(_0x10a064[_0x4885bd('0x2b')](0x1f4,_0xdc677b[_0x4885bd('0x28')]));});});},exports[_0x206311('0x1a')]=function(_0xeef068){const _0x2c7231=this;return new Promise(function(_0x32f32e,_0x5b7d8d){const _0x12590b=a397_0x4628;return db[_0x12590b('0x37')][_0x12590b('0x10')]({'raw':_0xeef068[_0x12590b('0x3e')]?_0xeef068[_0x12590b('0x3e')][_0x12590b('0x5b')]===undefined?!![]:![]:!![],'where':_0xeef068['options']?_0xeef068[_0x12590b('0x3e')]['where']||null:null,'attributes':_0xeef068[_0x12590b('0x3e')]?_0xeef068[_0x12590b('0x3e')]['attributes']||null:null,'include':_0xeef068[_0x12590b('0x3e')]?_0xeef068['options'][_0x12590b('0x50')]?_[_0x12590b('0x4d')](_0xeef068[_0x12590b('0x3e')]['include'],function(_0x3abf36){const _0xd1aca3=_0x12590b;return{'model':db[_0x3abf36[_0xd1aca3('0x9')]],'as':_0x3abf36['as'],'attributes':_0x3abf36[_0xd1aca3('0x31')],'include':_0x3abf36[_0xd1aca3('0x50')]?_[_0xd1aca3('0x4d')](_0x3abf36[_0xd1aca3('0x50')],function(_0x3f0e18){const _0x4f88ad=_0xd1aca3;return{'model':db[_0x3f0e18[_0x4f88ad('0x9')]],'as':_0x3f0e18['as'],'attributes':_0x3f0e18[_0x4f88ad('0x31')],'include':_0x3f0e18['include']?_[_0x4f88ad('0x4d')](_0x3f0e18[_0x4f88ad('0x50')],function(_0x471478){const _0x112033=_0x4f88ad;return{'model':db[_0x471478[_0x112033('0x9')]],'as':_0x471478['as'],'attributes':_0x471478[_0x112033('0x31')]};}):[]};}):[]};}):[]:[]})[_0x12590b('0x44')](function(_0xa80a1d){const _0x39a89a=_0x12590b;logger[_0x39a89a('0x41')]('ShowMailAccount',_0xeef068),logger[_0x39a89a('0x4a')](_0x39a89a('0x1a'),_0xeef068,JSON[_0x39a89a('0x15')](_0xa80a1d)),_0x32f32e(_0xa80a1d);})[_0x12590b('0x29')](function(_0x22cc65){const _0x582eb4=_0x12590b;logger[_0x582eb4('0x2b')](_0x582eb4('0x1a'),_0x22cc65['message'],_0xeef068),_0x5b7d8d(_0x2c7231[_0x582eb4('0x2b')](0x1f4,_0x22cc65['message']));});});},exports[_0x206311('0x7')]=function(_0x4bf74c){const _0x4e0f57=this;return new Promise(function(_0x5c66a5,_0x5905c0){const _0x571970=a397_0x4628,_0x5a7b6b={'body':_0x4bf74c[_0x571970('0x5c')],'channel':'mail'},_0x5efe38=_[_0x571970('0x32')](_['keys'](db['CmContact'][_0x571970('0x14')]),[_0x571970('0x5d'),_0x571970('0x22'),'CompanyId',_0x571970('0x26')]);let _0x5ee776=![];_0x4bf74c[_0x571970('0x5c')]['id']&&delete _0x4bf74c[_0x571970('0x5c')]['id'];if(_[_0x571970('0x53')](_0x4bf74c[_0x571970('0x5c')][_0x571970('0x51')]))return _0x5905c0(_0x4e0f57['error'](0x1f4,_0x571970('0x3c')));if(_[_0x571970('0x53')](_0x4bf74c[_0x571970('0x5c')]['body'])||_0x4bf74c[_0x571970('0x5c')][_0x571970('0x5c')]==='')return _0x5905c0(_0x4e0f57[_0x571970('0x2b')](0x1f4,'body\x20is\x20mandatory\x20and\x20not\x20empty'));if(_[_0x571970('0x53')](_0x4bf74c[_0x571970('0x5c')][_0x571970('0x13')]))return _0x5905c0(_0x4e0f57['error'](0x1f4,_0x571970('0x18')+_0x5efe38));if(!_[_0x571970('0x59')](_0x5efe38,_0x4bf74c[_0x571970('0x5c')]['mapKey']))return _0x5905c0(_0x4e0f57[_0x571970('0x2b')](0x1f4,_0x571970('0xb')+_0x5efe38));const _0x506612={};return _0x506612[_0x4bf74c[_0x571970('0x5c')]['mapKey']]=_0x4bf74c['body'][_0x571970('0x51')],db['MailAccount'][_0x571970('0x10')]({'where':{'id':_0x4bf74c['account']['id']},'include':[{'model':db[_0x571970('0x1f')],'as':'List','include':[{'model':db[_0x571970('0x2a')],'as':_0x571970('0x4f'),'where':_0x506612,'limit':0x1,'order':[['updatedAt',_0x571970('0x4')]]}]},{'model':db[_0x571970('0x46')],'as':_0x571970('0x27')},{'model':db[_0x571970('0x3d')],'as':_0x571970('0xe')}]})[_0x571970('0x44')](function(_0x1a55d3){const _0x245c85=_0x571970;if(_0x1a55d3&&_0x1a55d3[_0x245c85('0xc')]){const _0x292dea=_0x1a55d3[_0x245c85('0x19')]({'plain':!![]});_0x5a7b6b[_0x245c85('0x5e')]=_[_0x245c85('0x4c')](_0x292dea,[_0x245c85('0x27'),_0x245c85('0xc')]),_0x5a7b6b[_0x245c85('0x1c')]=_0x292dea[_0x245c85('0x27')],_0x5a7b6b[_0x245c85('0x11')]=_0x292dea['List'],_0x5a7b6b[_0x245c85('0x1c')]=_[_0x245c85('0x2d')](_0x5a7b6b[_0x245c85('0x1c')],[_0x245c85('0x39')],[_0x245c85('0xa')]);_0x5a7b6b['account']&&_0x5a7b6b['account'][_0x245c85('0x5a')]&&_0x5a7b6b[_0x245c85('0x1c')][_0x245c85('0x1d')]({'id':0x0,'priority':_0x5a7b6b[_0x245c85('0x1c')]['length']+0x1,'app':_0x245c85('0x2f'),'appdata':_0x245c85('0x5a'),'interval':_0x245c85('0x61')});_0x5a7b6b[_0x245c85('0x5e')][_0x245c85('0x27')]&&delete _0x5a7b6b['account']['Applications'];_0x5a7b6b[_0x245c85('0x5e')][_0x245c85('0xc')]&&delete _0x5a7b6b[_0x245c85('0x5e')][_0x245c85('0xc')];if(_0x292dea[_0x245c85('0xc')][_0x245c85('0x4f')][_0x245c85('0x56')])return _0x292dea[_0x245c85('0xc')][_0x245c85('0x4f')][0x0];const _0x212765=_[_0x245c85('0x20')](_0x4bf74c[_0x245c85('0x5c')],{'firstName':_0x4bf74c[_0x245c85('0x5c')][_0x245c85('0x51')],'ListId':_0x292dea[_0x245c85('0x26')]});return _0x212765[_0x4bf74c[_0x245c85('0x5c')][_0x245c85('0x13')]]=_0x4bf74c[_0x245c85('0x5c')][_0x245c85('0x51')],db[_0x245c85('0x2a')]['create'](_0x212765);}else throw new Error('List\x20not\x20found');})[_0x571970('0x44')](function(_0x1d32e9){const _0x28a79d=_0x571970;_0x5a7b6b[_0x28a79d('0x49')]=_0x1d32e9;if(_0x1d32e9){!_0x4bf74c[_0x28a79d('0x5c')]['message'][_0x28a79d('0x1')]&&(_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x28')]['messageId']=rs[_0x28a79d('0x52')]()+'\x20'+_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x28')][_0x28a79d('0x51')]);const _0x3010ed={'ContactId':_0x1d32e9['id'],'MailAccountId':_0x4bf74c['account']['id'],'inReplyTo':_0x4bf74c[_0x28a79d('0x5c')]['message'][_0x28a79d('0x1')],'to':_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x28')]['from'],'cc':_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x28')]['cc'],'subject':_0x4bf74c['body'][_0x28a79d('0x28')][_0x28a79d('0x2')],'attach':_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x28')][_0x28a79d('0x3f')],'lastMsgAt':moment()[_0x28a79d('0x5')](_0x28a79d('0x16')),'lastMsgDirection':'in','lastMsgBody':_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x5c')]};return db[_0x28a79d('0x4e')][_0x28a79d('0x10')]({'where':{'closed':![],'MailAccountId':_0x4bf74c['account']['id']},'include':[{'model':db[_0x28a79d('0x55')],'as':_0x28a79d('0x24'),'attributes':[_0x28a79d('0x1')],'where':{'messageId':_0x4bf74c[_0x28a79d('0x5c')][_0x28a79d('0x28')][_0x28a79d('0x40')]}}]})[_0x28a79d('0x44')](function(_0x16d103){const _0x5b1551=_0x28a79d;if(_0x16d103)return[_0x16d103,![]];return db[_0x5b1551('0x4e')][_0x5b1551('0x33')](_0x3010ed)['then'](function(_0xdced89){return[_0xdced89,!![]];});});}else throw new Error('Contact\x20not\x20found');})[_0x571970('0x44')](function([_0x2a1c4d,_0x58eb54]){const _0x10dc65=_0x571970;if(_0x2a1c4d)return _0x5ee776=_0x58eb54,!_0x58eb54?_0x2a1c4d[_0x10dc65('0x2c')]({'inReplyTo':_0x4bf74c[_0x10dc65('0x5c')][_0x10dc65('0x28')]['messageId'],'to':_0x4bf74c[_0x10dc65('0x5c')][_0x10dc65('0x28')][_0x10dc65('0x51')],'cc':_0x4bf74c[_0x10dc65('0x5c')][_0x10dc65('0x28')]['cc'],'subject':_0x4bf74c[_0x10dc65('0x5c')][_0x10dc65('0x28')]['subject'],'attach':_0x2a1c4d['attach']?_0x2a1c4d[_0x10dc65('0x3f')]:_0x4bf74c[_0x10dc65('0x5c')][_0x10dc65('0x28')]['attach'],'substatus':null,'substatusAt':moment()[_0x10dc65('0x5')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgAt':moment()['format'](_0x10dc65('0x16')),'lastMsgDirection':'in','lastMsgBody':_0x4bf74c[_0x10dc65('0x5c')][_0x10dc65('0x5c')]}):_0x2a1c4d;})[_0x571970('0x44')](function(_0x52c1a9){const _0x1ad30a=_0x571970;return _0x5a7b6b[_0x1ad30a('0x17')]=_0x52c1a9['get']({'plain':!![]}),_0x5a7b6b['interaction']['created']=_0x5ee776,db[_0x1ad30a('0x55')][_0x1ad30a('0x33')]({'body':_0x4bf74c[_0x1ad30a('0x5c')][_0x1ad30a('0x5c')],'MailAccountId':_0x4bf74c[_0x1ad30a('0x5e')]['id'],'MailInteractionId':_0x52c1a9['id'],'direction':'in','ContactId':_0x5a7b6b['contact']['id'],'messageId':_0x4bf74c['body'][_0x1ad30a('0x28')][_0x1ad30a('0x1')],'from':_0x4bf74c[_0x1ad30a('0x5c')][_0x1ad30a('0x28')]['from'],'to':_0x4bf74c[_0x1ad30a('0x5c')][_0x1ad30a('0x28')]['to'],'cc':_0x4bf74c[_0x1ad30a('0x5c')][_0x1ad30a('0x28')]['cc'],'subject':_0x4bf74c[_0x1ad30a('0x5c')]['message'][_0x1ad30a('0x2')],'attach':_0x4bf74c[_0x1ad30a('0x5c')][_0x1ad30a('0x28')][_0x1ad30a('0x3f')],'Attachments':_0x4bf74c['body']['message'][_0x1ad30a('0x12')],'originTo':_0x4bf74c[_0x1ad30a('0x5c')]['message'][_0x1ad30a('0x34')],'originCc':_0x4bf74c['body'][_0x1ad30a('0x28')][_0x1ad30a('0x58')],'bcc':_0x4bf74c[_0x1ad30a('0x5c')][_0x1ad30a('0x28')][_0x1ad30a('0x1b')]},{'include':[{'model':db[_0x1ad30a('0x4b')],'as':_0x1ad30a('0x23')}]});})[_0x571970('0x44')](function(_0x56d838){const _0x277fac=_0x571970;_0x5a7b6b[_0x277fac('0x28')]=_0x56d838;if(_0x5a7b6b[_0x277fac('0x17')][_0x277fac('0x47')])return db[_0x277fac('0xd')][_0x277fac('0x10')]({'attributes':['id',_0x277fac('0x5f')],'where':{'id':_0x5a7b6b[_0x277fac('0x17')][_0x277fac('0x47')]}})['then'](function(_0x579ac9){const _0x458003=_0x277fac;return _0x579ac9&&_0x5a7b6b[_0x458003('0x1c')][_0x458003('0x6')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x579ac9[_0x458003('0x5f')]+','+(_0x5a7b6b[_0x458003('0x5e')][_0x458003('0x3a')]||0xa),'interval':_0x458003('0x61')}),respondWithRpcPromise('Start',_0x458003('0x60'),_0x5a7b6b);});return respondWithRpcPromise(_0x277fac('0x35'),'startRouting',_0x5a7b6b);})[_0x571970('0x44')](function(_0xf750ff){_0x5c66a5(_0xf750ff);})[_0x571970('0x29')](function(_0x47187d){const _0x561401=_0x571970;_0x5905c0(_0x4e0f57['error'](0x1f4,_0x47187d[_0x561401('0x28')]));});});};