87272b70f8e16b149d5c4add0b8c28837d3e4445
[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 _0x5a43=['subject','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','spread','update','interaction','created','contact','originCc','bcc','ChatWebsiteId','originChannel','Email','ChatOfflineMessageId','UserId','User','unshift','agent','name','waitForTheAssignedAgent','Start','startRouting','lodash','util','randomstring','ioredis','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','http','defaults','redis','./mailAccount.socket','register','request','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','error','code','message','result','catch','MailAccount','findAll','options','raw','attributes','limit','include','model','map','then','GetMailAccount','stringify','find','where','ShowMailAccount','NotifyMailAccount','body','mail','difference','keys','rawAttributes','createdAt','updatedAt','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','List','Contacts','DESC','MailApplication','MailServerOut','get','account','Applications','applications','orderBy','priority','asc','autoclose','push','length','close','*,*,*,*','CmContact','create','List\x20not\x20found','messageId','generate'];(function(_0x2d8434,_0x43d781){var _0x26b9e4=function(_0x54f5b8){while(--_0x54f5b8){_0x2d8434['push'](_0x2d8434['shift']());}};_0x26b9e4(++_0x43d781);}(_0x5a43,0x159));var _0x35a4=function(_0x3cecfd,_0x43c2e9){_0x3cecfd=_0x3cecfd-0x0;var _0x348c2d=_0x5a43[_0x3cecfd];return _0x348c2d;};'use strict';var _=require(_0x35a4('0x0'));var util=require(_0x35a4('0x1'));var moment=require('moment');var BPromise=require('bluebird');var rs=require(_0x35a4('0x2'));var fs=require('fs');var Redis=require(_0x35a4('0x3'));var db=require('../../mysqldb')['db'];var utils=require(_0x35a4('0x4'));var logger=require(_0x35a4('0x5'))(_0x35a4('0x6'));var config=require(_0x35a4('0x7'));var jayson=require(_0x35a4('0x8'));var client=jayson['client'][_0x35a4('0x9')]({'port':0x232c});config['redis']=_[_0x35a4('0xa')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x35a4('0xb')]));require(_0x35a4('0xc'))[_0x35a4('0xd')](socket);function respondWithRpcPromise(_0x5ef542,_0x3637e8,_0x315d2c){return new BPromise(function(_0x4da0fe,_0x2a4e82){return client[_0x35a4('0xe')](_0x5ef542,_0x315d2c)['then'](function(_0x327a59){logger[_0x35a4('0xf')](_0x35a4('0x10'),_0x3637e8,_0x35a4('0x11'));logger[_0x35a4('0x12')](_0x35a4('0x13'),_0x3637e8,_0x35a4('0x11'),JSON['stringify'](_0x327a59));if(_0x327a59[_0x35a4('0x14')]){if(_0x327a59[_0x35a4('0x14')][_0x35a4('0x15')]===0x1f4){logger['error'](_0x35a4('0x10'),_0x3637e8,_0x327a59[_0x35a4('0x14')][_0x35a4('0x16')]);return _0x2a4e82(_0x327a59[_0x35a4('0x14')][_0x35a4('0x16')]);}logger[_0x35a4('0x14')](_0x35a4('0x10'),_0x3637e8,_0x327a59[_0x35a4('0x14')][_0x35a4('0x16')]);return _0x4da0fe(_0x327a59[_0x35a4('0x14')][_0x35a4('0x16')]);}else{logger[_0x35a4('0xf')](_0x35a4('0x10'),_0x3637e8,_0x35a4('0x11'));_0x4da0fe(_0x327a59[_0x35a4('0x17')][_0x35a4('0x16')]);}})[_0x35a4('0x18')](function(_0x1fb62e){logger[_0x35a4('0x14')](_0x35a4('0x10'),_0x3637e8,_0x1fb62e);_0x2a4e82(_0x1fb62e);});});}exports['GetMailAccount']=function(_0x47851f){var _0x3d304c=this;return new Promise(function(_0x201c2d,_0x201641){return db[_0x35a4('0x19')][_0x35a4('0x1a')]({'raw':_0x47851f[_0x35a4('0x1b')]?_0x47851f['options'][_0x35a4('0x1c')]===undefined?!![]:![]:!![],'where':_0x47851f['options']?_0x47851f[_0x35a4('0x1b')]['where']||null:null,'attributes':_0x47851f[_0x35a4('0x1b')]?_0x47851f[_0x35a4('0x1b')][_0x35a4('0x1d')]||null:null,'limit':_0x47851f[_0x35a4('0x1b')]?_0x47851f[_0x35a4('0x1b')][_0x35a4('0x1e')]||null:null,'include':_0x47851f[_0x35a4('0x1b')]?_0x47851f[_0x35a4('0x1b')][_0x35a4('0x1f')]?_['map'](_0x47851f[_0x35a4('0x1b')][_0x35a4('0x1f')],function(_0x179e58){return{'model':db[_0x179e58[_0x35a4('0x20')]],'as':_0x179e58['as'],'attributes':_0x179e58[_0x35a4('0x1d')],'include':_0x179e58[_0x35a4('0x1f')]?_[_0x35a4('0x21')](_0x179e58[_0x35a4('0x1f')],function(_0x55cc3a){return{'model':db[_0x55cc3a['model']],'as':_0x55cc3a['as'],'attributes':_0x55cc3a[_0x35a4('0x1d')],'include':_0x55cc3a[_0x35a4('0x1f')]?_[_0x35a4('0x21')](_0x55cc3a['include'],function(_0x1bbcb6){return{'model':db[_0x1bbcb6[_0x35a4('0x20')]],'as':_0x1bbcb6['as'],'attributes':_0x1bbcb6[_0x35a4('0x1d')]};}):[]};}):[]};}):[]:[]})[_0x35a4('0x22')](function(_0x1bb05a){logger[_0x35a4('0xf')]('GetMailAccount',_0x47851f);logger[_0x35a4('0x12')](_0x35a4('0x23'),_0x47851f,JSON[_0x35a4('0x24')](_0x1bb05a));_0x201c2d(_0x1bb05a);})['catch'](function(_0x469e7a){logger['error'](_0x35a4('0x23'),_0x469e7a['message'],_0x47851f);_0x201641(_0x3d304c['error'](0x1f4,_0x469e7a[_0x35a4('0x16')]));});});};exports['ShowMailAccount']=function(_0x411ae5){var _0x1881d4=this;return new Promise(function(_0x55f7ad,_0x2300a4){return db[_0x35a4('0x19')][_0x35a4('0x25')]({'raw':_0x411ae5['options']?_0x411ae5[_0x35a4('0x1b')]['raw']===undefined?!![]:![]:!![],'where':_0x411ae5[_0x35a4('0x1b')]?_0x411ae5[_0x35a4('0x1b')][_0x35a4('0x26')]||null:null,'attributes':_0x411ae5[_0x35a4('0x1b')]?_0x411ae5[_0x35a4('0x1b')][_0x35a4('0x1d')]||null:null,'include':_0x411ae5['options']?_0x411ae5[_0x35a4('0x1b')]['include']?_['map'](_0x411ae5[_0x35a4('0x1b')]['include'],function(_0x108027){return{'model':db[_0x108027['model']],'as':_0x108027['as'],'attributes':_0x108027[_0x35a4('0x1d')],'include':_0x108027[_0x35a4('0x1f')]?_[_0x35a4('0x21')](_0x108027[_0x35a4('0x1f')],function(_0x4107b8){return{'model':db[_0x4107b8[_0x35a4('0x20')]],'as':_0x4107b8['as'],'attributes':_0x4107b8['attributes'],'include':_0x4107b8[_0x35a4('0x1f')]?_[_0x35a4('0x21')](_0x4107b8[_0x35a4('0x1f')],function(_0x204f55){return{'model':db[_0x204f55['model']],'as':_0x204f55['as'],'attributes':_0x204f55[_0x35a4('0x1d')]};}):[]};}):[]};}):[]:[]})[_0x35a4('0x22')](function(_0x92d5){logger[_0x35a4('0xf')](_0x35a4('0x27'),_0x411ae5);logger[_0x35a4('0x12')](_0x35a4('0x27'),_0x411ae5,JSON['stringify'](_0x92d5));_0x55f7ad(_0x92d5);})['catch'](function(_0x2924c5){logger[_0x35a4('0x14')](_0x35a4('0x27'),_0x2924c5[_0x35a4('0x16')],_0x411ae5);_0x2300a4(_0x1881d4[_0x35a4('0x14')](0x1f4,_0x2924c5[_0x35a4('0x16')]));});});};exports[_0x35a4('0x28')]=function(_0x47b76f){var _0x1032f5=this;return new Promise(function(_0x277a98,_0x1c4415){var _0x9ba719={'body':_0x47b76f[_0x35a4('0x29')],'channel':_0x35a4('0x2a')};var _0xa979f3=_[_0x35a4('0x2b')](_[_0x35a4('0x2c')](db['CmContact'][_0x35a4('0x2d')]),[_0x35a4('0x2e'),_0x35a4('0x2f'),'CompanyId',_0x35a4('0x30')]);var _0x8ea9d8=![];if(_0x47b76f[_0x35a4('0x29')]['id']){delete _0x47b76f[_0x35a4('0x29')]['id'];}if(_[_0x35a4('0x31')](_0x47b76f[_0x35a4('0x29')][_0x35a4('0x32')])){return _0x1c4415(_0x1032f5['error'](0x1f4,_0x35a4('0x33')));}if(_[_0x35a4('0x31')](_0x47b76f[_0x35a4('0x29')]['body'])||_0x47b76f['body'][_0x35a4('0x29')]===''){return _0x1c4415(_0x1032f5['error'](0x1f4,_0x35a4('0x34')));}if(_['isNil'](_0x47b76f[_0x35a4('0x29')][_0x35a4('0x35')])){return _0x1c4415(_0x1032f5[_0x35a4('0x14')](0x1f4,_0x35a4('0x36')+_0xa979f3));}if(!_['includes'](_0xa979f3,_0x47b76f[_0x35a4('0x29')][_0x35a4('0x35')])){return _0x1c4415(_0x1032f5[_0x35a4('0x14')](0x1f4,_0x35a4('0x37')+_0xa979f3));}var _0xf5590d={};_0xf5590d[_0x47b76f[_0x35a4('0x29')]['mapKey']]=_0x47b76f[_0x35a4('0x29')][_0x35a4('0x32')];return db[_0x35a4('0x19')][_0x35a4('0x25')]({'where':{'id':_0x47b76f['account']['id']},'include':[{'model':db[_0x35a4('0x38')],'as':_0x35a4('0x39'),'include':[{'model':db['CmContact'],'as':_0x35a4('0x3a'),'where':_0xf5590d,'limit':0x1,'order':[['updatedAt',_0x35a4('0x3b')]]}]},{'model':db[_0x35a4('0x3c')],'as':'Applications'},{'model':db[_0x35a4('0x3d')],'as':'Smtp'}]})[_0x35a4('0x22')](function(_0x447703){if(_0x447703&&_0x447703[_0x35a4('0x39')]){var _0x44e3bf=_0x447703[_0x35a4('0x3e')]({'plain':!![]});_0x9ba719[_0x35a4('0x3f')]=_['omit'](_0x44e3bf,[_0x35a4('0x40'),'List']);_0x9ba719[_0x35a4('0x41')]=_0x44e3bf[_0x35a4('0x40')];_0x9ba719['list']=_0x44e3bf[_0x35a4('0x39')];_0x9ba719[_0x35a4('0x41')]=_[_0x35a4('0x42')](_0x9ba719[_0x35a4('0x41')],[_0x35a4('0x43')],[_0x35a4('0x44')]);if(_0x9ba719[_0x35a4('0x3f')]&&_0x9ba719['account'][_0x35a4('0x45')]){_0x9ba719['applications'][_0x35a4('0x46')]({'id':0x0,'priority':_0x9ba719[_0x35a4('0x41')][_0x35a4('0x47')]+0x1,'app':_0x35a4('0x48'),'appdata':_0x35a4('0x45'),'interval':_0x35a4('0x49')});}if(_0x9ba719[_0x35a4('0x3f')]['Applications']){delete _0x9ba719[_0x35a4('0x3f')][_0x35a4('0x40')];}if(_0x9ba719[_0x35a4('0x3f')][_0x35a4('0x39')]){delete _0x9ba719[_0x35a4('0x3f')][_0x35a4('0x39')];}if(_0x44e3bf[_0x35a4('0x39')][_0x35a4('0x3a')][_0x35a4('0x47')]){return _0x44e3bf['List'][_0x35a4('0x3a')][0x0];}var _0x560deb=_[_0x35a4('0xa')](_0x47b76f[_0x35a4('0x29')],{'firstName':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x32')],'ListId':_0x44e3bf[_0x35a4('0x30')]});_0x560deb[_0x47b76f[_0x35a4('0x29')][_0x35a4('0x35')]]=_0x47b76f[_0x35a4('0x29')][_0x35a4('0x32')];return db[_0x35a4('0x4a')][_0x35a4('0x4b')](_0x560deb);}else{throw new Error(_0x35a4('0x4c'));}})['then'](function(_0x2fa81a){_0x9ba719['contact']=_0x2fa81a;if(_0x2fa81a){if(!_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x4d')]){_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x4d')]=rs[_0x35a4('0x4e')]()+'\x20'+_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x32')];}var _0x519f88={'ContactId':_0x2fa81a['id'],'MailAccountId':_0x47b76f[_0x35a4('0x3f')]['id'],'inReplyTo':_0x47b76f[_0x35a4('0x29')]['message'][_0x35a4('0x4d')],'to':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x32')],'cc':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')]['cc'],'subject':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x4f')],'attach':_0x47b76f[_0x35a4('0x29')]['message'][_0x35a4('0x50')],'lastMsgAt':moment()[_0x35a4('0x51')](_0x35a4('0x52')),'lastMsgDirection':'in'};return db[_0x35a4('0x53')][_0x35a4('0x25')]({'where':{'closed':![],'MailAccountId':_0x47b76f[_0x35a4('0x3f')]['id']},'include':[{'model':db[_0x35a4('0x54')],'as':_0x35a4('0x55'),'attributes':[_0x35a4('0x4d')],'where':{'messageId':_0x47b76f[_0x35a4('0x29')]['message'][_0x35a4('0x56')]}}]})['then'](function(_0x566dd8){if(_0x566dd8){return[_0x566dd8,![]];}return db[_0x35a4('0x53')][_0x35a4('0x4b')](_0x519f88)[_0x35a4('0x22')](function(_0x1490d4){return[_0x1490d4,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0x35a4('0x57')](function(_0x4d6387,_0x41d9b1){if(_0x4d6387){_0x8ea9d8=_0x41d9b1;if(!_0x41d9b1){return _0x4d6387[_0x35a4('0x58')]({'inReplyTo':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x4d')],'to':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x32')],'cc':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')]['cc'],'subject':_0x47b76f[_0x35a4('0x29')]['message'][_0x35a4('0x4f')],'attach':_0x4d6387[_0x35a4('0x50')]?_0x4d6387[_0x35a4('0x50')]:_0x47b76f[_0x35a4('0x29')]['message'][_0x35a4('0x50')],'substatus':null,'substatusAt':moment()[_0x35a4('0x51')](_0x35a4('0x52')),'lastMsgAt':moment()['format'](_0x35a4('0x52')),'lastMsgDirection':'in'});}else{return _0x4d6387;}}})[_0x35a4('0x22')](function(_0x304d3f){_0x9ba719[_0x35a4('0x59')]=_0x304d3f[_0x35a4('0x3e')]({'plain':!![]});_0x9ba719[_0x35a4('0x59')][_0x35a4('0x5a')]=_0x8ea9d8;return db['MailMessage'][_0x35a4('0x4b')]({'body':_0x47b76f[_0x35a4('0x29')]['body'],'MailAccountId':_0x47b76f['account']['id'],'MailInteractionId':_0x304d3f['id'],'direction':'in','ContactId':_0x9ba719[_0x35a4('0x5b')]['id'],'messageId':_0x47b76f[_0x35a4('0x29')]['message'][_0x35a4('0x4d')],'from':_0x47b76f['body'][_0x35a4('0x16')][_0x35a4('0x32')],'to':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')]['to'],'cc':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')]['cc'],'subject':_0x47b76f[_0x35a4('0x29')]['message']['subject'],'attach':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x50')],'Attachments':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')]['attachments'],'originTo':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')]['originTo'],'originCc':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x5c')],'bcc':_0x47b76f['body'][_0x35a4('0x16')][_0x35a4('0x5d')],'ChatWebsiteId':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x5e')]||null,'originChannel':_0x47b76f[_0x35a4('0x29')][_0x35a4('0x16')][_0x35a4('0x5f')]||_0x35a4('0x60'),'ChatOfflineMessageId':_0x47b76f['body'][_0x35a4('0x16')][_0x35a4('0x61')]||null},{'include':[{'model':db['Attachment'],'as':'Attachments'}]});})['then'](function(_0x1b7688){_0x9ba719[_0x35a4('0x16')]=_0x1b7688;if(_0x9ba719[_0x35a4('0x59')][_0x35a4('0x62')]){return db[_0x35a4('0x63')][_0x35a4('0x25')]({'attributes':['id','name'],'where':{'id':_0x9ba719['interaction'][_0x35a4('0x62')]}})[_0x35a4('0x22')](function(_0x31f153){if(_0x31f153){_0x9ba719[_0x35a4('0x41')][_0x35a4('0x64')]({'id':0x0,'priority':0x0,'app':_0x35a4('0x65'),'appdata':_0x31f153[_0x35a4('0x66')]+','+(_0x9ba719[_0x35a4('0x3f')][_0x35a4('0x67')]||0xa),'interval':_0x35a4('0x49')});}return respondWithRpcPromise(_0x35a4('0x68'),'startRouting',_0x9ba719);});}return respondWithRpcPromise(_0x35a4('0x68'),_0x35a4('0x69'),_0x9ba719);})[_0x35a4('0x22')](function(_0x327a6a){_0x277a98(_0x327a6a);})[_0x35a4('0x18')](function(_0x1d5332){_0x1c4415(_0x1032f5[_0x35a4('0x14')](0x1f4,_0x1d5332['message']));});});};