80fb83992a42ab746767ed7e3be60aa30924e5f3
[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 _0x7b05=['from','from\x20is\x20mandatory','isNil','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','account','CmList','Contacts','updatedAt','DESC','MailApplication','Applications','MailServerOut','List','get','omit','applications','list','orderBy','priority','asc','autoclose','push','*,*,*,*','length','messageId','attach','MailInteraction','create','spread','update','subject','format','YYYY-MM-DD\x20HH:mm:ss','interaction','MailMessage','attachments','originTo','Attachment','Attachments','User','name','UserId','agent','waitForTheAssignedAgent','Start','startRouting','lodash','util','moment','bluebird','ioredis','../../config/utils','../../config/logger','rpc','../../config/environment','jayson/promise','http','defaults','redis','register','request','then','info','MailAccount,\x20%s,\x20%s','debug','MailAccount,\x20%s,\x20%s,\x20%s','request\x20sent','error','code','message','catch','GetMailAccount','MailAccount','options','raw','where','attributes','limit','include','map','model','ShowMailAccount','find','stringify','body','mail','difference','keys','CmContact','rawAttributes','createdAt','CompanyId','ListId'];(function(_0x7777c6,_0x5f331d){var _0x396d4f=function(_0x4c9e8c){while(--_0x4c9e8c){_0x7777c6['push'](_0x7777c6['shift']());}};_0x396d4f(++_0x5f331d);}(_0x7b05,0x91));var _0x57b0=function(_0x11f0e3,_0x5170bc){_0x11f0e3=_0x11f0e3-0x0;var _0x2892e8=_0x7b05[_0x11f0e3];return _0x2892e8;};'use strict';var _=require(_0x57b0('0x0'));var util=require(_0x57b0('0x1'));var moment=require(_0x57b0('0x2'));var BPromise=require(_0x57b0('0x3'));var rs=require('randomstring');var fs=require('fs');var Redis=require(_0x57b0('0x4'));var db=require('../../mysqldb')['db'];var utils=require(_0x57b0('0x5'));var logger=require(_0x57b0('0x6'))(_0x57b0('0x7'));var config=require(_0x57b0('0x8'));var jayson=require(_0x57b0('0x9'));var client=jayson['client'][_0x57b0('0xa')]({'port':0x232c});config['redis']=_[_0x57b0('0xb')](config[_0x57b0('0xc')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require('./mailAccount.socket')[_0x57b0('0xd')](socket);function respondWithRpcPromise(_0x3faabd,_0x58b35b,_0x51034a){return new BPromise(function(_0x55b609,_0x2843dc){return client[_0x57b0('0xe')](_0x3faabd,_0x51034a)[_0x57b0('0xf')](function(_0x2e9979){logger[_0x57b0('0x10')](_0x57b0('0x11'),_0x58b35b,'request\x20sent');logger[_0x57b0('0x12')](_0x57b0('0x13'),_0x58b35b,_0x57b0('0x14'),JSON['stringify'](_0x2e9979));if(_0x2e9979['error']){if(_0x2e9979[_0x57b0('0x15')][_0x57b0('0x16')]===0x1f4){logger[_0x57b0('0x15')](_0x57b0('0x11'),_0x58b35b,_0x2e9979['error'][_0x57b0('0x17')]);return _0x2843dc(_0x2e9979['error'][_0x57b0('0x17')]);}logger[_0x57b0('0x15')]('MailAccount,\x20%s,\x20%s',_0x58b35b,_0x2e9979[_0x57b0('0x15')][_0x57b0('0x17')]);return _0x55b609(_0x2e9979[_0x57b0('0x15')][_0x57b0('0x17')]);}else{logger[_0x57b0('0x10')]('MailAccount,\x20%s,\x20%s',_0x58b35b,_0x57b0('0x14'));_0x55b609(_0x2e9979['result']['message']);}})[_0x57b0('0x18')](function(_0x26c6ed){logger[_0x57b0('0x15')](_0x57b0('0x11'),_0x58b35b,_0x26c6ed);_0x2843dc(_0x26c6ed);});});}exports[_0x57b0('0x19')]=function(_0x28af98){var _0x568276=this;return new Promise(function(_0x19fafd,_0x1d81ff){return db[_0x57b0('0x1a')]['findAll']({'raw':_0x28af98[_0x57b0('0x1b')]?_0x28af98[_0x57b0('0x1b')][_0x57b0('0x1c')]===undefined?!![]:![]:!![],'where':_0x28af98[_0x57b0('0x1b')]?_0x28af98[_0x57b0('0x1b')][_0x57b0('0x1d')]||null:null,'attributes':_0x28af98[_0x57b0('0x1b')]?_0x28af98[_0x57b0('0x1b')][_0x57b0('0x1e')]||null:null,'limit':_0x28af98['options']?_0x28af98['options'][_0x57b0('0x1f')]||null:null,'include':_0x28af98['options']?_0x28af98[_0x57b0('0x1b')][_0x57b0('0x20')]?_[_0x57b0('0x21')](_0x28af98[_0x57b0('0x1b')][_0x57b0('0x20')],function(_0xbf54ac){return{'model':db[_0xbf54ac[_0x57b0('0x22')]],'as':_0xbf54ac['as'],'attributes':_0xbf54ac[_0x57b0('0x1e')],'include':_0xbf54ac[_0x57b0('0x20')]?_[_0x57b0('0x21')](_0xbf54ac[_0x57b0('0x20')],function(_0x43b90f){return{'model':db[_0x43b90f[_0x57b0('0x22')]],'as':_0x43b90f['as'],'attributes':_0x43b90f['attributes'],'include':_0x43b90f[_0x57b0('0x20')]?_[_0x57b0('0x21')](_0x43b90f[_0x57b0('0x20')],function(_0x228dfe){return{'model':db[_0x228dfe['model']],'as':_0x228dfe['as'],'attributes':_0x228dfe['attributes']};}):[]};}):[]};}):[]:[]})[_0x57b0('0xf')](function(_0x3aaf57){logger[_0x57b0('0x10')]('GetMailAccount',_0x28af98);logger['debug'](_0x57b0('0x19'),_0x28af98,JSON['stringify'](_0x3aaf57));_0x19fafd(_0x3aaf57);})[_0x57b0('0x18')](function(_0x5b6c1d){logger['error'](_0x57b0('0x19'),_0x5b6c1d[_0x57b0('0x17')],_0x28af98);_0x1d81ff(_0x568276['error'](0x1f4,_0x5b6c1d[_0x57b0('0x17')]));});});};exports[_0x57b0('0x23')]=function(_0x55ca86){var _0x16d6e1=this;return new Promise(function(_0xc7ed11,_0x36526a){return db['MailAccount'][_0x57b0('0x24')]({'raw':_0x55ca86[_0x57b0('0x1b')]?_0x55ca86[_0x57b0('0x1b')][_0x57b0('0x1c')]===undefined?!![]:![]:!![],'where':_0x55ca86[_0x57b0('0x1b')]?_0x55ca86['options']['where']||null:null,'attributes':_0x55ca86['options']?_0x55ca86['options'][_0x57b0('0x1e')]||null:null,'include':_0x55ca86[_0x57b0('0x1b')]?_0x55ca86[_0x57b0('0x1b')][_0x57b0('0x20')]?_[_0x57b0('0x21')](_0x55ca86['options'][_0x57b0('0x20')],function(_0x879623){return{'model':db[_0x879623[_0x57b0('0x22')]],'as':_0x879623['as'],'attributes':_0x879623[_0x57b0('0x1e')],'include':_0x879623['include']?_[_0x57b0('0x21')](_0x879623[_0x57b0('0x20')],function(_0x4b0739){return{'model':db[_0x4b0739[_0x57b0('0x22')]],'as':_0x4b0739['as'],'attributes':_0x4b0739[_0x57b0('0x1e')],'include':_0x4b0739['include']?_['map'](_0x4b0739[_0x57b0('0x20')],function(_0x1b1d95){return{'model':db[_0x1b1d95[_0x57b0('0x22')]],'as':_0x1b1d95['as'],'attributes':_0x1b1d95[_0x57b0('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x57b0('0xf')](function(_0x43ca03){logger[_0x57b0('0x10')](_0x57b0('0x23'),_0x55ca86);logger[_0x57b0('0x12')](_0x57b0('0x23'),_0x55ca86,JSON[_0x57b0('0x25')](_0x43ca03));_0xc7ed11(_0x43ca03);})[_0x57b0('0x18')](function(_0x560ba1){logger[_0x57b0('0x15')](_0x57b0('0x23'),_0x560ba1[_0x57b0('0x17')],_0x55ca86);_0x36526a(_0x16d6e1['error'](0x1f4,_0x560ba1['message']));});});};exports['NotifyMailAccount']=function(_0x14ce66){var _0x2204c8=this;return new Promise(function(_0xc3b922,_0x197ccf){var _0x4e3210={'body':_0x14ce66[_0x57b0('0x26')],'channel':_0x57b0('0x27')};var _0x5e1e9d=_[_0x57b0('0x28')](_[_0x57b0('0x29')](db[_0x57b0('0x2a')][_0x57b0('0x2b')]),[_0x57b0('0x2c'),'updatedAt',_0x57b0('0x2d'),_0x57b0('0x2e')]);var _0xbf870b=![];if(_0x14ce66[_0x57b0('0x26')]['id']){delete _0x14ce66['body']['id'];}if(_['isNil'](_0x14ce66[_0x57b0('0x26')][_0x57b0('0x2f')])){return _0x197ccf(_0x2204c8['error'](0x1f4,_0x57b0('0x30')));}if(_[_0x57b0('0x31')](_0x14ce66[_0x57b0('0x26')]['body'])||_0x14ce66['body'][_0x57b0('0x26')]===''){return _0x197ccf(_0x2204c8[_0x57b0('0x15')](0x1f4,_0x57b0('0x32')));}if(_[_0x57b0('0x31')](_0x14ce66[_0x57b0('0x26')]['mapKey'])){return _0x197ccf(_0x2204c8[_0x57b0('0x15')](0x1f4,_0x57b0('0x33')+_0x5e1e9d));}if(!_[_0x57b0('0x34')](_0x5e1e9d,_0x14ce66[_0x57b0('0x26')][_0x57b0('0x35')])){return _0x197ccf(_0x2204c8['error'](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x5e1e9d));}var _0x4a0600={};_0x4a0600[_0x14ce66['body'][_0x57b0('0x35')]]=_0x14ce66[_0x57b0('0x26')][_0x57b0('0x2f')];return db[_0x57b0('0x1a')][_0x57b0('0x24')]({'where':{'id':_0x14ce66[_0x57b0('0x36')]['id']},'include':[{'model':db[_0x57b0('0x37')],'as':'List','include':[{'model':db[_0x57b0('0x2a')],'as':_0x57b0('0x38'),'where':_0x4a0600,'limit':0x1,'order':[[_0x57b0('0x39'),_0x57b0('0x3a')]]}]},{'model':db[_0x57b0('0x3b')],'as':_0x57b0('0x3c')},{'model':db[_0x57b0('0x3d')],'as':'Smtp'}]})[_0x57b0('0xf')](function(_0x535f22){if(_0x535f22&&_0x535f22[_0x57b0('0x3e')]){var _0x5b6737=_0x535f22[_0x57b0('0x3f')]({'plain':!![]});_0x4e3210['account']=_[_0x57b0('0x40')](_0x5b6737,[_0x57b0('0x3c'),'List']);_0x4e3210[_0x57b0('0x41')]=_0x5b6737[_0x57b0('0x3c')];_0x4e3210[_0x57b0('0x42')]=_0x5b6737['List'];_0x4e3210[_0x57b0('0x41')]=_[_0x57b0('0x43')](_0x4e3210['applications'],[_0x57b0('0x44')],[_0x57b0('0x45')]);if(_0x4e3210['account']&&_0x4e3210[_0x57b0('0x36')][_0x57b0('0x46')]){_0x4e3210[_0x57b0('0x41')][_0x57b0('0x47')]({'id':0x0,'priority':_0x4e3210[_0x57b0('0x41')]['length']+0x1,'app':'close','appdata':_0x57b0('0x46'),'interval':_0x57b0('0x48')});}if(_0x4e3210[_0x57b0('0x36')]['Applications']){delete _0x4e3210[_0x57b0('0x36')][_0x57b0('0x3c')];}if(_0x4e3210[_0x57b0('0x36')][_0x57b0('0x3e')]){delete _0x4e3210[_0x57b0('0x36')][_0x57b0('0x3e')];}if(_0x5b6737[_0x57b0('0x3e')][_0x57b0('0x38')][_0x57b0('0x49')]){return _0x5b6737[_0x57b0('0x3e')][_0x57b0('0x38')][0x0];}var _0x446d19=_[_0x57b0('0xb')](_0x14ce66[_0x57b0('0x26')],{'firstName':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x2f')],'ListId':_0x5b6737[_0x57b0('0x2e')]});_0x446d19[_0x14ce66[_0x57b0('0x26')]['mapKey']]=_0x14ce66[_0x57b0('0x26')][_0x57b0('0x2f')];return db[_0x57b0('0x2a')]['create'](_0x446d19);}else{throw new Error('List\x20not\x20found');}})[_0x57b0('0xf')](function(_0x5a33a1){_0x4e3210['contact']=_0x5a33a1;if(_0x5a33a1){if(!_0x14ce66[_0x57b0('0x26')]['message'][_0x57b0('0x4a')]){_0x14ce66['body'][_0x57b0('0x17')][_0x57b0('0x4a')]=rs['generate']()+'\x20'+_0x14ce66['body'][_0x57b0('0x17')][_0x57b0('0x2f')];}var _0x5d67e0={'ContactId':_0x5a33a1['id'],'MailAccountId':_0x14ce66[_0x57b0('0x36')]['id'],'inReplyTo':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')][_0x57b0('0x4a')],'to':_0x14ce66['body'][_0x57b0('0x17')][_0x57b0('0x2f')],'cc':_0x14ce66['body'][_0x57b0('0x17')]['cc'],'subject':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['subject'],'attach':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')][_0x57b0('0x4b')],'lastMsgAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in','lastMsgBody':_0x14ce66[_0x57b0('0x26')]['body']};return db[_0x57b0('0x4c')]['find']({'where':{'closed':![],'MailAccountId':_0x14ce66[_0x57b0('0x36')]['id']},'include':[{'model':db['MailMessage'],'as':'Messages','attributes':[_0x57b0('0x4a')],'where':{'messageId':_0x14ce66[_0x57b0('0x26')]['message']['inReplyTo']}}]})['then'](function(_0x12105f){if(_0x12105f){return[_0x12105f,![]];}return db[_0x57b0('0x4c')][_0x57b0('0x4d')](_0x5d67e0)[_0x57b0('0xf')](function(_0x5ca216){return[_0x5ca216,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0x57b0('0x4e')](function(_0x2cd2b0,_0x3917fd){if(_0x2cd2b0){_0xbf870b=_0x3917fd;if(!_0x3917fd){return _0x2cd2b0[_0x57b0('0x4f')]({'inReplyTo':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['messageId'],'to':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')][_0x57b0('0x2f')],'cc':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['cc'],'subject':_0x14ce66['body'][_0x57b0('0x17')][_0x57b0('0x50')],'attach':_0x2cd2b0[_0x57b0('0x4b')]?_0x2cd2b0[_0x57b0('0x4b')]:_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['attach'],'substatus':null,'substatusAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgAt':moment()[_0x57b0('0x51')](_0x57b0('0x52')),'lastMsgDirection':'in','lastMsgBody':_0x14ce66['body'][_0x57b0('0x26')]});}else{return _0x2cd2b0;}}})[_0x57b0('0xf')](function(_0x15253e){_0x4e3210[_0x57b0('0x53')]=_0x15253e['get']({'plain':!![]});_0x4e3210['interaction']['created']=_0xbf870b;return db[_0x57b0('0x54')][_0x57b0('0x4d')]({'body':_0x14ce66[_0x57b0('0x26')]['body'],'MailAccountId':_0x14ce66[_0x57b0('0x36')]['id'],'MailInteractionId':_0x15253e['id'],'direction':'in','ContactId':_0x4e3210['contact']['id'],'messageId':_0x14ce66['body']['message'][_0x57b0('0x4a')],'from':_0x14ce66['body'][_0x57b0('0x17')][_0x57b0('0x2f')],'to':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['to'],'cc':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['cc'],'subject':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')][_0x57b0('0x50')],'attach':_0x14ce66['body'][_0x57b0('0x17')][_0x57b0('0x4b')],'Attachments':_0x14ce66[_0x57b0('0x26')]['message'][_0x57b0('0x55')],'originTo':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')][_0x57b0('0x56')],'originCc':_0x14ce66[_0x57b0('0x26')]['message']['originCc'],'bcc':_0x14ce66[_0x57b0('0x26')][_0x57b0('0x17')]['bcc']},{'include':[{'model':db[_0x57b0('0x57')],'as':_0x57b0('0x58')}]});})[_0x57b0('0xf')](function(_0x53aec2){_0x4e3210['message']=_0x53aec2;if(_0x4e3210[_0x57b0('0x53')]['UserId']){return db[_0x57b0('0x59')][_0x57b0('0x24')]({'attributes':['id',_0x57b0('0x5a')],'where':{'id':_0x4e3210[_0x57b0('0x53')][_0x57b0('0x5b')]}})[_0x57b0('0xf')](function(_0x247e35){if(_0x247e35){_0x4e3210[_0x57b0('0x41')]['unshift']({'id':0x0,'priority':0x0,'app':_0x57b0('0x5c'),'appdata':_0x247e35[_0x57b0('0x5a')]+','+(_0x4e3210[_0x57b0('0x36')][_0x57b0('0x5d')]||0xa),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0x57b0('0x5e'),_0x57b0('0x5f'),_0x4e3210);});}return respondWithRpcPromise('Start',_0x57b0('0x5f'),_0x4e3210);})[_0x57b0('0xf')](function(_0x3ca0aa){_0xc3b922(_0x3ca0aa);})[_0x57b0('0x18')](function(_0x55c976){_0x197ccf(_0x2204c8['error'](0x1f4,_0x55c976[_0x57b0('0x17')]));});});};