Built motion from commit (unavailable).|2.5.21
[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 _0x8ce3=['register','request','then','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','message','result','catch','MailAccount','findAll','options','raw','attributes','limit','include','map','model','GetMailAccount','ShowMailAccount','find','where','NotifyMailAccount','body','mail','difference','rawAttributes','createdAt','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','CmList','CmContact','DESC','Applications','MailServerOut','List','get','account','list','applications','orderBy','priority','autoclose','push','*,*,*,*','Contacts','length','create','List\x20not\x20found','contact','messageId','generate','attach','format','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','spread','update','subject','YYYY-MM-DD\x20HH:mm:ss','interaction','created','attachments','originTo','originCc','bcc','Attachment','Attachments','UserId','User','name','waitForTheAssignedAgent','startRouting','Start','lodash','util','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','client','http','defaults','redis','localhost'];(function(_0xa3a40c,_0x531de5){var _0x48e2c4=function(_0x36b005){while(--_0x36b005){_0xa3a40c['push'](_0xa3a40c['shift']());}};_0x48e2c4(++_0x531de5);}(_0x8ce3,0xc3));var _0x38ce=function(_0x4320a7,_0x239a92){_0x4320a7=_0x4320a7-0x0;var _0x3700bf=_0x8ce3[_0x4320a7];return _0x3700bf;};'use strict';var _=require(_0x38ce('0x0'));var util=require(_0x38ce('0x1'));var moment=require('moment');var BPromise=require(_0x38ce('0x2'));var rs=require(_0x38ce('0x3'));var fs=require('fs');var Redis=require(_0x38ce('0x4'));var db=require(_0x38ce('0x5'))['db'];var utils=require(_0x38ce('0x6'));var logger=require(_0x38ce('0x7'))(_0x38ce('0x8'));var config=require(_0x38ce('0x9'));var jayson=require('jayson/promise');var client=jayson[_0x38ce('0xa')][_0x38ce('0xb')]({'port':0x232c});config['redis']=_[_0x38ce('0xc')](config[_0x38ce('0xd')],{'host':_0x38ce('0xe'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x38ce('0xd')]));require('./mailAccount.socket')[_0x38ce('0xf')](socket);function respondWithRpcPromise(_0x4bd97b,_0x133bd5,_0x31c2e1){return new BPromise(function(_0xa9931b,_0x2c0cab){return client[_0x38ce('0x10')](_0x4bd97b,_0x31c2e1)[_0x38ce('0x11')](function(_0x2a9045){logger[_0x38ce('0x12')](_0x38ce('0x13'),_0x133bd5,_0x38ce('0x14'));logger[_0x38ce('0x15')](_0x38ce('0x16'),_0x133bd5,_0x38ce('0x14'),JSON[_0x38ce('0x17')](_0x2a9045));if(_0x2a9045[_0x38ce('0x18')]){if(_0x2a9045[_0x38ce('0x18')]['code']===0x1f4){logger['error'](_0x38ce('0x13'),_0x133bd5,_0x2a9045[_0x38ce('0x18')][_0x38ce('0x19')]);return _0x2c0cab(_0x2a9045['error'][_0x38ce('0x19')]);}logger['error'](_0x38ce('0x13'),_0x133bd5,_0x2a9045[_0x38ce('0x18')]['message']);return _0xa9931b(_0x2a9045[_0x38ce('0x18')][_0x38ce('0x19')]);}else{logger[_0x38ce('0x12')]('MailAccount,\x20%s,\x20%s',_0x133bd5,_0x38ce('0x14'));_0xa9931b(_0x2a9045[_0x38ce('0x1a')][_0x38ce('0x19')]);}})[_0x38ce('0x1b')](function(_0x3166ab){logger[_0x38ce('0x18')](_0x38ce('0x13'),_0x133bd5,_0x3166ab);_0x2c0cab(_0x3166ab);});});}exports['GetMailAccount']=function(_0x2a9447){var _0x34a8c7=this;return new Promise(function(_0x280586,_0x303391){return db[_0x38ce('0x1c')][_0x38ce('0x1d')]({'raw':_0x2a9447[_0x38ce('0x1e')]?_0x2a9447[_0x38ce('0x1e')][_0x38ce('0x1f')]===undefined?!![]:![]:!![],'where':_0x2a9447[_0x38ce('0x1e')]?_0x2a9447[_0x38ce('0x1e')]['where']||null:null,'attributes':_0x2a9447[_0x38ce('0x1e')]?_0x2a9447[_0x38ce('0x1e')][_0x38ce('0x20')]||null:null,'limit':_0x2a9447[_0x38ce('0x1e')]?_0x2a9447['options'][_0x38ce('0x21')]||null:null,'include':_0x2a9447[_0x38ce('0x1e')]?_0x2a9447['options'][_0x38ce('0x22')]?_[_0x38ce('0x23')](_0x2a9447[_0x38ce('0x1e')]['include'],function(_0x4a7e8e){return{'model':db[_0x4a7e8e[_0x38ce('0x24')]],'as':_0x4a7e8e['as'],'attributes':_0x4a7e8e[_0x38ce('0x20')],'include':_0x4a7e8e[_0x38ce('0x22')]?_[_0x38ce('0x23')](_0x4a7e8e[_0x38ce('0x22')],function(_0x2a59fe){return{'model':db[_0x2a59fe[_0x38ce('0x24')]],'as':_0x2a59fe['as'],'attributes':_0x2a59fe[_0x38ce('0x20')],'include':_0x2a59fe[_0x38ce('0x22')]?_[_0x38ce('0x23')](_0x2a59fe[_0x38ce('0x22')],function(_0x381703){return{'model':db[_0x381703['model']],'as':_0x381703['as'],'attributes':_0x381703[_0x38ce('0x20')]};}):[]};}):[]};}):[]:[]})[_0x38ce('0x11')](function(_0x5b4a7a){logger[_0x38ce('0x12')](_0x38ce('0x25'),_0x2a9447);logger['debug'](_0x38ce('0x25'),_0x2a9447,JSON[_0x38ce('0x17')](_0x5b4a7a));_0x280586(_0x5b4a7a);})['catch'](function(_0x28d775){logger['error'](_0x38ce('0x25'),_0x28d775[_0x38ce('0x19')],_0x2a9447);_0x303391(_0x34a8c7[_0x38ce('0x18')](0x1f4,_0x28d775[_0x38ce('0x19')]));});});};exports[_0x38ce('0x26')]=function(_0x23019f){var _0x34262a=this;return new Promise(function(_0x2f5695,_0x4ec0d3){return db['MailAccount'][_0x38ce('0x27')]({'raw':_0x23019f[_0x38ce('0x1e')]?_0x23019f[_0x38ce('0x1e')][_0x38ce('0x1f')]===undefined?!![]:![]:!![],'where':_0x23019f[_0x38ce('0x1e')]?_0x23019f['options'][_0x38ce('0x28')]||null:null,'attributes':_0x23019f[_0x38ce('0x1e')]?_0x23019f[_0x38ce('0x1e')][_0x38ce('0x20')]||null:null,'include':_0x23019f[_0x38ce('0x1e')]?_0x23019f[_0x38ce('0x1e')][_0x38ce('0x22')]?_['map'](_0x23019f[_0x38ce('0x1e')][_0x38ce('0x22')],function(_0x5b8c69){return{'model':db[_0x5b8c69[_0x38ce('0x24')]],'as':_0x5b8c69['as'],'attributes':_0x5b8c69[_0x38ce('0x20')],'include':_0x5b8c69[_0x38ce('0x22')]?_[_0x38ce('0x23')](_0x5b8c69[_0x38ce('0x22')],function(_0x3d6838){return{'model':db[_0x3d6838[_0x38ce('0x24')]],'as':_0x3d6838['as'],'attributes':_0x3d6838[_0x38ce('0x20')],'include':_0x3d6838['include']?_[_0x38ce('0x23')](_0x3d6838[_0x38ce('0x22')],function(_0x2314a2){return{'model':db[_0x2314a2[_0x38ce('0x24')]],'as':_0x2314a2['as'],'attributes':_0x2314a2['attributes']};}):[]};}):[]};}):[]:[]})[_0x38ce('0x11')](function(_0x54a15a){logger[_0x38ce('0x12')](_0x38ce('0x26'),_0x23019f);logger[_0x38ce('0x15')](_0x38ce('0x26'),_0x23019f,JSON[_0x38ce('0x17')](_0x54a15a));_0x2f5695(_0x54a15a);})[_0x38ce('0x1b')](function(_0x4a6d2b){logger[_0x38ce('0x18')]('ShowMailAccount',_0x4a6d2b[_0x38ce('0x19')],_0x23019f);_0x4ec0d3(_0x34262a[_0x38ce('0x18')](0x1f4,_0x4a6d2b[_0x38ce('0x19')]));});});};exports[_0x38ce('0x29')]=function(_0x277e94){var _0x1476e9=this;return new Promise(function(_0x5209ee,_0x14fb63){var _0x1e0574={'body':_0x277e94[_0x38ce('0x2a')],'channel':_0x38ce('0x2b')};var _0xd4bfe9=_[_0x38ce('0x2c')](_['keys'](db['CmContact'][_0x38ce('0x2d')]),[_0x38ce('0x2e'),_0x38ce('0x2f'),_0x38ce('0x30'),_0x38ce('0x31')]);var _0x3966d2=![];if(_0x277e94[_0x38ce('0x2a')]['id']){delete _0x277e94['body']['id'];}if(_[_0x38ce('0x32')](_0x277e94[_0x38ce('0x2a')]['from'])){return _0x14fb63(_0x1476e9[_0x38ce('0x18')](0x1f4,_0x38ce('0x33')));}if(_[_0x38ce('0x32')](_0x277e94['body'][_0x38ce('0x2a')])||_0x277e94[_0x38ce('0x2a')][_0x38ce('0x2a')]===''){return _0x14fb63(_0x1476e9[_0x38ce('0x18')](0x1f4,_0x38ce('0x34')));}if(_[_0x38ce('0x32')](_0x277e94['body'][_0x38ce('0x35')])){return _0x14fb63(_0x1476e9[_0x38ce('0x18')](0x1f4,_0x38ce('0x36')+_0xd4bfe9));}if(!_[_0x38ce('0x37')](_0xd4bfe9,_0x277e94[_0x38ce('0x2a')][_0x38ce('0x35')])){return _0x14fb63(_0x1476e9['error'](0x1f4,_0x38ce('0x38')+_0xd4bfe9));}var _0x1847e5={};_0x1847e5[_0x277e94['body']['mapKey']]=_0x277e94['body'][_0x38ce('0x39')];return db[_0x38ce('0x1c')]['find']({'where':{'id':_0x277e94['account']['id']},'include':[{'model':db[_0x38ce('0x3a')],'as':'List','include':[{'model':db[_0x38ce('0x3b')],'as':'Contacts','where':_0x1847e5,'limit':0x1,'order':[[_0x38ce('0x2f'),_0x38ce('0x3c')]]}]},{'model':db['MailApplication'],'as':_0x38ce('0x3d')},{'model':db[_0x38ce('0x3e')],'as':'Smtp'}]})[_0x38ce('0x11')](function(_0x3bbd00){if(_0x3bbd00&&_0x3bbd00[_0x38ce('0x3f')]){var _0x5ce80e=_0x3bbd00[_0x38ce('0x40')]({'plain':!![]});_0x1e0574[_0x38ce('0x41')]=_['omit'](_0x5ce80e,[_0x38ce('0x3d'),_0x38ce('0x3f')]);_0x1e0574['applications']=_0x5ce80e[_0x38ce('0x3d')];_0x1e0574[_0x38ce('0x42')]=_0x5ce80e[_0x38ce('0x3f')];_0x1e0574[_0x38ce('0x43')]=_[_0x38ce('0x44')](_0x1e0574[_0x38ce('0x43')],[_0x38ce('0x45')],['asc']);if(_0x1e0574[_0x38ce('0x41')]&&_0x1e0574[_0x38ce('0x41')][_0x38ce('0x46')]){_0x1e0574[_0x38ce('0x43')][_0x38ce('0x47')]({'id':0x0,'priority':_0x1e0574[_0x38ce('0x43')]['length']+0x1,'app':'close','appdata':_0x38ce('0x46'),'interval':_0x38ce('0x48')});}if(_0x1e0574[_0x38ce('0x41')][_0x38ce('0x3d')]){delete _0x1e0574[_0x38ce('0x41')][_0x38ce('0x3d')];}if(_0x1e0574['account']['List']){delete _0x1e0574[_0x38ce('0x41')]['List'];}if(_0x5ce80e['List'][_0x38ce('0x49')][_0x38ce('0x4a')]){return _0x5ce80e[_0x38ce('0x3f')][_0x38ce('0x49')][0x0];}var _0xd031c0=_['defaults'](_0x277e94[_0x38ce('0x2a')],{'firstName':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x39')],'ListId':_0x5ce80e[_0x38ce('0x31')]});_0xd031c0[_0x277e94[_0x38ce('0x2a')][_0x38ce('0x35')]]=_0x277e94[_0x38ce('0x2a')][_0x38ce('0x39')];return db[_0x38ce('0x3b')][_0x38ce('0x4b')](_0xd031c0);}else{throw new Error(_0x38ce('0x4c'));}})['then'](function(_0x5d1df1){_0x1e0574[_0x38ce('0x4d')]=_0x5d1df1;if(_0x5d1df1){if(!_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x4e')]){_0x277e94['body'][_0x38ce('0x19')][_0x38ce('0x4e')]=rs[_0x38ce('0x4f')]()+'\x20'+_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x39')];}var _0x1534ca={'ContactId':_0x5d1df1['id'],'MailAccountId':_0x277e94['account']['id'],'inReplyTo':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')]['messageId'],'to':_0x277e94['body'][_0x38ce('0x19')][_0x38ce('0x39')],'cc':_0x277e94[_0x38ce('0x2a')]['message']['cc'],'subject':_0x277e94['body'][_0x38ce('0x19')]['subject'],'attach':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x50')],'lastMsgAt':moment()[_0x38ce('0x51')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in','lastMsgBody':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x2a')]};return db[_0x38ce('0x52')]['find']({'where':{'closed':![],'MailAccountId':_0x277e94[_0x38ce('0x41')]['id']},'include':[{'model':db[_0x38ce('0x53')],'as':_0x38ce('0x54'),'attributes':['messageId'],'where':{'messageId':_0x277e94[_0x38ce('0x2a')]['message'][_0x38ce('0x55')]}}]})['then'](function(_0x146999){if(_0x146999){return[_0x146999,![]];}return db[_0x38ce('0x52')][_0x38ce('0x4b')](_0x1534ca)[_0x38ce('0x11')](function(_0x587e62){return[_0x587e62,!![]];});});}else{throw new Error(_0x38ce('0x56'));}})[_0x38ce('0x57')](function(_0x425002,_0x2ff08b){if(_0x425002){_0x3966d2=_0x2ff08b;if(!_0x2ff08b){return _0x425002[_0x38ce('0x58')]({'inReplyTo':_0x277e94[_0x38ce('0x2a')]['message'][_0x38ce('0x4e')],'to':_0x277e94['body'][_0x38ce('0x19')][_0x38ce('0x39')],'cc':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')]['cc'],'subject':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x59')],'attach':_0x425002[_0x38ce('0x50')]?_0x425002['attach']:_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x50')],'substatus':null,'substatusAt':moment()[_0x38ce('0x51')](_0x38ce('0x5a')),'lastMsgAt':moment()[_0x38ce('0x51')](_0x38ce('0x5a')),'lastMsgDirection':'in','lastMsgBody':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x2a')]});}else{return _0x425002;}}})[_0x38ce('0x11')](function(_0x79b918){_0x1e0574[_0x38ce('0x5b')]=_0x79b918[_0x38ce('0x40')]({'plain':!![]});_0x1e0574[_0x38ce('0x5b')][_0x38ce('0x5c')]=_0x3966d2;return db[_0x38ce('0x53')][_0x38ce('0x4b')]({'body':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x2a')],'MailAccountId':_0x277e94[_0x38ce('0x41')]['id'],'MailInteractionId':_0x79b918['id'],'direction':'in','ContactId':_0x1e0574['contact']['id'],'messageId':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x4e')],'from':_0x277e94['body']['message'][_0x38ce('0x39')],'to':_0x277e94[_0x38ce('0x2a')]['message']['to'],'cc':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')]['cc'],'subject':_0x277e94['body']['message'][_0x38ce('0x59')],'attach':_0x277e94[_0x38ce('0x2a')]['message'][_0x38ce('0x50')],'Attachments':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x5d')],'originTo':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x5e')],'originCc':_0x277e94[_0x38ce('0x2a')]['message'][_0x38ce('0x5f')],'bcc':_0x277e94[_0x38ce('0x2a')][_0x38ce('0x19')][_0x38ce('0x60')]},{'include':[{'model':db[_0x38ce('0x61')],'as':_0x38ce('0x62')}]});})[_0x38ce('0x11')](function(_0x595d81){_0x1e0574['message']=_0x595d81;if(_0x1e0574['interaction'][_0x38ce('0x63')]){return db[_0x38ce('0x64')][_0x38ce('0x27')]({'attributes':['id',_0x38ce('0x65')],'where':{'id':_0x1e0574[_0x38ce('0x5b')][_0x38ce('0x63')]}})[_0x38ce('0x11')](function(_0x4b2dfb){if(_0x4b2dfb){_0x1e0574[_0x38ce('0x43')]['unshift']({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x4b2dfb['name']+','+(_0x1e0574[_0x38ce('0x41')][_0x38ce('0x66')]||0xa),'interval':_0x38ce('0x48')});}return respondWithRpcPromise('Start',_0x38ce('0x67'),_0x1e0574);});}return respondWithRpcPromise(_0x38ce('0x68'),_0x38ce('0x67'),_0x1e0574);})[_0x38ce('0x11')](function(_0x32a8e3){_0x5209ee(_0x32a8e3);})['catch'](function(_0x19b414){_0x14fb63(_0x1476e9['error'](0x1f4,_0x19b414[_0x38ce('0x19')]));});});};