143befc174e13e5ad488054c22e2d34a3a235ecc
[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 _0x1ca7=['body','difference','keys','CmContact','createdAt','updatedAt','CompanyId','ListId','isNil','from','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','Contacts','DESC','MailApplication','Applications','MailServerOut','Smtp','List','get','account','omit','list','orderBy','priority','autoclose','push','applications','length','close','create','List\x20not\x20found','contact','messageId','generate','subject','attach','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','spread','update','format','interaction','attachments','bcc','Attachment','Attachments','UserId','User','name','agent','waitForTheAssignedAgent','*,*,*,*','Start','startRouting','lodash','util','ioredis','../../mysqldb','../../config/utils','rpc','../../config/environment','http','redis','defaults','socket.io-emitter','register','request','then','info','request\x20sent','debug','stringify','error','code','MailAccount,\x20%s,\x20%s','message','result','catch','MailAccount','findAll','options','raw','where','limit','include','map','model','attributes','GetMailAccount','ShowMailAccount','find'];(function(_0x179314,_0x32d3a2){var _0x3556f7=function(_0x16244e){while(--_0x16244e){_0x179314['push'](_0x179314['shift']());}};_0x3556f7(++_0x32d3a2);}(_0x1ca7,0x157));var _0x71ca=function(_0x484e36,_0x4482d8){_0x484e36=_0x484e36-0x0;var _0xafc85d=_0x1ca7[_0x484e36];return _0xafc85d;};'use strict';var _=require(_0x71ca('0x0'));var util=require(_0x71ca('0x1'));var moment=require('moment');var BPromise=require('bluebird');var rs=require('randomstring');var fs=require('fs');var Redis=require(_0x71ca('0x2'));var db=require(_0x71ca('0x3'))['db'];var utils=require(_0x71ca('0x4'));var logger=require('../../config/logger')(_0x71ca('0x5'));var config=require(_0x71ca('0x6'));var jayson=require('jayson/promise');var client=jayson['client'][_0x71ca('0x7')]({'port':0x232c});config[_0x71ca('0x8')]=_[_0x71ca('0x9')](config[_0x71ca('0x8')],{'host':'localhost','port':0x18eb});var socket=require(_0x71ca('0xa'))(new Redis(config[_0x71ca('0x8')]));require('./mailAccount.socket')[_0x71ca('0xb')](socket);function respondWithRpcPromise(_0x2aff62,_0x1fcf82,_0x5def7b){return new BPromise(function(_0x25a84b,_0x342d5c){return client[_0x71ca('0xc')](_0x2aff62,_0x5def7b)[_0x71ca('0xd')](function(_0x1a9590){logger[_0x71ca('0xe')]('MailAccount,\x20%s,\x20%s',_0x1fcf82,_0x71ca('0xf'));logger[_0x71ca('0x10')]('MailAccount,\x20%s,\x20%s,\x20%s',_0x1fcf82,_0x71ca('0xf'),JSON[_0x71ca('0x11')](_0x1a9590));if(_0x1a9590[_0x71ca('0x12')]){if(_0x1a9590[_0x71ca('0x12')][_0x71ca('0x13')]===0x1f4){logger[_0x71ca('0x12')](_0x71ca('0x14'),_0x1fcf82,_0x1a9590[_0x71ca('0x12')][_0x71ca('0x15')]);return _0x342d5c(_0x1a9590[_0x71ca('0x12')]['message']);}logger['error'](_0x71ca('0x14'),_0x1fcf82,_0x1a9590['error'][_0x71ca('0x15')]);return _0x25a84b(_0x1a9590[_0x71ca('0x12')]['message']);}else{logger['info'](_0x71ca('0x14'),_0x1fcf82,'request\x20sent');_0x25a84b(_0x1a9590[_0x71ca('0x16')][_0x71ca('0x15')]);}})[_0x71ca('0x17')](function(_0x3750fa){logger['error']('MailAccount,\x20%s,\x20%s',_0x1fcf82,_0x3750fa);_0x342d5c(_0x3750fa);});});}exports['GetMailAccount']=function(_0x13733f){var _0x3bbb35=this;return new Promise(function(_0x46ffec,_0x257ab1){return db[_0x71ca('0x18')][_0x71ca('0x19')]({'raw':_0x13733f[_0x71ca('0x1a')]?_0x13733f[_0x71ca('0x1a')][_0x71ca('0x1b')]===undefined?!![]:![]:!![],'where':_0x13733f[_0x71ca('0x1a')]?_0x13733f['options'][_0x71ca('0x1c')]||null:null,'attributes':_0x13733f['options']?_0x13733f[_0x71ca('0x1a')]['attributes']||null:null,'limit':_0x13733f['options']?_0x13733f[_0x71ca('0x1a')][_0x71ca('0x1d')]||null:null,'include':_0x13733f[_0x71ca('0x1a')]?_0x13733f[_0x71ca('0x1a')][_0x71ca('0x1e')]?_[_0x71ca('0x1f')](_0x13733f[_0x71ca('0x1a')][_0x71ca('0x1e')],function(_0x35f577){return{'model':db[_0x35f577[_0x71ca('0x20')]],'as':_0x35f577['as'],'attributes':_0x35f577[_0x71ca('0x21')],'include':_0x35f577[_0x71ca('0x1e')]?_[_0x71ca('0x1f')](_0x35f577[_0x71ca('0x1e')],function(_0x5e90b0){return{'model':db[_0x5e90b0[_0x71ca('0x20')]],'as':_0x5e90b0['as'],'attributes':_0x5e90b0[_0x71ca('0x21')],'include':_0x5e90b0[_0x71ca('0x1e')]?_[_0x71ca('0x1f')](_0x5e90b0[_0x71ca('0x1e')],function(_0x30deeb){return{'model':db[_0x30deeb[_0x71ca('0x20')]],'as':_0x30deeb['as'],'attributes':_0x30deeb['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0xabe6e7){logger[_0x71ca('0xe')](_0x71ca('0x22'),_0x13733f);logger['debug']('GetMailAccount',_0x13733f,JSON['stringify'](_0xabe6e7));_0x46ffec(_0xabe6e7);})[_0x71ca('0x17')](function(_0x501357){logger['error'](_0x71ca('0x22'),_0x501357['message'],_0x13733f);_0x257ab1(_0x3bbb35[_0x71ca('0x12')](0x1f4,_0x501357[_0x71ca('0x15')]));});});};exports[_0x71ca('0x23')]=function(_0x5e6c23){var _0x15e975=this;return new Promise(function(_0x290eba,_0xc390ff){return db[_0x71ca('0x18')][_0x71ca('0x24')]({'raw':_0x5e6c23['options']?_0x5e6c23[_0x71ca('0x1a')][_0x71ca('0x1b')]===undefined?!![]:![]:!![],'where':_0x5e6c23[_0x71ca('0x1a')]?_0x5e6c23[_0x71ca('0x1a')][_0x71ca('0x1c')]||null:null,'attributes':_0x5e6c23[_0x71ca('0x1a')]?_0x5e6c23[_0x71ca('0x1a')][_0x71ca('0x21')]||null:null,'include':_0x5e6c23['options']?_0x5e6c23['options'][_0x71ca('0x1e')]?_[_0x71ca('0x1f')](_0x5e6c23[_0x71ca('0x1a')][_0x71ca('0x1e')],function(_0x5bdeda){return{'model':db[_0x5bdeda[_0x71ca('0x20')]],'as':_0x5bdeda['as'],'attributes':_0x5bdeda[_0x71ca('0x21')],'include':_0x5bdeda[_0x71ca('0x1e')]?_['map'](_0x5bdeda[_0x71ca('0x1e')],function(_0x53e22a){return{'model':db[_0x53e22a[_0x71ca('0x20')]],'as':_0x53e22a['as'],'attributes':_0x53e22a[_0x71ca('0x21')],'include':_0x53e22a[_0x71ca('0x1e')]?_[_0x71ca('0x1f')](_0x53e22a[_0x71ca('0x1e')],function(_0x16de9e){return{'model':db[_0x16de9e[_0x71ca('0x20')]],'as':_0x16de9e['as'],'attributes':_0x16de9e[_0x71ca('0x21')]};}):[]};}):[]};}):[]:[]})[_0x71ca('0xd')](function(_0x3bc9fd){logger[_0x71ca('0xe')]('ShowMailAccount',_0x5e6c23);logger[_0x71ca('0x10')](_0x71ca('0x23'),_0x5e6c23,JSON[_0x71ca('0x11')](_0x3bc9fd));_0x290eba(_0x3bc9fd);})['catch'](function(_0x45c8f4){logger[_0x71ca('0x12')](_0x71ca('0x23'),_0x45c8f4[_0x71ca('0x15')],_0x5e6c23);_0xc390ff(_0x15e975[_0x71ca('0x12')](0x1f4,_0x45c8f4['message']));});});};exports['NotifyMailAccount']=function(_0x2e2546){var _0x9a7046=this;return new Promise(function(_0x1ed83a,_0x973028){var _0x316212={'body':_0x2e2546[_0x71ca('0x25')],'channel':'mail'};var _0x4b01b4=_[_0x71ca('0x26')](_[_0x71ca('0x27')](db[_0x71ca('0x28')]['rawAttributes']),[_0x71ca('0x29'),_0x71ca('0x2a'),_0x71ca('0x2b'),_0x71ca('0x2c')]);var _0x437e46=![];if(_0x2e2546[_0x71ca('0x25')]['id']){delete _0x2e2546[_0x71ca('0x25')]['id'];}if(_[_0x71ca('0x2d')](_0x2e2546[_0x71ca('0x25')][_0x71ca('0x2e')])){return _0x973028(_0x9a7046['error'](0x1f4,'from\x20is\x20mandatory'));}if(_[_0x71ca('0x2d')](_0x2e2546['body']['body'])||_0x2e2546['body']['body']===''){return _0x973028(_0x9a7046['error'](0x1f4,_0x71ca('0x2f')));}if(_[_0x71ca('0x2d')](_0x2e2546[_0x71ca('0x25')][_0x71ca('0x30')])){return _0x973028(_0x9a7046[_0x71ca('0x12')](0x1f4,'mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x4b01b4));}if(!_['includes'](_0x4b01b4,_0x2e2546[_0x71ca('0x25')][_0x71ca('0x30')])){return _0x973028(_0x9a7046[_0x71ca('0x12')](0x1f4,_0x71ca('0x31')+_0x4b01b4));}var _0x43197a={};_0x43197a[_0x2e2546[_0x71ca('0x25')]['mapKey']]=_0x2e2546[_0x71ca('0x25')][_0x71ca('0x2e')];return db[_0x71ca('0x18')][_0x71ca('0x24')]({'where':{'id':_0x2e2546['account']['id']},'include':[{'model':db[_0x71ca('0x32')],'as':'List','include':[{'model':db[_0x71ca('0x28')],'as':_0x71ca('0x33'),'where':_0x43197a,'limit':0x1,'order':[[_0x71ca('0x2a'),_0x71ca('0x34')]]}]},{'model':db[_0x71ca('0x35')],'as':_0x71ca('0x36')},{'model':db[_0x71ca('0x37')],'as':_0x71ca('0x38')}]})[_0x71ca('0xd')](function(_0x29d170){if(_0x29d170&&_0x29d170[_0x71ca('0x39')]){var _0x264613=_0x29d170[_0x71ca('0x3a')]({'plain':!![]});_0x316212[_0x71ca('0x3b')]=_[_0x71ca('0x3c')](_0x264613,[_0x71ca('0x36'),_0x71ca('0x39')]);_0x316212['applications']=_0x264613[_0x71ca('0x36')];_0x316212[_0x71ca('0x3d')]=_0x264613[_0x71ca('0x39')];_0x316212['applications']=_[_0x71ca('0x3e')](_0x316212['applications'],[_0x71ca('0x3f')],['asc']);if(_0x316212[_0x71ca('0x3b')]&&_0x316212[_0x71ca('0x3b')][_0x71ca('0x40')]){_0x316212['applications'][_0x71ca('0x41')]({'id':0x0,'priority':_0x316212[_0x71ca('0x42')][_0x71ca('0x43')]+0x1,'app':_0x71ca('0x44'),'appdata':'autoclose','interval':'*,*,*,*'});}if(_0x316212[_0x71ca('0x3b')][_0x71ca('0x36')]){delete _0x316212['account'][_0x71ca('0x36')];}if(_0x316212[_0x71ca('0x3b')]['List']){delete _0x316212[_0x71ca('0x3b')]['List'];}if(_0x264613[_0x71ca('0x39')][_0x71ca('0x33')][_0x71ca('0x43')]){return _0x264613[_0x71ca('0x39')][_0x71ca('0x33')][0x0];}var _0x5d0e64=_['defaults'](_0x2e2546[_0x71ca('0x25')],{'firstName':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x2e')],'ListId':_0x264613['ListId']});_0x5d0e64[_0x2e2546[_0x71ca('0x25')][_0x71ca('0x30')]]=_0x2e2546[_0x71ca('0x25')][_0x71ca('0x2e')];return db[_0x71ca('0x28')][_0x71ca('0x45')](_0x5d0e64);}else{throw new Error(_0x71ca('0x46'));}})[_0x71ca('0xd')](function(_0x46a279){_0x316212[_0x71ca('0x47')]=_0x46a279;if(_0x46a279){if(!_0x2e2546['body']['message'][_0x71ca('0x48')]){_0x2e2546[_0x71ca('0x25')]['message'][_0x71ca('0x48')]=rs[_0x71ca('0x49')]()+'\x20'+_0x2e2546['body'][_0x71ca('0x15')]['from'];}var _0x442eed={'ContactId':_0x46a279['id'],'MailAccountId':_0x2e2546[_0x71ca('0x3b')]['id'],'inReplyTo':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x48')],'to':_0x2e2546['body'][_0x71ca('0x15')]['from'],'cc':_0x2e2546['body'][_0x71ca('0x15')]['cc'],'subject':_0x2e2546['body'][_0x71ca('0x15')][_0x71ca('0x4a')],'attach':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x4b')],'lastMsgAt':moment()['format'](_0x71ca('0x4c')),'lastMsgDirection':'in','lastMsgBody':_0x2e2546['body']['body']};return db[_0x71ca('0x4d')][_0x71ca('0x24')]({'where':{'closed':![],'MailAccountId':_0x2e2546['account']['id']},'include':[{'model':db[_0x71ca('0x4e')],'as':'Messages','attributes':['messageId'],'where':{'messageId':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')]['inReplyTo']}}]})[_0x71ca('0xd')](function(_0x7a93bb){if(_0x7a93bb){return[_0x7a93bb,![]];}return db[_0x71ca('0x4d')][_0x71ca('0x45')](_0x442eed)['then'](function(_0x1bb39d){return[_0x1bb39d,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0x71ca('0x4f')](function(_0x191cdc,_0x4bb759){if(_0x191cdc){_0x437e46=_0x4bb759;if(!_0x4bb759){return _0x191cdc[_0x71ca('0x50')]({'inReplyTo':_0x2e2546['body']['message'][_0x71ca('0x48')],'to':_0x2e2546[_0x71ca('0x25')]['message'][_0x71ca('0x2e')],'cc':_0x2e2546['body'][_0x71ca('0x15')]['cc'],'subject':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x4a')],'attach':_0x191cdc[_0x71ca('0x4b')]?_0x191cdc['attach']:_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')]['attach'],'substatus':null,'substatusAt':moment()[_0x71ca('0x51')](_0x71ca('0x4c')),'lastMsgAt':moment()[_0x71ca('0x51')](_0x71ca('0x4c')),'lastMsgDirection':'in','lastMsgBody':_0x2e2546['body']['body']});}else{return _0x191cdc;}}})[_0x71ca('0xd')](function(_0x5e5912){_0x316212[_0x71ca('0x52')]=_0x5e5912[_0x71ca('0x3a')]({'plain':!![]});_0x316212['interaction']['created']=_0x437e46;return db['MailMessage']['create']({'body':_0x2e2546['body']['body'],'MailAccountId':_0x2e2546[_0x71ca('0x3b')]['id'],'MailInteractionId':_0x5e5912['id'],'direction':'in','ContactId':_0x316212[_0x71ca('0x47')]['id'],'messageId':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x48')],'from':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x2e')],'to':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')]['to'],'cc':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')]['cc'],'subject':_0x2e2546['body'][_0x71ca('0x15')][_0x71ca('0x4a')],'attach':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x4b')],'Attachments':_0x2e2546[_0x71ca('0x25')]['message'][_0x71ca('0x53')],'originTo':_0x2e2546['body'][_0x71ca('0x15')]['originTo'],'originCc':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')]['originCc'],'bcc':_0x2e2546[_0x71ca('0x25')][_0x71ca('0x15')][_0x71ca('0x54')]},{'include':[{'model':db[_0x71ca('0x55')],'as':_0x71ca('0x56')}]});})[_0x71ca('0xd')](function(_0x6e5094){_0x316212['message']=_0x6e5094;if(_0x316212[_0x71ca('0x52')][_0x71ca('0x57')]){return db[_0x71ca('0x58')][_0x71ca('0x24')]({'attributes':['id',_0x71ca('0x59')],'where':{'id':_0x316212[_0x71ca('0x52')][_0x71ca('0x57')]}})[_0x71ca('0xd')](function(_0x4aaa73){if(_0x4aaa73){_0x316212[_0x71ca('0x42')]['unshift']({'id':0x0,'priority':0x0,'app':_0x71ca('0x5a'),'appdata':_0x4aaa73[_0x71ca('0x59')]+','+(_0x316212[_0x71ca('0x3b')][_0x71ca('0x5b')]||0xa),'interval':_0x71ca('0x5c')});}return respondWithRpcPromise(_0x71ca('0x5d'),'startRouting',_0x316212);});}return respondWithRpcPromise(_0x71ca('0x5d'),_0x71ca('0x5e'),_0x316212);})[_0x71ca('0xd')](function(_0x8b1c4f){_0x1ed83a(_0x8b1c4f);})[_0x71ca('0x17')](function(_0x2595c5){_0x973028(_0x9a7046[_0x71ca('0x12')](0x1f4,_0x2595c5['message']));});});};