Built motion from commit 21186556.|2.6.19
[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 _0x2f97=['subject','createdAt','Attachment','Attachments','decode','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','push','readFileSync','build','writeFileSync','unlinkSync','eml-format','path','ioredis','html-to-text','../../components/parsers/qs','../../config/logger','mail-interactions','../../config/environment','redis','defaults','localhost','./mailMessage.socket','register','jayson/promise','client','http','request','then','MailMessage,\x20%s,\x20%s','debug','MailMessage,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','info','catch','status','json','offset','undefined','limit','count','update','destroy','end','sendStatus','stack','name','send','index','map','MailMessage','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','include','show','params','body','find','describe','create','direction','out','secret','split','MailAccount','findOne','MailAccountId','CmList','List','CmContact','updatedAt','DESC','MailServerOut','CloudProvider','getSmtpOptions','attachments','join','server/files/attachments','basename','Contacts','ListId','SendMail','messageId','YYYY-MM-DD\x20HH:mm:ss','accept','mail','read','format','UserId','user','agent','MailInteraction','MailInteractionId','isNil','interaction','get','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','null','acceptMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','QueryTypes','SELECT','contact','User','fullname','email','manual','interface','SIP/%s','channel','motionChannel','event','EventManager','acceptmessage','reject','role','RejectMessage','rejectMessage','sequelize','Sequelize','internal','fromString','rejectmessage','download','toString','root','files','tmp','server','message-%s.eml','from','bcc'];(function(_0x31bab3,_0x44554f){var _0x4fd382=function(_0x42b336){while(--_0x42b336){_0x31bab3['push'](_0x31bab3['shift']());}};_0x4fd382(++_0x44554f);}(_0x2f97,0x1e7));var _0x72f9=function(_0x38fbf6,_0x5f5144){_0x38fbf6=_0x38fbf6-0x0;var _0x391946=_0x2f97[_0x38fbf6];return _0x391946;};'use strict';var emlformat=require(_0x72f9('0x0'));var moment=require('moment');var BPromise=require('bluebird');var util=require('util');var path=require(_0x72f9('0x1'));var fs=require('fs');var _=require('lodash');var Redis=require(_0x72f9('0x2'));var htmlToText=require(_0x72f9('0x3'));var qs=require(_0x72f9('0x4'));var logger=require(_0x72f9('0x5'))('api');var interaction_log=require(_0x72f9('0x5'))(_0x72f9('0x6'));var config=require(_0x72f9('0x7'));var db=require('../../mysqldb')['db'];config[_0x72f9('0x8')]=_[_0x72f9('0x9')](config[_0x72f9('0x8')],{'host':_0x72f9('0xa'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x72f9('0x8')]));var htmlEntities=require('html-entities');require(_0x72f9('0xb'))[_0x72f9('0xc')](socket);var jayson=require(_0x72f9('0xd'));var client=jayson[_0x72f9('0xe')][_0x72f9('0xf')]({'port':0x232c});var client9003=jayson[_0x72f9('0xe')][_0x72f9('0xf')]({'port':0x232b});var client9002=jayson[_0x72f9('0xe')]['http']({'port':0x232a});function respondWithRpcPromise(_0x1349a9,_0x29b607,_0x24c958,_0x456aad){return new BPromise(function(_0x29444a,_0x311fbb){var _0x49e8aa=_0x456aad||client;return _0x49e8aa[_0x72f9('0x10')](_0x1349a9,_0x24c958)[_0x72f9('0x11')](function(_0x170066){logger['info'](_0x72f9('0x12'),_0x29b607,'request\x20sent');logger[_0x72f9('0x13')](_0x72f9('0x14'),_0x29b607,_0x72f9('0x15'),JSON[_0x72f9('0x16')](_0x170066));if(_0x170066[_0x72f9('0x17')]){if(_0x170066[_0x72f9('0x17')][_0x72f9('0x18')]===0x1f4){logger[_0x72f9('0x17')](_0x72f9('0x12'),_0x29b607,_0x170066[_0x72f9('0x17')]['message']);return _0x311fbb(_0x170066['error'][_0x72f9('0x19')]);}logger['error'](_0x72f9('0x12'),_0x29b607,_0x170066[_0x72f9('0x17')][_0x72f9('0x19')]);return _0x29444a(_0x170066[_0x72f9('0x17')]['message']);}else{logger[_0x72f9('0x1a')]('MailMessage,\x20%s,\x20%s',_0x29b607,_0x72f9('0x15'));_0x29444a(_0x170066['result'][_0x72f9('0x19')]);}})[_0x72f9('0x1b')](function(_0x42187d){logger[_0x72f9('0x17')](_0x72f9('0x12'),_0x29b607,_0x42187d);_0x311fbb(_0x42187d);});});}function respondWithResult(_0x285a07,_0xb2b6be){_0xb2b6be=_0xb2b6be||0xc8;return function(_0x3ccaba){if(_0x3ccaba){return _0x285a07[_0x72f9('0x1c')](_0xb2b6be)[_0x72f9('0x1d')](_0x3ccaba);}};}function respondWithFilteredResult(_0x456ed7,_0x48a815){return function(_0x411409){if(_0x411409){var _0x3f6d2f=typeof _0x48a815[_0x72f9('0x1e')]===_0x72f9('0x1f')&&typeof _0x48a815[_0x72f9('0x20')]===_0x72f9('0x1f');var _0x4c1077=_0x411409[_0x72f9('0x21')];var _0x566434=_0x3f6d2f?0x0:_0x48a815[_0x72f9('0x1e')];var _0x5b883c=_0x3f6d2f?_0x411409[_0x72f9('0x21')]:_0x48a815[_0x72f9('0x1e')]+_0x48a815[_0x72f9('0x20')];var _0x57dd0b;if(_0x5b883c>=_0x4c1077){_0x5b883c=_0x4c1077;_0x57dd0b=0xc8;}else{_0x57dd0b=0xce;}_0x456ed7[_0x72f9('0x1c')](_0x57dd0b);return _0x456ed7['set']('Content-Range',_0x566434+'-'+_0x5b883c+'/'+_0x4c1077)[_0x72f9('0x1d')](_0x411409);}return null;};}function saveUpdates(_0x8398b8){return function(_0x4377cd){if(_0x4377cd){return _0x4377cd[_0x72f9('0x22')](_0x8398b8)[_0x72f9('0x11')](function(_0x32252e){return _0x32252e;});}return null;};}function removeEntity(_0x46a2b9){return function(_0x17ef94){if(_0x17ef94){return _0x17ef94[_0x72f9('0x23')]()[_0x72f9('0x11')](function(){_0x46a2b9[_0x72f9('0x1c')](0xcc)[_0x72f9('0x24')]();});}};}function handleEntityNotFound(_0xfc07bc){return function(_0x340f75){if(!_0x340f75){_0xfc07bc[_0x72f9('0x25')](0x194);}return _0x340f75;};}function handleError(_0x57f901,_0x2c3436){_0x2c3436=_0x2c3436||0x1f4;return function(_0x1b622d){logger[_0x72f9('0x17')](_0x1b622d[_0x72f9('0x26')]);if(_0x1b622d[_0x72f9('0x27')]){delete _0x1b622d[_0x72f9('0x27')];}_0x57f901['status'](_0x2c3436)[_0x72f9('0x28')](_0x1b622d);};}exports[_0x72f9('0x29')]=function(_0x124724,_0x16a9d9){var _0x1ffe4a={},_0x4b0fd4={},_0x1566de={'count':0x0,'rows':[]};var _0x593b1c=_[_0x72f9('0x2a')](db[_0x72f9('0x2b')]['rawAttributes'],function(_0x4cbd97){return{'name':_0x4cbd97[_0x72f9('0x2c')],'type':_0x4cbd97[_0x72f9('0x2d')][_0x72f9('0x2e')]};});_0x4b0fd4[_0x72f9('0x2f')]=_[_0x72f9('0x2a')](_0x593b1c,_0x72f9('0x27'));_0x4b0fd4[_0x72f9('0x30')]=_[_0x72f9('0x31')](_0x124724['query']);_0x4b0fd4[_0x72f9('0x32')]=_[_0x72f9('0x33')](_0x4b0fd4[_0x72f9('0x2f')],_0x4b0fd4['query']);_0x1ffe4a[_0x72f9('0x34')]=_[_0x72f9('0x33')](_0x4b0fd4[_0x72f9('0x2f')],qs[_0x72f9('0x35')](_0x124724['query'][_0x72f9('0x35')]));_0x1ffe4a[_0x72f9('0x34')]=_0x1ffe4a[_0x72f9('0x34')][_0x72f9('0x36')]?_0x1ffe4a[_0x72f9('0x34')]:_0x4b0fd4[_0x72f9('0x2f')];if(!_0x124724[_0x72f9('0x30')][_0x72f9('0x37')](_0x72f9('0x38'))){_0x1ffe4a['limit']=qs['limit'](_0x124724['query'][_0x72f9('0x20')]);_0x1ffe4a[_0x72f9('0x1e')]=qs[_0x72f9('0x1e')](_0x124724['query'][_0x72f9('0x1e')]);}_0x1ffe4a[_0x72f9('0x39')]=qs[_0x72f9('0x3a')](_0x124724[_0x72f9('0x30')][_0x72f9('0x3a')]);_0x1ffe4a[_0x72f9('0x3b')]=qs['filters'](_[_0x72f9('0x3c')](_0x124724[_0x72f9('0x30')],_0x4b0fd4[_0x72f9('0x32')]),_0x593b1c);if(_0x124724[_0x72f9('0x30')][_0x72f9('0x3d')]){_0x1ffe4a[_0x72f9('0x3b')]=_[_0x72f9('0x3e')](_0x1ffe4a['where'],{'$or':_[_0x72f9('0x2a')](_0x593b1c,function(_0x54f25e){if(_0x54f25e[_0x72f9('0x2d')]!==_0x72f9('0x3f')){var _0x52be29={};_0x52be29[_0x54f25e[_0x72f9('0x27')]]={'$like':'%'+_0x124724[_0x72f9('0x30')][_0x72f9('0x3d')]+'%'};return _0x52be29;}})});}_0x1ffe4a=_[_0x72f9('0x3e')]({},_0x1ffe4a,_0x124724[_0x72f9('0x40')]);var _0x24f13f={'where':_0x1ffe4a[_0x72f9('0x3b')]};return db['MailMessage'][_0x72f9('0x21')](_0x24f13f)['then'](function(_0x2372b9){_0x1566de[_0x72f9('0x21')]=_0x2372b9;if(_0x124724[_0x72f9('0x30')][_0x72f9('0x41')]){_0x1ffe4a[_0x72f9('0x42')]=[{'all':!![]}];}return db[_0x72f9('0x2b')]['findAll'](_0x1ffe4a);})[_0x72f9('0x11')](function(_0x383818){_0x1566de['rows']=_0x383818;return _0x1566de;})[_0x72f9('0x11')](respondWithFilteredResult(_0x16a9d9,_0x1ffe4a))[_0x72f9('0x1b')](handleError(_0x16a9d9,null));};exports[_0x72f9('0x43')]=function(_0xcafde1,_0x4da667){var _0x2b2895={'raw':!![],'where':{'id':_0xcafde1[_0x72f9('0x44')]['id']}},_0x33b92e={};_0x33b92e[_0x72f9('0x2f')]=_[_0x72f9('0x31')](db[_0x72f9('0x2b')]['rawAttributes']);_0x33b92e[_0x72f9('0x30')]=_[_0x72f9('0x31')](_0xcafde1['query']);_0x33b92e['filters']=_['intersection'](_0x33b92e[_0x72f9('0x2f')],_0x33b92e[_0x72f9('0x30')]);_0x2b2895[_0x72f9('0x34')]=_[_0x72f9('0x33')](_0x33b92e[_0x72f9('0x2f')],qs[_0x72f9('0x35')](_0xcafde1[_0x72f9('0x30')][_0x72f9('0x35')]));_0x2b2895[_0x72f9('0x34')]=_0x2b2895[_0x72f9('0x34')][_0x72f9('0x36')]?_0x2b2895['attributes']:_0x33b92e[_0x72f9('0x2f')];if(_0xcafde1['query'][_0x72f9('0x41')]){_0x2b2895[_0x72f9('0x42')]=[{'all':!![]}];}_0x2b2895=_['merge']({},_0x2b2895,_0xcafde1[_0x72f9('0x40')]);return db[_0x72f9('0x2b')]['find'](_0x2b2895)[_0x72f9('0x11')](handleEntityNotFound(_0x4da667,null))['then'](respondWithResult(_0x4da667,null))[_0x72f9('0x1b')](handleError(_0x4da667,null));};exports[_0x72f9('0x22')]=function(_0x353b6b,_0x5bee1e){if(_0x353b6b[_0x72f9('0x45')]['id']){delete _0x353b6b[_0x72f9('0x45')]['id'];}return db['MailMessage'][_0x72f9('0x46')]({'where':{'id':_0x353b6b['params']['id']}})[_0x72f9('0x11')](handleEntityNotFound(_0x5bee1e,null))[_0x72f9('0x11')](saveUpdates(_0x353b6b[_0x72f9('0x45')],null))[_0x72f9('0x11')](respondWithResult(_0x5bee1e,null))[_0x72f9('0x1b')](handleError(_0x5bee1e,null));};exports['destroy']=function(_0x24d7ec,_0x3875ac){return db[_0x72f9('0x2b')][_0x72f9('0x46')]({'where':{'id':_0x24d7ec[_0x72f9('0x44')]['id']}})['then'](handleEntityNotFound(_0x3875ac,null))[_0x72f9('0x11')](removeEntity(_0x3875ac,null))[_0x72f9('0x1b')](handleError(_0x3875ac,null));};exports['describe']=function(_0x1fbdd1,_0x6e40aa){return db[_0x72f9('0x2b')][_0x72f9('0x47')]()[_0x72f9('0x11')](respondWithResult(_0x6e40aa,null))[_0x72f9('0x1b')](handleError(_0x6e40aa,null));};exports[_0x72f9('0x48')]=function(_0x574c4d,_0x5b35cc){return db[_0x72f9('0x2b')]['create'](_0x574c4d['body'])[_0x72f9('0x11')](function(_0x3dc633){if(_0x3dc633){if(_0x3dc633[_0x72f9('0x49')]===_0x72f9('0x4a')&&!_0x3dc633[_0x72f9('0x4b')]){var _0x7fed15,_0xfea1f5,_0x3497b2;if(_0x574c4d[_0x72f9('0x45')]['to']){_0xfea1f5=_0x574c4d[_0x72f9('0x45')]['to'][_0x72f9('0x4c')](',')[0x0];}return db[_0x72f9('0x4d')][_0x72f9('0x4e')]({'where':{'id':_0x3dc633[_0x72f9('0x4f')]},'include':[{'model':db[_0x72f9('0x50')],'as':_0x72f9('0x51'),'include':[{'model':db[_0x72f9('0x52')],'as':'Contacts','where':{'email':_0xfea1f5},'limit':0x1,'order':[[_0x72f9('0x53'),_0x72f9('0x54')]]}]},{'model':db[_0x72f9('0x55')],'as':'Smtp','raw':!![],'include':[{'model':db['CloudProvider'],'as':_0x72f9('0x56'),'raw':!![]}]}]})['then'](function(_0x1feac2){_0x7fed15=_0x1feac2;if(!_0x7fed15||!_0x7fed15['Smtp'])return;var _0x4fdd35=_0x7fed15[_0x72f9('0x57')]();if(_0x574c4d['body'][_0x72f9('0x58')]&&_0x574c4d[_0x72f9('0x45')][_0x72f9('0x58')][_0x72f9('0x36')]){for(var _0x364286=0x0;_0x364286<_0x574c4d['body'][_0x72f9('0x58')][_0x72f9('0x36')];_0x364286+=0x1){_0x574c4d[_0x72f9('0x45')][_0x72f9('0x58')][_0x364286]={'filename':_0x574c4d[_0x72f9('0x45')][_0x72f9('0x58')][_0x364286][_0x72f9('0x27')],'path':path[_0x72f9('0x59')](config['root'],_0x72f9('0x5a'),_0x574c4d[_0x72f9('0x45')][_0x72f9('0x58')][_0x364286][_0x72f9('0x5b')])};}}_0x3497b2={'account':_0x4fdd35,'message':_0x574c4d['body']};return;})['then'](function(){if(_0x7fed15[_0x72f9('0x51')]){if(_0x7fed15[_0x72f9('0x51')][_0x72f9('0x5c')][_0x72f9('0x36')]){return _0x7fed15['List'][_0x72f9('0x5c')][0x0];}else{return db[_0x72f9('0x52')]['create'](_['defaults'](_0x574c4d[_0x72f9('0x45')],{'firstName':_0x574c4d[_0x72f9('0x45')]['to'],'email':_0x574c4d[_0x72f9('0x45')]['to'],'phone':_0x574c4d['body']['to'],'ListId':_0x7fed15[_0x72f9('0x5d')]}));}}else{throw new Error('List\x20not\x20found');}})['then'](function(){if(_0x3497b2){return respondWithRpcPromise(_0x72f9('0x5e'),'sendMail',_0x3497b2,client9003)[_0x72f9('0x11')](function(_0x3fdbea){return _0x3dc633['update']({'read':!![],'messageId':_0x3fdbea[_0x72f9('0x5f')],'sentAt':moment()['format'](_0x72f9('0x60'))});})[_0x72f9('0x1b')](function(_0x572aed){logger['error']('%s',JSON[_0x72f9('0x16')](_0x572aed));return _0x3dc633;});}return _0x3dc633;});}return _0x3dc633;}})['then'](respondWithResult(_0x5b35cc,0xc9))[_0x72f9('0x1b')](handleError(_0x5b35cc,null));};exports[_0x72f9('0x61')]=function(_0xb9c8db,_0x17f37d){var _0x43245f={'agent':{},'channel':_0x72f9('0x62')};if(_0xb9c8db[_0x72f9('0x45')]['id']){delete _0xb9c8db['body']['id'];}_0xb9c8db[_0x72f9('0x45')][_0x72f9('0x63')]=!![];_0xb9c8db['body']['readAt']=moment()[_0x72f9('0x64')](_0x72f9('0x60'));_0xb9c8db[_0x72f9('0x45')][_0x72f9('0x65')]=_0xb9c8db[_0x72f9('0x45')]['UserId']||_0xb9c8db[_0x72f9('0x66')]['id'];_0x43245f[_0x72f9('0x67')]['id']=_0xb9c8db[_0x72f9('0x45')]['UserId'];return db[_0x72f9('0x2b')]['find']({'where':{'id':_0xb9c8db[_0x72f9('0x44')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0x17f37d,null))[_0x72f9('0x11')](saveUpdates(_0xb9c8db['body'],null))[_0x72f9('0x11')](function(_0x596d83){if(_0x596d83){_0x43245f[_0x72f9('0x19')]=_0x596d83['get']({'plain':!![]});return db[_0x72f9('0x68')][_0x72f9('0x46')]({'where':{'id':_0x596d83[_0x72f9('0x69')]}});}return null;})[_0x72f9('0x11')](handleEntityNotFound(_0x17f37d,null))[_0x72f9('0x11')](function(_0xca2147){if(_0xca2147){return _0xca2147[_0x72f9('0x22')]({'UserId':_0xb9c8db[_0x72f9('0x45')][_0x72f9('0x65')],'read1stAt':_[_0x72f9('0x6a')](_0xca2147['read1stAt'])?moment()[_0x72f9('0x64')]('YYYY-MM-DD\x20HH:mm:ss'):undefined});}return null;})[_0x72f9('0x11')](function(_0x5564e4){if(_0x5564e4){_0x43245f[_0x72f9('0x6b')]=_0x5564e4[_0x72f9('0x6c')]({'plain':!![]});interaction_log[_0x72f9('0x1a')](_0x72f9('0x6d'),_0xb9c8db['user']['id'],_0xb9c8db['user']['name'],_0xb9c8db[_0x72f9('0x66')]['role'],_0x43245f[_0x72f9('0x6b')]['id'],_0xb9c8db[_0x72f9('0x45')]?JSON['stringify'](_0xb9c8db['body']):_0x72f9('0x6e'));return respondWithRpcPromise('AcceptMessage',_0x72f9('0x6f'),_0x43245f);}return null;})[_0x72f9('0x11')](function(_0x156d43){var _0x520815=_0x72f9('0x70')+_0x156d43[_0x72f9('0x19')]['ContactId'];return db['sequelize']['query'](_0x520815,{'type':db['Sequelize'][_0x72f9('0x71')][_0x72f9('0x72')],'raw':!![]})[_0x72f9('0x11')](function(_0x4cbf13){return _0x4cbf13;});})[_0x72f9('0x11')](function(_0x1f8f04){if(_0x1f8f04){_0x43245f['message'][_0x72f9('0x73')]=_0x1f8f04[0x0];if(!_0xb9c8db[_0x72f9('0x45')][_0x72f9('0x65')])return;return db[_0x72f9('0x74')][_0x72f9('0x46')]({'where':{'id':_0xb9c8db[_0x72f9('0x45')][_0x72f9('0x65')],'role':_0x72f9('0x67')},'attributes':['id',_0x72f9('0x27'),_0x72f9('0x75'),_0x72f9('0x76'),'internal'],'raw':!![]});}return null;})[_0x72f9('0x11')](function(_0x1cccfa){if(!_0x1cccfa)return null;_0x43245f[_0x72f9('0x19')]['agent']=_0x1cccfa;return _0x43245f;})[_0x72f9('0x11')](function(_0x4b8b2b){if(_0x4b8b2b){if(_0xb9c8db[_0x72f9('0x45')][_0x72f9('0x77')]){_0x43245f['message'][_0x72f9('0x78')]=util[_0x72f9('0x64')](_0x72f9('0x79'),_0xb9c8db[_0x72f9('0x66')][_0x72f9('0x27')]);_0x43245f[_0x72f9('0x19')][_0x72f9('0x7a')]=_0x43245f[_0x72f9('0x7a')];_0x43245f['message'][_0x72f9('0x7b')]=_0x43245f[_0x72f9('0x7a')];_0x43245f['message'][_0x72f9('0x7c')]=_0x72f9('0x61');_0x43245f[_0x72f9('0x19')][_0x72f9('0x45')]=htmlToText['fromString'](_0x43245f[_0x72f9('0x19')]['body']);return respondWithRpcPromise('EventManager',_0x72f9('0x7d'),{'event':_0x72f9('0x7e'),'message':_0x43245f[_0x72f9('0x19')]},client9002)[_0x72f9('0x11')](function(){return _0x43245f;});}return _0x43245f;}return null;})[_0x72f9('0x11')](respondWithResult(_0x17f37d,null))[_0x72f9('0x1b')](handleError(_0x17f37d,null));};exports[_0x72f9('0x7f')]=function(_0xe9b356,_0x14458d){var _0x5798d1={'agent':{},'channel':_0x72f9('0x62')};if(_0xe9b356[_0x72f9('0x45')]['id']){delete _0xe9b356[_0x72f9('0x45')]['id'];}_0xe9b356['body']['UserId']=_0xe9b356[_0x72f9('0x45')][_0x72f9('0x65')]||_0xe9b356[_0x72f9('0x66')]['id'];_0x5798d1[_0x72f9('0x67')]['id']=_0xe9b356[_0x72f9('0x45')][_0x72f9('0x65')];return db[_0x72f9('0x2b')][_0x72f9('0x46')]({'where':{'id':_0xe9b356['params']['id'],'UserId':null}})['then'](handleEntityNotFound(_0x14458d,null))['then'](function(_0xeabd96){if(_0xeabd96){_0x5798d1[_0x72f9('0x19')]=_0xeabd96[_0x72f9('0x6c')]({'plain':!![]});return db[_0x72f9('0x68')][_0x72f9('0x46')]({'where':{'id':_0xeabd96['MailInteractionId']}});}return null;})[_0x72f9('0x11')](handleEntityNotFound(_0x14458d,null))[_0x72f9('0x11')](function(_0x1c2fe6){if(_0x1c2fe6){_0x5798d1[_0x72f9('0x6b')]=_0x1c2fe6[_0x72f9('0x6c')]({'plain':!![]});interaction_log['info']('[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0xe9b356[_0x72f9('0x66')]['id'],_0xe9b356[_0x72f9('0x66')][_0x72f9('0x27')],_0xe9b356[_0x72f9('0x66')][_0x72f9('0x80')],_0x5798d1[_0x72f9('0x6b')]['id'],_0xe9b356['body']?JSON[_0x72f9('0x16')](_0xe9b356[_0x72f9('0x45')]):'null');return respondWithRpcPromise(_0x72f9('0x81'),_0x72f9('0x82'),_0x5798d1);}return null;})[_0x72f9('0x11')](function(_0x24f1ce){var _0x149b25=_0x72f9('0x70')+_0x24f1ce[_0x72f9('0x19')]['ContactId'];return db[_0x72f9('0x83')][_0x72f9('0x30')](_0x149b25,{'type':db[_0x72f9('0x84')][_0x72f9('0x71')][_0x72f9('0x72')],'raw':!![]})['then'](function(_0x564d17){return _0x564d17;});})[_0x72f9('0x11')](function(_0x207853){if(_0x207853){_0x5798d1[_0x72f9('0x19')][_0x72f9('0x73')]=_0x207853[0x0];if(!_0xe9b356[_0x72f9('0x45')][_0x72f9('0x65')])return;return db['User'][_0x72f9('0x46')]({'where':{'id':_0xe9b356[_0x72f9('0x45')][_0x72f9('0x65')],'role':_0x72f9('0x67')},'attributes':['id',_0x72f9('0x27'),_0x72f9('0x75'),_0x72f9('0x76'),_0x72f9('0x85')],'raw':!![]});}return null;})[_0x72f9('0x11')](function(_0x5d35ee){if(!_0x5d35ee)return null;_0x5798d1[_0x72f9('0x19')][_0x72f9('0x67')]=_0x5d35ee;return _0x5798d1;})[_0x72f9('0x11')](function(_0x2af965){if(_0x2af965){_0x2af965[_0x72f9('0x19')]['interface']=util['format']('SIP/%s',_0xe9b356[_0x72f9('0x66')][_0x72f9('0x27')]);_0x2af965[_0x72f9('0x19')][_0x72f9('0x7a')]=_0x5798d1[_0x72f9('0x7a')];_0x2af965['message'][_0x72f9('0x7b')]=_0x5798d1[_0x72f9('0x7a')];_0x2af965[_0x72f9('0x19')][_0x72f9('0x7c')]=_0x72f9('0x7f');_0x2af965['message'][_0x72f9('0x45')]=htmlToText[_0x72f9('0x86')](_0x5798d1[_0x72f9('0x19')][_0x72f9('0x45')]);respondWithRpcPromise(_0x72f9('0x7d'),_0x72f9('0x7d'),{'event':_0x72f9('0x87'),'message':_0x2af965[_0x72f9('0x19')]},client9002);return _0x5798d1;}return null;})[_0x72f9('0x11')](respondWithResult(_0x14458d,null))['catch'](handleError(_0x14458d,null));};exports[_0x72f9('0x88')]=function(_0x2ac102,_0x4838e3){var _0x4bde2d=moment()['unix']()[_0x72f9('0x89')]();var _0x193199=path['join'](config[_0x72f9('0x8a')],'server',_0x72f9('0x8b'),_0x72f9('0x8c'));var _0x487c62=path[_0x72f9('0x59')](config[_0x72f9('0x8a')],_0x72f9('0x8d'),'files','attachments');var _0x43b998=util[_0x72f9('0x64')](_0x72f9('0x8e'),_0x4bde2d);return db[_0x72f9('0x2b')][_0x72f9('0x46')]({'where':{'id':_0x2ac102[_0x72f9('0x44')]['id']},'attributes':['id',_0x72f9('0x45'),_0x72f9('0x8f'),'to','cc',_0x72f9('0x90'),_0x72f9('0x91'),_0x72f9('0x92')],'include':_0x2ac102[_0x72f9('0x30')][_0x72f9('0x58')]?[{'model':db[_0x72f9('0x93')],'as':_0x72f9('0x94')}]:undefined})['then'](handleEntityNotFound(_0x4838e3,null))['then'](function(_0x563b43){if(_0x563b43){var _0x15e6e1=_0x563b43;var _0x477329={'from':_0x15e6e1[_0x72f9('0x8f')],'to':_0x15e6e1['to'],'subject':_0x15e6e1['subject'],'html':htmlEntities[_0x72f9('0x95')](_0x15e6e1['body']),'headers':{'Date':moment(_0x15e6e1[_0x72f9('0x92')])[_0x72f9('0x64')](_0x72f9('0x96'))},'attachments':[]};if(_0x563b43['cc']){_0x477329[_0x72f9('0x97')]['Cc']=_0x563b43['cc'];}if(_0x563b43[_0x72f9('0x90')]){_0x477329[_0x72f9('0x97')][_0x72f9('0x98')]=_0x563b43[_0x72f9('0x90')];}return new BPromise(function(_0x231e33,_0x246162){if(_0x563b43[_0x72f9('0x94')]){for(var _0x45fa30=0x0;_0x45fa30<_0x563b43[_0x72f9('0x94')]['length'];_0x45fa30++){var _0x297b43=_0x563b43[_0x72f9('0x94')][_0x45fa30];var _0x4697f3=path[_0x72f9('0x59')](_0x487c62,_0x297b43[_0x72f9('0x5b')]);if(fs['existsSync'](_0x4697f3)){_0x477329['attachments'][_0x72f9('0x99')]({'name':_0x297b43[_0x72f9('0x27')],'contentType':_0x297b43[_0x72f9('0x2d')],'data':fs[_0x72f9('0x9a')](_0x4697f3)});}}}emlformat[_0x72f9('0x9b')](_0x477329,function(_0x2be9f1,_0x241761){if(_0x2be9f1)return _0x246162(_0x2be9f1);fs[_0x72f9('0x9c')](path[_0x72f9('0x59')](_0x193199,_0x43b998),_0x241761);return _0x231e33(_0x241761);});});}})[_0x72f9('0x11')](function(_0x42a183){if(_0x42a183){return _0x4838e3['download'](path[_0x72f9('0x59')](_0x193199,_0x43b998),_0x43b998,function(_0x24b446){if(!_0x24b446)fs[_0x72f9('0x9d')](path[_0x72f9('0x59')](_0x193199,_0x43b998));});}})['catch'](handleError(_0x4838e3,null));};