Built motion from commit (unavailable).|2.3.0
[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 _0x71fa=['then','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','catch','MailAccount','findAll','options','raw','where','attributes','limit','include','map','model','GetMailAccount','ShowMailAccount','NotifyMailAccount','mail','CmContact','rawAttributes','updatedAt','CompanyId','ListId','body','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','includes','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','from','find','account','CmList','List','Contacts','DESC','MailApplication','Smtp','applications','orderBy','priority','autoclose','length','close','*,*,*,*','Applications','create','contact','messageId','generate','subject','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','Contact\x20not\x20found','spread','update','interaction','get','created','originTo','originCc','bcc','UserId','User','name','unshift','agent','waitForTheAssignedAgent','Start','startRouting','lodash','util','moment','randomstring','../../config/utils','rpc','../../config/environment','jayson/promise','http','redis','defaults','socket.io-emitter','./mailAccount.socket','register','request'];(function(_0x4a0505,_0x1978e2){var _0x2de63c=function(_0x20664e){while(--_0x20664e){_0x4a0505['push'](_0x4a0505['shift']());}};_0x2de63c(++_0x1978e2);}(_0x71fa,0x175));var _0xa71f=function(_0x385236,_0x35d695){_0x385236=_0x385236-0x0;var _0x2f8988=_0x71fa[_0x385236];return _0x2f8988;};'use strict';var _=require(_0xa71f('0x0'));var util=require(_0xa71f('0x1'));var moment=require(_0xa71f('0x2'));var BPromise=require('bluebird');var rs=require(_0xa71f('0x3'));var fs=require('fs');var Redis=require('ioredis');var db=require('../../mysqldb')['db'];var utils=require(_0xa71f('0x4'));var logger=require('../../config/logger')(_0xa71f('0x5'));var config=require(_0xa71f('0x6'));var jayson=require(_0xa71f('0x7'));var client=jayson['client'][_0xa71f('0x8')]({'port':0x232c});config[_0xa71f('0x9')]=_[_0xa71f('0xa')](config[_0xa71f('0x9')],{'host':'localhost','port':0x18eb});var socket=require(_0xa71f('0xb'))(new Redis(config[_0xa71f('0x9')]));require(_0xa71f('0xc'))[_0xa71f('0xd')](socket);function respondWithRpcPromise(_0x434af2,_0x3f0a7d,_0x4f0487){return new BPromise(function(_0x2429fb,_0x5e0f89){return client[_0xa71f('0xe')](_0x434af2,_0x4f0487)[_0xa71f('0xf')](function(_0x53e74c){logger[_0xa71f('0x10')](_0xa71f('0x11'),_0x3f0a7d,_0xa71f('0x12'));logger[_0xa71f('0x13')](_0xa71f('0x14'),_0x3f0a7d,_0xa71f('0x12'),JSON[_0xa71f('0x15')](_0x53e74c));if(_0x53e74c['error']){if(_0x53e74c[_0xa71f('0x16')][_0xa71f('0x17')]===0x1f4){logger[_0xa71f('0x16')]('MailAccount,\x20%s,\x20%s',_0x3f0a7d,_0x53e74c[_0xa71f('0x16')][_0xa71f('0x18')]);return _0x5e0f89(_0x53e74c['error'][_0xa71f('0x18')]);}logger[_0xa71f('0x16')](_0xa71f('0x11'),_0x3f0a7d,_0x53e74c[_0xa71f('0x16')][_0xa71f('0x18')]);return _0x2429fb(_0x53e74c[_0xa71f('0x16')][_0xa71f('0x18')]);}else{logger[_0xa71f('0x10')](_0xa71f('0x11'),_0x3f0a7d,_0xa71f('0x12'));_0x2429fb(_0x53e74c['result'][_0xa71f('0x18')]);}})[_0xa71f('0x19')](function(_0x34cf3b){logger[_0xa71f('0x16')](_0xa71f('0x11'),_0x3f0a7d,_0x34cf3b);_0x5e0f89(_0x34cf3b);});});}exports['GetMailAccount']=function(_0x18120a){var _0x201f31=this;return new Promise(function(_0x5e6ff5,_0x2624f2){return db[_0xa71f('0x1a')][_0xa71f('0x1b')]({'raw':_0x18120a[_0xa71f('0x1c')]?_0x18120a['options'][_0xa71f('0x1d')]===undefined?!![]:![]:!![],'where':_0x18120a[_0xa71f('0x1c')]?_0x18120a[_0xa71f('0x1c')][_0xa71f('0x1e')]||null:null,'attributes':_0x18120a['options']?_0x18120a[_0xa71f('0x1c')][_0xa71f('0x1f')]||null:null,'limit':_0x18120a[_0xa71f('0x1c')]?_0x18120a[_0xa71f('0x1c')][_0xa71f('0x20')]||null:null,'include':_0x18120a[_0xa71f('0x1c')]?_0x18120a[_0xa71f('0x1c')][_0xa71f('0x21')]?_[_0xa71f('0x22')](_0x18120a[_0xa71f('0x1c')]['include'],function(_0x5c6b46){return{'model':db[_0x5c6b46[_0xa71f('0x23')]],'as':_0x5c6b46['as'],'attributes':_0x5c6b46[_0xa71f('0x1f')],'include':_0x5c6b46[_0xa71f('0x21')]?_[_0xa71f('0x22')](_0x5c6b46[_0xa71f('0x21')],function(_0x2f27c5){return{'model':db[_0x2f27c5[_0xa71f('0x23')]],'as':_0x2f27c5['as'],'attributes':_0x2f27c5[_0xa71f('0x1f')],'include':_0x2f27c5[_0xa71f('0x21')]?_[_0xa71f('0x22')](_0x2f27c5['include'],function(_0x30b89d){return{'model':db[_0x30b89d[_0xa71f('0x23')]],'as':_0x30b89d['as'],'attributes':_0x30b89d['attributes']};}):[]};}):[]};}):[]:[]})[_0xa71f('0xf')](function(_0x32c8c1){logger['info']('GetMailAccount',_0x18120a);logger['debug'](_0xa71f('0x24'),_0x18120a,JSON[_0xa71f('0x15')](_0x32c8c1));_0x5e6ff5(_0x32c8c1);})[_0xa71f('0x19')](function(_0x3a7e80){logger[_0xa71f('0x16')](_0xa71f('0x24'),_0x3a7e80[_0xa71f('0x18')],_0x18120a);_0x2624f2(_0x201f31['error'](0x1f4,_0x3a7e80[_0xa71f('0x18')]));});});};exports[_0xa71f('0x25')]=function(_0x434439){var _0x7d6601=this;return new Promise(function(_0xb066e4,_0x4330f9){return db[_0xa71f('0x1a')]['find']({'raw':_0x434439[_0xa71f('0x1c')]?_0x434439['options'][_0xa71f('0x1d')]===undefined?!![]:![]:!![],'where':_0x434439['options']?_0x434439[_0xa71f('0x1c')][_0xa71f('0x1e')]||null:null,'attributes':_0x434439[_0xa71f('0x1c')]?_0x434439[_0xa71f('0x1c')][_0xa71f('0x1f')]||null:null,'include':_0x434439['options']?_0x434439[_0xa71f('0x1c')]['include']?_[_0xa71f('0x22')](_0x434439['options'][_0xa71f('0x21')],function(_0x413f44){return{'model':db[_0x413f44[_0xa71f('0x23')]],'as':_0x413f44['as'],'attributes':_0x413f44['attributes'],'include':_0x413f44['include']?_[_0xa71f('0x22')](_0x413f44[_0xa71f('0x21')],function(_0x2b8bd6){return{'model':db[_0x2b8bd6[_0xa71f('0x23')]],'as':_0x2b8bd6['as'],'attributes':_0x2b8bd6[_0xa71f('0x1f')],'include':_0x2b8bd6[_0xa71f('0x21')]?_[_0xa71f('0x22')](_0x2b8bd6[_0xa71f('0x21')],function(_0x4f0d1f){return{'model':db[_0x4f0d1f['model']],'as':_0x4f0d1f['as'],'attributes':_0x4f0d1f[_0xa71f('0x1f')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x2d9cff){logger[_0xa71f('0x10')](_0xa71f('0x25'),_0x434439);logger[_0xa71f('0x13')](_0xa71f('0x25'),_0x434439,JSON[_0xa71f('0x15')](_0x2d9cff));_0xb066e4(_0x2d9cff);})['catch'](function(_0x3834bd){logger[_0xa71f('0x16')](_0xa71f('0x25'),_0x3834bd[_0xa71f('0x18')],_0x434439);_0x4330f9(_0x7d6601['error'](0x1f4,_0x3834bd[_0xa71f('0x18')]));});});};exports[_0xa71f('0x26')]=function(_0x1ae787){var _0x51c8ad=this;return new Promise(function(_0x48b339,_0x106294){var _0x236c49={'body':_0x1ae787['body'],'channel':_0xa71f('0x27')};var _0xdb5a1f=_['difference'](_['keys'](db[_0xa71f('0x28')][_0xa71f('0x29')]),['createdAt',_0xa71f('0x2a'),_0xa71f('0x2b'),_0xa71f('0x2c')]);var _0x1809c3=![];if(_0x1ae787[_0xa71f('0x2d')]['id']){delete _0x1ae787[_0xa71f('0x2d')]['id'];}if(_[_0xa71f('0x2e')](_0x1ae787[_0xa71f('0x2d')]['from'])){return _0x106294(_0x51c8ad[_0xa71f('0x16')](0x1f4,_0xa71f('0x2f')));}if(_[_0xa71f('0x2e')](_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x2d')])||_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x2d')]===''){return _0x106294(_0x51c8ad['error'](0x1f4,_0xa71f('0x30')));}if(_[_0xa71f('0x2e')](_0x1ae787[_0xa71f('0x2d')]['mapKey'])){return _0x106294(_0x51c8ad[_0xa71f('0x16')](0x1f4,'mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0xdb5a1f));}if(!_[_0xa71f('0x31')](_0xdb5a1f,_0x1ae787['body'][_0xa71f('0x32')])){return _0x106294(_0x51c8ad[_0xa71f('0x16')](0x1f4,_0xa71f('0x33')+_0xdb5a1f));}var _0x4296a3={};_0x4296a3[_0x1ae787['body']['mapKey']]=_0x1ae787['body'][_0xa71f('0x34')];return db[_0xa71f('0x1a')][_0xa71f('0x35')]({'where':{'id':_0x1ae787[_0xa71f('0x36')]['id']},'include':[{'model':db[_0xa71f('0x37')],'as':_0xa71f('0x38'),'include':[{'model':db[_0xa71f('0x28')],'as':_0xa71f('0x39'),'where':_0x4296a3,'limit':0x1,'order':[[_0xa71f('0x2a'),_0xa71f('0x3a')]]}]},{'model':db[_0xa71f('0x3b')],'as':'Applications'},{'model':db['MailServerOut'],'as':_0xa71f('0x3c')}]})[_0xa71f('0xf')](function(_0x48ce01){if(_0x48ce01&&_0x48ce01[_0xa71f('0x38')]){var _0x32b4ad=_0x48ce01['get']({'plain':!![]});_0x236c49[_0xa71f('0x36')]=_['omit'](_0x32b4ad,['Applications',_0xa71f('0x38')]);_0x236c49[_0xa71f('0x3d')]=_0x32b4ad['Applications'];_0x236c49['list']=_0x32b4ad['List'];_0x236c49['applications']=_[_0xa71f('0x3e')](_0x236c49[_0xa71f('0x3d')],[_0xa71f('0x3f')],['asc']);if(_0x236c49['account']&&_0x236c49[_0xa71f('0x36')][_0xa71f('0x40')]){_0x236c49['applications']['push']({'id':0x0,'priority':_0x236c49['applications'][_0xa71f('0x41')]+0x1,'app':_0xa71f('0x42'),'appdata':_0xa71f('0x40'),'interval':_0xa71f('0x43')});}if(_0x236c49[_0xa71f('0x36')]['Applications']){delete _0x236c49[_0xa71f('0x36')][_0xa71f('0x44')];}if(_0x236c49[_0xa71f('0x36')][_0xa71f('0x38')]){delete _0x236c49['account'][_0xa71f('0x38')];}if(_0x32b4ad[_0xa71f('0x38')][_0xa71f('0x39')]['length']){return _0x32b4ad[_0xa71f('0x38')]['Contacts'][0x0];}var _0xf43ec3=_[_0xa71f('0xa')](_0x1ae787['body'],{'firstName':_0x1ae787[_0xa71f('0x2d')]['from'],'ListId':_0x32b4ad['ListId']});_0xf43ec3[_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x32')]]=_0x1ae787[_0xa71f('0x2d')]['from'];return db[_0xa71f('0x28')][_0xa71f('0x45')](_0xf43ec3);}else{throw new Error('List\x20not\x20found');}})[_0xa71f('0xf')](function(_0x4830d1){_0x236c49[_0xa71f('0x46')]=_0x4830d1;if(_0x4830d1){if(!_0x1ae787['body'][_0xa71f('0x18')][_0xa71f('0x47')]){_0x1ae787['body'][_0xa71f('0x18')][_0xa71f('0x47')]=rs[_0xa71f('0x48')]()+'\x20'+_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x34')];}var _0x5c2fbf={'ContactId':_0x4830d1['id'],'MailAccountId':_0x1ae787[_0xa71f('0x36')]['id'],'inReplyTo':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x47')],'to':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x34')],'cc':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')]['cc'],'subject':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x49')],'attach':_0x1ae787['body']['message'][_0xa71f('0x4a')],'lastMsgAt':moment()[_0xa71f('0x4b')](_0xa71f('0x4c')),'lastMsgDirection':'in','lastMsgBody':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x2d')]};return db[_0xa71f('0x4d')][_0xa71f('0x35')]({'where':{'closed':![],'MailAccountId':_0x1ae787['account']['id']},'include':[{'model':db[_0xa71f('0x4e')],'as':_0xa71f('0x4f'),'attributes':[_0xa71f('0x47')],'where':{'messageId':_0x1ae787[_0xa71f('0x2d')]['message']['inReplyTo']}}]})['then'](function(_0x1c52cf){if(_0x1c52cf){return[_0x1c52cf,![]];}return db[_0xa71f('0x4d')][_0xa71f('0x45')](_0x5c2fbf)['then'](function(_0x4ae707){return[_0x4ae707,!![]];});});}else{throw new Error(_0xa71f('0x50'));}})[_0xa71f('0x51')](function(_0x4e1caf,_0x396fc9){if(_0x4e1caf){_0x1809c3=_0x396fc9;if(!_0x396fc9){return _0x4e1caf[_0xa71f('0x52')]({'inReplyTo':_0x1ae787[_0xa71f('0x2d')]['message'][_0xa71f('0x47')],'to':_0x1ae787['body']['message'][_0xa71f('0x34')],'cc':_0x1ae787['body']['message']['cc'],'subject':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')]['subject'],'attach':_0x4e1caf[_0xa71f('0x4a')]?_0x4e1caf['attach']:_0x1ae787['body']['message'][_0xa71f('0x4a')],'substatus':null,'substatusAt':moment()['format'](_0xa71f('0x4c')),'lastMsgAt':moment()[_0xa71f('0x4b')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in','lastMsgBody':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x2d')]});}else{return _0x4e1caf;}}})[_0xa71f('0xf')](function(_0xe526a3){_0x236c49[_0xa71f('0x53')]=_0xe526a3[_0xa71f('0x54')]({'plain':!![]});_0x236c49[_0xa71f('0x53')][_0xa71f('0x55')]=_0x1809c3;return db[_0xa71f('0x4e')][_0xa71f('0x45')]({'body':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x2d')],'MailAccountId':_0x1ae787['account']['id'],'MailInteractionId':_0xe526a3['id'],'direction':'in','ContactId':_0x236c49[_0xa71f('0x46')]['id'],'messageId':_0x1ae787[_0xa71f('0x2d')]['message']['messageId'],'from':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x34')],'to':_0x1ae787[_0xa71f('0x2d')]['message']['to'],'cc':_0x1ae787[_0xa71f('0x2d')]['message']['cc'],'subject':_0x1ae787['body']['message'][_0xa71f('0x49')],'attach':_0x1ae787[_0xa71f('0x2d')]['message'][_0xa71f('0x4a')],'Attachments':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')]['attachments'],'originTo':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x56')],'originCc':_0x1ae787[_0xa71f('0x2d')][_0xa71f('0x18')][_0xa71f('0x57')],'bcc':_0x1ae787['body'][_0xa71f('0x18')][_0xa71f('0x58')]},{'include':[{'model':db['Attachment'],'as':'Attachments'}]});})['then'](function(_0x12713e){_0x236c49[_0xa71f('0x18')]=_0x12713e;if(_0x236c49[_0xa71f('0x53')][_0xa71f('0x59')]){return db[_0xa71f('0x5a')][_0xa71f('0x35')]({'attributes':['id',_0xa71f('0x5b')],'where':{'id':_0x236c49[_0xa71f('0x53')][_0xa71f('0x59')]}})[_0xa71f('0xf')](function(_0x11213a){if(_0x11213a){_0x236c49[_0xa71f('0x3d')][_0xa71f('0x5c')]({'id':0x0,'priority':0x0,'app':_0xa71f('0x5d'),'appdata':_0x11213a['name']+','+(_0x236c49[_0xa71f('0x36')][_0xa71f('0x5e')]||0xa),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0xa71f('0x5f'),_0xa71f('0x60'),_0x236c49);});}return respondWithRpcPromise(_0xa71f('0x5f'),'startRouting',_0x236c49);})[_0xa71f('0xf')](function(_0x1184ff){_0x48b339(_0x1184ff);})[_0xa71f('0x19')](function(_0x996356){_0x106294(_0x51c8ad[_0xa71f('0x16')](0x1f4,_0x996356[_0xa71f('0x18')]));});});};