3b7d2eb1865ccb1eeec3de7f4ba845f4b80944ba
[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 _0x4574=['then','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','stringify','error','message','result','catch','GetMailAccount','options','where','attributes','include','map','model','MailAccount','find','ShowMailAccount','NotifyMailAccount','body','difference','CmContact','rawAttributes','ListId','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','from','account','updatedAt','DESC','MailApplication','Applications','MailServerOut','Smtp','List','get','applications','list','asc','autoclose','length','*,*,*,*','Contacts','create','contact','messageId','generate','subject','attach','format','YYYY-MM-DD\x20HH:mm:ss','MailInteraction','MailMessage','Messages','inReplyTo','spread','update','interaction','created','originTo','bcc','Email','ChatOfflineMessageId','Attachment','Attachments','UserId','User','name','unshift','agent','startRouting','Start','moment','bluebird','randomstring','../../config/utils','../../config/logger','../../config/environment','jayson/promise','http','redis','localhost','socket.io-emitter','request'];(function(_0x230975,_0x3c93d8){var _0x1a7624=function(_0x21f79c){while(--_0x21f79c){_0x230975['push'](_0x230975['shift']());}};_0x1a7624(++_0x3c93d8);}(_0x4574,0x1bb));var _0x4457=function(_0xd05b4,_0x55abaa){_0xd05b4=_0xd05b4-0x0;var _0x58d35c=_0x4574[_0xd05b4];return _0x58d35c;};'use strict';var _=require('lodash');var util=require('util');var moment=require(_0x4457('0x0'));var BPromise=require(_0x4457('0x1'));var rs=require(_0x4457('0x2'));var fs=require('fs');var Redis=require('ioredis');var db=require('../../mysqldb')['db'];var utils=require(_0x4457('0x3'));var logger=require(_0x4457('0x4'))('rpc');var config=require(_0x4457('0x5'));var jayson=require(_0x4457('0x6'));var client=jayson['client'][_0x4457('0x7')]({'port':0x232c});config[_0x4457('0x8')]=_['defaults'](config[_0x4457('0x8')],{'host':_0x4457('0x9'),'port':0x18eb});var socket=require(_0x4457('0xa'))(new Redis(config[_0x4457('0x8')]));require('./mailAccount.socket')['register'](socket);function respondWithRpcPromise(_0x238eef,_0x476100,_0xbd65bd){return new BPromise(function(_0x13a3a8,_0x5b2d17){return client[_0x4457('0xb')](_0x238eef,_0xbd65bd)[_0x4457('0xc')](function(_0x482e2d){logger[_0x4457('0xd')](_0x4457('0xe'),_0x476100,_0x4457('0xf'));logger[_0x4457('0x10')](_0x4457('0x11'),_0x476100,'request\x20sent',JSON[_0x4457('0x12')](_0x482e2d));if(_0x482e2d[_0x4457('0x13')]){if(_0x482e2d[_0x4457('0x13')]['code']===0x1f4){logger[_0x4457('0x13')]('MailAccount,\x20%s,\x20%s',_0x476100,_0x482e2d['error'][_0x4457('0x14')]);return _0x5b2d17(_0x482e2d[_0x4457('0x13')][_0x4457('0x14')]);}logger[_0x4457('0x13')](_0x4457('0xe'),_0x476100,_0x482e2d['error']['message']);return _0x13a3a8(_0x482e2d[_0x4457('0x13')]['message']);}else{logger[_0x4457('0xd')](_0x4457('0xe'),_0x476100,_0x4457('0xf'));_0x13a3a8(_0x482e2d[_0x4457('0x15')][_0x4457('0x14')]);}})[_0x4457('0x16')](function(_0x45554d){logger['error']('MailAccount,\x20%s,\x20%s',_0x476100,_0x45554d);_0x5b2d17(_0x45554d);});});}exports[_0x4457('0x17')]=function(_0x4f8762){var _0x4e3e12=this;return new Promise(function(_0x25211a,_0x284ffd){return db['MailAccount']['findAll']({'raw':_0x4f8762['options']?_0x4f8762['options']['raw']===undefined?!![]:![]:!![],'where':_0x4f8762[_0x4457('0x18')]?_0x4f8762[_0x4457('0x18')][_0x4457('0x19')]||null:null,'attributes':_0x4f8762[_0x4457('0x18')]?_0x4f8762[_0x4457('0x18')][_0x4457('0x1a')]||null:null,'limit':_0x4f8762[_0x4457('0x18')]?_0x4f8762[_0x4457('0x18')]['limit']||null:null,'include':_0x4f8762[_0x4457('0x18')]?_0x4f8762[_0x4457('0x18')][_0x4457('0x1b')]?_[_0x4457('0x1c')](_0x4f8762[_0x4457('0x18')][_0x4457('0x1b')],function(_0x5dedd5){return{'model':db[_0x5dedd5[_0x4457('0x1d')]],'as':_0x5dedd5['as'],'attributes':_0x5dedd5['attributes'],'include':_0x5dedd5[_0x4457('0x1b')]?_['map'](_0x5dedd5[_0x4457('0x1b')],function(_0xa33aba){return{'model':db[_0xa33aba[_0x4457('0x1d')]],'as':_0xa33aba['as'],'attributes':_0xa33aba[_0x4457('0x1a')],'include':_0xa33aba['include']?_[_0x4457('0x1c')](_0xa33aba[_0x4457('0x1b')],function(_0x290309){return{'model':db[_0x290309[_0x4457('0x1d')]],'as':_0x290309['as'],'attributes':_0x290309[_0x4457('0x1a')]};}):[]};}):[]};}):[]:[]})[_0x4457('0xc')](function(_0x1d4e6b){logger[_0x4457('0xd')](_0x4457('0x17'),_0x4f8762);logger[_0x4457('0x10')](_0x4457('0x17'),_0x4f8762,JSON[_0x4457('0x12')](_0x1d4e6b));_0x25211a(_0x1d4e6b);})[_0x4457('0x16')](function(_0x492df8){logger[_0x4457('0x13')](_0x4457('0x17'),_0x492df8['message'],_0x4f8762);_0x284ffd(_0x4e3e12[_0x4457('0x13')](0x1f4,_0x492df8[_0x4457('0x14')]));});});};exports['ShowMailAccount']=function(_0x783977){var _0x3c16f7=this;return new Promise(function(_0x33b3bc,_0x280449){return db[_0x4457('0x1e')][_0x4457('0x1f')]({'raw':_0x783977['options']?_0x783977['options']['raw']===undefined?!![]:![]:!![],'where':_0x783977['options']?_0x783977['options']['where']||null:null,'attributes':_0x783977[_0x4457('0x18')]?_0x783977['options'][_0x4457('0x1a')]||null:null,'include':_0x783977[_0x4457('0x18')]?_0x783977[_0x4457('0x18')][_0x4457('0x1b')]?_[_0x4457('0x1c')](_0x783977['options'][_0x4457('0x1b')],function(_0xad369){return{'model':db[_0xad369[_0x4457('0x1d')]],'as':_0xad369['as'],'attributes':_0xad369[_0x4457('0x1a')],'include':_0xad369[_0x4457('0x1b')]?_['map'](_0xad369[_0x4457('0x1b')],function(_0x443df7){return{'model':db[_0x443df7[_0x4457('0x1d')]],'as':_0x443df7['as'],'attributes':_0x443df7[_0x4457('0x1a')],'include':_0x443df7[_0x4457('0x1b')]?_[_0x4457('0x1c')](_0x443df7[_0x4457('0x1b')],function(_0xc6c71a){return{'model':db[_0xc6c71a[_0x4457('0x1d')]],'as':_0xc6c71a['as'],'attributes':_0xc6c71a['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x56f972){logger['info'](_0x4457('0x20'),_0x783977);logger[_0x4457('0x10')]('ShowMailAccount',_0x783977,JSON[_0x4457('0x12')](_0x56f972));_0x33b3bc(_0x56f972);})[_0x4457('0x16')](function(_0x43e525){logger[_0x4457('0x13')](_0x4457('0x20'),_0x43e525[_0x4457('0x14')],_0x783977);_0x280449(_0x3c16f7[_0x4457('0x13')](0x1f4,_0x43e525['message']));});});};exports[_0x4457('0x21')]=function(_0x5a5115){var _0x5d26e6=this;return new Promise(function(_0x2b8f53,_0x27f697){var _0x133c9e={'body':_0x5a5115[_0x4457('0x22')],'channel':'mail'};var _0xb8c9e1=_[_0x4457('0x23')](_['keys'](db[_0x4457('0x24')][_0x4457('0x25')]),['createdAt','updatedAt','CompanyId',_0x4457('0x26')]);var _0x53b209=![];if(_0x5a5115[_0x4457('0x22')]['id']){delete _0x5a5115['body']['id'];}if(_[_0x4457('0x27')](_0x5a5115[_0x4457('0x22')]['from'])){return _0x27f697(_0x5d26e6[_0x4457('0x13')](0x1f4,_0x4457('0x28')));}if(_[_0x4457('0x27')](_0x5a5115[_0x4457('0x22')]['body'])||_0x5a5115[_0x4457('0x22')][_0x4457('0x22')]===''){return _0x27f697(_0x5d26e6[_0x4457('0x13')](0x1f4,_0x4457('0x29')));}if(_[_0x4457('0x27')](_0x5a5115[_0x4457('0x22')]['mapKey'])){return _0x27f697(_0x5d26e6[_0x4457('0x13')](0x1f4,_0x4457('0x2a')+_0xb8c9e1));}if(!_[_0x4457('0x2b')](_0xb8c9e1,_0x5a5115[_0x4457('0x22')][_0x4457('0x2c')])){return _0x27f697(_0x5d26e6[_0x4457('0x13')](0x1f4,'mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0xb8c9e1));}var _0x14c620={};_0x14c620[_0x5a5115['body']['mapKey']]=_0x5a5115['body'][_0x4457('0x2d')];return db['MailAccount'][_0x4457('0x1f')]({'where':{'id':_0x5a5115[_0x4457('0x2e')]['id']},'include':[{'model':db['CmList'],'as':'List','include':[{'model':db[_0x4457('0x24')],'as':'Contacts','where':_0x14c620,'limit':0x1,'order':[[_0x4457('0x2f'),_0x4457('0x30')]]}]},{'model':db[_0x4457('0x31')],'as':_0x4457('0x32')},{'model':db[_0x4457('0x33')],'as':_0x4457('0x34')}]})[_0x4457('0xc')](function(_0x1d4d86){if(_0x1d4d86&&_0x1d4d86[_0x4457('0x35')]){var _0x18e7d9=_0x1d4d86[_0x4457('0x36')]({'plain':!![]});_0x133c9e[_0x4457('0x2e')]=_['omit'](_0x18e7d9,['Applications',_0x4457('0x35')]);_0x133c9e[_0x4457('0x37')]=_0x18e7d9['Applications'];_0x133c9e[_0x4457('0x38')]=_0x18e7d9['List'];_0x133c9e['applications']=_['orderBy'](_0x133c9e['applications'],['priority'],[_0x4457('0x39')]);if(_0x133c9e[_0x4457('0x2e')]&&_0x133c9e[_0x4457('0x2e')][_0x4457('0x3a')]){_0x133c9e['applications']['push']({'id':0x0,'priority':_0x133c9e[_0x4457('0x37')][_0x4457('0x3b')]+0x1,'app':'close','appdata':_0x4457('0x3a'),'interval':_0x4457('0x3c')});}if(_0x133c9e[_0x4457('0x2e')][_0x4457('0x32')]){delete _0x133c9e[_0x4457('0x2e')][_0x4457('0x32')];}if(_0x133c9e['account'][_0x4457('0x35')]){delete _0x133c9e[_0x4457('0x2e')][_0x4457('0x35')];}if(_0x18e7d9['List'][_0x4457('0x3d')][_0x4457('0x3b')]){return _0x18e7d9[_0x4457('0x35')]['Contacts'][0x0];}var _0x59d3ab=_['defaults'](_0x5a5115['body'],{'firstName':_0x5a5115[_0x4457('0x22')][_0x4457('0x2d')],'ListId':_0x18e7d9[_0x4457('0x26')]});_0x59d3ab[_0x5a5115[_0x4457('0x22')][_0x4457('0x2c')]]=_0x5a5115[_0x4457('0x22')][_0x4457('0x2d')];return db['CmContact'][_0x4457('0x3e')](_0x59d3ab);}else{throw new Error('List\x20not\x20found');}})[_0x4457('0xc')](function(_0x36dab7){_0x133c9e[_0x4457('0x3f')]=_0x36dab7;if(_0x36dab7){if(!_0x5a5115[_0x4457('0x22')]['message'][_0x4457('0x40')]){_0x5a5115[_0x4457('0x22')][_0x4457('0x14')][_0x4457('0x40')]=rs[_0x4457('0x41')]()+'\x20'+_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x2d')];}var _0x5f4fe0={'ContactId':_0x36dab7['id'],'MailAccountId':_0x5a5115['account']['id'],'inReplyTo':_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x40')],'to':_0x5a5115[_0x4457('0x22')]['message'][_0x4457('0x2d')],'cc':_0x5a5115['body'][_0x4457('0x14')]['cc'],'subject':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')][_0x4457('0x42')],'attach':_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x43')],'lastMsgAt':moment()[_0x4457('0x44')](_0x4457('0x45')),'lastMsgDirection':'in'};return db[_0x4457('0x46')][_0x4457('0x1f')]({'where':{'closed':![],'MailAccountId':_0x5a5115[_0x4457('0x2e')]['id']},'include':[{'model':db[_0x4457('0x47')],'as':_0x4457('0x48'),'attributes':[_0x4457('0x40')],'where':{'messageId':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')][_0x4457('0x49')]}}]})['then'](function(_0x473dbb){if(_0x473dbb){return[_0x473dbb,![]];}return db[_0x4457('0x46')][_0x4457('0x3e')](_0x5f4fe0)['then'](function(_0x43c7c5){return[_0x43c7c5,!![]];});});}else{throw new Error('Contact\x20not\x20found');}})[_0x4457('0x4a')](function(_0x395d98,_0x2797c6){if(_0x395d98){_0x53b209=_0x2797c6;if(!_0x2797c6){return _0x395d98[_0x4457('0x4b')]({'inReplyTo':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')][_0x4457('0x40')],'to':_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x2d')],'cc':_0x5a5115['body'][_0x4457('0x14')]['cc'],'subject':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')]['subject'],'attach':_0x395d98[_0x4457('0x43')]?_0x395d98[_0x4457('0x43')]:_0x5a5115[_0x4457('0x22')]['message'][_0x4457('0x43')],'substatus':null,'substatusAt':moment()[_0x4457('0x44')](_0x4457('0x45')),'lastMsgAt':moment()[_0x4457('0x44')](_0x4457('0x45')),'lastMsgDirection':'in'});}else{return _0x395d98;}}})[_0x4457('0xc')](function(_0x45a56c){_0x133c9e['interaction']=_0x45a56c[_0x4457('0x36')]({'plain':!![]});_0x133c9e[_0x4457('0x4c')][_0x4457('0x4d')]=_0x53b209;return db[_0x4457('0x47')][_0x4457('0x3e')]({'body':_0x5a5115[_0x4457('0x22')]['body'],'MailAccountId':_0x5a5115['account']['id'],'MailInteractionId':_0x45a56c['id'],'direction':'in','ContactId':_0x133c9e[_0x4457('0x3f')]['id'],'messageId':_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x40')],'from':_0x5a5115['body'][_0x4457('0x14')]['from'],'to':_0x5a5115[_0x4457('0x22')]['message']['to'],'cc':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')]['cc'],'subject':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')][_0x4457('0x42')],'attach':_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x43')],'Attachments':_0x5a5115['body'][_0x4457('0x14')]['attachments'],'originTo':_0x5a5115[_0x4457('0x22')]['message'][_0x4457('0x4e')],'originCc':_0x5a5115[_0x4457('0x22')]['message']['originCc'],'bcc':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')][_0x4457('0x4f')],'ChatWebsiteId':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')]['ChatWebsiteId']||null,'originChannel':_0x5a5115[_0x4457('0x22')][_0x4457('0x14')]['originChannel']||_0x4457('0x50'),'ChatOfflineMessageId':_0x5a5115['body'][_0x4457('0x14')][_0x4457('0x51')]||null},{'include':[{'model':db[_0x4457('0x52')],'as':_0x4457('0x53')}]});})[_0x4457('0xc')](function(_0x235461){_0x133c9e[_0x4457('0x14')]=_0x235461;if(_0x133c9e[_0x4457('0x4c')][_0x4457('0x54')]){return db[_0x4457('0x55')][_0x4457('0x1f')]({'attributes':['id',_0x4457('0x56')],'where':{'id':_0x133c9e['interaction']['UserId']}})[_0x4457('0xc')](function(_0x46fd1e){if(_0x46fd1e){_0x133c9e[_0x4457('0x37')][_0x4457('0x57')]({'id':0x0,'priority':0x0,'app':_0x4457('0x58'),'appdata':_0x46fd1e[_0x4457('0x56')]+','+(_0x133c9e[_0x4457('0x2e')]['waitForTheAssignedAgent']||0xa),'interval':_0x4457('0x3c')});}return respondWithRpcPromise('Start',_0x4457('0x59'),_0x133c9e);});}return respondWithRpcPromise(_0x4457('0x5a'),_0x4457('0x59'),_0x133c9e);})[_0x4457('0xc')](function(_0x260d25){_0x2b8f53(_0x260d25);})[_0x4457('0x16')](function(_0x1b7e3a){_0x27f697(_0x5d26e6[_0x4457('0x13')](0x1f4,_0x1b7e3a[_0x4457('0x14')]));});});};