Built motion from commit c116dc8b.|2.6.30
[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 _0x4351=['ChatWebsiteId','originChannel','Attachment','User','name','UserId','unshift','agent','queueId','queue','waitForTheAssignedQueue','Start','startRouting','lodash','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis','defaults','socket.io-emitter','register','request','then','MailAccount,\x20%s,\x20%s','debug','request\x20sent','code','error','message','info','result','catch','findAll','options','raw','attributes','limit','include','map','model','GetMailAccount','stringify','find','smtp','setDataValue','smtpOptions','getSmtpOptions','ShowMailAccount','mail','difference','CmContact','rawAttributes','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','account','List','Contacts','MailApplication','MailServerOut','CloudProvider','get','omit','Applications','applications','list','orderBy','priority','asc','push','length','close','autoclose','*,*,*,*','create','contact','messageId','generate','subject','attach','MailInteraction','MailMessage','inReplyTo','update','format','YYYY-MM-DD\x20HH:mm:ss','interaction','attachments','originTo','originCc','bcc'];(function(_0x198c12,_0xc60219){var _0x2ddc79=function(_0xa058b0){while(--_0xa058b0){_0x198c12['push'](_0x198c12['shift']());}};_0x2ddc79(++_0xc60219);}(_0x4351,0x1b5));var _0x1435=function(_0x334841,_0x2bbc4c){_0x334841=_0x334841-0x0;var _0x5beb4b=_0x4351[_0x334841];return _0x5beb4b;};'use strict';var _=require(_0x1435('0x0'));var moment=require(_0x1435('0x1'));var BPromise=require(_0x1435('0x2'));var rs=require(_0x1435('0x3'));var Redis=require(_0x1435('0x4'));var db=require(_0x1435('0x5'))['db'];var logger=require(_0x1435('0x6'))(_0x1435('0x7'));var config=require(_0x1435('0x8'));var jayson=require(_0x1435('0x9'));var client=jayson[_0x1435('0xa')][_0x1435('0xb')]({'port':0x232c});config[_0x1435('0xc')]=_[_0x1435('0xd')](config[_0x1435('0xc')],{'host':'localhost','port':0x18eb});var socket=require(_0x1435('0xe'))(new Redis(config['redis']));require('./mailAccount.socket')[_0x1435('0xf')](socket);function respondWithRpcPromise(_0x22a221,_0x14a9e1,_0x35a509){return new BPromise(function(_0xade491,_0x55cd88){return client[_0x1435('0x10')](_0x22a221,_0x35a509)[_0x1435('0x11')](function(_0x5a64a4){logger['info'](_0x1435('0x12'),_0x14a9e1,'request\x20sent');logger[_0x1435('0x13')]('MailAccount,\x20%s,\x20%s,\x20%s',_0x14a9e1,_0x1435('0x14'),JSON['stringify'](_0x5a64a4));if(_0x5a64a4['error']){if(_0x5a64a4['error'][_0x1435('0x15')]===0x1f4){logger[_0x1435('0x16')](_0x1435('0x12'),_0x14a9e1,_0x5a64a4['error']['message']);return _0x55cd88(_0x5a64a4[_0x1435('0x16')]['message']);}logger[_0x1435('0x16')](_0x1435('0x12'),_0x14a9e1,_0x5a64a4[_0x1435('0x16')][_0x1435('0x17')]);return _0xade491(_0x5a64a4[_0x1435('0x16')][_0x1435('0x17')]);}else{logger[_0x1435('0x18')](_0x1435('0x12'),_0x14a9e1,_0x1435('0x14'));_0xade491(_0x5a64a4[_0x1435('0x19')][_0x1435('0x17')]);}})[_0x1435('0x1a')](function(_0x68f503){logger[_0x1435('0x16')](_0x1435('0x12'),_0x14a9e1,_0x68f503);_0x55cd88(_0x68f503);});});}exports['GetMailAccount']=function(_0x26da28){var _0x34f555=this;return new Promise(function(_0x4cf63f,_0x4694cb){return db['MailAccount'][_0x1435('0x1b')]({'raw':_0x26da28[_0x1435('0x1c')]?_0x26da28[_0x1435('0x1c')][_0x1435('0x1d')]===undefined?!![]:![]:!![],'where':_0x26da28[_0x1435('0x1c')]?_0x26da28[_0x1435('0x1c')]['where']||null:null,'attributes':_0x26da28[_0x1435('0x1c')]?_0x26da28[_0x1435('0x1c')][_0x1435('0x1e')]||null:null,'limit':_0x26da28[_0x1435('0x1c')]?_0x26da28[_0x1435('0x1c')][_0x1435('0x1f')]||null:null,'include':_0x26da28[_0x1435('0x1c')]?_0x26da28[_0x1435('0x1c')][_0x1435('0x20')]?_[_0x1435('0x21')](_0x26da28[_0x1435('0x1c')]['include'],function(_0x523792){return{'model':db[_0x523792[_0x1435('0x22')]],'as':_0x523792['as'],'attributes':_0x523792[_0x1435('0x1e')],'include':_0x523792[_0x1435('0x20')]?_['map'](_0x523792[_0x1435('0x20')],function(_0x25cf29){return{'model':db[_0x25cf29[_0x1435('0x22')]],'as':_0x25cf29['as'],'attributes':_0x25cf29[_0x1435('0x1e')],'include':_0x25cf29[_0x1435('0x20')]?_['map'](_0x25cf29[_0x1435('0x20')],function(_0x7a2f63){return{'model':db[_0x7a2f63[_0x1435('0x22')]],'as':_0x7a2f63['as'],'attributes':_0x7a2f63[_0x1435('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x1435('0x11')](function(_0x108c25){logger[_0x1435('0x18')]('GetMailAccount',_0x26da28);logger[_0x1435('0x13')](_0x1435('0x23'),_0x26da28,JSON[_0x1435('0x24')](_0x108c25));_0x4cf63f(_0x108c25);})[_0x1435('0x1a')](function(_0x297edf){logger['error'](_0x1435('0x23'),_0x297edf[_0x1435('0x17')],_0x26da28);_0x4694cb(_0x34f555[_0x1435('0x16')](0x1f4,_0x297edf[_0x1435('0x17')]));});});};exports['ShowMailAccount']=function(_0x2afc79){var _0x1dc553=this;return new Promise(function(_0x49209a,_0x4503a1){return db['MailAccount'][_0x1435('0x25')]({'raw':_0x2afc79[_0x1435('0x1c')]?_0x2afc79['options'][_0x1435('0x1d')]===undefined?!![]:![]:!![],'where':_0x2afc79[_0x1435('0x1c')]?_0x2afc79['options']['where']||null:null,'attributes':_0x2afc79['options']?_0x2afc79[_0x1435('0x1c')]['attributes']||null:null,'include':_0x2afc79[_0x1435('0x1c')]?_0x2afc79['options'][_0x1435('0x20')]?_['map'](_0x2afc79[_0x1435('0x1c')][_0x1435('0x20')],function(_0x249edc){return{'model':db[_0x249edc[_0x1435('0x22')]],'as':_0x249edc['as'],'attributes':_0x249edc[_0x1435('0x1e')],'include':_0x249edc[_0x1435('0x20')]?_['map'](_0x249edc[_0x1435('0x20')],function(_0x138597){return{'model':db[_0x138597[_0x1435('0x22')]],'as':_0x138597['as'],'attributes':_0x138597[_0x1435('0x1e')],'include':_0x138597[_0x1435('0x20')]?_['map'](_0x138597[_0x1435('0x20')],function(_0x578845){return{'model':db[_0x578845[_0x1435('0x22')]],'as':_0x578845['as'],'attributes':_0x578845[_0x1435('0x1e')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x11cf64){if(_0x2afc79[_0x1435('0x1c')][_0x1435('0x26')]===!![])_0x11cf64[_0x1435('0x27')](_0x1435('0x28'),_0x11cf64[_0x1435('0x29')]());logger[_0x1435('0x18')](_0x1435('0x2a'),_0x2afc79);logger[_0x1435('0x13')](_0x1435('0x2a'),_0x2afc79,JSON[_0x1435('0x24')](_0x11cf64));_0x49209a(_0x11cf64);})[_0x1435('0x1a')](function(_0x552a61){logger[_0x1435('0x16')]('ShowMailAccount',_0x552a61[_0x1435('0x17')],_0x2afc79);_0x4503a1(_0x1dc553['error'](0x1f4,_0x552a61[_0x1435('0x17')]));});});};exports['NotifyMailAccount']=function(_0x473d6){var _0x516e44=this;return new Promise(function(_0x36bc74,_0x186e7a){var _0x398d3c={'body':_0x473d6['body'],'channel':_0x1435('0x2b')};var _0x1fe164=_[_0x1435('0x2c')](_['keys'](db[_0x1435('0x2d')][_0x1435('0x2e')]),['createdAt','updatedAt',_0x1435('0x2f'),_0x1435('0x30')]);var _0x2dd1f5=![];if(_0x473d6['body']['id']){delete _0x473d6['body']['id'];}if(_[_0x1435('0x31')](_0x473d6['body'][_0x1435('0x32')])){return _0x186e7a(_0x516e44['error'](0x1f4,_0x1435('0x33')));}if(_['isNil'](_0x473d6[_0x1435('0x34')]['body'])||_0x473d6[_0x1435('0x34')]['body']===''){return _0x186e7a(_0x516e44[_0x1435('0x16')](0x1f4,_0x1435('0x35')));}if(_['isNil'](_0x473d6[_0x1435('0x34')]['mapKey'])){return _0x186e7a(_0x516e44[_0x1435('0x16')](0x1f4,_0x1435('0x36')+_0x1fe164));}if(!_[_0x1435('0x37')](_0x1fe164,_0x473d6['body'][_0x1435('0x38')])){return _0x186e7a(_0x516e44[_0x1435('0x16')](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x1fe164));}var _0x1f73f4={};_0x1f73f4[_0x473d6[_0x1435('0x34')]['mapKey']]=_0x473d6[_0x1435('0x34')][_0x1435('0x32')];return db['MailAccount'][_0x1435('0x25')]({'where':{'id':_0x473d6[_0x1435('0x39')]['id']},'include':[{'model':db['CmList'],'as':_0x1435('0x3a'),'include':[{'model':db[_0x1435('0x2d')],'as':_0x1435('0x3b'),'where':_0x1f73f4,'limit':0x1,'order':[['updatedAt','DESC']]}]},{'model':db[_0x1435('0x3c')],'as':'Applications'},{'model':db[_0x1435('0x3d')],'as':'Smtp','include':[{'model':db[_0x1435('0x3e')],'as':_0x1435('0x3e')}]}]})[_0x1435('0x11')](function(_0x148862){if(_0x148862&&_0x148862[_0x1435('0x3a')]){var _0x3d2346=_0x148862[_0x1435('0x3f')]({'plain':!![]});_0x398d3c['account']=_[_0x1435('0x40')](_0x3d2346,[_0x1435('0x41'),_0x1435('0x3a')]);_0x398d3c['account'][_0x1435('0x28')]=_0x148862[_0x1435('0x29')]();_0x398d3c[_0x1435('0x42')]=_0x3d2346[_0x1435('0x41')];_0x398d3c[_0x1435('0x43')]=_0x3d2346[_0x1435('0x3a')];_0x398d3c[_0x1435('0x42')]=_[_0x1435('0x44')](_0x398d3c['applications'],[_0x1435('0x45')],[_0x1435('0x46')]);if(_0x398d3c[_0x1435('0x39')]&&_0x398d3c[_0x1435('0x39')]['autoclose']){_0x398d3c['applications'][_0x1435('0x47')]({'id':0x0,'priority':_0x398d3c[_0x1435('0x42')][_0x1435('0x48')]+0x1,'app':_0x1435('0x49'),'appdata':_0x1435('0x4a'),'interval':_0x1435('0x4b')});}if(_0x398d3c[_0x1435('0x39')][_0x1435('0x41')]){delete _0x398d3c[_0x1435('0x39')]['Applications'];}if(_0x398d3c['account'][_0x1435('0x3a')]){delete _0x398d3c['account'][_0x1435('0x3a')];}if(_0x3d2346[_0x1435('0x3a')][_0x1435('0x3b')][_0x1435('0x48')]){return _0x3d2346[_0x1435('0x3a')][_0x1435('0x3b')][0x0];}var _0xd776e6=_['defaults'](_0x473d6[_0x1435('0x34')],{'firstName':_0x473d6['body'][_0x1435('0x32')],'ListId':_0x3d2346[_0x1435('0x30')]});_0xd776e6[_0x473d6['body'][_0x1435('0x38')]]=_0x473d6[_0x1435('0x34')][_0x1435('0x32')];return db[_0x1435('0x2d')][_0x1435('0x4c')](_0xd776e6);}else{throw new Error('List\x20not\x20found');}})[_0x1435('0x11')](function(_0x3da03e){_0x398d3c[_0x1435('0x4d')]=_0x3da03e;if(_0x3da03e){if(!_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x4e')]){_0x473d6['body']['message'][_0x1435('0x4e')]=rs[_0x1435('0x4f')]()+'\x20'+_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x32')];}var _0x12c603={'ContactId':_0x3da03e['id'],'MailAccountId':_0x473d6[_0x1435('0x39')]['id'],'inReplyTo':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x4e')],'to':_0x473d6['body'][_0x1435('0x17')][_0x1435('0x32')],'cc':_0x473d6['body'][_0x1435('0x17')]['cc'],'subject':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x50')],'attach':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x51')],'lastMsgAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in'};return db[_0x1435('0x52')][_0x1435('0x25')]({'where':{'closed':![],'MailAccountId':_0x473d6[_0x1435('0x39')]['id']},'include':[{'model':db[_0x1435('0x53')],'as':'Messages','attributes':[_0x1435('0x4e')],'where':{'messageId':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x54')]}}]})[_0x1435('0x11')](function(_0x5d4807){if(_0x5d4807){return[_0x5d4807,![]];}return db[_0x1435('0x52')][_0x1435('0x4c')](_0x12c603)[_0x1435('0x11')](function(_0x44b211){return[_0x44b211,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})['spread'](function(_0x3e40d,_0x21bc66){if(_0x3e40d){_0x2dd1f5=_0x21bc66;if(!_0x21bc66){return _0x3e40d[_0x1435('0x55')]({'inReplyTo':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x4e')],'to':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x32')],'cc':_0x473d6['body'][_0x1435('0x17')]['cc'],'subject':_0x473d6[_0x1435('0x34')][_0x1435('0x17')]['subject'],'attach':_0x3e40d[_0x1435('0x51')]?_0x3e40d[_0x1435('0x51')]:_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x51')],'substatus':null,'substatusAt':moment()[_0x1435('0x56')](_0x1435('0x57')),'lastMsgAt':moment()[_0x1435('0x56')](_0x1435('0x57')),'lastMsgDirection':'in'});}else{return _0x3e40d;}}})['then'](function(_0x1347a6){_0x398d3c[_0x1435('0x58')]=_0x1347a6[_0x1435('0x3f')]({'plain':!![]});_0x398d3c[_0x1435('0x58')]['created']=_0x2dd1f5;return db['MailMessage'][_0x1435('0x4c')]({'body':_0x473d6['body']['body'],'MailAccountId':_0x473d6['account']['id'],'MailInteractionId':_0x1347a6['id'],'direction':'in','ContactId':_0x398d3c[_0x1435('0x4d')]['id'],'messageId':_0x473d6['body']['message'][_0x1435('0x4e')],'from':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x32')],'to':_0x473d6[_0x1435('0x34')]['message']['to'],'cc':_0x473d6[_0x1435('0x34')][_0x1435('0x17')]['cc'],'subject':_0x473d6[_0x1435('0x34')][_0x1435('0x17')]['subject'],'attach':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x51')],'Attachments':_0x473d6[_0x1435('0x34')]['message'][_0x1435('0x59')],'originTo':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x5a')],'originCc':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x5b')],'bcc':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x5c')],'ChatWebsiteId':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x5d')]||null,'originChannel':_0x473d6[_0x1435('0x34')][_0x1435('0x17')][_0x1435('0x5e')]||'Email','ChatOfflineMessageId':_0x473d6[_0x1435('0x34')][_0x1435('0x17')]['ChatOfflineMessageId']||null},{'include':[{'model':db[_0x1435('0x5f')],'as':'Attachments'}]});})['then'](function(_0x1d0b14){_0x398d3c[_0x1435('0x17')]=_0x1d0b14;if(!_0x398d3c['interaction']['UserId']){return;}return db[_0x1435('0x60')]['find']({'attributes':['id',_0x1435('0x61')],'where':{'id':_0x398d3c[_0x1435('0x58')][_0x1435('0x62')]}});})['then'](function(_0x332d5a){if(_0x332d5a){_0x398d3c[_0x1435('0x42')][_0x1435('0x63')]({'id':0x0,'priority':0x0,'app':_0x1435('0x64'),'appdata':_0x332d5a[_0x1435('0x61')]+','+(_0x398d3c[_0x1435('0x39')]['waitForTheAssignedAgent']||0xa),'interval':_0x1435('0x4b')});}if(!_0x398d3c[_0x1435('0x58')][_0x1435('0x65')]){return;}return db['MailQueue']['find']({'attributes':['id','name'],'where':{'id':_0x398d3c[_0x1435('0x58')][_0x1435('0x65')]}});})['then'](function(_0x15fd08){if(_0x15fd08){_0x398d3c[_0x1435('0x42')][_0x1435('0x63')]({'id':0x0,'priority':0x1,'app':_0x1435('0x66'),'appdata':_0x15fd08[_0x1435('0x61')]+','+(_0x398d3c[_0x1435('0x39')][_0x1435('0x67')]||0x12c),'interval':_0x1435('0x4b')});}return respondWithRpcPromise(_0x1435('0x68'),_0x1435('0x69'),_0x398d3c);})['then'](function(_0x2a3f87){_0x36bc74(_0x2a3f87);})[_0x1435('0x1a')](function(_0x1f1acc){_0x186e7a(_0x516e44['error'](0x1f4,_0x1f1acc[_0x1435('0x17')]));});});};