1b8ab5de6a99bc0defd277c703079317dd466dfe
[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 _0x3c6c=['MailQueue','queue','waitForTheAssignedQueue','Start','startRouting','lodash','bluebird','randomstring','ioredis','../../mysqldb','../../config/logger','rpc','../../config/environment','jayson/promise','http','defaults','register','request','info','MailAccount,\x20%s,\x20%s','MailAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','GetMailAccount','MailAccount','findAll','options','raw','where','attributes','limit','include','map','model','then','debug','ShowMailAccount','find','setDataValue','smtpOptions','getSmtpOptions','NotifyMailAccount','mail','difference','keys','CmContact','rawAttributes','createdAt','updatedAt','CompanyId','ListId','body','isNil','from','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','account','CmList','List','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','CloudProvider','get','omit','applications','list','orderBy','priority','asc','autoclose','length','*,*,*,*','create','List\x20not\x20found','contact','messageId','generate','subject','attach','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','spread','update','format','YYYY-MM-DD\x20HH:mm:ss','created','attachments','originTo','originCc','bcc','originChannel','Email','ChatOfflineMessageId','Attachment','Attachments','interaction','UserId','User','name','unshift','agent','waitForTheAssignedAgent','queueId'];(function(_0x53ccf2,_0x13cb67){var _0x5a2964=function(_0x3b0514){while(--_0x3b0514){_0x53ccf2['push'](_0x53ccf2['shift']());}};_0x5a2964(++_0x13cb67);}(_0x3c6c,0x7b));var _0xc3c6=function(_0x11ae92,_0x4742bb){_0x11ae92=_0x11ae92-0x0;var _0x1b9342=_0x3c6c[_0x11ae92];return _0x1b9342;};'use strict';var _=require(_0xc3c6('0x0'));var moment=require('moment');var BPromise=require(_0xc3c6('0x1'));var rs=require(_0xc3c6('0x2'));var Redis=require(_0xc3c6('0x3'));var db=require(_0xc3c6('0x4'))['db'];var logger=require(_0xc3c6('0x5'))(_0xc3c6('0x6'));var config=require(_0xc3c6('0x7'));var jayson=require(_0xc3c6('0x8'));var client=jayson['client'][_0xc3c6('0x9')]({'port':0x232c});config['redis']=_[_0xc3c6('0xa')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require('./mailAccount.socket')[_0xc3c6('0xb')](socket);function respondWithRpcPromise(_0x1c04d4,_0x5bc1c1,_0x168fdc){return new BPromise(function(_0x1ba453,_0x44b9c9){return client[_0xc3c6('0xc')](_0x1c04d4,_0x168fdc)['then'](function(_0x115b59){logger[_0xc3c6('0xd')](_0xc3c6('0xe'),_0x5bc1c1,'request\x20sent');logger['debug'](_0xc3c6('0xf'),_0x5bc1c1,_0xc3c6('0x10'),JSON[_0xc3c6('0x11')](_0x115b59));if(_0x115b59[_0xc3c6('0x12')]){if(_0x115b59[_0xc3c6('0x12')][_0xc3c6('0x13')]===0x1f4){logger[_0xc3c6('0x12')](_0xc3c6('0xe'),_0x5bc1c1,_0x115b59[_0xc3c6('0x12')][_0xc3c6('0x14')]);return _0x44b9c9(_0x115b59[_0xc3c6('0x12')][_0xc3c6('0x14')]);}logger[_0xc3c6('0x12')](_0xc3c6('0xe'),_0x5bc1c1,_0x115b59[_0xc3c6('0x12')][_0xc3c6('0x14')]);return _0x1ba453(_0x115b59[_0xc3c6('0x12')][_0xc3c6('0x14')]);}else{logger[_0xc3c6('0xd')](_0xc3c6('0xe'),_0x5bc1c1,_0xc3c6('0x10'));_0x1ba453(_0x115b59[_0xc3c6('0x15')][_0xc3c6('0x14')]);}})[_0xc3c6('0x16')](function(_0x3003e1){logger[_0xc3c6('0x12')]('MailAccount,\x20%s,\x20%s',_0x5bc1c1,_0x3003e1);_0x44b9c9(_0x3003e1);});});}exports[_0xc3c6('0x17')]=function(_0x5a2456){var _0x2150e0=this;return new Promise(function(_0x248bfe,_0x3893c4){return db[_0xc3c6('0x18')][_0xc3c6('0x19')]({'raw':_0x5a2456[_0xc3c6('0x1a')]?_0x5a2456[_0xc3c6('0x1a')][_0xc3c6('0x1b')]===undefined?!![]:![]:!![],'where':_0x5a2456[_0xc3c6('0x1a')]?_0x5a2456[_0xc3c6('0x1a')][_0xc3c6('0x1c')]||null:null,'attributes':_0x5a2456[_0xc3c6('0x1a')]?_0x5a2456[_0xc3c6('0x1a')][_0xc3c6('0x1d')]||null:null,'limit':_0x5a2456['options']?_0x5a2456[_0xc3c6('0x1a')][_0xc3c6('0x1e')]||null:null,'include':_0x5a2456[_0xc3c6('0x1a')]?_0x5a2456['options'][_0xc3c6('0x1f')]?_[_0xc3c6('0x20')](_0x5a2456[_0xc3c6('0x1a')][_0xc3c6('0x1f')],function(_0x2bc9bf){return{'model':db[_0x2bc9bf[_0xc3c6('0x21')]],'as':_0x2bc9bf['as'],'attributes':_0x2bc9bf[_0xc3c6('0x1d')],'include':_0x2bc9bf['include']?_[_0xc3c6('0x20')](_0x2bc9bf[_0xc3c6('0x1f')],function(_0x4ca76f){return{'model':db[_0x4ca76f[_0xc3c6('0x21')]],'as':_0x4ca76f['as'],'attributes':_0x4ca76f[_0xc3c6('0x1d')],'include':_0x4ca76f[_0xc3c6('0x1f')]?_[_0xc3c6('0x20')](_0x4ca76f[_0xc3c6('0x1f')],function(_0x420723){return{'model':db[_0x420723[_0xc3c6('0x21')]],'as':_0x420723['as'],'attributes':_0x420723[_0xc3c6('0x1d')]};}):[]};}):[]};}):[]:[]})[_0xc3c6('0x22')](function(_0x478a4b){logger[_0xc3c6('0xd')](_0xc3c6('0x17'),_0x5a2456);logger[_0xc3c6('0x23')](_0xc3c6('0x17'),_0x5a2456,JSON['stringify'](_0x478a4b));_0x248bfe(_0x478a4b);})[_0xc3c6('0x16')](function(_0x1bcc34){logger[_0xc3c6('0x12')](_0xc3c6('0x17'),_0x1bcc34[_0xc3c6('0x14')],_0x5a2456);_0x3893c4(_0x2150e0[_0xc3c6('0x12')](0x1f4,_0x1bcc34['message']));});});};exports[_0xc3c6('0x24')]=function(_0x249bad){var _0x2220ca=this;return new Promise(function(_0x2b715c,_0x380345){return db['MailAccount'][_0xc3c6('0x25')]({'raw':_0x249bad[_0xc3c6('0x1a')]?_0x249bad[_0xc3c6('0x1a')][_0xc3c6('0x1b')]===undefined?!![]:![]:!![],'where':_0x249bad[_0xc3c6('0x1a')]?_0x249bad[_0xc3c6('0x1a')][_0xc3c6('0x1c')]||null:null,'attributes':_0x249bad['options']?_0x249bad[_0xc3c6('0x1a')]['attributes']||null:null,'include':_0x249bad[_0xc3c6('0x1a')]?_0x249bad[_0xc3c6('0x1a')][_0xc3c6('0x1f')]?_[_0xc3c6('0x20')](_0x249bad[_0xc3c6('0x1a')][_0xc3c6('0x1f')],function(_0x4ba1ae){return{'model':db[_0x4ba1ae[_0xc3c6('0x21')]],'as':_0x4ba1ae['as'],'attributes':_0x4ba1ae[_0xc3c6('0x1d')],'include':_0x4ba1ae['include']?_[_0xc3c6('0x20')](_0x4ba1ae[_0xc3c6('0x1f')],function(_0x139944){return{'model':db[_0x139944[_0xc3c6('0x21')]],'as':_0x139944['as'],'attributes':_0x139944[_0xc3c6('0x1d')],'include':_0x139944[_0xc3c6('0x1f')]?_[_0xc3c6('0x20')](_0x139944['include'],function(_0x5daaf2){return{'model':db[_0x5daaf2[_0xc3c6('0x21')]],'as':_0x5daaf2['as'],'attributes':_0x5daaf2[_0xc3c6('0x1d')]};}):[]};}):[]};}):[]:[]})[_0xc3c6('0x22')](function(_0xfe1489){if(_0x249bad[_0xc3c6('0x1a')]['smtp']===!![])_0xfe1489[_0xc3c6('0x26')](_0xc3c6('0x27'),_0xfe1489[_0xc3c6('0x28')]());logger[_0xc3c6('0xd')](_0xc3c6('0x24'),_0x249bad);logger[_0xc3c6('0x23')](_0xc3c6('0x24'),_0x249bad,JSON[_0xc3c6('0x11')](_0xfe1489));_0x2b715c(_0xfe1489);})[_0xc3c6('0x16')](function(_0x52ef62){logger[_0xc3c6('0x12')](_0xc3c6('0x24'),_0x52ef62[_0xc3c6('0x14')],_0x249bad);_0x380345(_0x2220ca[_0xc3c6('0x12')](0x1f4,_0x52ef62[_0xc3c6('0x14')]));});});};exports[_0xc3c6('0x29')]=function(_0x3e839e){var _0x376e90=this;return new Promise(function(_0x59b2d9,_0x30692f){var _0x242e2b={'body':_0x3e839e['body'],'channel':_0xc3c6('0x2a')};var _0x5ea389=_[_0xc3c6('0x2b')](_[_0xc3c6('0x2c')](db[_0xc3c6('0x2d')][_0xc3c6('0x2e')]),[_0xc3c6('0x2f'),_0xc3c6('0x30'),_0xc3c6('0x31'),_0xc3c6('0x32')]);var _0x572339=![];if(_0x3e839e[_0xc3c6('0x33')]['id']){delete _0x3e839e['body']['id'];}if(_[_0xc3c6('0x34')](_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x35')])){return _0x30692f(_0x376e90[_0xc3c6('0x12')](0x1f4,'from\x20is\x20mandatory'));}if(_['isNil'](_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x33')])||_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x33')]===''){return _0x30692f(_0x376e90['error'](0x1f4,_0xc3c6('0x36')));}if(_[_0xc3c6('0x34')](_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x37')])){return _0x30692f(_0x376e90[_0xc3c6('0x12')](0x1f4,_0xc3c6('0x38')+_0x5ea389));}if(!_[_0xc3c6('0x39')](_0x5ea389,_0x3e839e['body']['mapKey'])){return _0x30692f(_0x376e90[_0xc3c6('0x12')](0x1f4,_0xc3c6('0x3a')+_0x5ea389));}var _0x490bbf={};_0x490bbf[_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x37')]]=_0x3e839e['body'][_0xc3c6('0x35')];return db[_0xc3c6('0x18')]['find']({'where':{'id':_0x3e839e[_0xc3c6('0x3b')]['id']},'include':[{'model':db[_0xc3c6('0x3c')],'as':_0xc3c6('0x3d'),'include':[{'model':db[_0xc3c6('0x2d')],'as':_0xc3c6('0x3e'),'where':_0x490bbf,'limit':0x1,'order':[[_0xc3c6('0x30'),_0xc3c6('0x3f')]]}]},{'model':db[_0xc3c6('0x40')],'as':_0xc3c6('0x41')},{'model':db[_0xc3c6('0x42')],'as':_0xc3c6('0x43'),'include':[{'model':db[_0xc3c6('0x44')],'as':_0xc3c6('0x44')}]}]})[_0xc3c6('0x22')](function(_0x55b8bc){if(_0x55b8bc&&_0x55b8bc['List']){var _0x387a41=_0x55b8bc[_0xc3c6('0x45')]({'plain':!![]});_0x242e2b[_0xc3c6('0x3b')]=_[_0xc3c6('0x46')](_0x387a41,['Applications','List']);_0x242e2b[_0xc3c6('0x3b')][_0xc3c6('0x27')]=_0x55b8bc[_0xc3c6('0x28')]();_0x242e2b[_0xc3c6('0x47')]=_0x387a41[_0xc3c6('0x41')];_0x242e2b[_0xc3c6('0x48')]=_0x387a41[_0xc3c6('0x3d')];_0x242e2b[_0xc3c6('0x47')]=_[_0xc3c6('0x49')](_0x242e2b[_0xc3c6('0x47')],[_0xc3c6('0x4a')],[_0xc3c6('0x4b')]);if(_0x242e2b[_0xc3c6('0x3b')]&&_0x242e2b[_0xc3c6('0x3b')][_0xc3c6('0x4c')]){_0x242e2b[_0xc3c6('0x47')]['push']({'id':0x0,'priority':_0x242e2b[_0xc3c6('0x47')][_0xc3c6('0x4d')]+0x1,'app':'close','appdata':'autoclose','interval':_0xc3c6('0x4e')});}if(_0x242e2b[_0xc3c6('0x3b')][_0xc3c6('0x41')]){delete _0x242e2b[_0xc3c6('0x3b')][_0xc3c6('0x41')];}if(_0x242e2b[_0xc3c6('0x3b')][_0xc3c6('0x3d')]){delete _0x242e2b['account']['List'];}if(_0x387a41[_0xc3c6('0x3d')][_0xc3c6('0x3e')][_0xc3c6('0x4d')]){return _0x387a41[_0xc3c6('0x3d')][_0xc3c6('0x3e')][0x0];}var _0x2e9314=_[_0xc3c6('0xa')](_0x3e839e[_0xc3c6('0x33')],{'firstName':_0x3e839e[_0xc3c6('0x33')]['from'],'ListId':_0x387a41[_0xc3c6('0x32')]});_0x2e9314[_0x3e839e['body']['mapKey']]=_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x35')];return db[_0xc3c6('0x2d')][_0xc3c6('0x4f')](_0x2e9314);}else{throw new Error(_0xc3c6('0x50'));}})[_0xc3c6('0x22')](function(_0x330cfc){_0x242e2b[_0xc3c6('0x51')]=_0x330cfc;if(_0x330cfc){if(!_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')]['messageId']){_0x3e839e[_0xc3c6('0x33')]['message'][_0xc3c6('0x52')]=rs[_0xc3c6('0x53')]()+'\x20'+_0x3e839e[_0xc3c6('0x33')]['message'][_0xc3c6('0x35')];}var _0x528383={'ContactId':_0x330cfc['id'],'MailAccountId':_0x3e839e['account']['id'],'inReplyTo':_0x3e839e['body'][_0xc3c6('0x14')][_0xc3c6('0x52')],'to':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x35')],'cc':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')]['cc'],'subject':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x54')],'attach':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x55')],'lastMsgAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':'in'};return db[_0xc3c6('0x56')][_0xc3c6('0x25')]({'where':{'closed':![],'MailAccountId':_0x3e839e[_0xc3c6('0x3b')]['id']},'include':[{'model':db[_0xc3c6('0x57')],'as':_0xc3c6('0x58'),'attributes':[_0xc3c6('0x52')],'where':{'messageId':_0x3e839e[_0xc3c6('0x33')]['message'][_0xc3c6('0x59')]}}]})['then'](function(_0x1709a8){if(_0x1709a8){return[_0x1709a8,![]];}return db['MailInteraction'][_0xc3c6('0x4f')](_0x528383)[_0xc3c6('0x22')](function(_0x5cc468){return[_0x5cc468,!![]];});});}else{throw new Error(_0xc3c6('0x5a'));}})[_0xc3c6('0x5b')](function(_0x333d8f,_0x501847){if(_0x333d8f){_0x572339=_0x501847;if(!_0x501847){return _0x333d8f[_0xc3c6('0x5c')]({'inReplyTo':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x52')],'to':_0x3e839e[_0xc3c6('0x33')]['message']['from'],'cc':_0x3e839e[_0xc3c6('0x33')]['message']['cc'],'subject':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')]['subject'],'attach':_0x333d8f[_0xc3c6('0x55')]?_0x333d8f[_0xc3c6('0x55')]:_0x3e839e['body']['message'][_0xc3c6('0x55')],'substatus':null,'substatusAt':moment()[_0xc3c6('0x5d')](_0xc3c6('0x5e')),'lastMsgAt':moment()['format'](_0xc3c6('0x5e')),'lastMsgDirection':'in'});}else{return _0x333d8f;}}})['then'](function(_0x3dd6d2){_0x242e2b['interaction']=_0x3dd6d2['get']({'plain':!![]});_0x242e2b['interaction'][_0xc3c6('0x5f')]=_0x572339;return db['MailMessage'][_0xc3c6('0x4f')]({'body':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x33')],'MailAccountId':_0x3e839e[_0xc3c6('0x3b')]['id'],'MailInteractionId':_0x3dd6d2['id'],'direction':'in','ContactId':_0x242e2b['contact']['id'],'messageId':_0x3e839e['body']['message']['messageId'],'from':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x35')],'to':_0x3e839e['body'][_0xc3c6('0x14')]['to'],'cc':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')]['cc'],'subject':_0x3e839e[_0xc3c6('0x33')]['message']['subject'],'attach':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x55')],'Attachments':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x60')],'originTo':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x61')],'originCc':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x62')],'bcc':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')][_0xc3c6('0x63')],'ChatWebsiteId':_0x3e839e[_0xc3c6('0x33')][_0xc3c6('0x14')]['ChatWebsiteId']||null,'originChannel':_0x3e839e[_0xc3c6('0x33')]['message'][_0xc3c6('0x64')]||_0xc3c6('0x65'),'ChatOfflineMessageId':_0x3e839e['body'][_0xc3c6('0x14')][_0xc3c6('0x66')]||null},{'include':[{'model':db[_0xc3c6('0x67')],'as':_0xc3c6('0x68')}]});})[_0xc3c6('0x22')](function(_0x408544){_0x242e2b[_0xc3c6('0x14')]=_0x408544;if(!_0x242e2b[_0xc3c6('0x69')][_0xc3c6('0x6a')]){return;}return db[_0xc3c6('0x6b')][_0xc3c6('0x25')]({'attributes':['id',_0xc3c6('0x6c')],'where':{'id':_0x242e2b['interaction'][_0xc3c6('0x6a')]}});})[_0xc3c6('0x22')](function(_0x25c747){if(_0x25c747){_0x242e2b[_0xc3c6('0x47')][_0xc3c6('0x6d')]({'id':0x0,'priority':0x0,'app':_0xc3c6('0x6e'),'appdata':_0x25c747[_0xc3c6('0x6c')]+','+(_0x242e2b['account'][_0xc3c6('0x6f')]||0xa),'interval':_0xc3c6('0x4e')});}if(!_0x242e2b[_0xc3c6('0x69')][_0xc3c6('0x70')]){return;}return db[_0xc3c6('0x71')]['find']({'attributes':['id',_0xc3c6('0x6c')],'where':{'id':_0x242e2b[_0xc3c6('0x69')]['queueId']}});})[_0xc3c6('0x22')](function(_0x104038){if(_0x104038){_0x242e2b[_0xc3c6('0x47')][_0xc3c6('0x6d')]({'id':0x0,'priority':0x1,'app':_0xc3c6('0x72'),'appdata':_0x104038[_0xc3c6('0x6c')]+','+(_0x242e2b[_0xc3c6('0x3b')][_0xc3c6('0x73')]||0x12c),'interval':_0xc3c6('0x4e')});}return respondWithRpcPromise(_0xc3c6('0x74'),_0xc3c6('0x75'),_0x242e2b);})[_0xc3c6('0x22')](function(_0x2ec1b5){_0x59b2d9(_0x2ec1b5);})['catch'](function(_0x2e7a09){_0x30692f(_0x376e90[_0xc3c6('0x12')](0x1f4,_0x2e7a09[_0xc3c6('0x14')]));});});};