e966de763e3451a58791d693e30712e1cab5af0b
[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 _0x2b60=['register','client','http','then','info','MailMessage,\x20%s,\x20%s','request\x20sent','debug','stringify','error','code','message','result','catch','status','json','undefined','limit','count','offset','set','Content-Range','destroy','end','sendStatus','stack','name','send','index','map','MailMessage','rawAttributes','fieldName','type','key','query','keys','intersection','attributes','model','fields','length','hasOwnProperty','nolimit','sort','filters','pick','where','merge','filter','includeAll','findAll','rows','show','params','include','update','body','find','describe','create','direction','out','secret','split','MailAccount','findOne','MailAccountId','CmList','List','CmContact','updatedAt','MailServerOut','Smtp','CloudProvider','getSmtpOptions','attachments','join','root','server/files/attachments','basename','Contacts','defaults','ListId','List\x20not\x20found','SendMail','sendMail','format','YYYY-MM-DD\x20HH:mm:ss','accept','mail','read','readAt','UserId','agent','get','MailInteraction','MailInteractionId','isNil','interaction','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','user','role','null','AcceptMessage','acceptMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','sequelize','Sequelize','QueryTypes','SELECT','contact','User','fullname','manual','SIP/%s','motionChannel','channel','event','fromString','EventManager','acceptmessage','reject','RejectMessage','rejectMessage','email','internal','interface','download','unix','toString','server','files','message-%s.eml','bcc','subject','createdAt','Attachment','Attachments','from','decode','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','existsSync','push','readFileSync','build','writeFileSync','unlinkSync','eml-format','lodash','html-to-text','../../components/parsers/qs','../../config/logger','mail-interactions','../../config/environment','../../mysqldb','redis','localhost','socket.io-emitter','html-entities','./mailMessage.socket'];(function(_0x2aaf91,_0x3c7fa1){var _0x709352=function(_0x4851eb){while(--_0x4851eb){_0x2aaf91['push'](_0x2aaf91['shift']());}};_0x709352(++_0x3c7fa1);}(_0x2b60,0x13b));var _0x02b6=function(_0x4fbe5f,_0x47ebaa){_0x4fbe5f=_0x4fbe5f-0x0;var _0x84e514=_0x2b60[_0x4fbe5f];return _0x84e514;};'use strict';var emlformat=require(_0x02b6('0x0'));var moment=require('moment');var BPromise=require('bluebird');var util=require('util');var path=require('path');var fs=require('fs');var _=require(_0x02b6('0x1'));var Redis=require('ioredis');var htmlToText=require(_0x02b6('0x2'));var qs=require(_0x02b6('0x3'));var logger=require(_0x02b6('0x4'))('api');var interaction_log=require('../../config/logger')(_0x02b6('0x5'));var config=require(_0x02b6('0x6'));var db=require(_0x02b6('0x7'))['db'];config['redis']=_['defaults'](config[_0x02b6('0x8')],{'host':_0x02b6('0x9'),'port':0x18eb});var socket=require(_0x02b6('0xa'))(new Redis(config[_0x02b6('0x8')]));var htmlEntities=require(_0x02b6('0xb'));require(_0x02b6('0xc'))[_0x02b6('0xd')](socket);var jayson=require('jayson/promise');var client=jayson[_0x02b6('0xe')][_0x02b6('0xf')]({'port':0x232c});var client9003=jayson[_0x02b6('0xe')][_0x02b6('0xf')]({'port':0x232b});var client9002=jayson[_0x02b6('0xe')][_0x02b6('0xf')]({'port':0x232a});function respondWithRpcPromise(_0x15d6c1,_0x3e74d9,_0x1d6df0,_0x392fec){return new BPromise(function(_0xab2cb9,_0x41f150){var _0x19c49b=_0x392fec||client;return _0x19c49b['request'](_0x15d6c1,_0x1d6df0)[_0x02b6('0x10')](function(_0x2557e5){logger[_0x02b6('0x11')](_0x02b6('0x12'),_0x3e74d9,_0x02b6('0x13'));logger[_0x02b6('0x14')]('MailMessage,\x20%s,\x20%s,\x20%s',_0x3e74d9,_0x02b6('0x13'),JSON[_0x02b6('0x15')](_0x2557e5));if(_0x2557e5[_0x02b6('0x16')]){if(_0x2557e5['error'][_0x02b6('0x17')]===0x1f4){logger[_0x02b6('0x16')](_0x02b6('0x12'),_0x3e74d9,_0x2557e5[_0x02b6('0x16')][_0x02b6('0x18')]);return _0x41f150(_0x2557e5[_0x02b6('0x16')][_0x02b6('0x18')]);}logger[_0x02b6('0x16')](_0x02b6('0x12'),_0x3e74d9,_0x2557e5[_0x02b6('0x16')][_0x02b6('0x18')]);return _0xab2cb9(_0x2557e5[_0x02b6('0x16')][_0x02b6('0x18')]);}else{logger['info'](_0x02b6('0x12'),_0x3e74d9,'request\x20sent');_0xab2cb9(_0x2557e5[_0x02b6('0x19')]['message']);}})[_0x02b6('0x1a')](function(_0x164b48){logger[_0x02b6('0x16')](_0x02b6('0x12'),_0x3e74d9,_0x164b48);_0x41f150(_0x164b48);});});}function respondWithResult(_0x5dfd6d,_0x32ab3b){_0x32ab3b=_0x32ab3b||0xc8;return function(_0x14d97d){if(_0x14d97d){return _0x5dfd6d[_0x02b6('0x1b')](_0x32ab3b)[_0x02b6('0x1c')](_0x14d97d);}};}function respondWithFilteredResult(_0x24db71,_0x1799c0){return function(_0x482a9d){if(_0x482a9d){var _0x3b68da=typeof _0x1799c0['offset']===_0x02b6('0x1d')&&typeof _0x1799c0[_0x02b6('0x1e')]===_0x02b6('0x1d');var _0x5f3250=_0x482a9d[_0x02b6('0x1f')];var _0x4982b5=_0x3b68da?0x0:_0x1799c0[_0x02b6('0x20')];var _0x18330d=_0x3b68da?_0x482a9d[_0x02b6('0x1f')]:_0x1799c0[_0x02b6('0x20')]+_0x1799c0['limit'];var _0x3ff071;if(_0x18330d>=_0x5f3250){_0x18330d=_0x5f3250;_0x3ff071=0xc8;}else{_0x3ff071=0xce;}_0x24db71[_0x02b6('0x1b')](_0x3ff071);return _0x24db71[_0x02b6('0x21')](_0x02b6('0x22'),_0x4982b5+'-'+_0x18330d+'/'+_0x5f3250)['json'](_0x482a9d);}return null;};}function saveUpdates(_0x3ba86a){return function(_0x2f881e){if(_0x2f881e){return _0x2f881e['update'](_0x3ba86a)[_0x02b6('0x10')](function(_0xf2da9c){return _0xf2da9c;});}return null;};}function removeEntity(_0x232e8e){return function(_0x209ac9){if(_0x209ac9){return _0x209ac9[_0x02b6('0x23')]()[_0x02b6('0x10')](function(){_0x232e8e['status'](0xcc)[_0x02b6('0x24')]();});}};}function handleEntityNotFound(_0x4383cf){return function(_0x2294a9){if(!_0x2294a9){_0x4383cf[_0x02b6('0x25')](0x194);}return _0x2294a9;};}function handleError(_0x53c2f6,_0x22cdaf){_0x22cdaf=_0x22cdaf||0x1f4;return function(_0x3cce93){logger['error'](_0x3cce93[_0x02b6('0x26')]);if(_0x3cce93[_0x02b6('0x27')]){delete _0x3cce93[_0x02b6('0x27')];}_0x53c2f6[_0x02b6('0x1b')](_0x22cdaf)[_0x02b6('0x28')](_0x3cce93);};}exports[_0x02b6('0x29')]=function(_0x5dc7c1,_0x5c275f){var _0x2ea065={},_0xd1242c={},_0x47592f={'count':0x0,'rows':[]};var _0x1291bc=_[_0x02b6('0x2a')](db[_0x02b6('0x2b')][_0x02b6('0x2c')],function(_0x48b198){return{'name':_0x48b198[_0x02b6('0x2d')],'type':_0x48b198[_0x02b6('0x2e')][_0x02b6('0x2f')]};});_0xd1242c['model']=_[_0x02b6('0x2a')](_0x1291bc,_0x02b6('0x27'));_0xd1242c[_0x02b6('0x30')]=_[_0x02b6('0x31')](_0x5dc7c1[_0x02b6('0x30')]);_0xd1242c['filters']=_[_0x02b6('0x32')](_0xd1242c['model'],_0xd1242c[_0x02b6('0x30')]);_0x2ea065[_0x02b6('0x33')]=_['intersection'](_0xd1242c[_0x02b6('0x34')],qs['fields'](_0x5dc7c1[_0x02b6('0x30')][_0x02b6('0x35')]));_0x2ea065[_0x02b6('0x33')]=_0x2ea065[_0x02b6('0x33')][_0x02b6('0x36')]?_0x2ea065['attributes']:_0xd1242c[_0x02b6('0x34')];if(!_0x5dc7c1[_0x02b6('0x30')][_0x02b6('0x37')](_0x02b6('0x38'))){_0x2ea065[_0x02b6('0x1e')]=qs[_0x02b6('0x1e')](_0x5dc7c1['query'][_0x02b6('0x1e')]);_0x2ea065[_0x02b6('0x20')]=qs[_0x02b6('0x20')](_0x5dc7c1['query'][_0x02b6('0x20')]);}_0x2ea065['order']=qs[_0x02b6('0x39')](_0x5dc7c1['query']['sort']);_0x2ea065['where']=qs[_0x02b6('0x3a')](_[_0x02b6('0x3b')](_0x5dc7c1['query'],_0xd1242c[_0x02b6('0x3a')]),_0x1291bc);if(_0x5dc7c1[_0x02b6('0x30')]['filter']){_0x2ea065[_0x02b6('0x3c')]=_[_0x02b6('0x3d')](_0x2ea065[_0x02b6('0x3c')],{'$or':_[_0x02b6('0x2a')](_0x1291bc,function(_0x3febb4){if(_0x3febb4['type']!=='VIRTUAL'){var _0x3b5350={};_0x3b5350[_0x3febb4[_0x02b6('0x27')]]={'$like':'%'+_0x5dc7c1[_0x02b6('0x30')][_0x02b6('0x3e')]+'%'};return _0x3b5350;}})});}_0x2ea065=_[_0x02b6('0x3d')]({},_0x2ea065,_0x5dc7c1['options']);var _0x2be117={'where':_0x2ea065[_0x02b6('0x3c')]};return db[_0x02b6('0x2b')][_0x02b6('0x1f')](_0x2be117)[_0x02b6('0x10')](function(_0x4bf4a7){_0x47592f['count']=_0x4bf4a7;if(_0x5dc7c1['query'][_0x02b6('0x3f')]){_0x2ea065['include']=[{'all':!![]}];}return db[_0x02b6('0x2b')][_0x02b6('0x40')](_0x2ea065);})['then'](function(_0x14eee3){_0x47592f[_0x02b6('0x41')]=_0x14eee3;return _0x47592f;})['then'](respondWithFilteredResult(_0x5c275f,_0x2ea065))[_0x02b6('0x1a')](handleError(_0x5c275f,null));};exports[_0x02b6('0x42')]=function(_0x43ffab,_0x400deb){var _0x2527b7={'raw':!![],'where':{'id':_0x43ffab[_0x02b6('0x43')]['id']}},_0x3cafb1={};_0x3cafb1[_0x02b6('0x34')]=_[_0x02b6('0x31')](db[_0x02b6('0x2b')][_0x02b6('0x2c')]);_0x3cafb1['query']=_[_0x02b6('0x31')](_0x43ffab[_0x02b6('0x30')]);_0x3cafb1[_0x02b6('0x3a')]=_[_0x02b6('0x32')](_0x3cafb1['model'],_0x3cafb1[_0x02b6('0x30')]);_0x2527b7[_0x02b6('0x33')]=_[_0x02b6('0x32')](_0x3cafb1['model'],qs[_0x02b6('0x35')](_0x43ffab[_0x02b6('0x30')]['fields']));_0x2527b7['attributes']=_0x2527b7['attributes'][_0x02b6('0x36')]?_0x2527b7[_0x02b6('0x33')]:_0x3cafb1['model'];if(_0x43ffab[_0x02b6('0x30')][_0x02b6('0x3f')]){_0x2527b7[_0x02b6('0x44')]=[{'all':!![]}];}_0x2527b7=_[_0x02b6('0x3d')]({},_0x2527b7,_0x43ffab['options']);return db[_0x02b6('0x2b')]['find'](_0x2527b7)['then'](handleEntityNotFound(_0x400deb,null))[_0x02b6('0x10')](respondWithResult(_0x400deb,null))[_0x02b6('0x1a')](handleError(_0x400deb,null));};exports[_0x02b6('0x45')]=function(_0x57bd6c,_0x3dee95){if(_0x57bd6c[_0x02b6('0x46')]['id']){delete _0x57bd6c[_0x02b6('0x46')]['id'];}return db[_0x02b6('0x2b')][_0x02b6('0x47')]({'where':{'id':_0x57bd6c[_0x02b6('0x43')]['id']}})[_0x02b6('0x10')](handleEntityNotFound(_0x3dee95,null))['then'](saveUpdates(_0x57bd6c['body'],null))['then'](respondWithResult(_0x3dee95,null))[_0x02b6('0x1a')](handleError(_0x3dee95,null));};exports['destroy']=function(_0x35566c,_0x257aa8){return db['MailMessage']['find']({'where':{'id':_0x35566c[_0x02b6('0x43')]['id']}})['then'](handleEntityNotFound(_0x257aa8,null))[_0x02b6('0x10')](removeEntity(_0x257aa8,null))[_0x02b6('0x1a')](handleError(_0x257aa8,null));};exports['describe']=function(_0xa68031,_0x3989c5){return db[_0x02b6('0x2b')][_0x02b6('0x48')]()[_0x02b6('0x10')](respondWithResult(_0x3989c5,null))['catch'](handleError(_0x3989c5,null));};exports[_0x02b6('0x49')]=function(_0x2e9d25,_0x48f98f){return db[_0x02b6('0x2b')]['create'](_0x2e9d25['body'])[_0x02b6('0x10')](function(_0x8cc3f0){if(_0x8cc3f0){if(_0x8cc3f0[_0x02b6('0x4a')]===_0x02b6('0x4b')&&!_0x8cc3f0[_0x02b6('0x4c')]){var _0x232c16,_0x4497b1,_0x68e9f5;if(_0x2e9d25[_0x02b6('0x46')]['to']){_0x4497b1=_0x2e9d25['body']['to'][_0x02b6('0x4d')](',')[0x0];}return db[_0x02b6('0x4e')][_0x02b6('0x4f')]({'where':{'id':_0x8cc3f0[_0x02b6('0x50')]},'include':[{'model':db[_0x02b6('0x51')],'as':_0x02b6('0x52'),'include':[{'model':db[_0x02b6('0x53')],'as':'Contacts','where':{'email':_0x4497b1},'limit':0x1,'order':[[_0x02b6('0x54'),'DESC']]}]},{'model':db[_0x02b6('0x55')],'as':_0x02b6('0x56'),'raw':!![],'include':[{'model':db[_0x02b6('0x57')],'as':'CloudProvider','raw':!![]}]}]})[_0x02b6('0x10')](function(_0x1221c4){_0x232c16=_0x1221c4;if(!_0x232c16||!_0x232c16['Smtp'])return;var _0x342dbd=_0x232c16[_0x02b6('0x58')]();if(_0x2e9d25[_0x02b6('0x46')][_0x02b6('0x59')]&&_0x2e9d25[_0x02b6('0x46')][_0x02b6('0x59')][_0x02b6('0x36')]){for(var _0x4e3ef0=0x0;_0x4e3ef0<_0x2e9d25[_0x02b6('0x46')][_0x02b6('0x59')][_0x02b6('0x36')];_0x4e3ef0+=0x1){_0x2e9d25[_0x02b6('0x46')][_0x02b6('0x59')][_0x4e3ef0]={'filename':_0x2e9d25[_0x02b6('0x46')][_0x02b6('0x59')][_0x4e3ef0][_0x02b6('0x27')],'path':path[_0x02b6('0x5a')](config[_0x02b6('0x5b')],_0x02b6('0x5c'),_0x2e9d25[_0x02b6('0x46')][_0x02b6('0x59')][_0x4e3ef0][_0x02b6('0x5d')])};}}_0x68e9f5={'account':_0x342dbd,'message':_0x2e9d25[_0x02b6('0x46')]};return;})['then'](function(){if(_0x232c16[_0x02b6('0x52')]){if(_0x232c16['List'][_0x02b6('0x5e')][_0x02b6('0x36')]){return _0x232c16[_0x02b6('0x52')][_0x02b6('0x5e')][0x0];}else{return db[_0x02b6('0x53')][_0x02b6('0x49')](_[_0x02b6('0x5f')](_0x2e9d25[_0x02b6('0x46')],{'firstName':_0x2e9d25['body']['to'],'email':_0x2e9d25[_0x02b6('0x46')]['to'],'phone':_0x2e9d25[_0x02b6('0x46')]['to'],'ListId':_0x232c16[_0x02b6('0x60')]}));}}else{throw new Error(_0x02b6('0x61'));}})[_0x02b6('0x10')](function(){if(_0x68e9f5){return respondWithRpcPromise(_0x02b6('0x62'),_0x02b6('0x63'),_0x68e9f5,client9003)[_0x02b6('0x10')](function(_0xb4d499){return _0x8cc3f0[_0x02b6('0x45')]({'read':!![],'messageId':_0xb4d499['messageId'],'sentAt':moment()[_0x02b6('0x64')](_0x02b6('0x65'))});})[_0x02b6('0x1a')](function(_0x2cc098){logger['error']('%s',JSON['stringify'](_0x2cc098));return _0x8cc3f0;});}return _0x8cc3f0;});}return _0x8cc3f0;}})[_0x02b6('0x10')](respondWithResult(_0x48f98f,0xc9))[_0x02b6('0x1a')](handleError(_0x48f98f,null));};exports[_0x02b6('0x66')]=function(_0x18b131,_0x4d5266){var _0x13e545={'agent':{},'channel':_0x02b6('0x67')};if(_0x18b131[_0x02b6('0x46')]['id']){delete _0x18b131[_0x02b6('0x46')]['id'];}_0x18b131[_0x02b6('0x46')][_0x02b6('0x68')]=!![];_0x18b131[_0x02b6('0x46')][_0x02b6('0x69')]=moment()[_0x02b6('0x64')]('YYYY-MM-DD\x20HH:mm:ss');_0x18b131['body'][_0x02b6('0x6a')]=_0x18b131[_0x02b6('0x46')][_0x02b6('0x6a')]||_0x18b131['user']['id'];_0x13e545[_0x02b6('0x6b')]['id']=_0x18b131['body'][_0x02b6('0x6a')];return db[_0x02b6('0x2b')]['find']({'where':{'id':_0x18b131[_0x02b6('0x43')]['id'],'UserId':null}})[_0x02b6('0x10')](handleEntityNotFound(_0x4d5266,null))['then'](saveUpdates(_0x18b131[_0x02b6('0x46')],null))['then'](function(_0x5d48e4){if(_0x5d48e4){_0x13e545[_0x02b6('0x18')]=_0x5d48e4[_0x02b6('0x6c')]({'plain':!![]});return db[_0x02b6('0x6d')][_0x02b6('0x47')]({'where':{'id':_0x5d48e4[_0x02b6('0x6e')]}});}return null;})['then'](handleEntityNotFound(_0x4d5266,null))[_0x02b6('0x10')](function(_0x2ef5a9){if(_0x2ef5a9){return _0x2ef5a9[_0x02b6('0x45')]({'UserId':_0x18b131['body']['UserId'],'read1stAt':_[_0x02b6('0x6f')](_0x2ef5a9['read1stAt'])?moment()['format'](_0x02b6('0x65')):undefined});}return null;})[_0x02b6('0x10')](function(_0x5beaa9){if(_0x5beaa9){_0x13e545[_0x02b6('0x70')]=_0x5beaa9[_0x02b6('0x6c')]({'plain':!![]});interaction_log[_0x02b6('0x11')](_0x02b6('0x71'),_0x18b131[_0x02b6('0x72')]['id'],_0x18b131[_0x02b6('0x72')][_0x02b6('0x27')],_0x18b131[_0x02b6('0x72')][_0x02b6('0x73')],_0x13e545[_0x02b6('0x70')]['id'],_0x18b131[_0x02b6('0x46')]?JSON[_0x02b6('0x15')](_0x18b131[_0x02b6('0x46')]):_0x02b6('0x74'));return respondWithRpcPromise(_0x02b6('0x75'),_0x02b6('0x76'),_0x13e545);}return null;})[_0x02b6('0x10')](function(_0x1ed9a4){var _0x31b2e6=_0x02b6('0x77')+_0x1ed9a4[_0x02b6('0x18')][_0x02b6('0x78')];return db[_0x02b6('0x79')]['query'](_0x31b2e6,{'type':db[_0x02b6('0x7a')][_0x02b6('0x7b')][_0x02b6('0x7c')],'raw':!![]})[_0x02b6('0x10')](function(_0xcdcbad){return _0xcdcbad;});})['then'](function(_0x8e7848){if(_0x8e7848){_0x13e545['message'][_0x02b6('0x7d')]=_0x8e7848[0x0];if(!_0x18b131['body']['UserId'])return;return db[_0x02b6('0x7e')]['find']({'where':{'id':_0x18b131[_0x02b6('0x46')][_0x02b6('0x6a')],'role':_0x02b6('0x6b')},'attributes':['id',_0x02b6('0x27'),_0x02b6('0x7f'),'email','internal'],'raw':!![]});}return null;})[_0x02b6('0x10')](function(_0x56a8f6){if(!_0x56a8f6)return null;_0x13e545['message'][_0x02b6('0x6b')]=_0x56a8f6;return _0x13e545;})[_0x02b6('0x10')](function(_0x2b6dce){if(_0x2b6dce){if(_0x18b131[_0x02b6('0x46')][_0x02b6('0x80')]){_0x13e545[_0x02b6('0x18')]['interface']=util[_0x02b6('0x64')](_0x02b6('0x81'),_0x18b131[_0x02b6('0x72')][_0x02b6('0x27')]);_0x13e545[_0x02b6('0x18')]['channel']=_0x13e545['channel'];_0x13e545[_0x02b6('0x18')][_0x02b6('0x82')]=_0x13e545[_0x02b6('0x83')];_0x13e545[_0x02b6('0x18')][_0x02b6('0x84')]=_0x02b6('0x66');_0x13e545[_0x02b6('0x18')]['body']=htmlToText[_0x02b6('0x85')](_0x13e545[_0x02b6('0x18')]['body']);return respondWithRpcPromise(_0x02b6('0x86'),_0x02b6('0x86'),{'event':_0x02b6('0x87'),'message':_0x13e545['message']},client9002)[_0x02b6('0x10')](function(){return _0x13e545;});}return _0x13e545;}return null;})['then'](respondWithResult(_0x4d5266,null))[_0x02b6('0x1a')](handleError(_0x4d5266,null));};exports[_0x02b6('0x88')]=function(_0x1d7ba2,_0x409505){var _0x46b45d={'agent':{},'channel':_0x02b6('0x67')};if(_0x1d7ba2[_0x02b6('0x46')]['id']){delete _0x1d7ba2[_0x02b6('0x46')]['id'];}_0x1d7ba2['body'][_0x02b6('0x6a')]=_0x1d7ba2['body'][_0x02b6('0x6a')]||_0x1d7ba2['user']['id'];_0x46b45d['agent']['id']=_0x1d7ba2[_0x02b6('0x46')]['UserId'];return db['MailMessage'][_0x02b6('0x47')]({'where':{'id':_0x1d7ba2[_0x02b6('0x43')]['id'],'UserId':null}})[_0x02b6('0x10')](handleEntityNotFound(_0x409505,null))[_0x02b6('0x10')](function(_0x51aa93){if(_0x51aa93){_0x46b45d[_0x02b6('0x18')]=_0x51aa93[_0x02b6('0x6c')]({'plain':!![]});return db[_0x02b6('0x6d')][_0x02b6('0x47')]({'where':{'id':_0x51aa93['MailInteractionId']}});}return null;})['then'](handleEntityNotFound(_0x409505,null))[_0x02b6('0x10')](function(_0x5eff31){if(_0x5eff31){_0x46b45d[_0x02b6('0x70')]=_0x5eff31[_0x02b6('0x6c')]({'plain':!![]});interaction_log[_0x02b6('0x11')]('[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x1d7ba2[_0x02b6('0x72')]['id'],_0x1d7ba2['user'][_0x02b6('0x27')],_0x1d7ba2[_0x02b6('0x72')]['role'],_0x46b45d['interaction']['id'],_0x1d7ba2['body']?JSON[_0x02b6('0x15')](_0x1d7ba2[_0x02b6('0x46')]):'null');return respondWithRpcPromise(_0x02b6('0x89'),_0x02b6('0x8a'),_0x46b45d);}return null;})[_0x02b6('0x10')](function(_0x3d071f){var _0x53dbd4=_0x02b6('0x77')+_0x3d071f[_0x02b6('0x18')][_0x02b6('0x78')];return db['sequelize'][_0x02b6('0x30')](_0x53dbd4,{'type':db[_0x02b6('0x7a')][_0x02b6('0x7b')][_0x02b6('0x7c')],'raw':!![]})['then'](function(_0xd24141){return _0xd24141;});})['then'](function(_0x3a9652){if(_0x3a9652){_0x46b45d['message'][_0x02b6('0x7d')]=_0x3a9652[0x0];if(!_0x1d7ba2[_0x02b6('0x46')][_0x02b6('0x6a')])return;return db[_0x02b6('0x7e')][_0x02b6('0x47')]({'where':{'id':_0x1d7ba2[_0x02b6('0x46')][_0x02b6('0x6a')],'role':_0x02b6('0x6b')},'attributes':['id',_0x02b6('0x27'),'fullname',_0x02b6('0x8b'),_0x02b6('0x8c')],'raw':!![]});}return null;})[_0x02b6('0x10')](function(_0x46ff41){if(!_0x46ff41)return null;_0x46b45d['message'][_0x02b6('0x6b')]=_0x46ff41;return _0x46b45d;})['then'](function(_0x416180){if(_0x416180){_0x416180[_0x02b6('0x18')][_0x02b6('0x8d')]=util[_0x02b6('0x64')]('SIP/%s',_0x1d7ba2[_0x02b6('0x72')][_0x02b6('0x27')]);_0x416180[_0x02b6('0x18')]['channel']=_0x46b45d[_0x02b6('0x83')];_0x416180['message']['motionChannel']=_0x46b45d[_0x02b6('0x83')];_0x416180[_0x02b6('0x18')][_0x02b6('0x84')]='reject';_0x416180[_0x02b6('0x18')]['body']=htmlToText['fromString'](_0x46b45d[_0x02b6('0x18')][_0x02b6('0x46')]);respondWithRpcPromise(_0x02b6('0x86'),_0x02b6('0x86'),{'event':'rejectmessage','message':_0x416180[_0x02b6('0x18')]},client9002);return _0x46b45d;}return null;})['then'](respondWithResult(_0x409505,null))[_0x02b6('0x1a')](handleError(_0x409505,null));};exports[_0x02b6('0x8e')]=function(_0x197cde,_0x37aa45){var _0x1d60b4=moment()[_0x02b6('0x8f')]()[_0x02b6('0x90')]();var _0x4dbfd0=path[_0x02b6('0x5a')](config[_0x02b6('0x5b')],_0x02b6('0x91'),'files','tmp');var _0x390d75=path['join'](config[_0x02b6('0x5b')],_0x02b6('0x91'),_0x02b6('0x92'),_0x02b6('0x59'));var _0x4fe87b=util['format'](_0x02b6('0x93'),_0x1d60b4);return db[_0x02b6('0x2b')][_0x02b6('0x47')]({'where':{'id':_0x197cde[_0x02b6('0x43')]['id']},'attributes':['id',_0x02b6('0x46'),'from','to','cc',_0x02b6('0x94'),_0x02b6('0x95'),_0x02b6('0x96')],'include':_0x197cde[_0x02b6('0x30')][_0x02b6('0x59')]?[{'model':db[_0x02b6('0x97')],'as':_0x02b6('0x98')}]:undefined})[_0x02b6('0x10')](handleEntityNotFound(_0x37aa45,null))[_0x02b6('0x10')](function(_0x3b24d4){if(_0x3b24d4){var _0xb6feba=_0x3b24d4;var _0x538eaa={'from':_0xb6feba[_0x02b6('0x99')],'to':_0xb6feba['to'],'subject':_0xb6feba['subject'],'html':htmlEntities[_0x02b6('0x9a')](_0xb6feba[_0x02b6('0x46')]),'headers':{'Date':moment(_0xb6feba['createdAt'])['format'](_0x02b6('0x9b'))},'attachments':[]};if(_0x3b24d4['cc']){_0x538eaa[_0x02b6('0x9c')]['Cc']=_0x3b24d4['cc'];}if(_0x3b24d4[_0x02b6('0x94')]){_0x538eaa[_0x02b6('0x9c')][_0x02b6('0x9d')]=_0x3b24d4[_0x02b6('0x94')];}return new BPromise(function(_0x266937,_0x86620a){if(_0x3b24d4['Attachments']){for(var _0x28ce88=0x0;_0x28ce88<_0x3b24d4[_0x02b6('0x98')][_0x02b6('0x36')];_0x28ce88++){var _0x1548ab=_0x3b24d4[_0x02b6('0x98')][_0x28ce88];var _0x4a3df7=path[_0x02b6('0x5a')](_0x390d75,_0x1548ab[_0x02b6('0x5d')]);if(fs[_0x02b6('0x9e')](_0x4a3df7)){_0x538eaa[_0x02b6('0x59')][_0x02b6('0x9f')]({'name':_0x1548ab[_0x02b6('0x27')],'contentType':_0x1548ab[_0x02b6('0x2e')],'data':fs[_0x02b6('0xa0')](_0x4a3df7)});}}}emlformat[_0x02b6('0xa1')](_0x538eaa,function(_0x19a9fb,_0xdcfcf8){if(_0x19a9fb)return _0x86620a(_0x19a9fb);fs[_0x02b6('0xa2')](path[_0x02b6('0x5a')](_0x4dbfd0,_0x4fe87b),_0xdcfcf8);return _0x266937(_0xdcfcf8);});});}})[_0x02b6('0x10')](function(_0x369d95){if(_0x369d95){return _0x37aa45['download'](path[_0x02b6('0x5a')](_0x4dbfd0,_0x4fe87b),_0x4fe87b,function(_0x5a50ef){if(!_0x5a50ef)fs[_0x02b6('0xa3')](path['join'](_0x4dbfd0,_0x4fe87b));});}})['catch'](handleError(_0x37aa45,null));};