f9c6985834ab2b8940fd3b8e2730ecb3922cd134
[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 _0x24ca=['localhost','socket.io-emitter','./mailAccount.socket','register','request','then','info','MailAccount,\x20%s,\x20%s','request\x20sent','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','options','raw','attributes','limit','include','map','model','GetMailAccount','debug','ShowMailAccount','MailAccount','smtp','setDataValue','smtpOptions','getSmtpOptions','NotifyMailAccount','body','difference','keys','CmContact','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','find','account','CmList','List','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','CloudProvider','get','omit','list','applications','orderBy','priority','asc','autoclose','push','length','close','create','List\x20not\x20found','contact','messageId','generate','attach','format','Messages','inReplyTo','MailInteraction','Contact\x20not\x20found','spread','update','subject','YYYY-MM-DD\x20HH:mm:ss','interaction','created','attachments','originCc','bcc','ChatWebsiteId','Email','ChatOfflineMessageId','Attachment','Attachments','UserId','name','unshift','agent','*,*,*,*','MailQueue','queue','waitForTheAssignedQueue','lodash','bluebird','randomstring','ioredis','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis','defaults'];(function(_0x1b4b23,_0x5dd6d6){var _0x2a325b=function(_0x175462){while(--_0x175462){_0x1b4b23['push'](_0x1b4b23['shift']());}};_0x2a325b(++_0x5dd6d6);}(_0x24ca,0x1bc));var _0xa24c=function(_0x254d9f,_0x54ddb5){_0x254d9f=_0x254d9f-0x0;var _0x1eedb9=_0x24ca[_0x254d9f];return _0x1eedb9;};'use strict';var _=require(_0xa24c('0x0'));var moment=require('moment');var BPromise=require(_0xa24c('0x1'));var rs=require(_0xa24c('0x2'));var Redis=require(_0xa24c('0x3'));var db=require('../../mysqldb')['db'];var logger=require(_0xa24c('0x4'))(_0xa24c('0x5'));var config=require(_0xa24c('0x6'));var jayson=require(_0xa24c('0x7'));var client=jayson[_0xa24c('0x8')][_0xa24c('0x9')]({'port':0x232c});config[_0xa24c('0xa')]=_[_0xa24c('0xb')](config[_0xa24c('0xa')],{'host':_0xa24c('0xc'),'port':0x18eb});var socket=require(_0xa24c('0xd'))(new Redis(config['redis']));require(_0xa24c('0xe'))[_0xa24c('0xf')](socket);function respondWithRpcPromise(_0x2d645c,_0x5eac78,_0x399f32){return new BPromise(function(_0x325c6d,_0x288f76){return client[_0xa24c('0x10')](_0x2d645c,_0x399f32)[_0xa24c('0x11')](function(_0x43aa5a){logger[_0xa24c('0x12')](_0xa24c('0x13'),_0x5eac78,_0xa24c('0x14'));logger['debug'](_0xa24c('0x15'),_0x5eac78,_0xa24c('0x14'),JSON[_0xa24c('0x16')](_0x43aa5a));if(_0x43aa5a[_0xa24c('0x17')]){if(_0x43aa5a['error'][_0xa24c('0x18')]===0x1f4){logger[_0xa24c('0x17')](_0xa24c('0x13'),_0x5eac78,_0x43aa5a[_0xa24c('0x17')][_0xa24c('0x19')]);return _0x288f76(_0x43aa5a[_0xa24c('0x17')][_0xa24c('0x19')]);}logger['error']('MailAccount,\x20%s,\x20%s',_0x5eac78,_0x43aa5a[_0xa24c('0x17')][_0xa24c('0x19')]);return _0x325c6d(_0x43aa5a['error']['message']);}else{logger[_0xa24c('0x12')](_0xa24c('0x13'),_0x5eac78,'request\x20sent');_0x325c6d(_0x43aa5a[_0xa24c('0x1a')]['message']);}})[_0xa24c('0x1b')](function(_0x2a610a){logger[_0xa24c('0x17')](_0xa24c('0x13'),_0x5eac78,_0x2a610a);_0x288f76(_0x2a610a);});});}exports['GetMailAccount']=function(_0x16d363){var _0x18b7e0=this;return new Promise(function(_0x773f70,_0x4d4bab){return db['MailAccount']['findAll']({'raw':_0x16d363['options']?_0x16d363[_0xa24c('0x1c')][_0xa24c('0x1d')]===undefined?!![]:![]:!![],'where':_0x16d363[_0xa24c('0x1c')]?_0x16d363[_0xa24c('0x1c')]['where']||null:null,'attributes':_0x16d363[_0xa24c('0x1c')]?_0x16d363['options'][_0xa24c('0x1e')]||null:null,'limit':_0x16d363[_0xa24c('0x1c')]?_0x16d363['options'][_0xa24c('0x1f')]||null:null,'include':_0x16d363[_0xa24c('0x1c')]?_0x16d363[_0xa24c('0x1c')][_0xa24c('0x20')]?_[_0xa24c('0x21')](_0x16d363['options'][_0xa24c('0x20')],function(_0x5cc9cb){return{'model':db[_0x5cc9cb[_0xa24c('0x22')]],'as':_0x5cc9cb['as'],'attributes':_0x5cc9cb[_0xa24c('0x1e')],'include':_0x5cc9cb[_0xa24c('0x20')]?_['map'](_0x5cc9cb[_0xa24c('0x20')],function(_0x31c4d9){return{'model':db[_0x31c4d9[_0xa24c('0x22')]],'as':_0x31c4d9['as'],'attributes':_0x31c4d9[_0xa24c('0x1e')],'include':_0x31c4d9[_0xa24c('0x20')]?_[_0xa24c('0x21')](_0x31c4d9[_0xa24c('0x20')],function(_0x5b23f4){return{'model':db[_0x5b23f4[_0xa24c('0x22')]],'as':_0x5b23f4['as'],'attributes':_0x5b23f4[_0xa24c('0x1e')]};}):[]};}):[]};}):[]:[]})[_0xa24c('0x11')](function(_0x12dbdc){logger['info'](_0xa24c('0x23'),_0x16d363);logger[_0xa24c('0x24')](_0xa24c('0x23'),_0x16d363,JSON['stringify'](_0x12dbdc));_0x773f70(_0x12dbdc);})[_0xa24c('0x1b')](function(_0x3dcb1b){logger[_0xa24c('0x17')]('GetMailAccount',_0x3dcb1b[_0xa24c('0x19')],_0x16d363);_0x4d4bab(_0x18b7e0['error'](0x1f4,_0x3dcb1b[_0xa24c('0x19')]));});});};exports[_0xa24c('0x25')]=function(_0x1a247b){var _0x5d025a=this;return new Promise(function(_0x1f52f8,_0x53f90){return db[_0xa24c('0x26')]['find']({'raw':_0x1a247b[_0xa24c('0x1c')]?_0x1a247b['options'][_0xa24c('0x1d')]===undefined?!![]:![]:!![],'where':_0x1a247b[_0xa24c('0x1c')]?_0x1a247b['options']['where']||null:null,'attributes':_0x1a247b[_0xa24c('0x1c')]?_0x1a247b[_0xa24c('0x1c')][_0xa24c('0x1e')]||null:null,'include':_0x1a247b[_0xa24c('0x1c')]?_0x1a247b[_0xa24c('0x1c')][_0xa24c('0x20')]?_[_0xa24c('0x21')](_0x1a247b['options'][_0xa24c('0x20')],function(_0x3ea442){return{'model':db[_0x3ea442[_0xa24c('0x22')]],'as':_0x3ea442['as'],'attributes':_0x3ea442[_0xa24c('0x1e')],'include':_0x3ea442[_0xa24c('0x20')]?_[_0xa24c('0x21')](_0x3ea442[_0xa24c('0x20')],function(_0x81d471){return{'model':db[_0x81d471[_0xa24c('0x22')]],'as':_0x81d471['as'],'attributes':_0x81d471[_0xa24c('0x1e')],'include':_0x81d471['include']?_[_0xa24c('0x21')](_0x81d471[_0xa24c('0x20')],function(_0x2b362d){return{'model':db[_0x2b362d[_0xa24c('0x22')]],'as':_0x2b362d['as'],'attributes':_0x2b362d['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x19a65f){if(_0x1a247b['options'][_0xa24c('0x27')]===!![])_0x19a65f[_0xa24c('0x28')](_0xa24c('0x29'),_0x19a65f[_0xa24c('0x2a')]());logger[_0xa24c('0x12')]('ShowMailAccount',_0x1a247b);logger['debug'](_0xa24c('0x25'),_0x1a247b,JSON[_0xa24c('0x16')](_0x19a65f));_0x1f52f8(_0x19a65f);})[_0xa24c('0x1b')](function(_0x213dc3){logger[_0xa24c('0x17')](_0xa24c('0x25'),_0x213dc3['message'],_0x1a247b);_0x53f90(_0x5d025a['error'](0x1f4,_0x213dc3[_0xa24c('0x19')]));});});};exports[_0xa24c('0x2b')]=function(_0x4037d6){var _0x2101f8=this;return new Promise(function(_0x43beff,_0x4c5b7e){var _0x171c51={'body':_0x4037d6[_0xa24c('0x2c')],'channel':'mail'};var _0x599627=_[_0xa24c('0x2d')](_[_0xa24c('0x2e')](db[_0xa24c('0x2f')]['rawAttributes']),['createdAt',_0xa24c('0x30'),_0xa24c('0x31'),_0xa24c('0x32')]);var _0x3ccc37=![];if(_0x4037d6[_0xa24c('0x2c')]['id']){delete _0x4037d6[_0xa24c('0x2c')]['id'];}if(_[_0xa24c('0x33')](_0x4037d6[_0xa24c('0x2c')]['from'])){return _0x4c5b7e(_0x2101f8['error'](0x1f4,_0xa24c('0x34')));}if(_[_0xa24c('0x33')](_0x4037d6[_0xa24c('0x2c')]['body'])||_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x2c')]===''){return _0x4c5b7e(_0x2101f8[_0xa24c('0x17')](0x1f4,_0xa24c('0x35')));}if(_[_0xa24c('0x33')](_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x36')])){return _0x4c5b7e(_0x2101f8[_0xa24c('0x17')](0x1f4,_0xa24c('0x37')+_0x599627));}if(!_[_0xa24c('0x38')](_0x599627,_0x4037d6['body'][_0xa24c('0x36')])){return _0x4c5b7e(_0x2101f8[_0xa24c('0x17')](0x1f4,_0xa24c('0x39')+_0x599627));}var _0x5aa2b2={};_0x5aa2b2[_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x36')]]=_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x3a')];return db[_0xa24c('0x26')][_0xa24c('0x3b')]({'where':{'id':_0x4037d6[_0xa24c('0x3c')]['id']},'include':[{'model':db[_0xa24c('0x3d')],'as':_0xa24c('0x3e'),'include':[{'model':db[_0xa24c('0x2f')],'as':_0xa24c('0x3f'),'where':_0x5aa2b2,'limit':0x1,'order':[['updatedAt',_0xa24c('0x40')]]}]},{'model':db[_0xa24c('0x41')],'as':_0xa24c('0x42')},{'model':db[_0xa24c('0x43')],'as':_0xa24c('0x44'),'include':[{'model':db[_0xa24c('0x45')],'as':_0xa24c('0x45')}]}]})[_0xa24c('0x11')](function(_0x184179){if(_0x184179&&_0x184179['List']){var _0x540da8=_0x184179[_0xa24c('0x46')]({'plain':!![]});_0x171c51['account']=_[_0xa24c('0x47')](_0x540da8,[_0xa24c('0x42'),'List']);_0x171c51[_0xa24c('0x3c')][_0xa24c('0x29')]=_0x184179[_0xa24c('0x2a')]();_0x171c51['applications']=_0x540da8['Applications'];_0x171c51[_0xa24c('0x48')]=_0x540da8['List'];_0x171c51[_0xa24c('0x49')]=_[_0xa24c('0x4a')](_0x171c51[_0xa24c('0x49')],[_0xa24c('0x4b')],[_0xa24c('0x4c')]);if(_0x171c51[_0xa24c('0x3c')]&&_0x171c51[_0xa24c('0x3c')][_0xa24c('0x4d')]){_0x171c51[_0xa24c('0x49')][_0xa24c('0x4e')]({'id':0x0,'priority':_0x171c51['applications'][_0xa24c('0x4f')]+0x1,'app':_0xa24c('0x50'),'appdata':_0xa24c('0x4d'),'interval':'*,*,*,*'});}if(_0x171c51['account']['Applications']){delete _0x171c51[_0xa24c('0x3c')]['Applications'];}if(_0x171c51[_0xa24c('0x3c')][_0xa24c('0x3e')]){delete _0x171c51['account'][_0xa24c('0x3e')];}if(_0x540da8[_0xa24c('0x3e')][_0xa24c('0x3f')][_0xa24c('0x4f')]){return _0x540da8[_0xa24c('0x3e')][_0xa24c('0x3f')][0x0];}var _0x3816f7=_[_0xa24c('0xb')](_0x4037d6['body'],{'firstName':_0x4037d6[_0xa24c('0x2c')]['from'],'ListId':_0x540da8['ListId']});_0x3816f7[_0x4037d6['body']['mapKey']]=_0x4037d6['body'][_0xa24c('0x3a')];return db['CmContact'][_0xa24c('0x51')](_0x3816f7);}else{throw new Error(_0xa24c('0x52'));}})[_0xa24c('0x11')](function(_0x1a5f17){_0x171c51[_0xa24c('0x53')]=_0x1a5f17;if(_0x1a5f17){if(!_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x54')]){_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x54')]=rs[_0xa24c('0x55')]()+'\x20'+_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x3a')];}var _0x5c3d11={'ContactId':_0x1a5f17['id'],'MailAccountId':_0x4037d6[_0xa24c('0x3c')]['id'],'inReplyTo':_0x4037d6[_0xa24c('0x2c')]['message'][_0xa24c('0x54')],'to':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x3a')],'cc':_0x4037d6['body']['message']['cc'],'subject':_0x4037d6[_0xa24c('0x2c')]['message']['subject'],'attach':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x56')],'lastMsgAt':moment()[_0xa24c('0x57')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in'};return db['MailInteraction'][_0xa24c('0x3b')]({'where':{'closed':![],'MailAccountId':_0x4037d6['account']['id']},'include':[{'model':db['MailMessage'],'as':_0xa24c('0x58'),'attributes':[_0xa24c('0x54')],'where':{'messageId':_0x4037d6[_0xa24c('0x2c')]['message'][_0xa24c('0x59')]}}]})[_0xa24c('0x11')](function(_0x5c3285){if(_0x5c3285){return[_0x5c3285,![]];}return db[_0xa24c('0x5a')][_0xa24c('0x51')](_0x5c3d11)[_0xa24c('0x11')](function(_0x4bee5c){return[_0x4bee5c,!![]];});});}else{throw new Error(_0xa24c('0x5b'));}})[_0xa24c('0x5c')](function(_0x21439a,_0x7aab6e){if(_0x21439a){_0x3ccc37=_0x7aab6e;if(!_0x7aab6e){return _0x21439a[_0xa24c('0x5d')]({'inReplyTo':_0x4037d6['body'][_0xa24c('0x19')][_0xa24c('0x54')],'to':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x3a')],'cc':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')]['cc'],'subject':_0x4037d6['body'][_0xa24c('0x19')][_0xa24c('0x5e')],'attach':_0x21439a['attach']?_0x21439a[_0xa24c('0x56')]:_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x56')],'substatus':null,'substatusAt':moment()[_0xa24c('0x57')](_0xa24c('0x5f')),'lastMsgAt':moment()[_0xa24c('0x57')](_0xa24c('0x5f')),'lastMsgDirection':'in'});}else{return _0x21439a;}}})[_0xa24c('0x11')](function(_0x32ddc3){_0x171c51['interaction']=_0x32ddc3[_0xa24c('0x46')]({'plain':!![]});_0x171c51[_0xa24c('0x60')][_0xa24c('0x61')]=_0x3ccc37;return db['MailMessage'][_0xa24c('0x51')]({'body':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x2c')],'MailAccountId':_0x4037d6['account']['id'],'MailInteractionId':_0x32ddc3['id'],'direction':'in','ContactId':_0x171c51[_0xa24c('0x53')]['id'],'messageId':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x54')],'from':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')]['from'],'to':_0x4037d6['body'][_0xa24c('0x19')]['to'],'cc':_0x4037d6['body'][_0xa24c('0x19')]['cc'],'subject':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x5e')],'attach':_0x4037d6[_0xa24c('0x2c')]['message'][_0xa24c('0x56')],'Attachments':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x62')],'originTo':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')]['originTo'],'originCc':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')][_0xa24c('0x63')],'bcc':_0x4037d6[_0xa24c('0x2c')]['message'][_0xa24c('0x64')],'ChatWebsiteId':_0x4037d6['body'][_0xa24c('0x19')][_0xa24c('0x65')]||null,'originChannel':_0x4037d6[_0xa24c('0x2c')][_0xa24c('0x19')]['originChannel']||_0xa24c('0x66'),'ChatOfflineMessageId':_0x4037d6[_0xa24c('0x2c')]['message'][_0xa24c('0x67')]||null},{'include':[{'model':db[_0xa24c('0x68')],'as':_0xa24c('0x69')}]});})[_0xa24c('0x11')](function(_0x8bd3fb){_0x171c51[_0xa24c('0x19')]=_0x8bd3fb;if(!_0x171c51[_0xa24c('0x60')][_0xa24c('0x6a')]){return;}return db['User'][_0xa24c('0x3b')]({'attributes':['id',_0xa24c('0x6b')],'where':{'id':_0x171c51[_0xa24c('0x60')][_0xa24c('0x6a')]}});})[_0xa24c('0x11')](function(_0x209b42){if(_0x209b42){_0x171c51[_0xa24c('0x49')][_0xa24c('0x6c')]({'id':0x0,'priority':0x0,'app':_0xa24c('0x6d'),'appdata':_0x209b42[_0xa24c('0x6b')]+','+(_0x171c51[_0xa24c('0x3c')]['waitForTheAssignedAgent']||0xa),'interval':_0xa24c('0x6e')});}if(!_0x171c51[_0xa24c('0x60')]['queueId']){return;}return db[_0xa24c('0x6f')][_0xa24c('0x3b')]({'attributes':['id',_0xa24c('0x6b')],'where':{'id':_0x171c51['interaction']['queueId']}});})[_0xa24c('0x11')](function(_0x4c9b19){if(_0x4c9b19){_0x171c51['applications'][_0xa24c('0x6c')]({'id':0x0,'priority':0x1,'app':_0xa24c('0x70'),'appdata':_0x4c9b19[_0xa24c('0x6b')]+','+(_0x171c51[_0xa24c('0x3c')][_0xa24c('0x71')]||0x12c),'interval':_0xa24c('0x6e')});}return respondWithRpcPromise('Start','startRouting',_0x171c51);})['then'](function(_0x263dab){_0x43beff(_0x263dab);})['catch'](function(_0x53462e){_0x4c5b7e(_0x2101f8[_0xa24c('0x17')](0x1f4,_0x53462e[_0xa24c('0x19')]));});});};