Built motion from commit 775e2a94.|2.6.3
[motion2.git] / server / api / mailMessage / mailMessage.controller.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 _0xd85e=['fromString','EventManager','reject','[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','fullname','interface','rejectmessage','download','unix','toString','join','server','files','from','subject','Attachment','createdAt','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','bcc','Bcc','Attachments','existsSync','push','readFileSync','build','writeFileSync','unlinkSync','eml-format','moment','util','path','lodash','ioredis','html-to-text','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./mailMessage.socket','register','client','http','then','MailMessage,\x20%s,\x20%s','MailMessage,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','message','result','catch','status','json','offset','undefined','count','limit','set','Content-Range','update','destroy','end','sendStatus','name','index','MailMessage','rawAttributes','fieldName','type','key','model','map','query','filters','intersection','fields','attributes','length','hasOwnProperty','order','sort','where','pick','merge','VIRTUAL','options','includeAll','include','show','keys','body','find','params','describe','create','direction','out','secret','MailAccount','findOne','MailAccountId','CmList','List','CmContact','Contacts','updatedAt','DESC','MailServerOut','Smtp','CloudProvider','attachments','root','server/files/attachments','basename','SendMail','messageId','format','mail','read','readAt','YYYY-MM-DD\x20HH:mm:ss','UserId','user','agent','get','MailInteraction','MailInteractionId','read1stAt','interaction','null','AcceptMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','sequelize','Sequelize','SELECT','contact','User','email','internal','SIP/%s','channel','motionChannel','event','accept'];(function(_0x167cc1,_0x56ef5a){var _0x10bc4b=function(_0xd6a255){while(--_0xd6a255){_0x167cc1['push'](_0x167cc1['shift']());}};_0x10bc4b(++_0x56ef5a);}(_0xd85e,0xb4));var _0xed85=function(_0x5338bb,_0x3231ae){_0x5338bb=_0x5338bb-0x0;var _0x548661=_0xd85e[_0x5338bb];return _0x548661;};'use strict';var emlformat=require(_0xed85('0x0'));var moment=require(_0xed85('0x1'));var BPromise=require('bluebird');var util=require(_0xed85('0x2'));var path=require(_0xed85('0x3'));var fs=require('fs');var _=require(_0xed85('0x4'));var Redis=require(_0xed85('0x5'));var htmlToText=require(_0xed85('0x6'));var qs=require('../../components/parsers/qs');var logger=require(_0xed85('0x7'))(_0xed85('0x8'));var interaction_log=require(_0xed85('0x7'))('mail-interactions');var config=require(_0xed85('0x9'));var db=require(_0xed85('0xa'))['db'];config[_0xed85('0xb')]=_[_0xed85('0xc')](config[_0xed85('0xb')],{'host':_0xed85('0xd'),'port':0x18eb});var socket=require(_0xed85('0xe'))(new Redis(config['redis']));var htmlEntities=require('html-entities');require(_0xed85('0xf'))[_0xed85('0x10')](socket);var jayson=require('jayson/promise');var client=jayson[_0xed85('0x11')][_0xed85('0x12')]({'port':0x232c});var client9003=jayson['client'][_0xed85('0x12')]({'port':0x232b});var client9002=jayson[_0xed85('0x11')][_0xed85('0x12')]({'port':0x232a});function respondWithRpcPromise(_0xa926cc,_0x4b624c,_0x2a22fd,_0x4d41e2){return new BPromise(function(_0x7b7d70,_0x5364dd){var _0x5b903e=_0x4d41e2||client;return _0x5b903e['request'](_0xa926cc,_0x2a22fd)[_0xed85('0x13')](function(_0x47f359){logger['info'](_0xed85('0x14'),_0x4b624c,'request\x20sent');logger['debug'](_0xed85('0x15'),_0x4b624c,_0xed85('0x16'),JSON[_0xed85('0x17')](_0x47f359));if(_0x47f359[_0xed85('0x18')]){if(_0x47f359[_0xed85('0x18')]['code']===0x1f4){logger['error']('MailMessage,\x20%s,\x20%s',_0x4b624c,_0x47f359[_0xed85('0x18')][_0xed85('0x19')]);return _0x5364dd(_0x47f359[_0xed85('0x18')][_0xed85('0x19')]);}logger[_0xed85('0x18')](_0xed85('0x14'),_0x4b624c,_0x47f359[_0xed85('0x18')][_0xed85('0x19')]);return _0x7b7d70(_0x47f359['error'][_0xed85('0x19')]);}else{logger['info']('MailMessage,\x20%s,\x20%s',_0x4b624c,_0xed85('0x16'));_0x7b7d70(_0x47f359[_0xed85('0x1a')][_0xed85('0x19')]);}})[_0xed85('0x1b')](function(_0x875110){logger[_0xed85('0x18')](_0xed85('0x14'),_0x4b624c,_0x875110);_0x5364dd(_0x875110);});});}function respondWithResult(_0x234de5,_0x434864){_0x434864=_0x434864||0xc8;return function(_0x6b5f84){if(_0x6b5f84){return _0x234de5[_0xed85('0x1c')](_0x434864)[_0xed85('0x1d')](_0x6b5f84);}};}function respondWithFilteredResult(_0x59eab3,_0x281139){return function(_0x2f7924){if(_0x2f7924){var _0x134696=typeof _0x281139[_0xed85('0x1e')]===_0xed85('0x1f')&&typeof _0x281139['limit']===_0xed85('0x1f');var _0x3c00e8=_0x2f7924[_0xed85('0x20')];var _0x3efcfc=_0x134696?0x0:_0x281139[_0xed85('0x1e')];var _0x5a4e1b=_0x134696?_0x2f7924[_0xed85('0x20')]:_0x281139['offset']+_0x281139[_0xed85('0x21')];var _0x2596be;if(_0x5a4e1b>=_0x3c00e8){_0x5a4e1b=_0x3c00e8;_0x2596be=0xc8;}else{_0x2596be=0xce;}_0x59eab3[_0xed85('0x1c')](_0x2596be);return _0x59eab3[_0xed85('0x22')](_0xed85('0x23'),_0x3efcfc+'-'+_0x5a4e1b+'/'+_0x3c00e8)['json'](_0x2f7924);}return null;};}function saveUpdates(_0x60b64){return function(_0x448f72){if(_0x448f72){return _0x448f72[_0xed85('0x24')](_0x60b64)[_0xed85('0x13')](function(_0xeebc1d){return _0xeebc1d;});}return null;};}function removeEntity(_0x32b1e7){return function(_0x373031){if(_0x373031){return _0x373031[_0xed85('0x25')]()[_0xed85('0x13')](function(){_0x32b1e7[_0xed85('0x1c')](0xcc)[_0xed85('0x26')]();});}};}function handleEntityNotFound(_0x119f0f){return function(_0x3f7f24){if(!_0x3f7f24){_0x119f0f[_0xed85('0x27')](0x194);}return _0x3f7f24;};}function handleError(_0x17fc36,_0x278b53){_0x278b53=_0x278b53||0x1f4;return function(_0x123852){logger['error'](_0x123852['stack']);if(_0x123852[_0xed85('0x28')]){delete _0x123852[_0xed85('0x28')];}_0x17fc36[_0xed85('0x1c')](_0x278b53)['send'](_0x123852);};}exports[_0xed85('0x29')]=function(_0x18a2bc,_0x2db95b){var _0x1ba9e4={},_0x4c4e6e={},_0x10ac19={'count':0x0,'rows':[]};var _0x12e12f=_['map'](db[_0xed85('0x2a')][_0xed85('0x2b')],function(_0x437715){return{'name':_0x437715[_0xed85('0x2c')],'type':_0x437715[_0xed85('0x2d')][_0xed85('0x2e')]};});_0x4c4e6e[_0xed85('0x2f')]=_[_0xed85('0x30')](_0x12e12f,'name');_0x4c4e6e[_0xed85('0x31')]=_['keys'](_0x18a2bc[_0xed85('0x31')]);_0x4c4e6e[_0xed85('0x32')]=_[_0xed85('0x33')](_0x4c4e6e[_0xed85('0x2f')],_0x4c4e6e[_0xed85('0x31')]);_0x1ba9e4['attributes']=_[_0xed85('0x33')](_0x4c4e6e[_0xed85('0x2f')],qs[_0xed85('0x34')](_0x18a2bc[_0xed85('0x31')][_0xed85('0x34')]));_0x1ba9e4['attributes']=_0x1ba9e4[_0xed85('0x35')][_0xed85('0x36')]?_0x1ba9e4[_0xed85('0x35')]:_0x4c4e6e[_0xed85('0x2f')];if(!_0x18a2bc['query'][_0xed85('0x37')]('nolimit')){_0x1ba9e4[_0xed85('0x21')]=qs[_0xed85('0x21')](_0x18a2bc[_0xed85('0x31')][_0xed85('0x21')]);_0x1ba9e4[_0xed85('0x1e')]=qs[_0xed85('0x1e')](_0x18a2bc[_0xed85('0x31')][_0xed85('0x1e')]);}_0x1ba9e4[_0xed85('0x38')]=qs[_0xed85('0x39')](_0x18a2bc[_0xed85('0x31')][_0xed85('0x39')]);_0x1ba9e4[_0xed85('0x3a')]=qs[_0xed85('0x32')](_[_0xed85('0x3b')](_0x18a2bc[_0xed85('0x31')],_0x4c4e6e[_0xed85('0x32')]),_0x12e12f);if(_0x18a2bc[_0xed85('0x31')]['filter']){_0x1ba9e4[_0xed85('0x3a')]=_[_0xed85('0x3c')](_0x1ba9e4['where'],{'$or':_[_0xed85('0x30')](_0x12e12f,function(_0x51897e){if(_0x51897e[_0xed85('0x2d')]!==_0xed85('0x3d')){var _0x57d8e6={};_0x57d8e6[_0x51897e['name']]={'$like':'%'+_0x18a2bc['query']['filter']+'%'};return _0x57d8e6;}})});}_0x1ba9e4=_[_0xed85('0x3c')]({},_0x1ba9e4,_0x18a2bc[_0xed85('0x3e')]);var _0x10cf7b={'where':_0x1ba9e4[_0xed85('0x3a')]};return db[_0xed85('0x2a')][_0xed85('0x20')](_0x10cf7b)[_0xed85('0x13')](function(_0x1dcc38){_0x10ac19[_0xed85('0x20')]=_0x1dcc38;if(_0x18a2bc[_0xed85('0x31')][_0xed85('0x3f')]){_0x1ba9e4[_0xed85('0x40')]=[{'all':!![]}];}return db[_0xed85('0x2a')]['findAll'](_0x1ba9e4);})[_0xed85('0x13')](function(_0x19aa4a){_0x10ac19['rows']=_0x19aa4a;return _0x10ac19;})['then'](respondWithFilteredResult(_0x2db95b,_0x1ba9e4))[_0xed85('0x1b')](handleError(_0x2db95b,null));};exports[_0xed85('0x41')]=function(_0x34103d,_0x4c725d){var _0x5341f8={'raw':!![],'where':{'id':_0x34103d['params']['id']}},_0x437eea={};_0x437eea[_0xed85('0x2f')]=_[_0xed85('0x42')](db[_0xed85('0x2a')][_0xed85('0x2b')]);_0x437eea[_0xed85('0x31')]=_['keys'](_0x34103d[_0xed85('0x31')]);_0x437eea[_0xed85('0x32')]=_[_0xed85('0x33')](_0x437eea[_0xed85('0x2f')],_0x437eea[_0xed85('0x31')]);_0x5341f8[_0xed85('0x35')]=_['intersection'](_0x437eea[_0xed85('0x2f')],qs[_0xed85('0x34')](_0x34103d[_0xed85('0x31')][_0xed85('0x34')]));_0x5341f8[_0xed85('0x35')]=_0x5341f8[_0xed85('0x35')][_0xed85('0x36')]?_0x5341f8[_0xed85('0x35')]:_0x437eea[_0xed85('0x2f')];if(_0x34103d['query']['includeAll']){_0x5341f8['include']=[{'all':!![]}];}_0x5341f8=_[_0xed85('0x3c')]({},_0x5341f8,_0x34103d[_0xed85('0x3e')]);return db['MailMessage']['find'](_0x5341f8)[_0xed85('0x13')](handleEntityNotFound(_0x4c725d,null))[_0xed85('0x13')](respondWithResult(_0x4c725d,null))['catch'](handleError(_0x4c725d,null));};exports[_0xed85('0x24')]=function(_0x405058,_0x39b0d3){if(_0x405058[_0xed85('0x43')]['id']){delete _0x405058['body']['id'];}return db[_0xed85('0x2a')][_0xed85('0x44')]({'where':{'id':_0x405058[_0xed85('0x45')]['id']}})[_0xed85('0x13')](handleEntityNotFound(_0x39b0d3,null))[_0xed85('0x13')](saveUpdates(_0x405058[_0xed85('0x43')],null))[_0xed85('0x13')](respondWithResult(_0x39b0d3,null))['catch'](handleError(_0x39b0d3,null));};exports[_0xed85('0x25')]=function(_0x513a18,_0x3ec00f){return db[_0xed85('0x2a')][_0xed85('0x44')]({'where':{'id':_0x513a18['params']['id']}})[_0xed85('0x13')](handleEntityNotFound(_0x3ec00f,null))['then'](removeEntity(_0x3ec00f,null))[_0xed85('0x1b')](handleError(_0x3ec00f,null));};exports[_0xed85('0x46')]=function(_0xfb47de,_0x13c61c){return db['MailMessage'][_0xed85('0x46')]()[_0xed85('0x13')](respondWithResult(_0x13c61c,null))[_0xed85('0x1b')](handleError(_0x13c61c,null));};exports[_0xed85('0x47')]=function(_0x21c919,_0x2c2c13){return db['MailMessage'][_0xed85('0x47')](_0x21c919[_0xed85('0x43')])['then'](function(_0x4eca26){if(_0x4eca26){if(_0x4eca26[_0xed85('0x48')]===_0xed85('0x49')&&!_0x4eca26[_0xed85('0x4a')]){var _0x2fff10,_0x16f1c3,_0x42c5b0;if(_0x21c919[_0xed85('0x43')]['to']){_0x16f1c3=_0x21c919['body']['to']['split'](',')[0x0];}return db[_0xed85('0x4b')][_0xed85('0x4c')]({'where':{'id':_0x4eca26[_0xed85('0x4d')]},'include':[{'model':db[_0xed85('0x4e')],'as':_0xed85('0x4f'),'include':[{'model':db[_0xed85('0x50')],'as':_0xed85('0x51'),'where':{'email':_0x16f1c3},'limit':0x1,'order':[[_0xed85('0x52'),_0xed85('0x53')]]}]},{'model':db[_0xed85('0x54')],'as':_0xed85('0x55'),'raw':!![],'include':[{'model':db[_0xed85('0x56')],'as':_0xed85('0x56'),'raw':!![]}]}]})[_0xed85('0x13')](function(_0x29a407){_0x2fff10=_0x29a407;if(!_0x2fff10||!_0x2fff10[_0xed85('0x55')])return;var _0x13b973=_0x2fff10['getSmtpOptions']();if(_0x21c919['body'][_0xed85('0x57')]&&_0x21c919[_0xed85('0x43')][_0xed85('0x57')]['length']){for(var _0x21c0a7=0x0;_0x21c0a7<_0x21c919['body'][_0xed85('0x57')]['length'];_0x21c0a7+=0x1){_0x21c919[_0xed85('0x43')][_0xed85('0x57')][_0x21c0a7]={'filename':_0x21c919[_0xed85('0x43')][_0xed85('0x57')][_0x21c0a7][_0xed85('0x28')],'path':path['join'](config[_0xed85('0x58')],_0xed85('0x59'),_0x21c919[_0xed85('0x43')][_0xed85('0x57')][_0x21c0a7][_0xed85('0x5a')])};}}_0x42c5b0={'account':_0x13b973,'message':_0x21c919[_0xed85('0x43')]};return;})['then'](function(){if(_0x2fff10[_0xed85('0x4f')]){if(_0x2fff10[_0xed85('0x4f')][_0xed85('0x51')][_0xed85('0x36')]){return _0x2fff10['List']['Contacts'][0x0];}else{return db[_0xed85('0x50')][_0xed85('0x47')](_['defaults'](_0x21c919[_0xed85('0x43')],{'firstName':_0x21c919[_0xed85('0x43')]['to'],'email':_0x21c919[_0xed85('0x43')]['to'],'phone':_0x21c919[_0xed85('0x43')]['to'],'ListId':_0x2fff10['ListId']}));}}else{throw new Error('List\x20not\x20found');}})[_0xed85('0x13')](function(){if(_0x42c5b0){return respondWithRpcPromise(_0xed85('0x5b'),'sendMail',_0x42c5b0,client9003)[_0xed85('0x13')](function(_0x44531e){return _0x4eca26[_0xed85('0x24')]({'read':!![],'messageId':_0x44531e[_0xed85('0x5c')],'sentAt':moment()[_0xed85('0x5d')]('YYYY-MM-DD\x20HH:mm:ss')});})['catch'](function(_0x5d8554){logger[_0xed85('0x18')]('%s',JSON['stringify'](_0x5d8554));return _0x4eca26;});}return _0x4eca26;});}return _0x4eca26;}})[_0xed85('0x13')](respondWithResult(_0x2c2c13,0xc9))[_0xed85('0x1b')](handleError(_0x2c2c13,null));};exports['accept']=function(_0x20f3b3,_0x32510e){var _0x29f812={'agent':{},'channel':_0xed85('0x5e')};if(_0x20f3b3[_0xed85('0x43')]['id']){delete _0x20f3b3[_0xed85('0x43')]['id'];}_0x20f3b3['body'][_0xed85('0x5f')]=!![];_0x20f3b3['body'][_0xed85('0x60')]=moment()[_0xed85('0x5d')](_0xed85('0x61'));_0x20f3b3[_0xed85('0x43')][_0xed85('0x62')]=_0x20f3b3[_0xed85('0x43')][_0xed85('0x62')]||_0x20f3b3[_0xed85('0x63')]['id'];_0x29f812[_0xed85('0x64')]['id']=_0x20f3b3[_0xed85('0x43')][_0xed85('0x62')];return db[_0xed85('0x2a')][_0xed85('0x44')]({'where':{'id':_0x20f3b3[_0xed85('0x45')]['id'],'UserId':null}})[_0xed85('0x13')](handleEntityNotFound(_0x32510e,null))[_0xed85('0x13')](saveUpdates(_0x20f3b3[_0xed85('0x43')],null))[_0xed85('0x13')](function(_0xb65f74){if(_0xb65f74){_0x29f812[_0xed85('0x19')]=_0xb65f74[_0xed85('0x65')]({'plain':!![]});return db[_0xed85('0x66')][_0xed85('0x44')]({'where':{'id':_0xb65f74[_0xed85('0x67')]}});}return null;})[_0xed85('0x13')](handleEntityNotFound(_0x32510e,null))['then'](function(_0x444706){if(_0x444706){return _0x444706[_0xed85('0x24')]({'UserId':_0x20f3b3[_0xed85('0x43')][_0xed85('0x62')],'read1stAt':_['isNil'](_0x444706[_0xed85('0x68')])?moment()[_0xed85('0x5d')]('YYYY-MM-DD\x20HH:mm:ss'):undefined});}return null;})[_0xed85('0x13')](function(_0xcadb15){if(_0xcadb15){_0x29f812[_0xed85('0x69')]=_0xcadb15[_0xed85('0x65')]({'plain':!![]});interaction_log['info']('[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x20f3b3[_0xed85('0x63')]['id'],_0x20f3b3[_0xed85('0x63')][_0xed85('0x28')],_0x20f3b3['user']['role'],_0x29f812[_0xed85('0x69')]['id'],_0x20f3b3[_0xed85('0x43')]?JSON[_0xed85('0x17')](_0x20f3b3[_0xed85('0x43')]):_0xed85('0x6a'));return respondWithRpcPromise(_0xed85('0x6b'),'acceptMessage',_0x29f812);}return null;})[_0xed85('0x13')](function(_0xc2056a){var _0xbe88b5=_0xed85('0x6c')+_0xc2056a[_0xed85('0x19')][_0xed85('0x6d')];return db[_0xed85('0x6e')][_0xed85('0x31')](_0xbe88b5,{'type':db[_0xed85('0x6f')]['QueryTypes'][_0xed85('0x70')],'raw':!![]})[_0xed85('0x13')](function(_0xd8bd00){return _0xd8bd00;});})[_0xed85('0x13')](function(_0x1bd3b8){if(_0x1bd3b8){_0x29f812[_0xed85('0x19')][_0xed85('0x71')]=_0x1bd3b8[0x0];if(!_0x20f3b3[_0xed85('0x43')][_0xed85('0x62')])return;return db[_0xed85('0x72')][_0xed85('0x44')]({'where':{'id':_0x20f3b3[_0xed85('0x43')]['UserId'],'role':_0xed85('0x64')},'attributes':['id',_0xed85('0x28'),'fullname',_0xed85('0x73'),_0xed85('0x74')],'raw':!![]});}return null;})[_0xed85('0x13')](function(_0x40e75d){if(!_0x40e75d)return null;_0x29f812['message'][_0xed85('0x64')]=_0x40e75d;return _0x29f812;})[_0xed85('0x13')](function(_0x3b3e74){if(_0x3b3e74){if(_0x20f3b3['body']['manual']){_0x29f812['message']['interface']=util[_0xed85('0x5d')](_0xed85('0x75'),_0x20f3b3[_0xed85('0x63')][_0xed85('0x28')]);_0x29f812[_0xed85('0x19')][_0xed85('0x76')]=_0x29f812[_0xed85('0x76')];_0x29f812[_0xed85('0x19')][_0xed85('0x77')]=_0x29f812[_0xed85('0x76')];_0x29f812[_0xed85('0x19')][_0xed85('0x78')]=_0xed85('0x79');_0x29f812[_0xed85('0x19')][_0xed85('0x43')]=htmlToText[_0xed85('0x7a')](_0x29f812[_0xed85('0x19')][_0xed85('0x43')]);return respondWithRpcPromise(_0xed85('0x7b'),_0xed85('0x7b'),{'event':'acceptmessage','message':_0x29f812[_0xed85('0x19')]},client9002)[_0xed85('0x13')](function(){return _0x29f812;});}return _0x29f812;}return null;})['then'](respondWithResult(_0x32510e,null))[_0xed85('0x1b')](handleError(_0x32510e,null));};exports[_0xed85('0x7c')]=function(_0x27e3e5,_0x1d0eb9){var _0x541858={'agent':{},'channel':_0xed85('0x5e')};if(_0x27e3e5[_0xed85('0x43')]['id']){delete _0x27e3e5[_0xed85('0x43')]['id'];}_0x27e3e5['body'][_0xed85('0x62')]=_0x27e3e5[_0xed85('0x43')][_0xed85('0x62')]||_0x27e3e5[_0xed85('0x63')]['id'];_0x541858[_0xed85('0x64')]['id']=_0x27e3e5['body'][_0xed85('0x62')];return db['MailMessage'][_0xed85('0x44')]({'where':{'id':_0x27e3e5['params']['id'],'UserId':null}})['then'](handleEntityNotFound(_0x1d0eb9,null))['then'](function(_0x2e39ae){if(_0x2e39ae){_0x541858['message']=_0x2e39ae[_0xed85('0x65')]({'plain':!![]});return db[_0xed85('0x66')][_0xed85('0x44')]({'where':{'id':_0x2e39ae['MailInteractionId']}});}return null;})['then'](handleEntityNotFound(_0x1d0eb9,null))[_0xed85('0x13')](function(_0x27fc1f){if(_0x27fc1f){_0x541858[_0xed85('0x69')]=_0x27fc1f[_0xed85('0x65')]({'plain':!![]});interaction_log['info'](_0xed85('0x7d'),_0x27e3e5[_0xed85('0x63')]['id'],_0x27e3e5[_0xed85('0x63')][_0xed85('0x28')],_0x27e3e5[_0xed85('0x63')]['role'],_0x541858[_0xed85('0x69')]['id'],_0x27e3e5[_0xed85('0x43')]?JSON[_0xed85('0x17')](_0x27e3e5['body']):'null');return respondWithRpcPromise(_0xed85('0x7e'),_0xed85('0x7f'),_0x541858);}return null;})['then'](function(_0xef9341){var _0x333763=_0xed85('0x6c')+_0xef9341[_0xed85('0x19')]['ContactId'];return db[_0xed85('0x6e')][_0xed85('0x31')](_0x333763,{'type':db['Sequelize']['QueryTypes'][_0xed85('0x70')],'raw':!![]})[_0xed85('0x13')](function(_0xf99058){return _0xf99058;});})[_0xed85('0x13')](function(_0x14737f){if(_0x14737f){_0x541858[_0xed85('0x19')][_0xed85('0x71')]=_0x14737f[0x0];if(!_0x27e3e5['body']['UserId'])return;return db[_0xed85('0x72')]['find']({'where':{'id':_0x27e3e5[_0xed85('0x43')][_0xed85('0x62')],'role':_0xed85('0x64')},'attributes':['id','name',_0xed85('0x80'),_0xed85('0x73'),_0xed85('0x74')],'raw':!![]});}return null;})[_0xed85('0x13')](function(_0x1be5ef){if(!_0x1be5ef)return null;_0x541858[_0xed85('0x19')][_0xed85('0x64')]=_0x1be5ef;return _0x541858;})[_0xed85('0x13')](function(_0x4caa56){if(_0x4caa56){_0x4caa56[_0xed85('0x19')][_0xed85('0x81')]=util[_0xed85('0x5d')](_0xed85('0x75'),_0x27e3e5[_0xed85('0x63')][_0xed85('0x28')]);_0x4caa56[_0xed85('0x19')][_0xed85('0x76')]=_0x541858[_0xed85('0x76')];_0x4caa56[_0xed85('0x19')][_0xed85('0x77')]=_0x541858[_0xed85('0x76')];_0x4caa56[_0xed85('0x19')][_0xed85('0x78')]=_0xed85('0x7c');_0x4caa56[_0xed85('0x19')][_0xed85('0x43')]=htmlToText[_0xed85('0x7a')](_0x541858[_0xed85('0x19')][_0xed85('0x43')]);respondWithRpcPromise(_0xed85('0x7b'),_0xed85('0x7b'),{'event':_0xed85('0x82'),'message':_0x4caa56[_0xed85('0x19')]},client9002);return _0x541858;}return null;})[_0xed85('0x13')](respondWithResult(_0x1d0eb9,null))['catch'](handleError(_0x1d0eb9,null));};exports[_0xed85('0x83')]=function(_0x3880f7,_0x877bb6){var _0x80274c=moment()[_0xed85('0x84')]()[_0xed85('0x85')]();var _0x161e37=path[_0xed85('0x86')](config[_0xed85('0x58')],_0xed85('0x87'),_0xed85('0x88'),'tmp');var _0x3f2ed1=path[_0xed85('0x86')](config[_0xed85('0x58')],_0xed85('0x87'),_0xed85('0x88'),_0xed85('0x57'));var _0x246c64=util[_0xed85('0x5d')]('message-%s.eml',_0x80274c);return db[_0xed85('0x2a')]['find']({'where':{'id':_0x3880f7[_0xed85('0x45')]['id']},'attributes':['id','body',_0xed85('0x89'),'to','cc','bcc',_0xed85('0x8a'),'createdAt'],'include':_0x3880f7[_0xed85('0x31')][_0xed85('0x57')]?[{'model':db[_0xed85('0x8b')],'as':'Attachments'}]:undefined})['then'](handleEntityNotFound(_0x877bb6,null))[_0xed85('0x13')](function(_0x35230c){if(_0x35230c){var _0x32dc5c=_0x35230c;var _0x20d31f={'from':_0x32dc5c[_0xed85('0x89')],'to':_0x32dc5c['to'],'subject':_0x32dc5c['subject'],'html':htmlEntities['decode'](_0x32dc5c['body']),'headers':{'Date':moment(_0x32dc5c[_0xed85('0x8c')])[_0xed85('0x5d')](_0xed85('0x8d'))},'attachments':[]};if(_0x35230c['cc']){_0x20d31f['headers']['Cc']=_0x35230c['cc'];}if(_0x35230c[_0xed85('0x8e')]){_0x20d31f['headers'][_0xed85('0x8f')]=_0x35230c[_0xed85('0x8e')];}return new BPromise(function(_0x23900d,_0x5ee571){if(_0x35230c[_0xed85('0x90')]){for(var _0x342f9a=0x0;_0x342f9a<_0x35230c['Attachments'][_0xed85('0x36')];_0x342f9a++){var _0x15d9f7=_0x35230c[_0xed85('0x90')][_0x342f9a];var _0x5c9dab=path[_0xed85('0x86')](_0x3f2ed1,_0x15d9f7[_0xed85('0x5a')]);if(fs[_0xed85('0x91')](_0x5c9dab)){_0x20d31f[_0xed85('0x57')][_0xed85('0x92')]({'name':_0x15d9f7['name'],'contentType':_0x15d9f7[_0xed85('0x2d')],'data':fs[_0xed85('0x93')](_0x5c9dab)});}}}emlformat[_0xed85('0x94')](_0x20d31f,function(_0x5bb9f6,_0x655e24){if(_0x5bb9f6)return _0x5ee571(_0x5bb9f6);fs[_0xed85('0x95')](path[_0xed85('0x86')](_0x161e37,_0x246c64),_0x655e24);return _0x23900d(_0x655e24);});});}})[_0xed85('0x13')](function(_0x490b9c){if(_0x490b9c){return _0x877bb6[_0xed85('0x83')](path[_0xed85('0x86')](_0x161e37,_0x246c64),_0x246c64,function(_0x39ea6e){if(!_0x39ea6e)fs[_0xed85('0x96')](path[_0xed85('0x86')](_0x161e37,_0x246c64));});}})[_0xed85('0x1b')](handleError(_0x877bb6,null));};