42374cd674cd7e9a74bc9cde97fd56c8adc6dcb9
[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 _0x16f6=['find','ShowMailAccount','NotifyMailAccount','body','mail','keys','CmContact','rawAttributes','updatedAt','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','account','CmList','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','List','list','applications','priority','asc','push','length','*,*,*,*','create','List\x20not\x20found','contact','generate','messageId','subject','attach','format','YYYY-MM-DD\x20HH:mm:ss','Messages','inReplyTo','MailInteraction','spread','update','get','interaction','created','MailMessage','originTo','originCc','bcc','Attachment','Attachments','UserId','User','name','unshift','agent','waitForTheAssignedAgent','Start','startRouting','lodash','util','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','http','redis','defaults','socket.io-emitter','./mailAccount.socket','then','MailAccount,\x20%s,\x20%s','request\x20sent','debug','stringify','error','message','info','result','catch','GetMailAccount','MailAccount','findAll','options','raw','where','map','include','attributes','model'];(function(_0x2237bd,_0x203d6b){var _0x364086=function(_0x5ec691){while(--_0x5ec691){_0x2237bd['push'](_0x2237bd['shift']());}};_0x364086(++_0x203d6b);}(_0x16f6,0xa6));var _0x616f=function(_0x53b938,_0x1ef2f1){_0x53b938=_0x53b938-0x0;var _0xf029bd=_0x16f6[_0x53b938];return _0xf029bd;};'use strict';var _=require(_0x616f('0x0'));var util=require(_0x616f('0x1'));var moment=require('moment');var BPromise=require(_0x616f('0x2'));var rs=require(_0x616f('0x3'));var fs=require('fs');var Redis=require(_0x616f('0x4'));var db=require(_0x616f('0x5'))['db'];var utils=require(_0x616f('0x6'));var logger=require('../../config/logger')(_0x616f('0x7'));var config=require(_0x616f('0x8'));var jayson=require('jayson/promise');var client=jayson['client'][_0x616f('0x9')]({'port':0x232c});config[_0x616f('0xa')]=_[_0x616f('0xb')](config[_0x616f('0xa')],{'host':'localhost','port':0x18eb});var socket=require(_0x616f('0xc'))(new Redis(config['redis']));require(_0x616f('0xd'))['register'](socket);function respondWithRpcPromise(_0x219f1e,_0x4491c0,_0x4d094d){return new BPromise(function(_0x47bad5,_0x1e327b){return client['request'](_0x219f1e,_0x4d094d)[_0x616f('0xe')](function(_0x337e44){logger['info'](_0x616f('0xf'),_0x4491c0,_0x616f('0x10'));logger[_0x616f('0x11')]('MailAccount,\x20%s,\x20%s,\x20%s',_0x4491c0,_0x616f('0x10'),JSON[_0x616f('0x12')](_0x337e44));if(_0x337e44['error']){if(_0x337e44[_0x616f('0x13')]['code']===0x1f4){logger[_0x616f('0x13')]('MailAccount,\x20%s,\x20%s',_0x4491c0,_0x337e44[_0x616f('0x13')]['message']);return _0x1e327b(_0x337e44[_0x616f('0x13')][_0x616f('0x14')]);}logger[_0x616f('0x13')](_0x616f('0xf'),_0x4491c0,_0x337e44['error'][_0x616f('0x14')]);return _0x47bad5(_0x337e44[_0x616f('0x13')]['message']);}else{logger[_0x616f('0x15')]('MailAccount,\x20%s,\x20%s',_0x4491c0,_0x616f('0x10'));_0x47bad5(_0x337e44[_0x616f('0x16')][_0x616f('0x14')]);}})[_0x616f('0x17')](function(_0x2529ca){logger[_0x616f('0x13')]('MailAccount,\x20%s,\x20%s',_0x4491c0,_0x2529ca);_0x1e327b(_0x2529ca);});});}exports[_0x616f('0x18')]=function(_0x225b87){var _0x21e0ad=this;return new Promise(function(_0x4bdbc0,_0x29d790){return db[_0x616f('0x19')][_0x616f('0x1a')]({'raw':_0x225b87[_0x616f('0x1b')]?_0x225b87['options'][_0x616f('0x1c')]===undefined?!![]:![]:!![],'where':_0x225b87[_0x616f('0x1b')]?_0x225b87['options'][_0x616f('0x1d')]||null:null,'attributes':_0x225b87[_0x616f('0x1b')]?_0x225b87['options']['attributes']||null:null,'limit':_0x225b87[_0x616f('0x1b')]?_0x225b87['options']['limit']||null:null,'include':_0x225b87[_0x616f('0x1b')]?_0x225b87['options']['include']?_[_0x616f('0x1e')](_0x225b87['options'][_0x616f('0x1f')],function(_0x6a6253){return{'model':db[_0x6a6253['model']],'as':_0x6a6253['as'],'attributes':_0x6a6253[_0x616f('0x20')],'include':_0x6a6253['include']?_[_0x616f('0x1e')](_0x6a6253[_0x616f('0x1f')],function(_0x575a8b){return{'model':db[_0x575a8b[_0x616f('0x21')]],'as':_0x575a8b['as'],'attributes':_0x575a8b['attributes'],'include':_0x575a8b['include']?_['map'](_0x575a8b['include'],function(_0x3730bb){return{'model':db[_0x3730bb['model']],'as':_0x3730bb['as'],'attributes':_0x3730bb[_0x616f('0x20')]};}):[]};}):[]};}):[]:[]})[_0x616f('0xe')](function(_0x73b466){logger[_0x616f('0x15')](_0x616f('0x18'),_0x225b87);logger['debug']('GetMailAccount',_0x225b87,JSON[_0x616f('0x12')](_0x73b466));_0x4bdbc0(_0x73b466);})[_0x616f('0x17')](function(_0x1f2797){logger[_0x616f('0x13')]('GetMailAccount',_0x1f2797['message'],_0x225b87);_0x29d790(_0x21e0ad[_0x616f('0x13')](0x1f4,_0x1f2797['message']));});});};exports['ShowMailAccount']=function(_0x650fe4){var _0x4eaf74=this;return new Promise(function(_0x16efbf,_0x12b8f3){return db[_0x616f('0x19')][_0x616f('0x22')]({'raw':_0x650fe4[_0x616f('0x1b')]?_0x650fe4[_0x616f('0x1b')][_0x616f('0x1c')]===undefined?!![]:![]:!![],'where':_0x650fe4['options']?_0x650fe4['options'][_0x616f('0x1d')]||null:null,'attributes':_0x650fe4['options']?_0x650fe4[_0x616f('0x1b')][_0x616f('0x20')]||null:null,'include':_0x650fe4['options']?_0x650fe4['options'][_0x616f('0x1f')]?_[_0x616f('0x1e')](_0x650fe4[_0x616f('0x1b')][_0x616f('0x1f')],function(_0x5719d7){return{'model':db[_0x5719d7[_0x616f('0x21')]],'as':_0x5719d7['as'],'attributes':_0x5719d7[_0x616f('0x20')],'include':_0x5719d7[_0x616f('0x1f')]?_[_0x616f('0x1e')](_0x5719d7[_0x616f('0x1f')],function(_0x4522e8){return{'model':db[_0x4522e8['model']],'as':_0x4522e8['as'],'attributes':_0x4522e8[_0x616f('0x20')],'include':_0x4522e8[_0x616f('0x1f')]?_[_0x616f('0x1e')](_0x4522e8[_0x616f('0x1f')],function(_0x19b24b){return{'model':db[_0x19b24b[_0x616f('0x21')]],'as':_0x19b24b['as'],'attributes':_0x19b24b[_0x616f('0x20')]};}):[]};}):[]};}):[]:[]})[_0x616f('0xe')](function(_0x19d3fe){logger['info']('ShowMailAccount',_0x650fe4);logger[_0x616f('0x11')]('ShowMailAccount',_0x650fe4,JSON[_0x616f('0x12')](_0x19d3fe));_0x16efbf(_0x19d3fe);})[_0x616f('0x17')](function(_0x5a6c12){logger[_0x616f('0x13')](_0x616f('0x23'),_0x5a6c12['message'],_0x650fe4);_0x12b8f3(_0x4eaf74[_0x616f('0x13')](0x1f4,_0x5a6c12[_0x616f('0x14')]));});});};exports[_0x616f('0x24')]=function(_0x1dc189){var _0x35ebc6=this;return new Promise(function(_0x1f8aed,_0x1d9a40){var _0x5344d9={'body':_0x1dc189[_0x616f('0x25')],'channel':_0x616f('0x26')};var _0x89e0b2=_['difference'](_[_0x616f('0x27')](db[_0x616f('0x28')][_0x616f('0x29')]),['createdAt',_0x616f('0x2a'),_0x616f('0x2b'),_0x616f('0x2c')]);var _0x517d24=![];if(_0x1dc189[_0x616f('0x25')]['id']){delete _0x1dc189[_0x616f('0x25')]['id'];}if(_[_0x616f('0x2d')](_0x1dc189[_0x616f('0x25')][_0x616f('0x2e')])){return _0x1d9a40(_0x35ebc6[_0x616f('0x13')](0x1f4,_0x616f('0x2f')));}if(_[_0x616f('0x2d')](_0x1dc189['body'][_0x616f('0x25')])||_0x1dc189[_0x616f('0x25')][_0x616f('0x25')]===''){return _0x1d9a40(_0x35ebc6[_0x616f('0x13')](0x1f4,_0x616f('0x30')));}if(_[_0x616f('0x2d')](_0x1dc189['body']['mapKey'])){return _0x1d9a40(_0x35ebc6[_0x616f('0x13')](0x1f4,_0x616f('0x31')+_0x89e0b2));}if(!_[_0x616f('0x32')](_0x89e0b2,_0x1dc189[_0x616f('0x25')][_0x616f('0x33')])){return _0x1d9a40(_0x35ebc6[_0x616f('0x13')](0x1f4,_0x616f('0x34')+_0x89e0b2));}var _0xef187b={};_0xef187b[_0x1dc189[_0x616f('0x25')][_0x616f('0x33')]]=_0x1dc189['body']['from'];return db[_0x616f('0x19')][_0x616f('0x22')]({'where':{'id':_0x1dc189[_0x616f('0x35')]['id']},'include':[{'model':db[_0x616f('0x36')],'as':'List','include':[{'model':db[_0x616f('0x28')],'as':_0x616f('0x37'),'where':_0xef187b,'limit':0x1,'order':[[_0x616f('0x2a'),_0x616f('0x38')]]}]},{'model':db[_0x616f('0x39')],'as':_0x616f('0x3a')},{'model':db[_0x616f('0x3b')],'as':_0x616f('0x3c')}]})[_0x616f('0xe')](function(_0x48eda2){if(_0x48eda2&&_0x48eda2[_0x616f('0x3d')]){var _0x5ec87f=_0x48eda2['get']({'plain':!![]});_0x5344d9[_0x616f('0x35')]=_['omit'](_0x5ec87f,[_0x616f('0x3a'),'List']);_0x5344d9['applications']=_0x5ec87f['Applications'];_0x5344d9[_0x616f('0x3e')]=_0x5ec87f['List'];_0x5344d9[_0x616f('0x3f')]=_['orderBy'](_0x5344d9[_0x616f('0x3f')],[_0x616f('0x40')],[_0x616f('0x41')]);if(_0x5344d9[_0x616f('0x35')]&&_0x5344d9[_0x616f('0x35')]['autoclose']){_0x5344d9[_0x616f('0x3f')][_0x616f('0x42')]({'id':0x0,'priority':_0x5344d9['applications'][_0x616f('0x43')]+0x1,'app':'close','appdata':'autoclose','interval':_0x616f('0x44')});}if(_0x5344d9[_0x616f('0x35')][_0x616f('0x3a')]){delete _0x5344d9[_0x616f('0x35')][_0x616f('0x3a')];}if(_0x5344d9[_0x616f('0x35')][_0x616f('0x3d')]){delete _0x5344d9[_0x616f('0x35')][_0x616f('0x3d')];}if(_0x5ec87f[_0x616f('0x3d')][_0x616f('0x37')][_0x616f('0x43')]){return _0x5ec87f[_0x616f('0x3d')]['Contacts'][0x0];}var _0x576b1d=_[_0x616f('0xb')](_0x1dc189[_0x616f('0x25')],{'firstName':_0x1dc189[_0x616f('0x25')]['from'],'ListId':_0x5ec87f[_0x616f('0x2c')]});_0x576b1d[_0x1dc189[_0x616f('0x25')][_0x616f('0x33')]]=_0x1dc189['body'][_0x616f('0x2e')];return db[_0x616f('0x28')][_0x616f('0x45')](_0x576b1d);}else{throw new Error(_0x616f('0x46'));}})[_0x616f('0xe')](function(_0x4a692f){_0x5344d9[_0x616f('0x47')]=_0x4a692f;if(_0x4a692f){if(!_0x1dc189['body']['message']['messageId']){_0x1dc189[_0x616f('0x25')]['message']['messageId']=rs[_0x616f('0x48')]()+'\x20'+_0x1dc189[_0x616f('0x25')]['message'][_0x616f('0x2e')];}var _0x26f911={'ContactId':_0x4a692f['id'],'MailAccountId':_0x1dc189[_0x616f('0x35')]['id'],'inReplyTo':_0x1dc189[_0x616f('0x25')]['message'][_0x616f('0x49')],'to':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')]['from'],'cc':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')]['cc'],'subject':_0x1dc189['body'][_0x616f('0x14')][_0x616f('0x4a')],'attach':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')][_0x616f('0x4b')],'lastMsgAt':moment()[_0x616f('0x4c')](_0x616f('0x4d')),'lastMsgDirection':'in','lastMsgBody':_0x1dc189['body']['body']};return db['MailInteraction'][_0x616f('0x22')]({'where':{'closed':![],'MailAccountId':_0x1dc189[_0x616f('0x35')]['id']},'include':[{'model':db['MailMessage'],'as':_0x616f('0x4e'),'attributes':[_0x616f('0x49')],'where':{'messageId':_0x1dc189['body']['message'][_0x616f('0x4f')]}}]})[_0x616f('0xe')](function(_0xb31512){if(_0xb31512){return[_0xb31512,![]];}return db[_0x616f('0x50')][_0x616f('0x45')](_0x26f911)[_0x616f('0xe')](function(_0x491443){return[_0x491443,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0x616f('0x51')](function(_0x143470,_0x5a199f){if(_0x143470){_0x517d24=_0x5a199f;if(!_0x5a199f){return _0x143470[_0x616f('0x52')]({'inReplyTo':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')][_0x616f('0x49')],'to':_0x1dc189[_0x616f('0x25')]['message']['from'],'cc':_0x1dc189[_0x616f('0x25')]['message']['cc'],'subject':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')][_0x616f('0x4a')],'attach':_0x143470[_0x616f('0x4b')]?_0x143470[_0x616f('0x4b')]:_0x1dc189[_0x616f('0x25')][_0x616f('0x14')]['attach'],'substatus':null,'substatusAt':moment()[_0x616f('0x4c')](_0x616f('0x4d')),'lastMsgAt':moment()[_0x616f('0x4c')](_0x616f('0x4d')),'lastMsgDirection':'in','lastMsgBody':_0x1dc189[_0x616f('0x25')][_0x616f('0x25')]});}else{return _0x143470;}}})[_0x616f('0xe')](function(_0x18a121){_0x5344d9['interaction']=_0x18a121[_0x616f('0x53')]({'plain':!![]});_0x5344d9[_0x616f('0x54')][_0x616f('0x55')]=_0x517d24;return db[_0x616f('0x56')][_0x616f('0x45')]({'body':_0x1dc189[_0x616f('0x25')][_0x616f('0x25')],'MailAccountId':_0x1dc189[_0x616f('0x35')]['id'],'MailInteractionId':_0x18a121['id'],'direction':'in','ContactId':_0x5344d9[_0x616f('0x47')]['id'],'messageId':_0x1dc189['body'][_0x616f('0x14')][_0x616f('0x49')],'from':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')][_0x616f('0x2e')],'to':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')]['to'],'cc':_0x1dc189['body']['message']['cc'],'subject':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')]['subject'],'attach':_0x1dc189['body']['message']['attach'],'Attachments':_0x1dc189['body']['message']['attachments'],'originTo':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')][_0x616f('0x57')],'originCc':_0x1dc189['body'][_0x616f('0x14')][_0x616f('0x58')],'bcc':_0x1dc189[_0x616f('0x25')][_0x616f('0x14')][_0x616f('0x59')]},{'include':[{'model':db[_0x616f('0x5a')],'as':_0x616f('0x5b')}]});})[_0x616f('0xe')](function(_0x447d83){_0x5344d9[_0x616f('0x14')]=_0x447d83;if(_0x5344d9[_0x616f('0x54')][_0x616f('0x5c')]){return db[_0x616f('0x5d')][_0x616f('0x22')]({'attributes':['id',_0x616f('0x5e')],'where':{'id':_0x5344d9['interaction'][_0x616f('0x5c')]}})[_0x616f('0xe')](function(_0x128a07){if(_0x128a07){_0x5344d9[_0x616f('0x3f')][_0x616f('0x5f')]({'id':0x0,'priority':0x0,'app':_0x616f('0x60'),'appdata':_0x128a07[_0x616f('0x5e')]+','+(_0x5344d9[_0x616f('0x35')][_0x616f('0x61')]||0xa),'interval':_0x616f('0x44')});}return respondWithRpcPromise(_0x616f('0x62'),_0x616f('0x63'),_0x5344d9);});}return respondWithRpcPromise(_0x616f('0x62'),_0x616f('0x63'),_0x5344d9);})['then'](function(_0x5376ea){_0x1f8aed(_0x5376ea);})['catch'](function(_0x398d8c){_0x1d9a40(_0x35ebc6[_0x616f('0x13')](0x1f4,_0x398d8c['message']));});});};