3a95b011031d036847b02c93c402b1f81cbc5801
[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 _0x5fd1=['spread','update','interaction','contact','attachments','originTo','originCc','ChatOfflineMessageId','Attachment','Attachments','UserId','name','unshift','agent','waitForTheAssignedAgent','MailQueue','queueId','queue','waitForTheAssignedQueue','startRouting','lodash','bluebird','randomstring','ioredis','rpc','../../config/environment','jayson/promise','http','redis','defaults','localhost','./mailAccount.socket','register','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','message','catch','GetMailAccount','MailAccount','findAll','options','raw','where','attributes','limit','include','map','model','then','ShowMailAccount','find','smtp','getSmtpOptions','NotifyMailAccount','body','mail','difference','keys','CmContact','rawAttributes','createdAt','updatedAt','CompanyId','ListId','isNil','from','body\x20is\x20mandatory\x20and\x20not\x20empty','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey','account','CmList','List','Contacts','DESC','MailApplication','Applications','MailServerOut','CloudProvider','get','omit','smtpOptions','applications','list','priority','asc','autoclose','length','close','*,*,*,*','create','List\x20not\x20found','messageId','subject','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found'];(function(_0xe5742c,_0x11503e){var _0x3db329=function(_0x2afb66){while(--_0x2afb66){_0xe5742c['push'](_0xe5742c['shift']());}};_0x3db329(++_0x11503e);}(_0x5fd1,0x155));var _0x15fd=function(_0x3c8605,_0x824f28){_0x3c8605=_0x3c8605-0x0;var _0x2793ec=_0x5fd1[_0x3c8605];return _0x2793ec;};'use strict';var _=require(_0x15fd('0x0'));var moment=require('moment');var BPromise=require(_0x15fd('0x1'));var rs=require(_0x15fd('0x2'));var Redis=require(_0x15fd('0x3'));var db=require('../../mysqldb')['db'];var logger=require('../../config/logger')(_0x15fd('0x4'));var config=require(_0x15fd('0x5'));var jayson=require(_0x15fd('0x6'));var client=jayson['client'][_0x15fd('0x7')]({'port':0x232c});config[_0x15fd('0x8')]=_[_0x15fd('0x9')](config[_0x15fd('0x8')],{'host':_0x15fd('0xa'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x15fd('0x8')]));require(_0x15fd('0xb'))[_0x15fd('0xc')](socket);function respondWithRpcPromise(_0x4c861f,_0x2d2fbb,_0x5c9c63){return new BPromise(function(_0x3dfdbb,_0x250457){return client['request'](_0x4c861f,_0x5c9c63)['then'](function(_0x4ebde5){logger[_0x15fd('0xd')](_0x15fd('0xe'),_0x2d2fbb,_0x15fd('0xf'));logger[_0x15fd('0x10')](_0x15fd('0x11'),_0x2d2fbb,_0x15fd('0xf'),JSON[_0x15fd('0x12')](_0x4ebde5));if(_0x4ebde5[_0x15fd('0x13')]){if(_0x4ebde5[_0x15fd('0x13')]['code']===0x1f4){logger['error']('MailAccount,\x20%s,\x20%s',_0x2d2fbb,_0x4ebde5[_0x15fd('0x13')][_0x15fd('0x14')]);return _0x250457(_0x4ebde5[_0x15fd('0x13')][_0x15fd('0x14')]);}logger[_0x15fd('0x13')](_0x15fd('0xe'),_0x2d2fbb,_0x4ebde5[_0x15fd('0x13')][_0x15fd('0x14')]);return _0x3dfdbb(_0x4ebde5['error']['message']);}else{logger[_0x15fd('0xd')]('MailAccount,\x20%s,\x20%s',_0x2d2fbb,_0x15fd('0xf'));_0x3dfdbb(_0x4ebde5['result'][_0x15fd('0x14')]);}})[_0x15fd('0x15')](function(_0x855bae){logger['error'](_0x15fd('0xe'),_0x2d2fbb,_0x855bae);_0x250457(_0x855bae);});});}exports[_0x15fd('0x16')]=function(_0x8b7b9e){var _0x229f8a=this;return new Promise(function(_0x2fb919,_0x36a90e){return db[_0x15fd('0x17')][_0x15fd('0x18')]({'raw':_0x8b7b9e[_0x15fd('0x19')]?_0x8b7b9e[_0x15fd('0x19')][_0x15fd('0x1a')]===undefined?!![]:![]:!![],'where':_0x8b7b9e['options']?_0x8b7b9e['options'][_0x15fd('0x1b')]||null:null,'attributes':_0x8b7b9e[_0x15fd('0x19')]?_0x8b7b9e[_0x15fd('0x19')][_0x15fd('0x1c')]||null:null,'limit':_0x8b7b9e[_0x15fd('0x19')]?_0x8b7b9e[_0x15fd('0x19')][_0x15fd('0x1d')]||null:null,'include':_0x8b7b9e['options']?_0x8b7b9e[_0x15fd('0x19')][_0x15fd('0x1e')]?_[_0x15fd('0x1f')](_0x8b7b9e['options'][_0x15fd('0x1e')],function(_0x49062f){return{'model':db[_0x49062f[_0x15fd('0x20')]],'as':_0x49062f['as'],'attributes':_0x49062f[_0x15fd('0x1c')],'include':_0x49062f[_0x15fd('0x1e')]?_['map'](_0x49062f[_0x15fd('0x1e')],function(_0x1cbf2c){return{'model':db[_0x1cbf2c[_0x15fd('0x20')]],'as':_0x1cbf2c['as'],'attributes':_0x1cbf2c['attributes'],'include':_0x1cbf2c[_0x15fd('0x1e')]?_['map'](_0x1cbf2c[_0x15fd('0x1e')],function(_0x7e70d6){return{'model':db[_0x7e70d6[_0x15fd('0x20')]],'as':_0x7e70d6['as'],'attributes':_0x7e70d6[_0x15fd('0x1c')]};}):[]};}):[]};}):[]:[]})[_0x15fd('0x21')](function(_0x6c213b){logger[_0x15fd('0xd')](_0x15fd('0x16'),_0x8b7b9e);logger[_0x15fd('0x10')](_0x15fd('0x16'),_0x8b7b9e,JSON[_0x15fd('0x12')](_0x6c213b));_0x2fb919(_0x6c213b);})['catch'](function(_0x47c22c){logger[_0x15fd('0x13')]('GetMailAccount',_0x47c22c[_0x15fd('0x14')],_0x8b7b9e);_0x36a90e(_0x229f8a[_0x15fd('0x13')](0x1f4,_0x47c22c['message']));});});};exports[_0x15fd('0x22')]=function(_0x197d15){var _0x5dc5fe=this;return new Promise(function(_0x104fbd,_0x199647){return db[_0x15fd('0x17')][_0x15fd('0x23')]({'raw':_0x197d15[_0x15fd('0x19')]?_0x197d15[_0x15fd('0x19')]['raw']===undefined?!![]:![]:!![],'where':_0x197d15[_0x15fd('0x19')]?_0x197d15[_0x15fd('0x19')][_0x15fd('0x1b')]||null:null,'attributes':_0x197d15[_0x15fd('0x19')]?_0x197d15['options'][_0x15fd('0x1c')]||null:null,'include':_0x197d15[_0x15fd('0x19')]?_0x197d15[_0x15fd('0x19')][_0x15fd('0x1e')]?_[_0x15fd('0x1f')](_0x197d15[_0x15fd('0x19')]['include'],function(_0x3b80a6){return{'model':db[_0x3b80a6[_0x15fd('0x20')]],'as':_0x3b80a6['as'],'attributes':_0x3b80a6['attributes'],'include':_0x3b80a6[_0x15fd('0x1e')]?_[_0x15fd('0x1f')](_0x3b80a6[_0x15fd('0x1e')],function(_0x5cb227){return{'model':db[_0x5cb227[_0x15fd('0x20')]],'as':_0x5cb227['as'],'attributes':_0x5cb227[_0x15fd('0x1c')],'include':_0x5cb227[_0x15fd('0x1e')]?_[_0x15fd('0x1f')](_0x5cb227[_0x15fd('0x1e')],function(_0x6b2ac8){return{'model':db[_0x6b2ac8['model']],'as':_0x6b2ac8['as'],'attributes':_0x6b2ac8['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x340d17){if(_0x197d15[_0x15fd('0x19')][_0x15fd('0x24')]===!![])_0x340d17['setDataValue']('smtpOptions',_0x340d17[_0x15fd('0x25')]());logger['info']('ShowMailAccount',_0x197d15);logger['debug'](_0x15fd('0x22'),_0x197d15,JSON[_0x15fd('0x12')](_0x340d17));_0x104fbd(_0x340d17);})[_0x15fd('0x15')](function(_0x59d0d7){logger[_0x15fd('0x13')](_0x15fd('0x22'),_0x59d0d7[_0x15fd('0x14')],_0x197d15);_0x199647(_0x5dc5fe['error'](0x1f4,_0x59d0d7[_0x15fd('0x14')]));});});};exports[_0x15fd('0x26')]=function(_0x2e81cb){var _0xe18038=this;return new Promise(function(_0x2e8b0a,_0x46b8f4){var _0x19a29f={'body':_0x2e81cb[_0x15fd('0x27')],'channel':_0x15fd('0x28')};var _0x5dd3c4=_[_0x15fd('0x29')](_[_0x15fd('0x2a')](db[_0x15fd('0x2b')][_0x15fd('0x2c')]),[_0x15fd('0x2d'),_0x15fd('0x2e'),_0x15fd('0x2f'),_0x15fd('0x30')]);var _0x343ce6=![];if(_0x2e81cb[_0x15fd('0x27')]['id']){delete _0x2e81cb[_0x15fd('0x27')]['id'];}if(_[_0x15fd('0x31')](_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x32')])){return _0x46b8f4(_0xe18038[_0x15fd('0x13')](0x1f4,'from\x20is\x20mandatory'));}if(_[_0x15fd('0x31')](_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x27')])||_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x27')]===''){return _0x46b8f4(_0xe18038[_0x15fd('0x13')](0x1f4,_0x15fd('0x33')));}if(_[_0x15fd('0x31')](_0x2e81cb['body']['mapKey'])){return _0x46b8f4(_0xe18038[_0x15fd('0x13')](0x1f4,'mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x5dd3c4));}if(!_[_0x15fd('0x34')](_0x5dd3c4,_0x2e81cb[_0x15fd('0x27')]['mapKey'])){return _0x46b8f4(_0xe18038[_0x15fd('0x13')](0x1f4,_0x15fd('0x35')+_0x5dd3c4));}var _0x131eb4={};_0x131eb4[_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x36')]]=_0x2e81cb[_0x15fd('0x27')]['from'];return db[_0x15fd('0x17')][_0x15fd('0x23')]({'where':{'id':_0x2e81cb[_0x15fd('0x37')]['id']},'include':[{'model':db[_0x15fd('0x38')],'as':_0x15fd('0x39'),'include':[{'model':db[_0x15fd('0x2b')],'as':_0x15fd('0x3a'),'where':_0x131eb4,'limit':0x1,'order':[[_0x15fd('0x2e'),_0x15fd('0x3b')]]}]},{'model':db[_0x15fd('0x3c')],'as':_0x15fd('0x3d')},{'model':db[_0x15fd('0x3e')],'as':'Smtp','include':[{'model':db[_0x15fd('0x3f')],'as':'CloudProvider'}]}]})[_0x15fd('0x21')](function(_0x34c250){if(_0x34c250&&_0x34c250[_0x15fd('0x39')]){var _0x5a1f32=_0x34c250[_0x15fd('0x40')]({'plain':!![]});_0x19a29f[_0x15fd('0x37')]=_[_0x15fd('0x41')](_0x5a1f32,[_0x15fd('0x3d'),'List']);_0x19a29f[_0x15fd('0x37')][_0x15fd('0x42')]=_0x34c250[_0x15fd('0x25')]();_0x19a29f[_0x15fd('0x43')]=_0x5a1f32['Applications'];_0x19a29f[_0x15fd('0x44')]=_0x5a1f32[_0x15fd('0x39')];_0x19a29f['applications']=_['orderBy'](_0x19a29f['applications'],[_0x15fd('0x45')],[_0x15fd('0x46')]);if(_0x19a29f[_0x15fd('0x37')]&&_0x19a29f[_0x15fd('0x37')][_0x15fd('0x47')]){_0x19a29f[_0x15fd('0x43')]['push']({'id':0x0,'priority':_0x19a29f[_0x15fd('0x43')][_0x15fd('0x48')]+0x1,'app':_0x15fd('0x49'),'appdata':'autoclose','interval':_0x15fd('0x4a')});}if(_0x19a29f['account'][_0x15fd('0x3d')]){delete _0x19a29f[_0x15fd('0x37')][_0x15fd('0x3d')];}if(_0x19a29f[_0x15fd('0x37')][_0x15fd('0x39')]){delete _0x19a29f[_0x15fd('0x37')][_0x15fd('0x39')];}if(_0x5a1f32[_0x15fd('0x39')][_0x15fd('0x3a')][_0x15fd('0x48')]){return _0x5a1f32[_0x15fd('0x39')][_0x15fd('0x3a')][0x0];}var _0x2db992=_['defaults'](_0x2e81cb[_0x15fd('0x27')],{'firstName':_0x2e81cb['body'][_0x15fd('0x32')],'ListId':_0x5a1f32['ListId']});_0x2db992[_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x36')]]=_0x2e81cb[_0x15fd('0x27')]['from'];return db['CmContact'][_0x15fd('0x4b')](_0x2db992);}else{throw new Error(_0x15fd('0x4c'));}})[_0x15fd('0x21')](function(_0x30ab06){_0x19a29f['contact']=_0x30ab06;if(_0x30ab06){if(!_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x4d')]){_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x4d')]=rs['generate']()+'\x20'+_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x32')];}var _0x40e7c8={'ContactId':_0x30ab06['id'],'MailAccountId':_0x2e81cb[_0x15fd('0x37')]['id'],'inReplyTo':_0x2e81cb[_0x15fd('0x27')]['message']['messageId'],'to':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['from'],'cc':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['cc'],'subject':_0x2e81cb['body'][_0x15fd('0x14')][_0x15fd('0x4e')],'attach':_0x2e81cb[_0x15fd('0x27')]['message'][_0x15fd('0x4f')],'lastMsgAt':moment()[_0x15fd('0x50')](_0x15fd('0x51')),'lastMsgDirection':'in'};return db[_0x15fd('0x52')][_0x15fd('0x23')]({'where':{'closed':![],'MailAccountId':_0x2e81cb[_0x15fd('0x37')]['id']},'include':[{'model':db[_0x15fd('0x53')],'as':_0x15fd('0x54'),'attributes':[_0x15fd('0x4d')],'where':{'messageId':_0x2e81cb[_0x15fd('0x27')]['message'][_0x15fd('0x55')]}}]})[_0x15fd('0x21')](function(_0x5b720b){if(_0x5b720b){return[_0x5b720b,![]];}return db['MailInteraction'][_0x15fd('0x4b')](_0x40e7c8)[_0x15fd('0x21')](function(_0x2bd6ef){return[_0x2bd6ef,!![]];});});}else{throw new Error(_0x15fd('0x56'));}})[_0x15fd('0x57')](function(_0x1b6f59,_0x4f2ff6){if(_0x1b6f59){_0x343ce6=_0x4f2ff6;if(!_0x4f2ff6){return _0x1b6f59[_0x15fd('0x58')]({'inReplyTo':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x4d')],'to':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x32')],'cc':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['cc'],'subject':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['subject'],'attach':_0x1b6f59['attach']?_0x1b6f59[_0x15fd('0x4f')]:_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x4f')],'substatus':null,'substatusAt':moment()[_0x15fd('0x50')](_0x15fd('0x51')),'lastMsgAt':moment()[_0x15fd('0x50')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in'});}else{return _0x1b6f59;}}})[_0x15fd('0x21')](function(_0x5de222){_0x19a29f[_0x15fd('0x59')]=_0x5de222['get']({'plain':!![]});_0x19a29f[_0x15fd('0x59')]['created']=_0x343ce6;return db[_0x15fd('0x53')][_0x15fd('0x4b')]({'body':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x27')],'MailAccountId':_0x2e81cb[_0x15fd('0x37')]['id'],'MailInteractionId':_0x5de222['id'],'direction':'in','ContactId':_0x19a29f[_0x15fd('0x5a')]['id'],'messageId':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x4d')],'from':_0x2e81cb['body']['message'][_0x15fd('0x32')],'to':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['to'],'cc':_0x2e81cb['body'][_0x15fd('0x14')]['cc'],'subject':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['subject'],'attach':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['attach'],'Attachments':_0x2e81cb[_0x15fd('0x27')]['message'][_0x15fd('0x5b')],'originTo':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')][_0x15fd('0x5c')],'originCc':_0x2e81cb[_0x15fd('0x27')]['message'][_0x15fd('0x5d')],'bcc':_0x2e81cb['body'][_0x15fd('0x14')]['bcc'],'ChatWebsiteId':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['ChatWebsiteId']||null,'originChannel':_0x2e81cb[_0x15fd('0x27')][_0x15fd('0x14')]['originChannel']||'Email','ChatOfflineMessageId':_0x2e81cb[_0x15fd('0x27')]['message'][_0x15fd('0x5e')]||null},{'include':[{'model':db[_0x15fd('0x5f')],'as':_0x15fd('0x60')}]});})[_0x15fd('0x21')](function(_0x2d1c93){_0x19a29f['message']=_0x2d1c93;if(!_0x19a29f[_0x15fd('0x59')][_0x15fd('0x61')]){return;}return db['User'][_0x15fd('0x23')]({'attributes':['id',_0x15fd('0x62')],'where':{'id':_0x19a29f[_0x15fd('0x59')][_0x15fd('0x61')]}});})[_0x15fd('0x21')](function(_0x1d529d){if(_0x1d529d){_0x19a29f[_0x15fd('0x43')][_0x15fd('0x63')]({'id':0x0,'priority':0x0,'app':_0x15fd('0x64'),'appdata':_0x1d529d[_0x15fd('0x62')]+','+(_0x19a29f[_0x15fd('0x37')][_0x15fd('0x65')]||0xa),'interval':_0x15fd('0x4a')});}if(!_0x19a29f[_0x15fd('0x59')]['queueId']){return;}return db[_0x15fd('0x66')]['find']({'attributes':['id',_0x15fd('0x62')],'where':{'id':_0x19a29f[_0x15fd('0x59')][_0x15fd('0x67')]}});})[_0x15fd('0x21')](function(_0x58f942){if(_0x58f942){_0x19a29f[_0x15fd('0x43')][_0x15fd('0x63')]({'id':0x0,'priority':0x1,'app':_0x15fd('0x68'),'appdata':_0x58f942[_0x15fd('0x62')]+','+(_0x19a29f['account'][_0x15fd('0x69')]||0x12c),'interval':_0x15fd('0x4a')});}return respondWithRpcPromise('Start',_0x15fd('0x6a'),_0x19a29f);})[_0x15fd('0x21')](function(_0x128ee3){_0x2e8b0a(_0x128ee3);})['catch'](function(_0x112122){_0x46b8f4(_0xe18038[_0x15fd('0x13')](0x1f4,_0x112122[_0x15fd('0x14')]));});});};