e439c3c9235e7889838599a547b2f00ee18417f4
[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 _0x9fe7=['length','List\x20not\x20found','contact','messageId','subject','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','Contact\x20not\x20found','spread','update','attach','interaction','created','create','attachments','originTo','originCc','ChatWebsiteId','Email','ChatOfflineMessageId','Attachments','UserId','User','name','unshift','waitForTheAssignedAgent','MailQueue','queue','waitForTheAssignedQueue','Start','startRouting','lodash','moment','randomstring','../../mysqldb','../../config/logger','rpc','jayson/promise','client','http','defaults','redis','localhost','./mailAccount.socket','register','then','info','MailAccount,\x20%s,\x20%s','debug','MailAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','GetMailAccount','MailAccount','options','raw','where','include','model','attributes','map','ShowMailAccount','find','smtp','setDataValue','smtpOptions','getSmtpOptions','NotifyMailAccount','body','mail','difference','rawAttributes','createdAt','updatedAt','CompanyId','ListId','from','from\x20is\x20mandatory','isNil','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','List','CmContact','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','CloudProvider','get','account','omit','list','orderBy','applications','priority','asc','autoclose','*,*,*,*'];(function(_0x29b609,_0x19d388){var _0x54c952=function(_0xb3c38d){while(--_0xb3c38d){_0x29b609['push'](_0x29b609['shift']());}};_0x54c952(++_0x19d388);}(_0x9fe7,0x1df));var _0x79fe=function(_0x3b5a4b,_0x2a2ee1){_0x3b5a4b=_0x3b5a4b-0x0;var _0x32258a=_0x9fe7[_0x3b5a4b];return _0x32258a;};'use strict';var _=require(_0x79fe('0x0'));var moment=require(_0x79fe('0x1'));var BPromise=require('bluebird');var rs=require(_0x79fe('0x2'));var Redis=require('ioredis');var db=require(_0x79fe('0x3'))['db'];var logger=require(_0x79fe('0x4'))(_0x79fe('0x5'));var config=require('../../config/environment');var jayson=require(_0x79fe('0x6'));var client=jayson[_0x79fe('0x7')][_0x79fe('0x8')]({'port':0x232c});config['redis']=_[_0x79fe('0x9')](config[_0x79fe('0xa')],{'host':_0x79fe('0xb'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x79fe('0xa')]));require(_0x79fe('0xc'))[_0x79fe('0xd')](socket);function respondWithRpcPromise(_0x26d723,_0xafc7cd,_0x5b914a){return new BPromise(function(_0x34c217,_0x253f7d){return client['request'](_0x26d723,_0x5b914a)[_0x79fe('0xe')](function(_0x3c79f8){logger[_0x79fe('0xf')](_0x79fe('0x10'),_0xafc7cd,'request\x20sent');logger[_0x79fe('0x11')](_0x79fe('0x12'),_0xafc7cd,_0x79fe('0x13'),JSON[_0x79fe('0x14')](_0x3c79f8));if(_0x3c79f8[_0x79fe('0x15')]){if(_0x3c79f8[_0x79fe('0x15')][_0x79fe('0x16')]===0x1f4){logger[_0x79fe('0x15')]('MailAccount,\x20%s,\x20%s',_0xafc7cd,_0x3c79f8[_0x79fe('0x15')][_0x79fe('0x17')]);return _0x253f7d(_0x3c79f8[_0x79fe('0x15')][_0x79fe('0x17')]);}logger[_0x79fe('0x15')](_0x79fe('0x10'),_0xafc7cd,_0x3c79f8['error'][_0x79fe('0x17')]);return _0x34c217(_0x3c79f8[_0x79fe('0x15')][_0x79fe('0x17')]);}else{logger[_0x79fe('0xf')]('MailAccount,\x20%s,\x20%s',_0xafc7cd,_0x79fe('0x13'));_0x34c217(_0x3c79f8[_0x79fe('0x18')][_0x79fe('0x17')]);}})[_0x79fe('0x19')](function(_0x536641){logger['error'](_0x79fe('0x10'),_0xafc7cd,_0x536641);_0x253f7d(_0x536641);});});}exports[_0x79fe('0x1a')]=function(_0x25e51f){var _0x4bca4d=this;return new Promise(function(_0x891611,_0x4141ec){return db[_0x79fe('0x1b')]['findAll']({'raw':_0x25e51f[_0x79fe('0x1c')]?_0x25e51f['options'][_0x79fe('0x1d')]===undefined?!![]:![]:!![],'where':_0x25e51f[_0x79fe('0x1c')]?_0x25e51f[_0x79fe('0x1c')][_0x79fe('0x1e')]||null:null,'attributes':_0x25e51f[_0x79fe('0x1c')]?_0x25e51f[_0x79fe('0x1c')]['attributes']||null:null,'limit':_0x25e51f['options']?_0x25e51f[_0x79fe('0x1c')]['limit']||null:null,'include':_0x25e51f[_0x79fe('0x1c')]?_0x25e51f[_0x79fe('0x1c')]['include']?_['map'](_0x25e51f[_0x79fe('0x1c')][_0x79fe('0x1f')],function(_0x59339b){return{'model':db[_0x59339b[_0x79fe('0x20')]],'as':_0x59339b['as'],'attributes':_0x59339b[_0x79fe('0x21')],'include':_0x59339b['include']?_['map'](_0x59339b['include'],function(_0x3720fa){return{'model':db[_0x3720fa[_0x79fe('0x20')]],'as':_0x3720fa['as'],'attributes':_0x3720fa[_0x79fe('0x21')],'include':_0x3720fa['include']?_[_0x79fe('0x22')](_0x3720fa[_0x79fe('0x1f')],function(_0x3bc5bc){return{'model':db[_0x3bc5bc['model']],'as':_0x3bc5bc['as'],'attributes':_0x3bc5bc['attributes']};}):[]};}):[]};}):[]:[]})[_0x79fe('0xe')](function(_0x51200f){logger[_0x79fe('0xf')]('GetMailAccount',_0x25e51f);logger['debug']('GetMailAccount',_0x25e51f,JSON['stringify'](_0x51200f));_0x891611(_0x51200f);})[_0x79fe('0x19')](function(_0xd2ec88){logger['error'](_0x79fe('0x1a'),_0xd2ec88['message'],_0x25e51f);_0x4141ec(_0x4bca4d[_0x79fe('0x15')](0x1f4,_0xd2ec88[_0x79fe('0x17')]));});});};exports[_0x79fe('0x23')]=function(_0x3558f2){var _0x128ca3=this;return new Promise(function(_0x5ac1c4,_0x436279){return db[_0x79fe('0x1b')][_0x79fe('0x24')]({'raw':_0x3558f2[_0x79fe('0x1c')]?_0x3558f2[_0x79fe('0x1c')][_0x79fe('0x1d')]===undefined?!![]:![]:!![],'where':_0x3558f2['options']?_0x3558f2[_0x79fe('0x1c')][_0x79fe('0x1e')]||null:null,'attributes':_0x3558f2[_0x79fe('0x1c')]?_0x3558f2[_0x79fe('0x1c')][_0x79fe('0x21')]||null:null,'include':_0x3558f2[_0x79fe('0x1c')]?_0x3558f2['options'][_0x79fe('0x1f')]?_['map'](_0x3558f2[_0x79fe('0x1c')]['include'],function(_0x2a533b){return{'model':db[_0x2a533b['model']],'as':_0x2a533b['as'],'attributes':_0x2a533b[_0x79fe('0x21')],'include':_0x2a533b[_0x79fe('0x1f')]?_[_0x79fe('0x22')](_0x2a533b['include'],function(_0x174423){return{'model':db[_0x174423[_0x79fe('0x20')]],'as':_0x174423['as'],'attributes':_0x174423[_0x79fe('0x21')],'include':_0x174423[_0x79fe('0x1f')]?_[_0x79fe('0x22')](_0x174423['include'],function(_0x53957b){return{'model':db[_0x53957b[_0x79fe('0x20')]],'as':_0x53957b['as'],'attributes':_0x53957b['attributes']};}):[]};}):[]};}):[]:[]})[_0x79fe('0xe')](function(_0x44d97e){if(_0x3558f2['options'][_0x79fe('0x25')]===!![])_0x44d97e[_0x79fe('0x26')](_0x79fe('0x27'),_0x44d97e[_0x79fe('0x28')]());logger[_0x79fe('0xf')](_0x79fe('0x23'),_0x3558f2);logger['debug'](_0x79fe('0x23'),_0x3558f2,JSON[_0x79fe('0x14')](_0x44d97e));_0x5ac1c4(_0x44d97e);})[_0x79fe('0x19')](function(_0x2d2908){logger['error'](_0x79fe('0x23'),_0x2d2908['message'],_0x3558f2);_0x436279(_0x128ca3[_0x79fe('0x15')](0x1f4,_0x2d2908['message']));});});};exports[_0x79fe('0x29')]=function(_0x4ab42e){var _0x83737e=this;return new Promise(function(_0x132662,_0x577341){var _0x5e27bc={'body':_0x4ab42e[_0x79fe('0x2a')],'channel':_0x79fe('0x2b')};var _0x69ae72=_[_0x79fe('0x2c')](_['keys'](db['CmContact'][_0x79fe('0x2d')]),[_0x79fe('0x2e'),_0x79fe('0x2f'),_0x79fe('0x30'),_0x79fe('0x31')]);var _0x3feb46=![];if(_0x4ab42e[_0x79fe('0x2a')]['id']){delete _0x4ab42e[_0x79fe('0x2a')]['id'];}if(_['isNil'](_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x32')])){return _0x577341(_0x83737e[_0x79fe('0x15')](0x1f4,_0x79fe('0x33')));}if(_[_0x79fe('0x34')](_0x4ab42e[_0x79fe('0x2a')]['body'])||_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x2a')]===''){return _0x577341(_0x83737e[_0x79fe('0x15')](0x1f4,_0x79fe('0x35')));}if(_[_0x79fe('0x34')](_0x4ab42e[_0x79fe('0x2a')]['mapKey'])){return _0x577341(_0x83737e[_0x79fe('0x15')](0x1f4,_0x79fe('0x36')+_0x69ae72));}if(!_[_0x79fe('0x37')](_0x69ae72,_0x4ab42e['body'][_0x79fe('0x38')])){return _0x577341(_0x83737e['error'](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x69ae72));}var _0x28068f={};_0x28068f[_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x38')]]=_0x4ab42e['body'][_0x79fe('0x32')];return db[_0x79fe('0x1b')][_0x79fe('0x24')]({'where':{'id':_0x4ab42e['account']['id']},'include':[{'model':db['CmList'],'as':_0x79fe('0x39'),'include':[{'model':db[_0x79fe('0x3a')],'as':_0x79fe('0x3b'),'where':_0x28068f,'limit':0x1,'order':[[_0x79fe('0x2f'),_0x79fe('0x3c')]]}]},{'model':db[_0x79fe('0x3d')],'as':_0x79fe('0x3e')},{'model':db[_0x79fe('0x3f')],'as':_0x79fe('0x40'),'include':[{'model':db['CloudProvider'],'as':_0x79fe('0x41')}]}]})[_0x79fe('0xe')](function(_0x496486){if(_0x496486&&_0x496486[_0x79fe('0x39')]){var _0x4bd648=_0x496486[_0x79fe('0x42')]({'plain':!![]});_0x5e27bc[_0x79fe('0x43')]=_[_0x79fe('0x44')](_0x4bd648,[_0x79fe('0x3e'),'List']);_0x5e27bc[_0x79fe('0x43')][_0x79fe('0x27')]=_0x496486[_0x79fe('0x28')]();_0x5e27bc['applications']=_0x4bd648['Applications'];_0x5e27bc[_0x79fe('0x45')]=_0x4bd648[_0x79fe('0x39')];_0x5e27bc['applications']=_[_0x79fe('0x46')](_0x5e27bc[_0x79fe('0x47')],[_0x79fe('0x48')],[_0x79fe('0x49')]);if(_0x5e27bc['account']&&_0x5e27bc[_0x79fe('0x43')][_0x79fe('0x4a')]){_0x5e27bc[_0x79fe('0x47')]['push']({'id':0x0,'priority':_0x5e27bc['applications']['length']+0x1,'app':'close','appdata':'autoclose','interval':_0x79fe('0x4b')});}if(_0x5e27bc[_0x79fe('0x43')]['Applications']){delete _0x5e27bc[_0x79fe('0x43')][_0x79fe('0x3e')];}if(_0x5e27bc[_0x79fe('0x43')]['List']){delete _0x5e27bc['account']['List'];}if(_0x4bd648[_0x79fe('0x39')]['Contacts'][_0x79fe('0x4c')]){return _0x4bd648[_0x79fe('0x39')][_0x79fe('0x3b')][0x0];}var _0x1ac188=_[_0x79fe('0x9')](_0x4ab42e[_0x79fe('0x2a')],{'firstName':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x32')],'ListId':_0x4bd648[_0x79fe('0x31')]});_0x1ac188[_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x38')]]=_0x4ab42e['body'][_0x79fe('0x32')];return db[_0x79fe('0x3a')]['create'](_0x1ac188);}else{throw new Error(_0x79fe('0x4d'));}})['then'](function(_0xc1ec58){_0x5e27bc[_0x79fe('0x4e')]=_0xc1ec58;if(_0xc1ec58){if(!_0x4ab42e['body'][_0x79fe('0x17')]['messageId']){_0x4ab42e[_0x79fe('0x2a')]['message']['messageId']=rs['generate']()+'\x20'+_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x32')];}var _0x350da0={'ContactId':_0xc1ec58['id'],'MailAccountId':_0x4ab42e[_0x79fe('0x43')]['id'],'inReplyTo':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x4f')],'to':_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x32')],'cc':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')]['cc'],'subject':_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x50')],'attach':_0x4ab42e['body'][_0x79fe('0x17')]['attach'],'lastMsgAt':moment()[_0x79fe('0x51')](_0x79fe('0x52')),'lastMsgDirection':'in'};return db[_0x79fe('0x53')][_0x79fe('0x24')]({'where':{'closed':![],'MailAccountId':_0x4ab42e['account']['id']},'include':[{'model':db[_0x79fe('0x54')],'as':_0x79fe('0x55'),'attributes':[_0x79fe('0x4f')],'where':{'messageId':_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x56')]}}]})[_0x79fe('0xe')](function(_0x50ed6c){if(_0x50ed6c){return[_0x50ed6c,![]];}return db[_0x79fe('0x53')]['create'](_0x350da0)['then'](function(_0x37ccea){return[_0x37ccea,!![]];});});}else{throw new Error(_0x79fe('0x57'));}})[_0x79fe('0x58')](function(_0x49de47,_0x58adb7){if(_0x49de47){_0x3feb46=_0x58adb7;if(!_0x58adb7){return _0x49de47[_0x79fe('0x59')]({'inReplyTo':_0x4ab42e['body']['message'][_0x79fe('0x4f')],'to':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x32')],'cc':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')]['cc'],'subject':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x50')],'attach':_0x49de47['attach']?_0x49de47[_0x79fe('0x5a')]:_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x5a')],'substatus':null,'substatusAt':moment()[_0x79fe('0x51')](_0x79fe('0x52')),'lastMsgAt':moment()[_0x79fe('0x51')](_0x79fe('0x52')),'lastMsgDirection':'in'});}else{return _0x49de47;}}})[_0x79fe('0xe')](function(_0x6cf38e){_0x5e27bc[_0x79fe('0x5b')]=_0x6cf38e[_0x79fe('0x42')]({'plain':!![]});_0x5e27bc[_0x79fe('0x5b')][_0x79fe('0x5c')]=_0x3feb46;return db['MailMessage'][_0x79fe('0x5d')]({'body':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x2a')],'MailAccountId':_0x4ab42e[_0x79fe('0x43')]['id'],'MailInteractionId':_0x6cf38e['id'],'direction':'in','ContactId':_0x5e27bc[_0x79fe('0x4e')]['id'],'messageId':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')]['messageId'],'from':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x32')],'to':_0x4ab42e['body']['message']['to'],'cc':_0x4ab42e['body']['message']['cc'],'subject':_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x50')],'attach':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x5a')],'Attachments':_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x5e')],'originTo':_0x4ab42e['body'][_0x79fe('0x17')][_0x79fe('0x5f')],'originCc':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x60')],'bcc':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')]['bcc'],'ChatWebsiteId':_0x4ab42e[_0x79fe('0x2a')]['message'][_0x79fe('0x61')]||null,'originChannel':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')]['originChannel']||_0x79fe('0x62'),'ChatOfflineMessageId':_0x4ab42e[_0x79fe('0x2a')][_0x79fe('0x17')][_0x79fe('0x63')]||null},{'include':[{'model':db['Attachment'],'as':_0x79fe('0x64')}]});})[_0x79fe('0xe')](function(_0x2d4bfa){_0x5e27bc['message']=_0x2d4bfa;if(!_0x5e27bc[_0x79fe('0x5b')][_0x79fe('0x65')]){return;}return db[_0x79fe('0x66')][_0x79fe('0x24')]({'attributes':['id',_0x79fe('0x67')],'where':{'id':_0x5e27bc[_0x79fe('0x5b')][_0x79fe('0x65')]}});})[_0x79fe('0xe')](function(_0x25edcd){if(_0x25edcd){_0x5e27bc['applications'][_0x79fe('0x68')]({'id':0x0,'priority':0x0,'app':'agent','appdata':_0x25edcd[_0x79fe('0x67')]+','+(_0x5e27bc[_0x79fe('0x43')][_0x79fe('0x69')]||0xa),'interval':_0x79fe('0x4b')});}if(!_0x5e27bc[_0x79fe('0x5b')]['queueId']){return;}return db[_0x79fe('0x6a')]['find']({'attributes':['id',_0x79fe('0x67')],'where':{'id':_0x5e27bc[_0x79fe('0x5b')]['queueId']}});})[_0x79fe('0xe')](function(_0x3d7392){if(_0x3d7392){_0x5e27bc[_0x79fe('0x47')][_0x79fe('0x68')]({'id':0x0,'priority':0x1,'app':_0x79fe('0x6b'),'appdata':_0x3d7392['name']+','+(_0x5e27bc[_0x79fe('0x43')][_0x79fe('0x6c')]||0x12c),'interval':_0x79fe('0x4b')});}return respondWithRpcPromise(_0x79fe('0x6d'),_0x79fe('0x6e'),_0x5e27bc);})[_0x79fe('0xe')](function(_0x477c6f){_0x132662(_0x477c6f);})[_0x79fe('0x19')](function(_0x38fc15){_0x577341(_0x83737e[_0x79fe('0x15')](0x1f4,_0x38fc15['message']));});});};