Built motion from commit 97153652.|2.6.26
[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 _0x6f5f=['SendMail','sendMail','messageId','YYYY-MM-DD\x20HH:mm:ss','accept','mail','readAt','format','UserId','agent','isNil','read1stAt','interaction','get','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','user','role','null','AcceptMessage','acceptMessage','ContactId','Sequelize','SELECT','User','fullname','email','internal','manual','interface','channel','motionChannel','event','fromString','EventManager','acceptmessage','MailInteraction','[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','sequelize','QueryTypes','contact','SIP/%s','reject','rejectmessage','download','unix','toString','server','files','tmp','message-%s.eml','from','bcc','subject','createdAt','Attachments','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','existsSync','push','readFileSync','build','writeFileSync','eml-format','moment','bluebird','util','lodash','ioredis','html-to-text','../../components/parsers/qs','../../config/logger','api','../../mysqldb','redis','defaults','localhost','socket.io-emitter','html-entities','./mailMessage.socket','register','jayson/promise','client','http','then','info','request\x20sent','MailMessage,\x20%s,\x20%s,\x20%s','stringify','error','MailMessage,\x20%s,\x20%s','message','result','catch','status','offset','undefined','limit','count','set','Content-Range','json','update','destroy','end','sendStatus','name','send','index','map','MailMessage','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','findAll','rows','show','params','rawAttributes','include','find','body','describe','create','direction','secret','split','findOne','MailAccountId','CmList','CmContact','updatedAt','MailServerOut','Smtp','CloudProvider','getSmtpOptions','attachments','join','root','server/files/attachments','basename','List','Contacts','ListId','List\x20not\x20found'];(function(_0x1e2080,_0x53283b){var _0x260111=function(_0x15da5e){while(--_0x15da5e){_0x1e2080['push'](_0x1e2080['shift']());}};_0x260111(++_0x53283b);}(_0x6f5f,0xe7));var _0xf6f5=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x6f5f[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0xf6f5('0x0'));var moment=require(_0xf6f5('0x1'));var BPromise=require(_0xf6f5('0x2'));var util=require(_0xf6f5('0x3'));var path=require('path');var fs=require('fs');var _=require(_0xf6f5('0x4'));var Redis=require(_0xf6f5('0x5'));var htmlToText=require(_0xf6f5('0x6'));var qs=require(_0xf6f5('0x7'));var logger=require(_0xf6f5('0x8'))(_0xf6f5('0x9'));var interaction_log=require(_0xf6f5('0x8'))('mail-interactions');var config=require('../../config/environment');var db=require(_0xf6f5('0xa'))['db'];config[_0xf6f5('0xb')]=_[_0xf6f5('0xc')](config[_0xf6f5('0xb')],{'host':_0xf6f5('0xd'),'port':0x18eb});var socket=require(_0xf6f5('0xe'))(new Redis(config[_0xf6f5('0xb')]));var htmlEntities=require(_0xf6f5('0xf'));require(_0xf6f5('0x10'))[_0xf6f5('0x11')](socket);var jayson=require(_0xf6f5('0x12'));var client=jayson[_0xf6f5('0x13')][_0xf6f5('0x14')]({'port':0x232c});var client9003=jayson[_0xf6f5('0x13')][_0xf6f5('0x14')]({'port':0x232b});var client9002=jayson[_0xf6f5('0x13')][_0xf6f5('0x14')]({'port':0x232a});function respondWithRpcPromise(_0x44290c,_0x425822,_0x8d6f6,_0x125677){return new BPromise(function(_0x52c6e3,_0x244499){var _0x3afb95=_0x125677||client;return _0x3afb95['request'](_0x44290c,_0x8d6f6)[_0xf6f5('0x15')](function(_0x116a35){logger[_0xf6f5('0x16')]('MailMessage,\x20%s,\x20%s',_0x425822,_0xf6f5('0x17'));logger['debug'](_0xf6f5('0x18'),_0x425822,_0xf6f5('0x17'),JSON[_0xf6f5('0x19')](_0x116a35));if(_0x116a35[_0xf6f5('0x1a')]){if(_0x116a35[_0xf6f5('0x1a')]['code']===0x1f4){logger['error'](_0xf6f5('0x1b'),_0x425822,_0x116a35[_0xf6f5('0x1a')][_0xf6f5('0x1c')]);return _0x244499(_0x116a35[_0xf6f5('0x1a')][_0xf6f5('0x1c')]);}logger[_0xf6f5('0x1a')](_0xf6f5('0x1b'),_0x425822,_0x116a35['error'][_0xf6f5('0x1c')]);return _0x52c6e3(_0x116a35[_0xf6f5('0x1a')][_0xf6f5('0x1c')]);}else{logger['info'](_0xf6f5('0x1b'),_0x425822,_0xf6f5('0x17'));_0x52c6e3(_0x116a35[_0xf6f5('0x1d')][_0xf6f5('0x1c')]);}})[_0xf6f5('0x1e')](function(_0x667aad){logger[_0xf6f5('0x1a')](_0xf6f5('0x1b'),_0x425822,_0x667aad);_0x244499(_0x667aad);});});}function respondWithResult(_0x110a70,_0x3ff047){_0x3ff047=_0x3ff047||0xc8;return function(_0x5a3f42){if(_0x5a3f42){return _0x110a70[_0xf6f5('0x1f')](_0x3ff047)['json'](_0x5a3f42);}};}function respondWithFilteredResult(_0x3ebc64,_0x23ce20){return function(_0x457ffa){if(_0x457ffa){var _0x267a8d=typeof _0x23ce20[_0xf6f5('0x20')]===_0xf6f5('0x21')&&typeof _0x23ce20[_0xf6f5('0x22')]===_0xf6f5('0x21');var _0x23ff20=_0x457ffa[_0xf6f5('0x23')];var _0xb9cdfc=_0x267a8d?0x0:_0x23ce20[_0xf6f5('0x20')];var _0x25581b=_0x267a8d?_0x457ffa[_0xf6f5('0x23')]:_0x23ce20['offset']+_0x23ce20[_0xf6f5('0x22')];var _0x214a54;if(_0x25581b>=_0x23ff20){_0x25581b=_0x23ff20;_0x214a54=0xc8;}else{_0x214a54=0xce;}_0x3ebc64[_0xf6f5('0x1f')](_0x214a54);return _0x3ebc64[_0xf6f5('0x24')](_0xf6f5('0x25'),_0xb9cdfc+'-'+_0x25581b+'/'+_0x23ff20)[_0xf6f5('0x26')](_0x457ffa);}return null;};}function saveUpdates(_0x3eff6c){return function(_0x2b967c){if(_0x2b967c){return _0x2b967c[_0xf6f5('0x27')](_0x3eff6c)[_0xf6f5('0x15')](function(_0x58d4c2){return _0x58d4c2;});}return null;};}function removeEntity(_0x453d16){return function(_0x1b4a81){if(_0x1b4a81){return _0x1b4a81[_0xf6f5('0x28')]()['then'](function(){_0x453d16[_0xf6f5('0x1f')](0xcc)[_0xf6f5('0x29')]();});}};}function handleEntityNotFound(_0x3c39cb){return function(_0x55f938){if(!_0x55f938){_0x3c39cb[_0xf6f5('0x2a')](0x194);}return _0x55f938;};}function handleError(_0x4d79b5,_0x6caf31){_0x6caf31=_0x6caf31||0x1f4;return function(_0x366f6d){logger[_0xf6f5('0x1a')](_0x366f6d['stack']);if(_0x366f6d[_0xf6f5('0x2b')]){delete _0x366f6d['name'];}_0x4d79b5['status'](_0x6caf31)[_0xf6f5('0x2c')](_0x366f6d);};}exports[_0xf6f5('0x2d')]=function(_0x1282ec,_0x20bd23){var _0x532851={},_0x1eeb1={},_0x12bdf6={'count':0x0,'rows':[]};var _0x2fb31a=_[_0xf6f5('0x2e')](db[_0xf6f5('0x2f')]['rawAttributes'],function(_0x2f3c43){return{'name':_0x2f3c43[_0xf6f5('0x30')],'type':_0x2f3c43[_0xf6f5('0x31')][_0xf6f5('0x32')]};});_0x1eeb1[_0xf6f5('0x33')]=_[_0xf6f5('0x2e')](_0x2fb31a,_0xf6f5('0x2b'));_0x1eeb1[_0xf6f5('0x34')]=_[_0xf6f5('0x35')](_0x1282ec[_0xf6f5('0x34')]);_0x1eeb1[_0xf6f5('0x36')]=_[_0xf6f5('0x37')](_0x1eeb1['model'],_0x1eeb1['query']);_0x532851[_0xf6f5('0x38')]=_['intersection'](_0x1eeb1['model'],qs[_0xf6f5('0x39')](_0x1282ec[_0xf6f5('0x34')][_0xf6f5('0x39')]));_0x532851[_0xf6f5('0x38')]=_0x532851['attributes'][_0xf6f5('0x3a')]?_0x532851['attributes']:_0x1eeb1[_0xf6f5('0x33')];if(!_0x1282ec[_0xf6f5('0x34')][_0xf6f5('0x3b')]('nolimit')){_0x532851['limit']=qs[_0xf6f5('0x22')](_0x1282ec[_0xf6f5('0x34')][_0xf6f5('0x22')]);_0x532851['offset']=qs[_0xf6f5('0x20')](_0x1282ec[_0xf6f5('0x34')][_0xf6f5('0x20')]);}_0x532851['order']=qs[_0xf6f5('0x3c')](_0x1282ec['query'][_0xf6f5('0x3c')]);_0x532851[_0xf6f5('0x3d')]=qs[_0xf6f5('0x36')](_[_0xf6f5('0x3e')](_0x1282ec['query'],_0x1eeb1[_0xf6f5('0x36')]),_0x2fb31a);if(_0x1282ec['query'][_0xf6f5('0x3f')]){_0x532851[_0xf6f5('0x3d')]=_[_0xf6f5('0x40')](_0x532851[_0xf6f5('0x3d')],{'$or':_[_0xf6f5('0x2e')](_0x2fb31a,function(_0x2012d1){if(_0x2012d1[_0xf6f5('0x31')]!==_0xf6f5('0x41')){var _0x4381a5={};_0x4381a5[_0x2012d1[_0xf6f5('0x2b')]]={'$like':'%'+_0x1282ec[_0xf6f5('0x34')][_0xf6f5('0x3f')]+'%'};return _0x4381a5;}})});}_0x532851=_[_0xf6f5('0x40')]({},_0x532851,_0x1282ec[_0xf6f5('0x42')]);var _0x2a323a={'where':_0x532851[_0xf6f5('0x3d')]};return db[_0xf6f5('0x2f')][_0xf6f5('0x23')](_0x2a323a)[_0xf6f5('0x15')](function(_0x292934){_0x12bdf6['count']=_0x292934;if(_0x1282ec[_0xf6f5('0x34')][_0xf6f5('0x43')]){_0x532851['include']=[{'all':!![]}];}return db[_0xf6f5('0x2f')][_0xf6f5('0x44')](_0x532851);})[_0xf6f5('0x15')](function(_0x4c5036){_0x12bdf6[_0xf6f5('0x45')]=_0x4c5036;return _0x12bdf6;})['then'](respondWithFilteredResult(_0x20bd23,_0x532851))[_0xf6f5('0x1e')](handleError(_0x20bd23,null));};exports[_0xf6f5('0x46')]=function(_0x3a806a,_0x578c5d){var _0x41e7ba={'raw':!![],'where':{'id':_0x3a806a[_0xf6f5('0x47')]['id']}},_0x5845c7={};_0x5845c7[_0xf6f5('0x33')]=_[_0xf6f5('0x35')](db[_0xf6f5('0x2f')][_0xf6f5('0x48')]);_0x5845c7[_0xf6f5('0x34')]=_['keys'](_0x3a806a[_0xf6f5('0x34')]);_0x5845c7['filters']=_[_0xf6f5('0x37')](_0x5845c7[_0xf6f5('0x33')],_0x5845c7[_0xf6f5('0x34')]);_0x41e7ba[_0xf6f5('0x38')]=_[_0xf6f5('0x37')](_0x5845c7[_0xf6f5('0x33')],qs[_0xf6f5('0x39')](_0x3a806a['query'][_0xf6f5('0x39')]));_0x41e7ba[_0xf6f5('0x38')]=_0x41e7ba['attributes'][_0xf6f5('0x3a')]?_0x41e7ba['attributes']:_0x5845c7[_0xf6f5('0x33')];if(_0x3a806a[_0xf6f5('0x34')]['includeAll']){_0x41e7ba[_0xf6f5('0x49')]=[{'all':!![]}];}_0x41e7ba=_[_0xf6f5('0x40')]({},_0x41e7ba,_0x3a806a[_0xf6f5('0x42')]);return db[_0xf6f5('0x2f')][_0xf6f5('0x4a')](_0x41e7ba)[_0xf6f5('0x15')](handleEntityNotFound(_0x578c5d,null))[_0xf6f5('0x15')](respondWithResult(_0x578c5d,null))[_0xf6f5('0x1e')](handleError(_0x578c5d,null));};exports[_0xf6f5('0x27')]=function(_0x51e7f2,_0x288eb4){if(_0x51e7f2['body']['id']){delete _0x51e7f2['body']['id'];}return db[_0xf6f5('0x2f')]['find']({'where':{'id':_0x51e7f2[_0xf6f5('0x47')]['id']}})['then'](handleEntityNotFound(_0x288eb4,null))['then'](saveUpdates(_0x51e7f2[_0xf6f5('0x4b')],null))[_0xf6f5('0x15')](respondWithResult(_0x288eb4,null))[_0xf6f5('0x1e')](handleError(_0x288eb4,null));};exports[_0xf6f5('0x28')]=function(_0x19da56,_0x46621b){return db[_0xf6f5('0x2f')][_0xf6f5('0x4a')]({'where':{'id':_0x19da56[_0xf6f5('0x47')]['id']}})[_0xf6f5('0x15')](handleEntityNotFound(_0x46621b,null))[_0xf6f5('0x15')](removeEntity(_0x46621b,null))[_0xf6f5('0x1e')](handleError(_0x46621b,null));};exports[_0xf6f5('0x4c')]=function(_0x1b59dd,_0x49cb6f){return db[_0xf6f5('0x2f')]['describe']()['then'](respondWithResult(_0x49cb6f,null))[_0xf6f5('0x1e')](handleError(_0x49cb6f,null));};exports[_0xf6f5('0x4d')]=function(_0x282165,_0x1f110a){return db[_0xf6f5('0x2f')][_0xf6f5('0x4d')](_0x282165[_0xf6f5('0x4b')])[_0xf6f5('0x15')](function(_0x5eaad1){if(_0x5eaad1){if(_0x5eaad1[_0xf6f5('0x4e')]==='out'&&!_0x5eaad1[_0xf6f5('0x4f')]){var _0x3a49e4,_0x51a1ba,_0x8c9356;if(_0x282165[_0xf6f5('0x4b')]['to']){_0x51a1ba=_0x282165[_0xf6f5('0x4b')]['to'][_0xf6f5('0x50')](',')[0x0];}return db['MailAccount'][_0xf6f5('0x51')]({'where':{'id':_0x5eaad1[_0xf6f5('0x52')]},'include':[{'model':db[_0xf6f5('0x53')],'as':'List','include':[{'model':db[_0xf6f5('0x54')],'as':'Contacts','where':{'email':_0x51a1ba},'limit':0x1,'order':[[_0xf6f5('0x55'),'DESC']]}]},{'model':db[_0xf6f5('0x56')],'as':_0xf6f5('0x57'),'raw':!![],'include':[{'model':db[_0xf6f5('0x58')],'as':_0xf6f5('0x58'),'raw':!![]}]}]})[_0xf6f5('0x15')](function(_0x16731b){_0x3a49e4=_0x16731b;if(!_0x3a49e4||!_0x3a49e4['Smtp'])return;var _0x187961=_0x3a49e4[_0xf6f5('0x59')]();if(_0x282165['body'][_0xf6f5('0x5a')]&&_0x282165[_0xf6f5('0x4b')]['attachments']['length']){for(var _0x4853b4=0x0;_0x4853b4<_0x282165[_0xf6f5('0x4b')]['attachments']['length'];_0x4853b4+=0x1){_0x282165[_0xf6f5('0x4b')][_0xf6f5('0x5a')][_0x4853b4]={'filename':_0x282165[_0xf6f5('0x4b')]['attachments'][_0x4853b4]['name'],'path':path[_0xf6f5('0x5b')](config[_0xf6f5('0x5c')],_0xf6f5('0x5d'),_0x282165[_0xf6f5('0x4b')][_0xf6f5('0x5a')][_0x4853b4][_0xf6f5('0x5e')])};}}_0x8c9356={'account':_0x187961,'message':_0x282165[_0xf6f5('0x4b')]};return;})[_0xf6f5('0x15')](function(){if(_0x3a49e4[_0xf6f5('0x5f')]){if(_0x3a49e4[_0xf6f5('0x5f')]['Contacts'][_0xf6f5('0x3a')]){return _0x3a49e4['List'][_0xf6f5('0x60')][0x0];}else{return db[_0xf6f5('0x54')]['create'](_['defaults'](_0x282165[_0xf6f5('0x4b')],{'firstName':_0x282165['body']['to'],'email':_0x282165[_0xf6f5('0x4b')]['to'],'phone':_0x282165['body']['to'],'ListId':_0x3a49e4[_0xf6f5('0x61')]}));}}else{throw new Error(_0xf6f5('0x62'));}})[_0xf6f5('0x15')](function(){if(_0x8c9356){return respondWithRpcPromise(_0xf6f5('0x63'),_0xf6f5('0x64'),_0x8c9356,client9003)['then'](function(_0x3cf39c){return _0x5eaad1['update']({'read':!![],'messageId':_0x3cf39c[_0xf6f5('0x65')],'sentAt':moment()['format'](_0xf6f5('0x66'))});})[_0xf6f5('0x1e')](function(_0x5e9791){logger[_0xf6f5('0x1a')]('%s',JSON[_0xf6f5('0x19')](_0x5e9791));return _0x5eaad1;});}return _0x5eaad1;});}return _0x5eaad1;}})['then'](respondWithResult(_0x1f110a,0xc9))[_0xf6f5('0x1e')](handleError(_0x1f110a,null));};exports[_0xf6f5('0x67')]=function(_0x5e9743,_0x2fa2b8){var _0x377c13={'agent':{},'channel':_0xf6f5('0x68')};if(_0x5e9743[_0xf6f5('0x4b')]['id']){delete _0x5e9743[_0xf6f5('0x4b')]['id'];}_0x5e9743[_0xf6f5('0x4b')]['read']=!![];_0x5e9743[_0xf6f5('0x4b')][_0xf6f5('0x69')]=moment()[_0xf6f5('0x6a')](_0xf6f5('0x66'));_0x5e9743['body']['UserId']=_0x5e9743[_0xf6f5('0x4b')][_0xf6f5('0x6b')]||_0x5e9743['user']['id'];_0x377c13[_0xf6f5('0x6c')]['id']=_0x5e9743[_0xf6f5('0x4b')][_0xf6f5('0x6b')];return db[_0xf6f5('0x2f')][_0xf6f5('0x4a')]({'where':{'id':_0x5e9743[_0xf6f5('0x47')]['id'],'UserId':null}})[_0xf6f5('0x15')](handleEntityNotFound(_0x2fa2b8,null))[_0xf6f5('0x15')](saveUpdates(_0x5e9743[_0xf6f5('0x4b')],null))['then'](function(_0x4f0513){if(_0x4f0513){_0x377c13['message']=_0x4f0513['get']({'plain':!![]});return db['MailInteraction'][_0xf6f5('0x4a')]({'where':{'id':_0x4f0513['MailInteractionId']}});}return null;})[_0xf6f5('0x15')](handleEntityNotFound(_0x2fa2b8,null))[_0xf6f5('0x15')](function(_0x2999d2){if(_0x2999d2){return _0x2999d2[_0xf6f5('0x27')]({'UserId':_0x5e9743['body'][_0xf6f5('0x6b')],'read1stAt':_[_0xf6f5('0x6d')](_0x2999d2[_0xf6f5('0x6e')])?moment()[_0xf6f5('0x6a')](_0xf6f5('0x66')):undefined});}return null;})[_0xf6f5('0x15')](function(_0x5da973){if(_0x5da973){_0x377c13[_0xf6f5('0x6f')]=_0x5da973[_0xf6f5('0x70')]({'plain':!![]});interaction_log[_0xf6f5('0x16')](_0xf6f5('0x71'),_0x5e9743[_0xf6f5('0x72')]['id'],_0x5e9743[_0xf6f5('0x72')]['name'],_0x5e9743[_0xf6f5('0x72')][_0xf6f5('0x73')],_0x377c13[_0xf6f5('0x6f')]['id'],_0x5e9743[_0xf6f5('0x4b')]?JSON['stringify'](_0x5e9743[_0xf6f5('0x4b')]):_0xf6f5('0x74'));return respondWithRpcPromise(_0xf6f5('0x75'),_0xf6f5('0x76'),_0x377c13);}return null;})[_0xf6f5('0x15')](function(_0x3c42ba){var _0x4a77af='SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20'+_0x3c42ba[_0xf6f5('0x1c')][_0xf6f5('0x77')];return db['sequelize']['query'](_0x4a77af,{'type':db[_0xf6f5('0x78')]['QueryTypes'][_0xf6f5('0x79')],'raw':!![]})[_0xf6f5('0x15')](function(_0x405c85){return _0x405c85;});})[_0xf6f5('0x15')](function(_0x308364){if(_0x308364){_0x377c13[_0xf6f5('0x1c')]['contact']=_0x308364[0x0];if(!_0x5e9743[_0xf6f5('0x4b')][_0xf6f5('0x6b')])return;return db[_0xf6f5('0x7a')]['find']({'where':{'id':_0x5e9743[_0xf6f5('0x4b')][_0xf6f5('0x6b')],'role':_0xf6f5('0x6c')},'attributes':['id',_0xf6f5('0x2b'),_0xf6f5('0x7b'),_0xf6f5('0x7c'),_0xf6f5('0x7d')],'raw':!![]});}return null;})[_0xf6f5('0x15')](function(_0x5b898c){if(!_0x5b898c)return null;_0x377c13[_0xf6f5('0x1c')]['agent']=_0x5b898c;return _0x377c13;})[_0xf6f5('0x15')](function(_0x1ed6ca){if(_0x1ed6ca){if(_0x5e9743[_0xf6f5('0x4b')][_0xf6f5('0x7e')]){_0x377c13[_0xf6f5('0x1c')][_0xf6f5('0x7f')]=util[_0xf6f5('0x6a')]('SIP/%s',_0x5e9743[_0xf6f5('0x72')]['name']);_0x377c13[_0xf6f5('0x1c')]['channel']=_0x377c13[_0xf6f5('0x80')];_0x377c13[_0xf6f5('0x1c')][_0xf6f5('0x81')]=_0x377c13['channel'];_0x377c13['message'][_0xf6f5('0x82')]=_0xf6f5('0x67');_0x377c13[_0xf6f5('0x1c')][_0xf6f5('0x4b')]=htmlToText[_0xf6f5('0x83')](_0x377c13[_0xf6f5('0x1c')][_0xf6f5('0x4b')]);return respondWithRpcPromise('EventManager',_0xf6f5('0x84'),{'event':_0xf6f5('0x85'),'message':_0x377c13[_0xf6f5('0x1c')]},client9002)[_0xf6f5('0x15')](function(){return _0x377c13;});}return _0x377c13;}return null;})[_0xf6f5('0x15')](respondWithResult(_0x2fa2b8,null))[_0xf6f5('0x1e')](handleError(_0x2fa2b8,null));};exports['reject']=function(_0x1bbb98,_0x12acc4){var _0x512c5b={'agent':{},'channel':'mail'};if(_0x1bbb98[_0xf6f5('0x4b')]['id']){delete _0x1bbb98['body']['id'];}_0x1bbb98[_0xf6f5('0x4b')]['UserId']=_0x1bbb98[_0xf6f5('0x4b')]['UserId']||_0x1bbb98['user']['id'];_0x512c5b[_0xf6f5('0x6c')]['id']=_0x1bbb98['body'][_0xf6f5('0x6b')];return db[_0xf6f5('0x2f')]['find']({'where':{'id':_0x1bbb98['params']['id'],'UserId':null}})[_0xf6f5('0x15')](handleEntityNotFound(_0x12acc4,null))[_0xf6f5('0x15')](function(_0x4673fc){if(_0x4673fc){_0x512c5b[_0xf6f5('0x1c')]=_0x4673fc[_0xf6f5('0x70')]({'plain':!![]});return db[_0xf6f5('0x86')][_0xf6f5('0x4a')]({'where':{'id':_0x4673fc['MailInteractionId']}});}return null;})[_0xf6f5('0x15')](handleEntityNotFound(_0x12acc4,null))[_0xf6f5('0x15')](function(_0x4a8c4b){if(_0x4a8c4b){_0x512c5b[_0xf6f5('0x6f')]=_0x4a8c4b['get']({'plain':!![]});interaction_log[_0xf6f5('0x16')](_0xf6f5('0x87'),_0x1bbb98[_0xf6f5('0x72')]['id'],_0x1bbb98[_0xf6f5('0x72')][_0xf6f5('0x2b')],_0x1bbb98[_0xf6f5('0x72')][_0xf6f5('0x73')],_0x512c5b['interaction']['id'],_0x1bbb98[_0xf6f5('0x4b')]?JSON[_0xf6f5('0x19')](_0x1bbb98[_0xf6f5('0x4b')]):'null');return respondWithRpcPromise(_0xf6f5('0x88'),_0xf6f5('0x89'),_0x512c5b);}return null;})['then'](function(_0x227a6f){var _0x3218c2=_0xf6f5('0x8a')+_0x227a6f[_0xf6f5('0x1c')][_0xf6f5('0x77')];return db[_0xf6f5('0x8b')][_0xf6f5('0x34')](_0x3218c2,{'type':db['Sequelize'][_0xf6f5('0x8c')][_0xf6f5('0x79')],'raw':!![]})[_0xf6f5('0x15')](function(_0x474dcb){return _0x474dcb;});})['then'](function(_0x551c42){if(_0x551c42){_0x512c5b['message'][_0xf6f5('0x8d')]=_0x551c42[0x0];if(!_0x1bbb98['body'][_0xf6f5('0x6b')])return;return db[_0xf6f5('0x7a')][_0xf6f5('0x4a')]({'where':{'id':_0x1bbb98[_0xf6f5('0x4b')][_0xf6f5('0x6b')],'role':_0xf6f5('0x6c')},'attributes':['id',_0xf6f5('0x2b'),'fullname',_0xf6f5('0x7c'),_0xf6f5('0x7d')],'raw':!![]});}return null;})[_0xf6f5('0x15')](function(_0x47b628){if(!_0x47b628)return null;_0x512c5b[_0xf6f5('0x1c')][_0xf6f5('0x6c')]=_0x47b628;return _0x512c5b;})['then'](function(_0x171b11){if(_0x171b11){_0x171b11[_0xf6f5('0x1c')][_0xf6f5('0x7f')]=util[_0xf6f5('0x6a')](_0xf6f5('0x8e'),_0x1bbb98[_0xf6f5('0x72')]['name']);_0x171b11[_0xf6f5('0x1c')][_0xf6f5('0x80')]=_0x512c5b[_0xf6f5('0x80')];_0x171b11[_0xf6f5('0x1c')][_0xf6f5('0x81')]=_0x512c5b['channel'];_0x171b11['message'][_0xf6f5('0x82')]=_0xf6f5('0x8f');_0x171b11[_0xf6f5('0x1c')][_0xf6f5('0x4b')]=htmlToText['fromString'](_0x512c5b['message']['body']);respondWithRpcPromise(_0xf6f5('0x84'),_0xf6f5('0x84'),{'event':_0xf6f5('0x90'),'message':_0x171b11[_0xf6f5('0x1c')]},client9002);return _0x512c5b;}return null;})['then'](respondWithResult(_0x12acc4,null))[_0xf6f5('0x1e')](handleError(_0x12acc4,null));};exports[_0xf6f5('0x91')]=function(_0x55654b,_0x1131ff){var _0x819147=moment()[_0xf6f5('0x92')]()[_0xf6f5('0x93')]();var _0x5ca623=path[_0xf6f5('0x5b')](config[_0xf6f5('0x5c')],_0xf6f5('0x94'),_0xf6f5('0x95'),_0xf6f5('0x96'));var _0x40d344=path['join'](config[_0xf6f5('0x5c')],_0xf6f5('0x94'),_0xf6f5('0x95'),_0xf6f5('0x5a'));var _0x1c197d=util[_0xf6f5('0x6a')](_0xf6f5('0x97'),_0x819147);return db['MailMessage'][_0xf6f5('0x4a')]({'where':{'id':_0x55654b[_0xf6f5('0x47')]['id']},'attributes':['id','body',_0xf6f5('0x98'),'to','cc',_0xf6f5('0x99'),_0xf6f5('0x9a'),_0xf6f5('0x9b')],'include':_0x55654b[_0xf6f5('0x34')][_0xf6f5('0x5a')]?[{'model':db['Attachment'],'as':_0xf6f5('0x9c')}]:undefined})['then'](handleEntityNotFound(_0x1131ff,null))['then'](function(_0x3cd256){if(_0x3cd256){var _0x552b97=_0x3cd256;var _0x306bab={'from':_0x552b97[_0xf6f5('0x98')],'to':_0x552b97['to'],'subject':_0x552b97[_0xf6f5('0x9a')],'html':htmlEntities['decode'](_0x552b97[_0xf6f5('0x4b')]),'headers':{'Date':moment(_0x552b97[_0xf6f5('0x9b')])['format'](_0xf6f5('0x9d'))},'attachments':[]};if(_0x3cd256['cc']){_0x306bab[_0xf6f5('0x9e')]['Cc']=_0x3cd256['cc'];}if(_0x3cd256['bcc']){_0x306bab[_0xf6f5('0x9e')][_0xf6f5('0x9f')]=_0x3cd256[_0xf6f5('0x99')];}return new BPromise(function(_0x5ef2d1,_0x366949){if(_0x3cd256[_0xf6f5('0x9c')]){for(var _0x2d1088=0x0;_0x2d1088<_0x3cd256[_0xf6f5('0x9c')]['length'];_0x2d1088++){var _0x48b63e=_0x3cd256[_0xf6f5('0x9c')][_0x2d1088];var _0xb59982=path['join'](_0x40d344,_0x48b63e[_0xf6f5('0x5e')]);if(fs[_0xf6f5('0xa0')](_0xb59982)){_0x306bab[_0xf6f5('0x5a')][_0xf6f5('0xa1')]({'name':_0x48b63e[_0xf6f5('0x2b')],'contentType':_0x48b63e[_0xf6f5('0x31')],'data':fs[_0xf6f5('0xa2')](_0xb59982)});}}}emlformat[_0xf6f5('0xa3')](_0x306bab,function(_0x436e35,_0x34fafb){if(_0x436e35)return _0x366949(_0x436e35);fs[_0xf6f5('0xa4')](path[_0xf6f5('0x5b')](_0x5ca623,_0x1c197d),_0x34fafb);return _0x5ef2d1(_0x34fafb);});});}})['then'](function(_0x4fd545){if(_0x4fd545){return _0x1131ff[_0xf6f5('0x91')](path[_0xf6f5('0x5b')](_0x5ca623,_0x1c197d),_0x1c197d,function(_0x55b36e){if(!_0x55b36e)fs['unlinkSync'](path[_0xf6f5('0x5b')](_0x5ca623,_0x1c197d));});}})[_0xf6f5('0x1e')](handleError(_0x1131ff,null));};