5d7d8936881844e9d3087ff897a50971d40d16d8
[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 _0xdb77=['message','result','GetMailAccount','MailAccount','findAll','options','raw','attributes','include','map','model','then','catch','ShowMailAccount','find','where','NotifyMailAccount','body','mail','difference','keys','rawAttributes','createdAt','updatedAt','CompanyId','ListId','isNil','from','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','account','List','CmContact','Applications','MailServerOut','get','omit','list','orderBy','applications','asc','length','close','autoclose','Contacts','create','contact','messageId','generate','subject','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','attach','interaction','created','originTo','originCc','ChatWebsiteId','originChannel','Email','ChatOfflineMessageId','Attachment','Attachments','UserId','User','name','unshift','agent','*,*,*,*','Start','startRouting','lodash','util','moment','bluebird','randomstring','ioredis','../../config/utils','../../config/logger','jayson/promise','client','redis','defaults','localhost','socket.io-emitter','request','info','MailAccount,\x20%s,\x20%s','debug','MailAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code'];(function(_0x5e05fe,_0x254ea2){var _0x3fbab3=function(_0x35ed15){while(--_0x35ed15){_0x5e05fe['push'](_0x5e05fe['shift']());}};_0x3fbab3(++_0x254ea2);}(_0xdb77,0x1e2));var _0x7db7=function(_0x44e12e,_0x48ac87){_0x44e12e=_0x44e12e-0x0;var _0x566404=_0xdb77[_0x44e12e];return _0x566404;};'use strict';var _=require(_0x7db7('0x0'));var util=require(_0x7db7('0x1'));var moment=require(_0x7db7('0x2'));var BPromise=require(_0x7db7('0x3'));var rs=require(_0x7db7('0x4'));var fs=require('fs');var Redis=require(_0x7db7('0x5'));var db=require('../../mysqldb')['db'];var utils=require(_0x7db7('0x6'));var logger=require(_0x7db7('0x7'))('rpc');var config=require('../../config/environment');var jayson=require(_0x7db7('0x8'));var client=jayson[_0x7db7('0x9')]['http']({'port':0x232c});config[_0x7db7('0xa')]=_[_0x7db7('0xb')](config[_0x7db7('0xa')],{'host':_0x7db7('0xc'),'port':0x18eb});var socket=require(_0x7db7('0xd'))(new Redis(config[_0x7db7('0xa')]));require('./mailAccount.socket')['register'](socket);function respondWithRpcPromise(_0x15906e,_0x508ed8,_0x271215){return new BPromise(function(_0x2f5f46,_0x127f85){return client[_0x7db7('0xe')](_0x15906e,_0x271215)['then'](function(_0x4c2dde){logger[_0x7db7('0xf')](_0x7db7('0x10'),_0x508ed8,'request\x20sent');logger[_0x7db7('0x11')](_0x7db7('0x12'),_0x508ed8,_0x7db7('0x13'),JSON[_0x7db7('0x14')](_0x4c2dde));if(_0x4c2dde[_0x7db7('0x15')]){if(_0x4c2dde[_0x7db7('0x15')][_0x7db7('0x16')]===0x1f4){logger['error'](_0x7db7('0x10'),_0x508ed8,_0x4c2dde[_0x7db7('0x15')][_0x7db7('0x17')]);return _0x127f85(_0x4c2dde['error'][_0x7db7('0x17')]);}logger['error'](_0x7db7('0x10'),_0x508ed8,_0x4c2dde[_0x7db7('0x15')][_0x7db7('0x17')]);return _0x2f5f46(_0x4c2dde[_0x7db7('0x15')][_0x7db7('0x17')]);}else{logger[_0x7db7('0xf')](_0x7db7('0x10'),_0x508ed8,_0x7db7('0x13'));_0x2f5f46(_0x4c2dde[_0x7db7('0x18')][_0x7db7('0x17')]);}})['catch'](function(_0x1d807f){logger[_0x7db7('0x15')](_0x7db7('0x10'),_0x508ed8,_0x1d807f);_0x127f85(_0x1d807f);});});}exports[_0x7db7('0x19')]=function(_0x1fc6bb){var _0x47f3d6=this;return new Promise(function(_0x4d28ce,_0x5e2661){return db[_0x7db7('0x1a')][_0x7db7('0x1b')]({'raw':_0x1fc6bb['options']?_0x1fc6bb[_0x7db7('0x1c')][_0x7db7('0x1d')]===undefined?!![]:![]:!![],'where':_0x1fc6bb[_0x7db7('0x1c')]?_0x1fc6bb[_0x7db7('0x1c')]['where']||null:null,'attributes':_0x1fc6bb[_0x7db7('0x1c')]?_0x1fc6bb[_0x7db7('0x1c')][_0x7db7('0x1e')]||null:null,'limit':_0x1fc6bb['options']?_0x1fc6bb[_0x7db7('0x1c')]['limit']||null:null,'include':_0x1fc6bb[_0x7db7('0x1c')]?_0x1fc6bb[_0x7db7('0x1c')][_0x7db7('0x1f')]?_[_0x7db7('0x20')](_0x1fc6bb[_0x7db7('0x1c')][_0x7db7('0x1f')],function(_0x4192dc){return{'model':db[_0x4192dc[_0x7db7('0x21')]],'as':_0x4192dc['as'],'attributes':_0x4192dc[_0x7db7('0x1e')],'include':_0x4192dc[_0x7db7('0x1f')]?_[_0x7db7('0x20')](_0x4192dc[_0x7db7('0x1f')],function(_0x4f7d36){return{'model':db[_0x4f7d36[_0x7db7('0x21')]],'as':_0x4f7d36['as'],'attributes':_0x4f7d36[_0x7db7('0x1e')],'include':_0x4f7d36['include']?_['map'](_0x4f7d36['include'],function(_0x2fb01c){return{'model':db[_0x2fb01c[_0x7db7('0x21')]],'as':_0x2fb01c['as'],'attributes':_0x2fb01c['attributes']};}):[]};}):[]};}):[]:[]})[_0x7db7('0x22')](function(_0xc957b9){logger[_0x7db7('0xf')](_0x7db7('0x19'),_0x1fc6bb);logger[_0x7db7('0x11')](_0x7db7('0x19'),_0x1fc6bb,JSON['stringify'](_0xc957b9));_0x4d28ce(_0xc957b9);})[_0x7db7('0x23')](function(_0x49d388){logger[_0x7db7('0x15')](_0x7db7('0x19'),_0x49d388[_0x7db7('0x17')],_0x1fc6bb);_0x5e2661(_0x47f3d6[_0x7db7('0x15')](0x1f4,_0x49d388[_0x7db7('0x17')]));});});};exports[_0x7db7('0x24')]=function(_0xcc7c6f){var _0x3171ce=this;return new Promise(function(_0x4c23c3,_0xcd81c4){return db['MailAccount'][_0x7db7('0x25')]({'raw':_0xcc7c6f[_0x7db7('0x1c')]?_0xcc7c6f[_0x7db7('0x1c')][_0x7db7('0x1d')]===undefined?!![]:![]:!![],'where':_0xcc7c6f['options']?_0xcc7c6f[_0x7db7('0x1c')][_0x7db7('0x26')]||null:null,'attributes':_0xcc7c6f['options']?_0xcc7c6f[_0x7db7('0x1c')]['attributes']||null:null,'include':_0xcc7c6f['options']?_0xcc7c6f['options'][_0x7db7('0x1f')]?_[_0x7db7('0x20')](_0xcc7c6f[_0x7db7('0x1c')][_0x7db7('0x1f')],function(_0x5835e2){return{'model':db[_0x5835e2[_0x7db7('0x21')]],'as':_0x5835e2['as'],'attributes':_0x5835e2[_0x7db7('0x1e')],'include':_0x5835e2['include']?_['map'](_0x5835e2[_0x7db7('0x1f')],function(_0x4642d4){return{'model':db[_0x4642d4[_0x7db7('0x21')]],'as':_0x4642d4['as'],'attributes':_0x4642d4['attributes'],'include':_0x4642d4[_0x7db7('0x1f')]?_['map'](_0x4642d4[_0x7db7('0x1f')],function(_0x234689){return{'model':db[_0x234689[_0x7db7('0x21')]],'as':_0x234689['as'],'attributes':_0x234689[_0x7db7('0x1e')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x27ff70){logger[_0x7db7('0xf')](_0x7db7('0x24'),_0xcc7c6f);logger[_0x7db7('0x11')](_0x7db7('0x24'),_0xcc7c6f,JSON[_0x7db7('0x14')](_0x27ff70));_0x4c23c3(_0x27ff70);})['catch'](function(_0x4851e0){logger[_0x7db7('0x15')](_0x7db7('0x24'),_0x4851e0[_0x7db7('0x17')],_0xcc7c6f);_0xcd81c4(_0x3171ce[_0x7db7('0x15')](0x1f4,_0x4851e0[_0x7db7('0x17')]));});});};exports[_0x7db7('0x27')]=function(_0x5bd10d){var _0xdea41=this;return new Promise(function(_0x1c0455,_0x1f3d71){var _0x486cf3={'body':_0x5bd10d[_0x7db7('0x28')],'channel':_0x7db7('0x29')};var _0x5e662b=_[_0x7db7('0x2a')](_[_0x7db7('0x2b')](db['CmContact'][_0x7db7('0x2c')]),[_0x7db7('0x2d'),_0x7db7('0x2e'),_0x7db7('0x2f'),_0x7db7('0x30')]);var _0x541881=![];if(_0x5bd10d[_0x7db7('0x28')]['id']){delete _0x5bd10d[_0x7db7('0x28')]['id'];}if(_[_0x7db7('0x31')](_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x32')])){return _0x1f3d71(_0xdea41['error'](0x1f4,_0x7db7('0x33')));}if(_[_0x7db7('0x31')](_0x5bd10d[_0x7db7('0x28')]['body'])||_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x28')]===''){return _0x1f3d71(_0xdea41[_0x7db7('0x15')](0x1f4,_0x7db7('0x34')));}if(_['isNil'](_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x35')])){return _0x1f3d71(_0xdea41['error'](0x1f4,'mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x5e662b));}if(!_['includes'](_0x5e662b,_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x35')])){return _0x1f3d71(_0xdea41[_0x7db7('0x15')](0x1f4,_0x7db7('0x36')+_0x5e662b));}var _0x5c280a={};_0x5c280a[_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x35')]]=_0x5bd10d['body'][_0x7db7('0x32')];return db['MailAccount']['find']({'where':{'id':_0x5bd10d[_0x7db7('0x37')]['id']},'include':[{'model':db['CmList'],'as':_0x7db7('0x38'),'include':[{'model':db[_0x7db7('0x39')],'as':'Contacts','where':_0x5c280a,'limit':0x1,'order':[[_0x7db7('0x2e'),'DESC']]}]},{'model':db['MailApplication'],'as':_0x7db7('0x3a')},{'model':db[_0x7db7('0x3b')],'as':'Smtp'}]})[_0x7db7('0x22')](function(_0x30f71d){if(_0x30f71d&&_0x30f71d[_0x7db7('0x38')]){var _0x3d6aa3=_0x30f71d[_0x7db7('0x3c')]({'plain':!![]});_0x486cf3[_0x7db7('0x37')]=_[_0x7db7('0x3d')](_0x3d6aa3,[_0x7db7('0x3a'),_0x7db7('0x38')]);_0x486cf3['applications']=_0x3d6aa3['Applications'];_0x486cf3[_0x7db7('0x3e')]=_0x3d6aa3[_0x7db7('0x38')];_0x486cf3['applications']=_[_0x7db7('0x3f')](_0x486cf3[_0x7db7('0x40')],['priority'],[_0x7db7('0x41')]);if(_0x486cf3['account']&&_0x486cf3[_0x7db7('0x37')]['autoclose']){_0x486cf3[_0x7db7('0x40')]['push']({'id':0x0,'priority':_0x486cf3[_0x7db7('0x40')][_0x7db7('0x42')]+0x1,'app':_0x7db7('0x43'),'appdata':_0x7db7('0x44'),'interval':'*,*,*,*'});}if(_0x486cf3[_0x7db7('0x37')][_0x7db7('0x3a')]){delete _0x486cf3[_0x7db7('0x37')][_0x7db7('0x3a')];}if(_0x486cf3['account'][_0x7db7('0x38')]){delete _0x486cf3[_0x7db7('0x37')]['List'];}if(_0x3d6aa3[_0x7db7('0x38')][_0x7db7('0x45')][_0x7db7('0x42')]){return _0x3d6aa3['List'][_0x7db7('0x45')][0x0];}var _0x3aac7b=_['defaults'](_0x5bd10d[_0x7db7('0x28')],{'firstName':_0x5bd10d[_0x7db7('0x28')]['from'],'ListId':_0x3d6aa3['ListId']});_0x3aac7b[_0x5bd10d['body'][_0x7db7('0x35')]]=_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x32')];return db[_0x7db7('0x39')][_0x7db7('0x46')](_0x3aac7b);}else{throw new Error('List\x20not\x20found');}})[_0x7db7('0x22')](function(_0x335669){_0x486cf3[_0x7db7('0x47')]=_0x335669;if(_0x335669){if(!_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x48')]){_0x5bd10d[_0x7db7('0x28')]['message'][_0x7db7('0x48')]=rs[_0x7db7('0x49')]()+'\x20'+_0x5bd10d[_0x7db7('0x28')]['message']['from'];}var _0x412f3e={'ContactId':_0x335669['id'],'MailAccountId':_0x5bd10d[_0x7db7('0x37')]['id'],'inReplyTo':_0x5bd10d[_0x7db7('0x28')]['message'][_0x7db7('0x48')],'to':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x32')],'cc':_0x5bd10d['body'][_0x7db7('0x17')]['cc'],'subject':_0x5bd10d['body'][_0x7db7('0x17')][_0x7db7('0x4a')],'attach':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')]['attach'],'lastMsgAt':moment()[_0x7db7('0x4b')](_0x7db7('0x4c')),'lastMsgDirection':'in'};return db[_0x7db7('0x4d')][_0x7db7('0x25')]({'where':{'closed':![],'MailAccountId':_0x5bd10d[_0x7db7('0x37')]['id']},'include':[{'model':db[_0x7db7('0x4e')],'as':_0x7db7('0x4f'),'attributes':[_0x7db7('0x48')],'where':{'messageId':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x50')]}}]})[_0x7db7('0x22')](function(_0x5d3fa4){if(_0x5d3fa4){return[_0x5d3fa4,![]];}return db[_0x7db7('0x4d')][_0x7db7('0x46')](_0x412f3e)[_0x7db7('0x22')](function(_0x232a1a){return[_0x232a1a,!![]];});});}else{throw new Error(_0x7db7('0x51'));}})['spread'](function(_0x5de83c,_0x3e978b){if(_0x5de83c){_0x541881=_0x3e978b;if(!_0x3e978b){return _0x5de83c['update']({'inReplyTo':_0x5bd10d['body'][_0x7db7('0x17')][_0x7db7('0x48')],'to':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x32')],'cc':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')]['cc'],'subject':_0x5bd10d['body'][_0x7db7('0x17')]['subject'],'attach':_0x5de83c[_0x7db7('0x52')]?_0x5de83c['attach']:_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')]['attach'],'substatus':null,'substatusAt':moment()[_0x7db7('0x4b')]('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgAt':moment()[_0x7db7('0x4b')](_0x7db7('0x4c')),'lastMsgDirection':'in'});}else{return _0x5de83c;}}})[_0x7db7('0x22')](function(_0x4c6c5e){_0x486cf3[_0x7db7('0x53')]=_0x4c6c5e[_0x7db7('0x3c')]({'plain':!![]});_0x486cf3[_0x7db7('0x53')][_0x7db7('0x54')]=_0x541881;return db['MailMessage'][_0x7db7('0x46')]({'body':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x28')],'MailAccountId':_0x5bd10d['account']['id'],'MailInteractionId':_0x4c6c5e['id'],'direction':'in','ContactId':_0x486cf3[_0x7db7('0x47')]['id'],'messageId':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x48')],'from':_0x5bd10d['body']['message'][_0x7db7('0x32')],'to':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')]['to'],'cc':_0x5bd10d[_0x7db7('0x28')]['message']['cc'],'subject':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x4a')],'attach':_0x5bd10d[_0x7db7('0x28')]['message'][_0x7db7('0x52')],'Attachments':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')]['attachments'],'originTo':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x55')],'originCc':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x56')],'bcc':_0x5bd10d['body']['message']['bcc'],'ChatWebsiteId':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x57')]||null,'originChannel':_0x5bd10d[_0x7db7('0x28')]['message'][_0x7db7('0x58')]||_0x7db7('0x59'),'ChatOfflineMessageId':_0x5bd10d[_0x7db7('0x28')][_0x7db7('0x17')][_0x7db7('0x5a')]||null},{'include':[{'model':db[_0x7db7('0x5b')],'as':_0x7db7('0x5c')}]});})[_0x7db7('0x22')](function(_0x2c5642){_0x486cf3[_0x7db7('0x17')]=_0x2c5642;if(_0x486cf3[_0x7db7('0x53')][_0x7db7('0x5d')]){return db[_0x7db7('0x5e')]['find']({'attributes':['id',_0x7db7('0x5f')],'where':{'id':_0x486cf3[_0x7db7('0x53')][_0x7db7('0x5d')]}})[_0x7db7('0x22')](function(_0x4852df){if(_0x4852df){_0x486cf3[_0x7db7('0x40')][_0x7db7('0x60')]({'id':0x0,'priority':0x0,'app':_0x7db7('0x61'),'appdata':_0x4852df[_0x7db7('0x5f')]+','+(_0x486cf3[_0x7db7('0x37')]['waitForTheAssignedAgent']||0xa),'interval':_0x7db7('0x62')});}return respondWithRpcPromise(_0x7db7('0x63'),_0x7db7('0x64'),_0x486cf3);});}return respondWithRpcPromise('Start',_0x7db7('0x64'),_0x486cf3);})['then'](function(_0x3131e4){_0x1c0455(_0x3131e4);})[_0x7db7('0x23')](function(_0x4080f8){_0x1f3d71(_0xdea41['error'](0x1f4,_0x4080f8['message']));});});};