773d2eba0ac22003427f9cc5dd4dd05eb7bac9e8
[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 _0xe21f=['redis','defaults','localhost','socket.io-emitter','./mailAccount.socket','register','request','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','info','result','catch','MailAccount','raw','options','where','limit','include','model','attributes','map','then','GetMailAccount','ShowMailAccount','find','body','mail','difference','keys','CmContact','createdAt','updatedAt','CompanyId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','account','CmList','DESC','MailApplication','Applications','MailServerOut','Smtp','get','applications','list','List','orderBy','priority','asc','autoclose','length','close','*,*,*,*','Contacts','ListId','create','List\x20not\x20found','messageId','generate','attach','format','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','spread','update','YYYY-MM-DD\x20HH:mm:ss','subject','attachments','originTo','bcc','originChannel','Email','Attachment','Attachments','UserId','User','interaction','unshift','agent','name','waitForTheAssignedAgent','Start','startRouting','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','jayson/promise','client','http'];(function(_0x396a75,_0x1f40f6){var _0x32140e=function(_0xa316ee){while(--_0xa316ee){_0x396a75['push'](_0x396a75['shift']());}};_0x32140e(++_0x1f40f6);}(_0xe21f,0x1a0));var _0xfe21=function(_0xc9ee2e,_0x21ca3d){_0xc9ee2e=_0xc9ee2e-0x0;var _0x1e8c91=_0xe21f[_0xc9ee2e];return _0x1e8c91;};'use strict';var _=require('lodash');var util=require(_0xfe21('0x0'));var moment=require(_0xfe21('0x1'));var BPromise=require(_0xfe21('0x2'));var rs=require(_0xfe21('0x3'));var fs=require('fs');var Redis=require(_0xfe21('0x4'));var db=require(_0xfe21('0x5'))['db'];var utils=require(_0xfe21('0x6'));var logger=require('../../config/logger')(_0xfe21('0x7'));var config=require(_0xfe21('0x8'));var jayson=require(_0xfe21('0x9'));var client=jayson[_0xfe21('0xa')][_0xfe21('0xb')]({'port':0x232c});config[_0xfe21('0xc')]=_[_0xfe21('0xd')](config[_0xfe21('0xc')],{'host':_0xfe21('0xe'),'port':0x18eb});var socket=require(_0xfe21('0xf'))(new Redis(config['redis']));require(_0xfe21('0x10'))[_0xfe21('0x11')](socket);function respondWithRpcPromise(_0x5e590f,_0x51cc75,_0x30f603){return new BPromise(function(_0x46c79c,_0x35f99e){return client[_0xfe21('0x12')](_0x5e590f,_0x30f603)['then'](function(_0xfef3aa){logger['info'](_0xfe21('0x13'),_0x51cc75,_0xfe21('0x14'));logger[_0xfe21('0x15')](_0xfe21('0x16'),_0x51cc75,_0xfe21('0x14'),JSON[_0xfe21('0x17')](_0xfef3aa));if(_0xfef3aa[_0xfe21('0x18')]){if(_0xfef3aa[_0xfe21('0x18')][_0xfe21('0x19')]===0x1f4){logger[_0xfe21('0x18')]('MailAccount,\x20%s,\x20%s',_0x51cc75,_0xfef3aa['error'][_0xfe21('0x1a')]);return _0x35f99e(_0xfef3aa[_0xfe21('0x18')][_0xfe21('0x1a')]);}logger[_0xfe21('0x18')](_0xfe21('0x13'),_0x51cc75,_0xfef3aa['error'][_0xfe21('0x1a')]);return _0x46c79c(_0xfef3aa[_0xfe21('0x18')]['message']);}else{logger[_0xfe21('0x1b')]('MailAccount,\x20%s,\x20%s',_0x51cc75,'request\x20sent');_0x46c79c(_0xfef3aa[_0xfe21('0x1c')][_0xfe21('0x1a')]);}})[_0xfe21('0x1d')](function(_0x1076ab){logger['error'](_0xfe21('0x13'),_0x51cc75,_0x1076ab);_0x35f99e(_0x1076ab);});});}exports['GetMailAccount']=function(_0x402bea){var _0x389f9c=this;return new Promise(function(_0x42f080,_0x2116cd){return db[_0xfe21('0x1e')]['findAll']({'raw':_0x402bea['options']?_0x402bea['options'][_0xfe21('0x1f')]===undefined?!![]:![]:!![],'where':_0x402bea['options']?_0x402bea[_0xfe21('0x20')][_0xfe21('0x21')]||null:null,'attributes':_0x402bea[_0xfe21('0x20')]?_0x402bea[_0xfe21('0x20')]['attributes']||null:null,'limit':_0x402bea[_0xfe21('0x20')]?_0x402bea[_0xfe21('0x20')][_0xfe21('0x22')]||null:null,'include':_0x402bea[_0xfe21('0x20')]?_0x402bea[_0xfe21('0x20')][_0xfe21('0x23')]?_['map'](_0x402bea[_0xfe21('0x20')][_0xfe21('0x23')],function(_0x2c36bd){return{'model':db[_0x2c36bd[_0xfe21('0x24')]],'as':_0x2c36bd['as'],'attributes':_0x2c36bd[_0xfe21('0x25')],'include':_0x2c36bd['include']?_[_0xfe21('0x26')](_0x2c36bd['include'],function(_0x2f1aab){return{'model':db[_0x2f1aab[_0xfe21('0x24')]],'as':_0x2f1aab['as'],'attributes':_0x2f1aab[_0xfe21('0x25')],'include':_0x2f1aab[_0xfe21('0x23')]?_['map'](_0x2f1aab[_0xfe21('0x23')],function(_0x58d5c2){return{'model':db[_0x58d5c2[_0xfe21('0x24')]],'as':_0x58d5c2['as'],'attributes':_0x58d5c2['attributes']};}):[]};}):[]};}):[]:[]})[_0xfe21('0x27')](function(_0x1d50fa){logger['info'](_0xfe21('0x28'),_0x402bea);logger['debug'](_0xfe21('0x28'),_0x402bea,JSON['stringify'](_0x1d50fa));_0x42f080(_0x1d50fa);})[_0xfe21('0x1d')](function(_0x263c5b){logger[_0xfe21('0x18')](_0xfe21('0x28'),_0x263c5b[_0xfe21('0x1a')],_0x402bea);_0x2116cd(_0x389f9c['error'](0x1f4,_0x263c5b[_0xfe21('0x1a')]));});});};exports[_0xfe21('0x29')]=function(_0xdf04ef){var _0x26fbb7=this;return new Promise(function(_0xecfbc6,_0x3bf091){return db[_0xfe21('0x1e')][_0xfe21('0x2a')]({'raw':_0xdf04ef[_0xfe21('0x20')]?_0xdf04ef[_0xfe21('0x20')][_0xfe21('0x1f')]===undefined?!![]:![]:!![],'where':_0xdf04ef[_0xfe21('0x20')]?_0xdf04ef[_0xfe21('0x20')]['where']||null:null,'attributes':_0xdf04ef[_0xfe21('0x20')]?_0xdf04ef[_0xfe21('0x20')]['attributes']||null:null,'include':_0xdf04ef['options']?_0xdf04ef[_0xfe21('0x20')][_0xfe21('0x23')]?_[_0xfe21('0x26')](_0xdf04ef[_0xfe21('0x20')][_0xfe21('0x23')],function(_0x12b42d){return{'model':db[_0x12b42d[_0xfe21('0x24')]],'as':_0x12b42d['as'],'attributes':_0x12b42d['attributes'],'include':_0x12b42d[_0xfe21('0x23')]?_['map'](_0x12b42d[_0xfe21('0x23')],function(_0x498731){return{'model':db[_0x498731[_0xfe21('0x24')]],'as':_0x498731['as'],'attributes':_0x498731[_0xfe21('0x25')],'include':_0x498731[_0xfe21('0x23')]?_[_0xfe21('0x26')](_0x498731[_0xfe21('0x23')],function(_0x109261){return{'model':db[_0x109261['model']],'as':_0x109261['as'],'attributes':_0x109261['attributes']};}):[]};}):[]};}):[]:[]})[_0xfe21('0x27')](function(_0x6bcaba){logger[_0xfe21('0x1b')](_0xfe21('0x29'),_0xdf04ef);logger[_0xfe21('0x15')](_0xfe21('0x29'),_0xdf04ef,JSON['stringify'](_0x6bcaba));_0xecfbc6(_0x6bcaba);})[_0xfe21('0x1d')](function(_0x5db410){logger[_0xfe21('0x18')](_0xfe21('0x29'),_0x5db410['message'],_0xdf04ef);_0x3bf091(_0x26fbb7[_0xfe21('0x18')](0x1f4,_0x5db410[_0xfe21('0x1a')]));});});};exports['NotifyMailAccount']=function(_0x5920d8){var _0x350be7=this;return new Promise(function(_0x9ac19a,_0x203b90){var _0x17ac5c={'body':_0x5920d8[_0xfe21('0x2b')],'channel':_0xfe21('0x2c')};var _0x3b5a7a=_[_0xfe21('0x2d')](_[_0xfe21('0x2e')](db[_0xfe21('0x2f')]['rawAttributes']),[_0xfe21('0x30'),_0xfe21('0x31'),_0xfe21('0x32'),'ListId']);var _0x2fe414=![];if(_0x5920d8[_0xfe21('0x2b')]['id']){delete _0x5920d8[_0xfe21('0x2b')]['id'];}if(_[_0xfe21('0x33')](_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x34')])){return _0x203b90(_0x350be7[_0xfe21('0x18')](0x1f4,_0xfe21('0x35')));}if(_['isNil'](_0x5920d8['body']['body'])||_0x5920d8['body'][_0xfe21('0x2b')]===''){return _0x203b90(_0x350be7[_0xfe21('0x18')](0x1f4,_0xfe21('0x36')));}if(_[_0xfe21('0x33')](_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x37')])){return _0x203b90(_0x350be7['error'](0x1f4,'mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3b5a7a));}if(!_['includes'](_0x3b5a7a,_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x37')])){return _0x203b90(_0x350be7['error'](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3b5a7a));}var _0x10f3b6={};_0x10f3b6[_0x5920d8['body']['mapKey']]=_0x5920d8['body'][_0xfe21('0x34')];return db[_0xfe21('0x1e')]['find']({'where':{'id':_0x5920d8[_0xfe21('0x38')]['id']},'include':[{'model':db[_0xfe21('0x39')],'as':'List','include':[{'model':db['CmContact'],'as':'Contacts','where':_0x10f3b6,'limit':0x1,'order':[['updatedAt',_0xfe21('0x3a')]]}]},{'model':db[_0xfe21('0x3b')],'as':_0xfe21('0x3c')},{'model':db[_0xfe21('0x3d')],'as':_0xfe21('0x3e')}]})[_0xfe21('0x27')](function(_0x3aab0e){if(_0x3aab0e&&_0x3aab0e['List']){var _0x462333=_0x3aab0e[_0xfe21('0x3f')]({'plain':!![]});_0x17ac5c[_0xfe21('0x38')]=_['omit'](_0x462333,[_0xfe21('0x3c'),'List']);_0x17ac5c[_0xfe21('0x40')]=_0x462333[_0xfe21('0x3c')];_0x17ac5c[_0xfe21('0x41')]=_0x462333[_0xfe21('0x42')];_0x17ac5c['applications']=_[_0xfe21('0x43')](_0x17ac5c[_0xfe21('0x40')],[_0xfe21('0x44')],[_0xfe21('0x45')]);if(_0x17ac5c[_0xfe21('0x38')]&&_0x17ac5c[_0xfe21('0x38')][_0xfe21('0x46')]){_0x17ac5c[_0xfe21('0x40')]['push']({'id':0x0,'priority':_0x17ac5c['applications'][_0xfe21('0x47')]+0x1,'app':_0xfe21('0x48'),'appdata':'autoclose','interval':_0xfe21('0x49')});}if(_0x17ac5c[_0xfe21('0x38')][_0xfe21('0x3c')]){delete _0x17ac5c[_0xfe21('0x38')]['Applications'];}if(_0x17ac5c[_0xfe21('0x38')][_0xfe21('0x42')]){delete _0x17ac5c['account'][_0xfe21('0x42')];}if(_0x462333['List'][_0xfe21('0x4a')][_0xfe21('0x47')]){return _0x462333[_0xfe21('0x42')]['Contacts'][0x0];}var _0x4bbce9=_[_0xfe21('0xd')](_0x5920d8[_0xfe21('0x2b')],{'firstName':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x34')],'ListId':_0x462333[_0xfe21('0x4b')]});_0x4bbce9[_0x5920d8['body'][_0xfe21('0x37')]]=_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x34')];return db['CmContact'][_0xfe21('0x4c')](_0x4bbce9);}else{throw new Error(_0xfe21('0x4d'));}})[_0xfe21('0x27')](function(_0x5501c3){_0x17ac5c['contact']=_0x5501c3;if(_0x5501c3){if(!_0x5920d8[_0xfe21('0x2b')]['message'][_0xfe21('0x4e')]){_0x5920d8['body'][_0xfe21('0x1a')][_0xfe21('0x4e')]=rs[_0xfe21('0x4f')]()+'\x20'+_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x34')];}var _0x4652dd={'ContactId':_0x5501c3['id'],'MailAccountId':_0x5920d8[_0xfe21('0x38')]['id'],'inReplyTo':_0x5920d8['body']['message'][_0xfe21('0x4e')],'to':_0x5920d8[_0xfe21('0x2b')]['message']['from'],'cc':_0x5920d8['body'][_0xfe21('0x1a')]['cc'],'subject':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')]['subject'],'attach':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x50')],'lastMsgAt':moment()[_0xfe21('0x51')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in'};return db[_0xfe21('0x52')]['find']({'where':{'closed':![],'MailAccountId':_0x5920d8[_0xfe21('0x38')]['id']},'include':[{'model':db[_0xfe21('0x53')],'as':_0xfe21('0x54'),'attributes':[_0xfe21('0x4e')],'where':{'messageId':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x55')]}}]})[_0xfe21('0x27')](function(_0x5e9c28){if(_0x5e9c28){return[_0x5e9c28,![]];}return db['MailInteraction'][_0xfe21('0x4c')](_0x4652dd)[_0xfe21('0x27')](function(_0x3acc9e){return[_0x3acc9e,!![]];});});}else{throw new Error(_0xfe21('0x56'));}})[_0xfe21('0x57')](function(_0x278eb1,_0x2c4915){if(_0x278eb1){_0x2fe414=_0x2c4915;if(!_0x2c4915){return _0x278eb1[_0xfe21('0x58')]({'inReplyTo':_0x5920d8['body'][_0xfe21('0x1a')][_0xfe21('0x4e')],'to':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x34')],'cc':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')]['cc'],'subject':_0x5920d8[_0xfe21('0x2b')]['message']['subject'],'attach':_0x278eb1[_0xfe21('0x50')]?_0x278eb1[_0xfe21('0x50')]:_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x50')],'substatus':null,'substatusAt':moment()[_0xfe21('0x51')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgAt':moment()['format'](_0xfe21('0x59')),'lastMsgDirection':'in'});}else{return _0x278eb1;}}})[_0xfe21('0x27')](function(_0x16645b){_0x17ac5c['interaction']=_0x16645b[_0xfe21('0x3f')]({'plain':!![]});_0x17ac5c['interaction']['created']=_0x2fe414;return db[_0xfe21('0x53')]['create']({'body':_0x5920d8['body'][_0xfe21('0x2b')],'MailAccountId':_0x5920d8[_0xfe21('0x38')]['id'],'MailInteractionId':_0x16645b['id'],'direction':'in','ContactId':_0x17ac5c['contact']['id'],'messageId':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x4e')],'from':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x34')],'to':_0x5920d8['body'][_0xfe21('0x1a')]['to'],'cc':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')]['cc'],'subject':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x5a')],'attach':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x50')],'Attachments':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x5b')],'originTo':_0x5920d8[_0xfe21('0x2b')]['message'][_0xfe21('0x5c')],'originCc':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')]['originCc'],'bcc':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')][_0xfe21('0x5d')],'ChatWebsiteId':_0x5920d8[_0xfe21('0x2b')][_0xfe21('0x1a')]['ChatWebsiteId']||null,'originChannel':_0x5920d8['body'][_0xfe21('0x1a')][_0xfe21('0x5e')]||_0xfe21('0x5f'),'ChatOfflineMessageId':_0x5920d8[_0xfe21('0x2b')]['message']['ChatOfflineMessageId']||null},{'include':[{'model':db[_0xfe21('0x60')],'as':_0xfe21('0x61')}]});})['then'](function(_0x1b4060){_0x17ac5c['message']=_0x1b4060;if(_0x17ac5c['interaction'][_0xfe21('0x62')]){return db[_0xfe21('0x63')][_0xfe21('0x2a')]({'attributes':['id','name'],'where':{'id':_0x17ac5c[_0xfe21('0x64')][_0xfe21('0x62')]}})['then'](function(_0x1f9881){if(_0x1f9881){_0x17ac5c[_0xfe21('0x40')][_0xfe21('0x65')]({'id':0x0,'priority':0x0,'app':_0xfe21('0x66'),'appdata':_0x1f9881[_0xfe21('0x67')]+','+(_0x17ac5c[_0xfe21('0x38')][_0xfe21('0x68')]||0xa),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0xfe21('0x69'),_0xfe21('0x6a'),_0x17ac5c);});}return respondWithRpcPromise('Start',_0xfe21('0x6a'),_0x17ac5c);})[_0xfe21('0x27')](function(_0x5b929d){_0x9ac19a(_0x5b929d);})[_0xfe21('0x1d')](function(_0x38a269){_0x203b90(_0x350be7['error'](0x1f4,_0x38a269[_0xfe21('0x1a')]));});});};