Built motion from commit d5e4af8c.|2.6.23
[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 _0x029b=['lodash','moment','randomstring','ioredis','../../config/logger','rpc','client','http','redis','defaults','socket.io-emitter','./mailAccount.socket','request','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','error','code','message','catch','GetMailAccount','MailAccount','findAll','options','attributes','limit','include','map','model','then','stringify','find','where','setDataValue','smtpOptions','getSmtpOptions','ShowMailAccount','body','mail','difference','CmContact','rawAttributes','updatedAt','CompanyId','ListId','from','from\x20is\x20mandatory','isNil','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','account','List','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','CloudProvider','list','applications','orderBy','priority','autoclose','close','*,*,*,*','length','create','List\x20not\x20found','messageId','generate','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','Messages','inReplyTo','Contact\x20not\x20found','interaction','get','created','contact','attachments','originTo','originCc','bcc','ChatWebsiteId','originChannel','Email','ChatOfflineMessageId','Attachment','Attachments','User','name','unshift','agent','MailQueue','queue','Start','startRouting'];(function(_0x42c3d2,_0x156aa3){var _0x56b0f0=function(_0x163583){while(--_0x163583){_0x42c3d2['push'](_0x42c3d2['shift']());}};_0x56b0f0(++_0x156aa3);}(_0x029b,0x66));var _0xb029=function(_0x2e304c,_0x5aa490){_0x2e304c=_0x2e304c-0x0;var _0x1385b8=_0x029b[_0x2e304c];return _0x1385b8;};'use strict';var _=require(_0xb029('0x0'));var moment=require(_0xb029('0x1'));var BPromise=require('bluebird');var rs=require(_0xb029('0x2'));var Redis=require(_0xb029('0x3'));var db=require('../../mysqldb')['db'];var logger=require(_0xb029('0x4'))(_0xb029('0x5'));var config=require('../../config/environment');var jayson=require('jayson/promise');var client=jayson[_0xb029('0x6')][_0xb029('0x7')]({'port':0x232c});config[_0xb029('0x8')]=_[_0xb029('0x9')](config[_0xb029('0x8')],{'host':'localhost','port':0x18eb});var socket=require(_0xb029('0xa'))(new Redis(config['redis']));require(_0xb029('0xb'))['register'](socket);function respondWithRpcPromise(_0x2c3772,_0x5a8bb7,_0x23fee0){return new BPromise(function(_0x4b3036,_0x2696e3){return client[_0xb029('0xc')](_0x2c3772,_0x23fee0)['then'](function(_0x266df5){logger[_0xb029('0xd')](_0xb029('0xe'),_0x5a8bb7,_0xb029('0xf'));logger[_0xb029('0x10')]('MailAccount,\x20%s,\x20%s,\x20%s',_0x5a8bb7,_0xb029('0xf'),JSON['stringify'](_0x266df5));if(_0x266df5[_0xb029('0x11')]){if(_0x266df5[_0xb029('0x11')][_0xb029('0x12')]===0x1f4){logger[_0xb029('0x11')]('MailAccount,\x20%s,\x20%s',_0x5a8bb7,_0x266df5[_0xb029('0x11')][_0xb029('0x13')]);return _0x2696e3(_0x266df5[_0xb029('0x11')]['message']);}logger[_0xb029('0x11')](_0xb029('0xe'),_0x5a8bb7,_0x266df5[_0xb029('0x11')][_0xb029('0x13')]);return _0x4b3036(_0x266df5[_0xb029('0x11')][_0xb029('0x13')]);}else{logger[_0xb029('0xd')](_0xb029('0xe'),_0x5a8bb7,_0xb029('0xf'));_0x4b3036(_0x266df5['result']['message']);}})[_0xb029('0x14')](function(_0x5373cd){logger[_0xb029('0x11')](_0xb029('0xe'),_0x5a8bb7,_0x5373cd);_0x2696e3(_0x5373cd);});});}exports[_0xb029('0x15')]=function(_0x497ca0){var _0x5e1741=this;return new Promise(function(_0x13ff77,_0x8d3e35){return db[_0xb029('0x16')][_0xb029('0x17')]({'raw':_0x497ca0[_0xb029('0x18')]?_0x497ca0[_0xb029('0x18')]['raw']===undefined?!![]:![]:!![],'where':_0x497ca0['options']?_0x497ca0['options']['where']||null:null,'attributes':_0x497ca0[_0xb029('0x18')]?_0x497ca0[_0xb029('0x18')][_0xb029('0x19')]||null:null,'limit':_0x497ca0[_0xb029('0x18')]?_0x497ca0['options'][_0xb029('0x1a')]||null:null,'include':_0x497ca0[_0xb029('0x18')]?_0x497ca0[_0xb029('0x18')][_0xb029('0x1b')]?_[_0xb029('0x1c')](_0x497ca0[_0xb029('0x18')][_0xb029('0x1b')],function(_0x3eb5ac){return{'model':db[_0x3eb5ac[_0xb029('0x1d')]],'as':_0x3eb5ac['as'],'attributes':_0x3eb5ac[_0xb029('0x19')],'include':_0x3eb5ac[_0xb029('0x1b')]?_[_0xb029('0x1c')](_0x3eb5ac[_0xb029('0x1b')],function(_0x599a14){return{'model':db[_0x599a14[_0xb029('0x1d')]],'as':_0x599a14['as'],'attributes':_0x599a14[_0xb029('0x19')],'include':_0x599a14['include']?_['map'](_0x599a14[_0xb029('0x1b')],function(_0x3b030a){return{'model':db[_0x3b030a[_0xb029('0x1d')]],'as':_0x3b030a['as'],'attributes':_0x3b030a[_0xb029('0x19')]};}):[]};}):[]};}):[]:[]})[_0xb029('0x1e')](function(_0x4af2ad){logger[_0xb029('0xd')]('GetMailAccount',_0x497ca0);logger[_0xb029('0x10')](_0xb029('0x15'),_0x497ca0,JSON[_0xb029('0x1f')](_0x4af2ad));_0x13ff77(_0x4af2ad);})['catch'](function(_0x1aa94b){logger[_0xb029('0x11')]('GetMailAccount',_0x1aa94b[_0xb029('0x13')],_0x497ca0);_0x8d3e35(_0x5e1741['error'](0x1f4,_0x1aa94b[_0xb029('0x13')]));});});};exports['ShowMailAccount']=function(_0x88a69b){var _0x3b0bfe=this;return new Promise(function(_0x392d0f,_0x2324f3){return db[_0xb029('0x16')][_0xb029('0x20')]({'raw':_0x88a69b[_0xb029('0x18')]?_0x88a69b[_0xb029('0x18')]['raw']===undefined?!![]:![]:!![],'where':_0x88a69b[_0xb029('0x18')]?_0x88a69b[_0xb029('0x18')][_0xb029('0x21')]||null:null,'attributes':_0x88a69b['options']?_0x88a69b[_0xb029('0x18')][_0xb029('0x19')]||null:null,'include':_0x88a69b['options']?_0x88a69b[_0xb029('0x18')]['include']?_[_0xb029('0x1c')](_0x88a69b['options'][_0xb029('0x1b')],function(_0x42f980){return{'model':db[_0x42f980[_0xb029('0x1d')]],'as':_0x42f980['as'],'attributes':_0x42f980[_0xb029('0x19')],'include':_0x42f980[_0xb029('0x1b')]?_[_0xb029('0x1c')](_0x42f980[_0xb029('0x1b')],function(_0x5a1d11){return{'model':db[_0x5a1d11[_0xb029('0x1d')]],'as':_0x5a1d11['as'],'attributes':_0x5a1d11[_0xb029('0x19')],'include':_0x5a1d11['include']?_[_0xb029('0x1c')](_0x5a1d11[_0xb029('0x1b')],function(_0x35105c){return{'model':db[_0x35105c[_0xb029('0x1d')]],'as':_0x35105c['as'],'attributes':_0x35105c['attributes']};}):[]};}):[]};}):[]:[]})[_0xb029('0x1e')](function(_0x3a2547){if(_0x88a69b[_0xb029('0x18')]['smtp']===!![])_0x3a2547[_0xb029('0x22')](_0xb029('0x23'),_0x3a2547[_0xb029('0x24')]());logger[_0xb029('0xd')](_0xb029('0x25'),_0x88a69b);logger[_0xb029('0x10')](_0xb029('0x25'),_0x88a69b,JSON[_0xb029('0x1f')](_0x3a2547));_0x392d0f(_0x3a2547);})[_0xb029('0x14')](function(_0x1392af){logger['error'](_0xb029('0x25'),_0x1392af[_0xb029('0x13')],_0x88a69b);_0x2324f3(_0x3b0bfe[_0xb029('0x11')](0x1f4,_0x1392af['message']));});});};exports['NotifyMailAccount']=function(_0x521b20){var _0x31477a=this;return new Promise(function(_0x41a857,_0x5c7082){var _0x1a7285={'body':_0x521b20[_0xb029('0x26')],'channel':_0xb029('0x27')};var _0x3bc456=_[_0xb029('0x28')](_['keys'](db[_0xb029('0x29')][_0xb029('0x2a')]),['createdAt',_0xb029('0x2b'),_0xb029('0x2c'),_0xb029('0x2d')]);var _0x56a470=![];if(_0x521b20[_0xb029('0x26')]['id']){delete _0x521b20[_0xb029('0x26')]['id'];}if(_['isNil'](_0x521b20['body'][_0xb029('0x2e')])){return _0x5c7082(_0x31477a[_0xb029('0x11')](0x1f4,_0xb029('0x2f')));}if(_[_0xb029('0x30')](_0x521b20['body']['body'])||_0x521b20[_0xb029('0x26')]['body']===''){return _0x5c7082(_0x31477a[_0xb029('0x11')](0x1f4,'body\x20is\x20mandatory\x20and\x20not\x20empty'));}if(_[_0xb029('0x30')](_0x521b20[_0xb029('0x26')][_0xb029('0x31')])){return _0x5c7082(_0x31477a[_0xb029('0x11')](0x1f4,_0xb029('0x32')+_0x3bc456));}if(!_[_0xb029('0x33')](_0x3bc456,_0x521b20['body'][_0xb029('0x31')])){return _0x5c7082(_0x31477a['error'](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3bc456));}var _0x2dde2e={};_0x2dde2e[_0x521b20['body'][_0xb029('0x31')]]=_0x521b20['body'][_0xb029('0x2e')];return db[_0xb029('0x16')][_0xb029('0x20')]({'where':{'id':_0x521b20[_0xb029('0x34')]['id']},'include':[{'model':db['CmList'],'as':_0xb029('0x35'),'include':[{'model':db[_0xb029('0x29')],'as':_0xb029('0x36'),'where':_0x2dde2e,'limit':0x1,'order':[[_0xb029('0x2b'),_0xb029('0x37')]]}]},{'model':db[_0xb029('0x38')],'as':_0xb029('0x39')},{'model':db[_0xb029('0x3a')],'as':_0xb029('0x3b'),'include':[{'model':db[_0xb029('0x3c')],'as':'CloudProvider'}]}]})[_0xb029('0x1e')](function(_0x2345cd){if(_0x2345cd&&_0x2345cd[_0xb029('0x35')]){var _0x3172a2=_0x2345cd['get']({'plain':!![]});_0x1a7285[_0xb029('0x34')]=_['omit'](_0x3172a2,[_0xb029('0x39'),_0xb029('0x35')]);_0x1a7285[_0xb029('0x34')][_0xb029('0x23')]=_0x2345cd[_0xb029('0x24')]();_0x1a7285['applications']=_0x3172a2['Applications'];_0x1a7285[_0xb029('0x3d')]=_0x3172a2[_0xb029('0x35')];_0x1a7285[_0xb029('0x3e')]=_[_0xb029('0x3f')](_0x1a7285[_0xb029('0x3e')],[_0xb029('0x40')],['asc']);if(_0x1a7285[_0xb029('0x34')]&&_0x1a7285[_0xb029('0x34')][_0xb029('0x41')]){_0x1a7285['applications']['push']({'id':0x0,'priority':_0x1a7285[_0xb029('0x3e')]['length']+0x1,'app':_0xb029('0x42'),'appdata':_0xb029('0x41'),'interval':_0xb029('0x43')});}if(_0x1a7285['account'][_0xb029('0x39')]){delete _0x1a7285[_0xb029('0x34')]['Applications'];}if(_0x1a7285[_0xb029('0x34')][_0xb029('0x35')]){delete _0x1a7285[_0xb029('0x34')]['List'];}if(_0x3172a2[_0xb029('0x35')][_0xb029('0x36')][_0xb029('0x44')]){return _0x3172a2[_0xb029('0x35')]['Contacts'][0x0];}var _0x3cbbfe=_[_0xb029('0x9')](_0x521b20[_0xb029('0x26')],{'firstName':_0x521b20['body'][_0xb029('0x2e')],'ListId':_0x3172a2['ListId']});_0x3cbbfe[_0x521b20[_0xb029('0x26')][_0xb029('0x31')]]=_0x521b20[_0xb029('0x26')][_0xb029('0x2e')];return db[_0xb029('0x29')][_0xb029('0x45')](_0x3cbbfe);}else{throw new Error(_0xb029('0x46'));}})['then'](function(_0x4f41ad){_0x1a7285['contact']=_0x4f41ad;if(_0x4f41ad){if(!_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x47')]){_0x521b20[_0xb029('0x26')][_0xb029('0x13')]['messageId']=rs[_0xb029('0x48')]()+'\x20'+_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x2e')];}var _0x5b7c6d={'ContactId':_0x4f41ad['id'],'MailAccountId':_0x521b20[_0xb029('0x34')]['id'],'inReplyTo':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x47')],'to':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x2e')],'cc':_0x521b20[_0xb029('0x26')]['message']['cc'],'subject':_0x521b20[_0xb029('0x26')][_0xb029('0x13')]['subject'],'attach':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x49')],'lastMsgAt':moment()[_0xb029('0x4a')](_0xb029('0x4b')),'lastMsgDirection':'in'};return db[_0xb029('0x4c')][_0xb029('0x20')]({'where':{'closed':![],'MailAccountId':_0x521b20[_0xb029('0x34')]['id']},'include':[{'model':db['MailMessage'],'as':_0xb029('0x4d'),'attributes':['messageId'],'where':{'messageId':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x4e')]}}]})['then'](function(_0xd82930){if(_0xd82930){return[_0xd82930,![]];}return db[_0xb029('0x4c')]['create'](_0x5b7c6d)['then'](function(_0x5792a1){return[_0x5792a1,!![]];});});}else{throw new Error(_0xb029('0x4f'));}})['spread'](function(_0x3d8ff6,_0x5419d5){if(_0x3d8ff6){_0x56a470=_0x5419d5;if(!_0x5419d5){return _0x3d8ff6['update']({'inReplyTo':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x47')],'to':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x2e')],'cc':_0x521b20[_0xb029('0x26')]['message']['cc'],'subject':_0x521b20[_0xb029('0x26')][_0xb029('0x13')]['subject'],'attach':_0x3d8ff6[_0xb029('0x49')]?_0x3d8ff6[_0xb029('0x49')]:_0x521b20[_0xb029('0x26')]['message'][_0xb029('0x49')],'substatus':null,'substatusAt':moment()[_0xb029('0x4a')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgAt':moment()[_0xb029('0x4a')](_0xb029('0x4b')),'lastMsgDirection':'in'});}else{return _0x3d8ff6;}}})['then'](function(_0x227bfd){_0x1a7285[_0xb029('0x50')]=_0x227bfd[_0xb029('0x51')]({'plain':!![]});_0x1a7285[_0xb029('0x50')][_0xb029('0x52')]=_0x56a470;return db['MailMessage'][_0xb029('0x45')]({'body':_0x521b20['body']['body'],'MailAccountId':_0x521b20[_0xb029('0x34')]['id'],'MailInteractionId':_0x227bfd['id'],'direction':'in','ContactId':_0x1a7285[_0xb029('0x53')]['id'],'messageId':_0x521b20[_0xb029('0x26')]['message'][_0xb029('0x47')],'from':_0x521b20[_0xb029('0x26')]['message']['from'],'to':_0x521b20[_0xb029('0x26')]['message']['to'],'cc':_0x521b20[_0xb029('0x26')]['message']['cc'],'subject':_0x521b20['body'][_0xb029('0x13')]['subject'],'attach':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x49')],'Attachments':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x54')],'originTo':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x55')],'originCc':_0x521b20['body'][_0xb029('0x13')][_0xb029('0x56')],'bcc':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x57')],'ChatWebsiteId':_0x521b20[_0xb029('0x26')][_0xb029('0x13')][_0xb029('0x58')]||null,'originChannel':_0x521b20[_0xb029('0x26')]['message'][_0xb029('0x59')]||_0xb029('0x5a'),'ChatOfflineMessageId':_0x521b20[_0xb029('0x26')]['message'][_0xb029('0x5b')]||null},{'include':[{'model':db[_0xb029('0x5c')],'as':_0xb029('0x5d')}]});})[_0xb029('0x1e')](function(_0x133e8c){_0x1a7285[_0xb029('0x13')]=_0x133e8c;if(!_0x1a7285[_0xb029('0x50')]['UserId']){return;}return db[_0xb029('0x5e')]['find']({'attributes':['id',_0xb029('0x5f')],'where':{'id':_0x1a7285[_0xb029('0x50')]['UserId']}});})[_0xb029('0x1e')](function(_0x493c86){if(_0x493c86){_0x1a7285['applications'][_0xb029('0x60')]({'id':0x0,'priority':0x0,'app':_0xb029('0x61'),'appdata':_0x493c86[_0xb029('0x5f')]+','+(_0x1a7285[_0xb029('0x34')]['waitForTheAssignedAgent']||0xa),'interval':'*,*,*,*'});}if(!_0x1a7285[_0xb029('0x50')]['queueId']){return;}return db[_0xb029('0x62')][_0xb029('0x20')]({'attributes':['id',_0xb029('0x5f')],'where':{'id':_0x1a7285[_0xb029('0x50')]['queueId']}});})[_0xb029('0x1e')](function(_0x5849c9){if(_0x5849c9){_0x1a7285['applications'][_0xb029('0x60')]({'id':0x0,'priority':0x1,'app':_0xb029('0x63'),'appdata':_0x5849c9[_0xb029('0x5f')]+','+(_0x1a7285[_0xb029('0x34')]['waitForTheAssignedQueue']||0x12c),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0xb029('0x64'),_0xb029('0x65'),_0x1a7285);})[_0xb029('0x1e')](function(_0xc0111b){_0x41a857(_0xc0111b);})[_0xb029('0x14')](function(_0x4969af){_0x5c7082(_0x31477a[_0xb029('0x11')](0x1f4,_0x4969af[_0xb029('0x13')]));});});};