0cea1484310b496630c2bb118a15300bcb7446f0
[motion-next.git] / server / api / mailAccount / mailAccount.rpc.js
1 const a397_0x1700=['raw','ListId','Attachments','../../mysqldb','where','unshift','close','keys','jayson/promise','interaction','ShowMailAccount','result','attach','inReplyTo','code','messageId','then','catch','account','generate','MailInteraction','applications','originCc','findOne','createdAt','isNil','error','Messages','GetMailAccount','create','debug','get','YYYY-MM-DD\x20HH:mm:ss','UserId','User','omit','moment','*,*,*,*','CmList','MailApplication','name','attributes','difference','List','mapKey','body\x20is\x20mandatory\x20and\x20not\x20empty','Contacts','register','format','rpc','MailServerOut','List\x20not\x20found','include','lodash','includes','CmContact','message','client','list','defaults','model','map','created','updatedAt','MailAccount,\x20%s,\x20%s','autoclose','rawAttributes','randomstring','options','../../rediscache','originTo','info','Smtp','request\x20sent','from\x20is\x20mandatory','Applications','body','MailMessage','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','subject','startRouting','stringify','attachments','from','bcc','request','MailAccount','length'];(function(_0x58fe90,_0x1700a0){const _0x514268=function(_0x45fdd0){while(--_0x45fdd0){_0x58fe90['push'](_0x58fe90['shift']());}};_0x514268(++_0x1700a0);}(a397_0x1700,0x1a8));const a397_0x5142=function(_0x58fe90,_0x1700a0){_0x58fe90=_0x58fe90-0x0;let _0x514268=a397_0x1700[_0x58fe90];return _0x514268;};const _0x51ae55=a397_0x5142;'use strict';const _=require(_0x51ae55('0x45')),db=require(_0x51ae55('0x13'))['db'],logger=require('../../config/logger')(_0x51ae55('0x41')),{getSocket}=require(_0x51ae55('0x55')),socket=getSocket(),rs=require(_0x51ae55('0x53')),moment=require(_0x51ae55('0x34')),jayson=require(_0x51ae55('0x18')),client=jayson[_0x51ae55('0x49')]['http']({'port':0x232c});require('./mailAccount.socket')[_0x51ae55('0x3f')](socket);function respondWithRpcPromise(_0x45fdd0,_0x2b888c,_0x41fe90){return new Promise(function(_0x28e1b6,_0xabbd48){const _0x114905=a397_0x5142;return client[_0x114905('0xd')](_0x45fdd0,_0x41fe90)[_0x114905('0x20')](function(_0x5187e0){const _0x22afa2=_0x114905;logger[_0x22afa2('0x57')](_0x22afa2('0x50'),_0x2b888c,_0x22afa2('0x1')),logger[_0x22afa2('0x2e')]('MailAccount,\x20%s,\x20%s,\x20%s',_0x2b888c,_0x22afa2('0x1'),JSON[_0x22afa2('0x9')](_0x5187e0));if(_0x5187e0['error']){if(_0x5187e0[_0x22afa2('0x2a')][_0x22afa2('0x1e')]===0x1f4)return logger[_0x22afa2('0x2a')]('MailAccount,\x20%s,\x20%s',_0x2b888c,_0x5187e0['error'][_0x22afa2('0x48')]),_0xabbd48(_0x5187e0[_0x22afa2('0x2a')]['message']);return logger[_0x22afa2('0x2a')](_0x22afa2('0x50'),_0x2b888c,_0x5187e0['error'][_0x22afa2('0x48')]),_0x28e1b6(_0x5187e0['error']['message']);}else logger[_0x22afa2('0x57')](_0x22afa2('0x50'),_0x2b888c,'request\x20sent'),_0x28e1b6(_0x5187e0[_0x22afa2('0x1b')]['message']);})[_0x114905('0x21')](function(_0x52b132){logger['error']('MailAccount,\x20%s,\x20%s',_0x2b888c,_0x52b132),_0xabbd48(_0x52b132);});});}exports[_0x51ae55('0x2c')]=function(_0xc22e56){const _0x5821a7=this;return new Promise(function(_0x12128a,_0x1bcf8d){const _0x146e02=a397_0x5142;return db['MailAccount']['findAll']({'raw':_0xc22e56[_0x146e02('0x54')]?_0xc22e56['options']['raw']===undefined?!![]:![]:!![],'where':_0xc22e56[_0x146e02('0x54')]?_0xc22e56[_0x146e02('0x54')][_0x146e02('0x14')]||null:null,'attributes':_0xc22e56[_0x146e02('0x54')]?_0xc22e56[_0x146e02('0x54')][_0x146e02('0x39')]||null:null,'limit':_0xc22e56[_0x146e02('0x54')]?_0xc22e56['options']['limit']||null:null,'include':_0xc22e56['options']?_0xc22e56[_0x146e02('0x54')][_0x146e02('0x44')]?_[_0x146e02('0x4d')](_0xc22e56[_0x146e02('0x54')][_0x146e02('0x44')],function(_0xde6d23){const _0x10ff18=_0x146e02;return{'model':db[_0xde6d23[_0x10ff18('0x4c')]],'as':_0xde6d23['as'],'attributes':_0xde6d23[_0x10ff18('0x39')],'include':_0xde6d23[_0x10ff18('0x44')]?_[_0x10ff18('0x4d')](_0xde6d23[_0x10ff18('0x44')],function(_0x2a5555){const _0x1dffd7=_0x10ff18;return{'model':db[_0x2a5555[_0x1dffd7('0x4c')]],'as':_0x2a5555['as'],'attributes':_0x2a5555[_0x1dffd7('0x39')],'include':_0x2a5555[_0x1dffd7('0x44')]?_[_0x1dffd7('0x4d')](_0x2a5555[_0x1dffd7('0x44')],function(_0x5c50bf){const _0x5bedd8=_0x1dffd7;return{'model':db[_0x5c50bf[_0x5bedd8('0x4c')]],'as':_0x5c50bf['as'],'attributes':_0x5c50bf[_0x5bedd8('0x39')]};}):[]};}):[]};}):[]:[]})[_0x146e02('0x20')](function(_0x57886f){const _0x1f6140=_0x146e02;logger[_0x1f6140('0x57')](_0x1f6140('0x2c'),_0xc22e56),logger[_0x1f6140('0x2e')](_0x1f6140('0x2c'),_0xc22e56,JSON[_0x1f6140('0x9')](_0x57886f)),_0x12128a(_0x57886f);})[_0x146e02('0x21')](function(_0x4e4357){const _0xe30c59=_0x146e02;logger[_0xe30c59('0x2a')](_0xe30c59('0x2c'),_0x4e4357[_0xe30c59('0x48')],_0xc22e56),_0x1bcf8d(_0x5821a7[_0xe30c59('0x2a')](0x1f4,_0x4e4357['message']));});});},exports['ShowMailAccount']=function(_0x20b70d){const _0x4eb7ef=this;return new Promise(function(_0x352744,_0x32c3c3){const _0x4738fd=a397_0x5142;return db[_0x4738fd('0xe')][_0x4738fd('0x27')]({'raw':_0x20b70d[_0x4738fd('0x54')]?_0x20b70d['options'][_0x4738fd('0x10')]===undefined?!![]:![]:!![],'where':_0x20b70d[_0x4738fd('0x54')]?_0x20b70d[_0x4738fd('0x54')][_0x4738fd('0x14')]||null:null,'attributes':_0x20b70d[_0x4738fd('0x54')]?_0x20b70d[_0x4738fd('0x54')][_0x4738fd('0x39')]||null:null,'include':_0x20b70d[_0x4738fd('0x54')]?_0x20b70d['options'][_0x4738fd('0x44')]?_[_0x4738fd('0x4d')](_0x20b70d[_0x4738fd('0x54')]['include'],function(_0x1a9dc9){const _0x5c678a=_0x4738fd;return{'model':db[_0x1a9dc9['model']],'as':_0x1a9dc9['as'],'attributes':_0x1a9dc9[_0x5c678a('0x39')],'include':_0x1a9dc9['include']?_[_0x5c678a('0x4d')](_0x1a9dc9[_0x5c678a('0x44')],function(_0x40e549){const _0x4f1a58=_0x5c678a;return{'model':db[_0x40e549[_0x4f1a58('0x4c')]],'as':_0x40e549['as'],'attributes':_0x40e549[_0x4f1a58('0x39')],'include':_0x40e549[_0x4f1a58('0x44')]?_[_0x4f1a58('0x4d')](_0x40e549[_0x4f1a58('0x44')],function(_0x21f604){return{'model':db[_0x21f604['model']],'as':_0x21f604['as'],'attributes':_0x21f604['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x7cc793){const _0x3d0374=_0x4738fd;logger[_0x3d0374('0x57')](_0x3d0374('0x1a'),_0x20b70d),logger['debug'](_0x3d0374('0x1a'),_0x20b70d,JSON[_0x3d0374('0x9')](_0x7cc793)),_0x352744(_0x7cc793);})[_0x4738fd('0x21')](function(_0x487027){const _0x5c5602=_0x4738fd;logger[_0x5c5602('0x2a')](_0x5c5602('0x1a'),_0x487027['message'],_0x20b70d),_0x32c3c3(_0x4eb7ef[_0x5c5602('0x2a')](0x1f4,_0x487027[_0x5c5602('0x48')]));});});},exports['NotifyMailAccount']=function(_0x5f447e){const _0x3dbf46=this;return new Promise(function(_0x3e5c66,_0x31cd0b){const _0x39b867=a397_0x5142,_0x34e4cf={'body':_0x5f447e['body'],'channel':'mail'},_0x5f0df0=_[_0x39b867('0x3a')](_[_0x39b867('0x17')](db['CmContact'][_0x39b867('0x52')]),[_0x39b867('0x28'),_0x39b867('0x4f'),'CompanyId',_0x39b867('0x11')]);let _0x5955b8=![];_0x5f447e[_0x39b867('0x4')]['id']&&delete _0x5f447e[_0x39b867('0x4')]['id'];if(_['isNil'](_0x5f447e['body'][_0x39b867('0xb')]))return _0x31cd0b(_0x3dbf46['error'](0x1f4,_0x39b867('0x2')));if(_[_0x39b867('0x29')](_0x5f447e[_0x39b867('0x4')]['body'])||_0x5f447e['body']['body']==='')return _0x31cd0b(_0x3dbf46[_0x39b867('0x2a')](0x1f4,_0x39b867('0x3d')));if(_[_0x39b867('0x29')](_0x5f447e[_0x39b867('0x4')][_0x39b867('0x3c')]))return _0x31cd0b(_0x3dbf46[_0x39b867('0x2a')](0x1f4,'mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x5f0df0));if(!_[_0x39b867('0x46')](_0x5f0df0,_0x5f447e['body'][_0x39b867('0x3c')]))return _0x31cd0b(_0x3dbf46[_0x39b867('0x2a')](0x1f4,_0x39b867('0x6')+_0x5f0df0));const _0x4740f8={};return _0x4740f8[_0x5f447e[_0x39b867('0x4')][_0x39b867('0x3c')]]=_0x5f447e[_0x39b867('0x4')]['from'],db['MailAccount'][_0x39b867('0x27')]({'where':{'id':_0x5f447e[_0x39b867('0x22')]['id']},'include':[{'model':db[_0x39b867('0x36')],'as':'List','include':[{'model':db[_0x39b867('0x47')],'as':'Contacts','where':_0x4740f8,'limit':0x1,'order':[['updatedAt','DESC']]}]},{'model':db[_0x39b867('0x37')],'as':_0x39b867('0x3')},{'model':db[_0x39b867('0x42')],'as':_0x39b867('0x0')}]})['then'](function(_0x1f7221){const _0x464f36=_0x39b867;if(_0x1f7221&&_0x1f7221[_0x464f36('0x3b')]){const _0x334ad4=_0x1f7221[_0x464f36('0x2f')]({'plain':!![]});_0x34e4cf[_0x464f36('0x22')]=_[_0x464f36('0x33')](_0x334ad4,['Applications',_0x464f36('0x3b')]),_0x34e4cf[_0x464f36('0x25')]=_0x334ad4[_0x464f36('0x3')],_0x34e4cf[_0x464f36('0x4a')]=_0x334ad4[_0x464f36('0x3b')],_0x34e4cf[_0x464f36('0x25')]=_['orderBy'](_0x34e4cf[_0x464f36('0x25')],['priority'],['asc']);_0x34e4cf[_0x464f36('0x22')]&&_0x34e4cf[_0x464f36('0x22')][_0x464f36('0x51')]&&_0x34e4cf[_0x464f36('0x25')]['push']({'id':0x0,'priority':_0x34e4cf[_0x464f36('0x25')]['length']+0x1,'app':_0x464f36('0x16'),'appdata':_0x464f36('0x51'),'interval':'*,*,*,*'});_0x34e4cf[_0x464f36('0x22')][_0x464f36('0x3')]&&delete _0x34e4cf[_0x464f36('0x22')][_0x464f36('0x3')];_0x34e4cf[_0x464f36('0x22')][_0x464f36('0x3b')]&&delete _0x34e4cf[_0x464f36('0x22')]['List'];if(_0x334ad4[_0x464f36('0x3b')][_0x464f36('0x3e')][_0x464f36('0xf')])return _0x334ad4[_0x464f36('0x3b')][_0x464f36('0x3e')][0x0];const _0x12dcee=_[_0x464f36('0x4b')](_0x5f447e[_0x464f36('0x4')],{'firstName':_0x5f447e['body'][_0x464f36('0xb')],'ListId':_0x334ad4[_0x464f36('0x11')]});return _0x12dcee[_0x5f447e[_0x464f36('0x4')][_0x464f36('0x3c')]]=_0x5f447e['body'][_0x464f36('0xb')],db[_0x464f36('0x47')][_0x464f36('0x2d')](_0x12dcee);}else throw new Error(_0x464f36('0x43'));})[_0x39b867('0x20')](function(_0x3f88cd){const _0x4a1a07=_0x39b867;_0x34e4cf['contact']=_0x3f88cd;if(_0x3f88cd){!_0x5f447e['body'][_0x4a1a07('0x48')][_0x4a1a07('0x1f')]&&(_0x5f447e[_0x4a1a07('0x4')][_0x4a1a07('0x48')][_0x4a1a07('0x1f')]=rs[_0x4a1a07('0x23')]()+'\x20'+_0x5f447e[_0x4a1a07('0x4')][_0x4a1a07('0x48')][_0x4a1a07('0xb')]);const _0x4a890f={'ContactId':_0x3f88cd['id'],'MailAccountId':_0x5f447e[_0x4a1a07('0x22')]['id'],'inReplyTo':_0x5f447e[_0x4a1a07('0x4')][_0x4a1a07('0x48')][_0x4a1a07('0x1f')],'to':_0x5f447e['body']['message'][_0x4a1a07('0xb')],'cc':_0x5f447e['body'][_0x4a1a07('0x48')]['cc'],'subject':_0x5f447e[_0x4a1a07('0x4')][_0x4a1a07('0x48')]['subject'],'attach':_0x5f447e['body'][_0x4a1a07('0x48')][_0x4a1a07('0x1c')],'lastMsgAt':moment()[_0x4a1a07('0x40')](_0x4a1a07('0x30')),'lastMsgDirection':'in','lastMsgBody':_0x5f447e[_0x4a1a07('0x4')]['body']};return db[_0x4a1a07('0x24')][_0x4a1a07('0x27')]({'where':{'closed':![],'MailAccountId':_0x5f447e[_0x4a1a07('0x22')]['id']},'include':[{'model':db[_0x4a1a07('0x5')],'as':_0x4a1a07('0x2b'),'attributes':[_0x4a1a07('0x1f')],'where':{'messageId':_0x5f447e[_0x4a1a07('0x4')][_0x4a1a07('0x48')][_0x4a1a07('0x1d')]}}]})['then'](function(_0x2e390a){const _0x5d4ab0=_0x4a1a07;if(_0x2e390a)return[_0x2e390a,![]];return db['MailInteraction'][_0x5d4ab0('0x2d')](_0x4a890f)[_0x5d4ab0('0x20')](function(_0x6969ee){return[_0x6969ee,!![]];});});}else throw new Error('Contact\x20not\x20found');})['then'](function([_0x36e297,_0x895bd5]){const _0x5b1447=_0x39b867;if(_0x36e297)return _0x5955b8=_0x895bd5,!_0x895bd5?_0x36e297['update']({'inReplyTo':_0x5f447e['body'][_0x5b1447('0x48')]['messageId'],'to':_0x5f447e[_0x5b1447('0x4')]['message'][_0x5b1447('0xb')],'cc':_0x5f447e[_0x5b1447('0x4')]['message']['cc'],'subject':_0x5f447e[_0x5b1447('0x4')]['message'][_0x5b1447('0x7')],'attach':_0x36e297[_0x5b1447('0x1c')]?_0x36e297['attach']:_0x5f447e['body']['message'][_0x5b1447('0x1c')],'substatus':null,'substatusAt':moment()[_0x5b1447('0x40')](_0x5b1447('0x30')),'lastMsgAt':moment()[_0x5b1447('0x40')](_0x5b1447('0x30')),'lastMsgDirection':'in','lastMsgBody':_0x5f447e['body']['body']}):_0x36e297;})[_0x39b867('0x20')](function(_0x466c6b){const _0x40372e=_0x39b867;return _0x34e4cf[_0x40372e('0x19')]=_0x466c6b[_0x40372e('0x2f')]({'plain':!![]}),_0x34e4cf['interaction'][_0x40372e('0x4e')]=_0x5955b8,db[_0x40372e('0x5')]['create']({'body':_0x5f447e['body']['body'],'MailAccountId':_0x5f447e[_0x40372e('0x22')]['id'],'MailInteractionId':_0x466c6b['id'],'direction':'in','ContactId':_0x34e4cf['contact']['id'],'messageId':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')][_0x40372e('0x1f')],'from':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')][_0x40372e('0xb')],'to':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')]['to'],'cc':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')]['cc'],'subject':_0x5f447e['body'][_0x40372e('0x48')][_0x40372e('0x7')],'attach':_0x5f447e['body'][_0x40372e('0x48')][_0x40372e('0x1c')],'Attachments':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')][_0x40372e('0xa')],'originTo':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')][_0x40372e('0x56')],'originCc':_0x5f447e[_0x40372e('0x4')][_0x40372e('0x48')][_0x40372e('0x26')],'bcc':_0x5f447e['body']['message'][_0x40372e('0xc')]},{'include':[{'model':db['Attachment'],'as':_0x40372e('0x12')}]});})['then'](function(_0x55f805){const _0x22b5fe=_0x39b867;_0x34e4cf[_0x22b5fe('0x48')]=_0x55f805;if(_0x34e4cf[_0x22b5fe('0x19')][_0x22b5fe('0x31')])return db[_0x22b5fe('0x32')]['findOne']({'attributes':['id',_0x22b5fe('0x38')],'where':{'id':_0x34e4cf[_0x22b5fe('0x19')]['UserId']}})[_0x22b5fe('0x20')](function(_0xc69578){const _0x4ebdcd=_0x22b5fe;return _0xc69578&&_0x34e4cf['applications'][_0x4ebdcd('0x15')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0xc69578[_0x4ebdcd('0x38')]+','+(_0x34e4cf[_0x4ebdcd('0x22')]['waitForTheAssignedAgent']||0xa),'interval':_0x4ebdcd('0x35')}),respondWithRpcPromise('Start',_0x4ebdcd('0x8'),_0x34e4cf);});return respondWithRpcPromise('Start',_0x22b5fe('0x8'),_0x34e4cf);})[_0x39b867('0x20')](function(_0x25f564){_0x3e5c66(_0x25f564);})[_0x39b867('0x21')](function(_0x4af8b3){_0x31cd0b(_0x3dbf46['error'](0x1f4,_0x4af8b3['message']));});});};