Built motion from commit 9d0783e6.|2.6.13
[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 _0xb42f=['[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','null','RejectMessage','rejectMessage','Sequelize','event','download','unix','toString','server','files','tmp','message-%s.eml','from','subject','createdAt','Attachments','decode','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','bcc','headers','existsSync','push','readFileSync','build','writeFileSync','unlinkSync','eml-format','bluebird','util','path','lodash','ioredis','html-to-text','../../components/parsers/qs','api','../../config/logger','mail-interactions','../../config/environment','../../mysqldb','defaults','redis','localhost','socket.io-emitter','html-entities','./mailMessage.socket','register','jayson/promise','client','http','request','then','info','request\x20sent','debug','MailMessage,\x20%s,\x20%s,\x20%s','stringify','error','code','MailMessage,\x20%s,\x20%s','message','result','catch','status','json','offset','undefined','limit','count','set','Content-Range','update','destroy','end','stack','name','send','map','MailMessage','rawAttributes','fieldName','type','key','model','query','attributes','intersection','fields','hasOwnProperty','nolimit','order','sort','where','filters','filter','merge','VIRTUAL','options','includeAll','include','findAll','show','body','params','find','describe','create','direction','out','secret','split','MailAccount','findOne','MailAccountId','CmList','List','CmContact','Contacts','updatedAt','DESC','Smtp','CloudProvider','getSmtpOptions','attachments','length','join','root','basename','ListId','List\x20not\x20found','SendMail','sendMail','messageId','YYYY-MM-DD\x20HH:mm:ss','readAt','UserId','user','get','MailInteraction','MailInteractionId','isNil','read1stAt','format','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','interaction','acceptMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','sequelize','QueryTypes','SELECT','contact','User','fullname','email','internal','agent','manual','interface','SIP/%s','channel','motionChannel','accept','fromString','EventManager','acceptmessage','reject'];(function(_0x406c07,_0x188e1b){var _0x38722c=function(_0x2ab052){while(--_0x2ab052){_0x406c07['push'](_0x406c07['shift']());}};_0x38722c(++_0x188e1b);}(_0xb42f,0x16c));var _0xfb42=function(_0x3416c3,_0x36c13a){_0x3416c3=_0x3416c3-0x0;var _0x52000e=_0xb42f[_0x3416c3];return _0x52000e;};'use strict';var emlformat=require(_0xfb42('0x0'));var moment=require('moment');var BPromise=require(_0xfb42('0x1'));var util=require(_0xfb42('0x2'));var path=require(_0xfb42('0x3'));var fs=require('fs');var _=require(_0xfb42('0x4'));var Redis=require(_0xfb42('0x5'));var htmlToText=require(_0xfb42('0x6'));var qs=require(_0xfb42('0x7'));var logger=require('../../config/logger')(_0xfb42('0x8'));var interaction_log=require(_0xfb42('0x9'))(_0xfb42('0xa'));var config=require(_0xfb42('0xb'));var db=require(_0xfb42('0xc'))['db'];config['redis']=_[_0xfb42('0xd')](config[_0xfb42('0xe')],{'host':_0xfb42('0xf'),'port':0x18eb});var socket=require(_0xfb42('0x10'))(new Redis(config['redis']));var htmlEntities=require(_0xfb42('0x11'));require(_0xfb42('0x12'))[_0xfb42('0x13')](socket);var jayson=require(_0xfb42('0x14'));var client=jayson[_0xfb42('0x15')][_0xfb42('0x16')]({'port':0x232c});var client9003=jayson['client']['http']({'port':0x232b});var client9002=jayson[_0xfb42('0x15')][_0xfb42('0x16')]({'port':0x232a});function respondWithRpcPromise(_0x2748b3,_0x50ea00,_0x1bd94c,_0x17fb8d){return new BPromise(function(_0x4d01a9,_0x8e1f64){var _0x39a12f=_0x17fb8d||client;return _0x39a12f[_0xfb42('0x17')](_0x2748b3,_0x1bd94c)[_0xfb42('0x18')](function(_0x2ce4eb){logger[_0xfb42('0x19')]('MailMessage,\x20%s,\x20%s',_0x50ea00,_0xfb42('0x1a'));logger[_0xfb42('0x1b')](_0xfb42('0x1c'),_0x50ea00,_0xfb42('0x1a'),JSON[_0xfb42('0x1d')](_0x2ce4eb));if(_0x2ce4eb[_0xfb42('0x1e')]){if(_0x2ce4eb[_0xfb42('0x1e')][_0xfb42('0x1f')]===0x1f4){logger['error'](_0xfb42('0x20'),_0x50ea00,_0x2ce4eb[_0xfb42('0x1e')]['message']);return _0x8e1f64(_0x2ce4eb[_0xfb42('0x1e')][_0xfb42('0x21')]);}logger['error'](_0xfb42('0x20'),_0x50ea00,_0x2ce4eb['error'][_0xfb42('0x21')]);return _0x4d01a9(_0x2ce4eb['error'][_0xfb42('0x21')]);}else{logger[_0xfb42('0x19')]('MailMessage,\x20%s,\x20%s',_0x50ea00,_0xfb42('0x1a'));_0x4d01a9(_0x2ce4eb[_0xfb42('0x22')][_0xfb42('0x21')]);}})[_0xfb42('0x23')](function(_0x3c636d){logger['error']('MailMessage,\x20%s,\x20%s',_0x50ea00,_0x3c636d);_0x8e1f64(_0x3c636d);});});}function respondWithResult(_0x3f56a5,_0x555d5c){_0x555d5c=_0x555d5c||0xc8;return function(_0x5daed5){if(_0x5daed5){return _0x3f56a5[_0xfb42('0x24')](_0x555d5c)[_0xfb42('0x25')](_0x5daed5);}};}function respondWithFilteredResult(_0x58f700,_0xd48879){return function(_0x18a4a1){if(_0x18a4a1){var _0x24bb85=typeof _0xd48879[_0xfb42('0x26')]===_0xfb42('0x27')&&typeof _0xd48879[_0xfb42('0x28')]===_0xfb42('0x27');var _0x238b05=_0x18a4a1['count'];var _0x1b3cf8=_0x24bb85?0x0:_0xd48879[_0xfb42('0x26')];var _0x1cfdf8=_0x24bb85?_0x18a4a1[_0xfb42('0x29')]:_0xd48879['offset']+_0xd48879[_0xfb42('0x28')];var _0x31f12b;if(_0x1cfdf8>=_0x238b05){_0x1cfdf8=_0x238b05;_0x31f12b=0xc8;}else{_0x31f12b=0xce;}_0x58f700[_0xfb42('0x24')](_0x31f12b);return _0x58f700[_0xfb42('0x2a')](_0xfb42('0x2b'),_0x1b3cf8+'-'+_0x1cfdf8+'/'+_0x238b05)[_0xfb42('0x25')](_0x18a4a1);}return null;};}function saveUpdates(_0x5b6b65){return function(_0x4388d0){if(_0x4388d0){return _0x4388d0[_0xfb42('0x2c')](_0x5b6b65)[_0xfb42('0x18')](function(_0x51034b){return _0x51034b;});}return null;};}function removeEntity(_0x28d212){return function(_0x547fe8){if(_0x547fe8){return _0x547fe8[_0xfb42('0x2d')]()[_0xfb42('0x18')](function(){_0x28d212[_0xfb42('0x24')](0xcc)[_0xfb42('0x2e')]();});}};}function handleEntityNotFound(_0x5cd074){return function(_0x2705e1){if(!_0x2705e1){_0x5cd074['sendStatus'](0x194);}return _0x2705e1;};}function handleError(_0x49b67f,_0x1858c8){_0x1858c8=_0x1858c8||0x1f4;return function(_0x5c1a17){logger['error'](_0x5c1a17[_0xfb42('0x2f')]);if(_0x5c1a17['name']){delete _0x5c1a17[_0xfb42('0x30')];}_0x49b67f[_0xfb42('0x24')](_0x1858c8)[_0xfb42('0x31')](_0x5c1a17);};}exports['index']=function(_0x42c68c,_0x2d2a87){var _0x22e792={},_0xb2b9f={},_0x2a257c={'count':0x0,'rows':[]};var _0x2b667f=_[_0xfb42('0x32')](db[_0xfb42('0x33')][_0xfb42('0x34')],function(_0x1c0e45){return{'name':_0x1c0e45[_0xfb42('0x35')],'type':_0x1c0e45[_0xfb42('0x36')][_0xfb42('0x37')]};});_0xb2b9f[_0xfb42('0x38')]=_['map'](_0x2b667f,_0xfb42('0x30'));_0xb2b9f[_0xfb42('0x39')]=_['keys'](_0x42c68c[_0xfb42('0x39')]);_0xb2b9f['filters']=_['intersection'](_0xb2b9f[_0xfb42('0x38')],_0xb2b9f[_0xfb42('0x39')]);_0x22e792[_0xfb42('0x3a')]=_[_0xfb42('0x3b')](_0xb2b9f[_0xfb42('0x38')],qs[_0xfb42('0x3c')](_0x42c68c[_0xfb42('0x39')][_0xfb42('0x3c')]));_0x22e792[_0xfb42('0x3a')]=_0x22e792['attributes']['length']?_0x22e792[_0xfb42('0x3a')]:_0xb2b9f[_0xfb42('0x38')];if(!_0x42c68c[_0xfb42('0x39')][_0xfb42('0x3d')](_0xfb42('0x3e'))){_0x22e792[_0xfb42('0x28')]=qs[_0xfb42('0x28')](_0x42c68c[_0xfb42('0x39')][_0xfb42('0x28')]);_0x22e792[_0xfb42('0x26')]=qs['offset'](_0x42c68c[_0xfb42('0x39')]['offset']);}_0x22e792[_0xfb42('0x3f')]=qs[_0xfb42('0x40')](_0x42c68c[_0xfb42('0x39')][_0xfb42('0x40')]);_0x22e792[_0xfb42('0x41')]=qs[_0xfb42('0x42')](_['pick'](_0x42c68c[_0xfb42('0x39')],_0xb2b9f[_0xfb42('0x42')]),_0x2b667f);if(_0x42c68c[_0xfb42('0x39')][_0xfb42('0x43')]){_0x22e792[_0xfb42('0x41')]=_[_0xfb42('0x44')](_0x22e792[_0xfb42('0x41')],{'$or':_['map'](_0x2b667f,function(_0x2c632a){if(_0x2c632a[_0xfb42('0x36')]!==_0xfb42('0x45')){var _0x14612a={};_0x14612a[_0x2c632a[_0xfb42('0x30')]]={'$like':'%'+_0x42c68c[_0xfb42('0x39')][_0xfb42('0x43')]+'%'};return _0x14612a;}})});}_0x22e792=_[_0xfb42('0x44')]({},_0x22e792,_0x42c68c[_0xfb42('0x46')]);var _0x32f6f6={'where':_0x22e792[_0xfb42('0x41')]};return db[_0xfb42('0x33')][_0xfb42('0x29')](_0x32f6f6)[_0xfb42('0x18')](function(_0x23d361){_0x2a257c['count']=_0x23d361;if(_0x42c68c[_0xfb42('0x39')][_0xfb42('0x47')]){_0x22e792[_0xfb42('0x48')]=[{'all':!![]}];}return db[_0xfb42('0x33')][_0xfb42('0x49')](_0x22e792);})[_0xfb42('0x18')](function(_0x58e717){_0x2a257c['rows']=_0x58e717;return _0x2a257c;})[_0xfb42('0x18')](respondWithFilteredResult(_0x2d2a87,_0x22e792))[_0xfb42('0x23')](handleError(_0x2d2a87,null));};exports[_0xfb42('0x4a')]=function(_0x2b093e,_0xef6eb1){var _0x3f8e08={'raw':!![],'where':{'id':_0x2b093e['params']['id']}},_0x4ea8d6={};_0x4ea8d6[_0xfb42('0x38')]=_['keys'](db['MailMessage']['rawAttributes']);_0x4ea8d6['query']=_['keys'](_0x2b093e[_0xfb42('0x39')]);_0x4ea8d6[_0xfb42('0x42')]=_[_0xfb42('0x3b')](_0x4ea8d6['model'],_0x4ea8d6[_0xfb42('0x39')]);_0x3f8e08[_0xfb42('0x3a')]=_[_0xfb42('0x3b')](_0x4ea8d6[_0xfb42('0x38')],qs[_0xfb42('0x3c')](_0x2b093e['query'][_0xfb42('0x3c')]));_0x3f8e08[_0xfb42('0x3a')]=_0x3f8e08[_0xfb42('0x3a')]['length']?_0x3f8e08[_0xfb42('0x3a')]:_0x4ea8d6[_0xfb42('0x38')];if(_0x2b093e[_0xfb42('0x39')]['includeAll']){_0x3f8e08[_0xfb42('0x48')]=[{'all':!![]}];}_0x3f8e08=_[_0xfb42('0x44')]({},_0x3f8e08,_0x2b093e[_0xfb42('0x46')]);return db[_0xfb42('0x33')]['find'](_0x3f8e08)[_0xfb42('0x18')](handleEntityNotFound(_0xef6eb1,null))['then'](respondWithResult(_0xef6eb1,null))[_0xfb42('0x23')](handleError(_0xef6eb1,null));};exports[_0xfb42('0x2c')]=function(_0x4361c4,_0x1ed341){if(_0x4361c4[_0xfb42('0x4b')]['id']){delete _0x4361c4[_0xfb42('0x4b')]['id'];}return db['MailMessage']['find']({'where':{'id':_0x4361c4[_0xfb42('0x4c')]['id']}})[_0xfb42('0x18')](handleEntityNotFound(_0x1ed341,null))['then'](saveUpdates(_0x4361c4['body'],null))[_0xfb42('0x18')](respondWithResult(_0x1ed341,null))[_0xfb42('0x23')](handleError(_0x1ed341,null));};exports['destroy']=function(_0x4df4a3,_0x51647b){return db['MailMessage'][_0xfb42('0x4d')]({'where':{'id':_0x4df4a3['params']['id']}})[_0xfb42('0x18')](handleEntityNotFound(_0x51647b,null))[_0xfb42('0x18')](removeEntity(_0x51647b,null))['catch'](handleError(_0x51647b,null));};exports[_0xfb42('0x4e')]=function(_0x50b9b7,_0x18feea){return db[_0xfb42('0x33')][_0xfb42('0x4e')]()['then'](respondWithResult(_0x18feea,null))[_0xfb42('0x23')](handleError(_0x18feea,null));};exports[_0xfb42('0x4f')]=function(_0x1a8968,_0x462e75){return db[_0xfb42('0x33')]['create'](_0x1a8968[_0xfb42('0x4b')])['then'](function(_0x28e658){if(_0x28e658){if(_0x28e658[_0xfb42('0x50')]===_0xfb42('0x51')&&!_0x28e658[_0xfb42('0x52')]){var _0x274eba,_0x579b8a,_0x41bb75;if(_0x1a8968[_0xfb42('0x4b')]['to']){_0x579b8a=_0x1a8968['body']['to'][_0xfb42('0x53')](',')[0x0];}return db[_0xfb42('0x54')][_0xfb42('0x55')]({'where':{'id':_0x28e658[_0xfb42('0x56')]},'include':[{'model':db[_0xfb42('0x57')],'as':_0xfb42('0x58'),'include':[{'model':db[_0xfb42('0x59')],'as':_0xfb42('0x5a'),'where':{'email':_0x579b8a},'limit':0x1,'order':[[_0xfb42('0x5b'),_0xfb42('0x5c')]]}]},{'model':db['MailServerOut'],'as':_0xfb42('0x5d'),'raw':!![],'include':[{'model':db[_0xfb42('0x5e')],'as':_0xfb42('0x5e'),'raw':!![]}]}]})[_0xfb42('0x18')](function(_0x16d436){_0x274eba=_0x16d436;if(!_0x274eba||!_0x274eba[_0xfb42('0x5d')])return;var _0x587ebd=_0x274eba[_0xfb42('0x5f')]();if(_0x1a8968[_0xfb42('0x4b')]['attachments']&&_0x1a8968[_0xfb42('0x4b')][_0xfb42('0x60')][_0xfb42('0x61')]){for(var _0x4aed82=0x0;_0x4aed82<_0x1a8968[_0xfb42('0x4b')][_0xfb42('0x60')][_0xfb42('0x61')];_0x4aed82+=0x1){_0x1a8968[_0xfb42('0x4b')][_0xfb42('0x60')][_0x4aed82]={'filename':_0x1a8968[_0xfb42('0x4b')]['attachments'][_0x4aed82]['name'],'path':path[_0xfb42('0x62')](config[_0xfb42('0x63')],'server/files/attachments',_0x1a8968[_0xfb42('0x4b')][_0xfb42('0x60')][_0x4aed82][_0xfb42('0x64')])};}}_0x41bb75={'account':_0x587ebd,'message':_0x1a8968[_0xfb42('0x4b')]};return;})['then'](function(){if(_0x274eba[_0xfb42('0x58')]){if(_0x274eba['List'][_0xfb42('0x5a')][_0xfb42('0x61')]){return _0x274eba['List'][_0xfb42('0x5a')][0x0];}else{return db[_0xfb42('0x59')]['create'](_['defaults'](_0x1a8968['body'],{'firstName':_0x1a8968['body']['to'],'email':_0x1a8968['body']['to'],'phone':_0x1a8968[_0xfb42('0x4b')]['to'],'ListId':_0x274eba[_0xfb42('0x65')]}));}}else{throw new Error(_0xfb42('0x66'));}})[_0xfb42('0x18')](function(){if(_0x41bb75){return respondWithRpcPromise(_0xfb42('0x67'),_0xfb42('0x68'),_0x41bb75,client9003)['then'](function(_0x171d02){return _0x28e658[_0xfb42('0x2c')]({'read':!![],'messageId':_0x171d02[_0xfb42('0x69')],'sentAt':moment()['format'](_0xfb42('0x6a'))});})[_0xfb42('0x23')](function(_0x55e398){logger['error']('%s',JSON[_0xfb42('0x1d')](_0x55e398));return _0x28e658;});}return _0x28e658;});}return _0x28e658;}})[_0xfb42('0x18')](respondWithResult(_0x462e75,0xc9))[_0xfb42('0x23')](handleError(_0x462e75,null));};exports['accept']=function(_0x2da62a,_0x3a0a59){var _0x476c37={'agent':{},'channel':'mail'};if(_0x2da62a[_0xfb42('0x4b')]['id']){delete _0x2da62a[_0xfb42('0x4b')]['id'];}_0x2da62a['body']['read']=!![];_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x6b')]=moment()['format'](_0xfb42('0x6a'));_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x6c')]=_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x6c')]||_0x2da62a[_0xfb42('0x6d')]['id'];_0x476c37['agent']['id']=_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x6c')];return db[_0xfb42('0x33')]['find']({'where':{'id':_0x2da62a['params']['id'],'UserId':null}})['then'](handleEntityNotFound(_0x3a0a59,null))[_0xfb42('0x18')](saveUpdates(_0x2da62a[_0xfb42('0x4b')],null))['then'](function(_0x119df9){if(_0x119df9){_0x476c37[_0xfb42('0x21')]=_0x119df9[_0xfb42('0x6e')]({'plain':!![]});return db[_0xfb42('0x6f')][_0xfb42('0x4d')]({'where':{'id':_0x119df9[_0xfb42('0x70')]}});}return null;})[_0xfb42('0x18')](handleEntityNotFound(_0x3a0a59,null))[_0xfb42('0x18')](function(_0x35594b){if(_0x35594b){return _0x35594b[_0xfb42('0x2c')]({'UserId':_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x6c')],'read1stAt':_[_0xfb42('0x71')](_0x35594b[_0xfb42('0x72')])?moment()[_0xfb42('0x73')]('YYYY-MM-DD\x20HH:mm:ss'):undefined});}return null;})['then'](function(_0xdb1956){if(_0xdb1956){_0x476c37['interaction']=_0xdb1956[_0xfb42('0x6e')]({'plain':!![]});interaction_log[_0xfb42('0x19')](_0xfb42('0x74'),_0x2da62a[_0xfb42('0x6d')]['id'],_0x2da62a[_0xfb42('0x6d')][_0xfb42('0x30')],_0x2da62a['user']['role'],_0x476c37[_0xfb42('0x75')]['id'],_0x2da62a[_0xfb42('0x4b')]?JSON[_0xfb42('0x1d')](_0x2da62a[_0xfb42('0x4b')]):'null');return respondWithRpcPromise('AcceptMessage',_0xfb42('0x76'),_0x476c37);}return null;})[_0xfb42('0x18')](function(_0x91ed02){var _0x1beb99=_0xfb42('0x77')+_0x91ed02[_0xfb42('0x21')][_0xfb42('0x78')];return db[_0xfb42('0x79')][_0xfb42('0x39')](_0x1beb99,{'type':db['Sequelize'][_0xfb42('0x7a')][_0xfb42('0x7b')],'raw':!![]})[_0xfb42('0x18')](function(_0x5eef01){return _0x5eef01;});})[_0xfb42('0x18')](function(_0x2d2664){if(_0x2d2664){_0x476c37[_0xfb42('0x21')][_0xfb42('0x7c')]=_0x2d2664[0x0];if(!_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x6c')])return;return db[_0xfb42('0x7d')]['find']({'where':{'id':_0x2da62a[_0xfb42('0x4b')]['UserId'],'role':'agent'},'attributes':['id',_0xfb42('0x30'),_0xfb42('0x7e'),_0xfb42('0x7f'),_0xfb42('0x80')],'raw':!![]});}return null;})['then'](function(_0x134906){if(!_0x134906)return null;_0x476c37['message'][_0xfb42('0x81')]=_0x134906;return _0x476c37;})[_0xfb42('0x18')](function(_0x232559){if(_0x232559){if(_0x2da62a[_0xfb42('0x4b')][_0xfb42('0x82')]){_0x476c37[_0xfb42('0x21')][_0xfb42('0x83')]=util[_0xfb42('0x73')](_0xfb42('0x84'),_0x2da62a[_0xfb42('0x6d')][_0xfb42('0x30')]);_0x476c37[_0xfb42('0x21')][_0xfb42('0x85')]=_0x476c37[_0xfb42('0x85')];_0x476c37[_0xfb42('0x21')][_0xfb42('0x86')]=_0x476c37['channel'];_0x476c37[_0xfb42('0x21')]['event']=_0xfb42('0x87');_0x476c37[_0xfb42('0x21')][_0xfb42('0x4b')]=htmlToText[_0xfb42('0x88')](_0x476c37[_0xfb42('0x21')][_0xfb42('0x4b')]);return respondWithRpcPromise(_0xfb42('0x89'),_0xfb42('0x89'),{'event':_0xfb42('0x8a'),'message':_0x476c37[_0xfb42('0x21')]},client9002)[_0xfb42('0x18')](function(){return _0x476c37;});}return _0x476c37;}return null;})[_0xfb42('0x18')](respondWithResult(_0x3a0a59,null))[_0xfb42('0x23')](handleError(_0x3a0a59,null));};exports[_0xfb42('0x8b')]=function(_0x4d8f7f,_0x424cc0){var _0x1150da={'agent':{},'channel':'mail'};if(_0x4d8f7f['body']['id']){delete _0x4d8f7f[_0xfb42('0x4b')]['id'];}_0x4d8f7f['body']['UserId']=_0x4d8f7f[_0xfb42('0x4b')][_0xfb42('0x6c')]||_0x4d8f7f[_0xfb42('0x6d')]['id'];_0x1150da[_0xfb42('0x81')]['id']=_0x4d8f7f[_0xfb42('0x4b')]['UserId'];return db[_0xfb42('0x33')][_0xfb42('0x4d')]({'where':{'id':_0x4d8f7f[_0xfb42('0x4c')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0x424cc0,null))[_0xfb42('0x18')](function(_0x31be92){if(_0x31be92){_0x1150da[_0xfb42('0x21')]=_0x31be92[_0xfb42('0x6e')]({'plain':!![]});return db[_0xfb42('0x6f')][_0xfb42('0x4d')]({'where':{'id':_0x31be92[_0xfb42('0x70')]}});}return null;})[_0xfb42('0x18')](handleEntityNotFound(_0x424cc0,null))[_0xfb42('0x18')](function(_0x51a034){if(_0x51a034){_0x1150da[_0xfb42('0x75')]=_0x51a034['get']({'plain':!![]});interaction_log['info'](_0xfb42('0x8c'),_0x4d8f7f['user']['id'],_0x4d8f7f[_0xfb42('0x6d')][_0xfb42('0x30')],_0x4d8f7f[_0xfb42('0x6d')][_0xfb42('0x8d')],_0x1150da[_0xfb42('0x75')]['id'],_0x4d8f7f['body']?JSON['stringify'](_0x4d8f7f[_0xfb42('0x4b')]):_0xfb42('0x8e'));return respondWithRpcPromise(_0xfb42('0x8f'),_0xfb42('0x90'),_0x1150da);}return null;})[_0xfb42('0x18')](function(_0x560901){var _0x1cbc40=_0xfb42('0x77')+_0x560901[_0xfb42('0x21')][_0xfb42('0x78')];return db[_0xfb42('0x79')][_0xfb42('0x39')](_0x1cbc40,{'type':db[_0xfb42('0x91')]['QueryTypes'][_0xfb42('0x7b')],'raw':!![]})[_0xfb42('0x18')](function(_0x1746ce){return _0x1746ce;});})['then'](function(_0x4d49fa){if(_0x4d49fa){_0x1150da['message'][_0xfb42('0x7c')]=_0x4d49fa[0x0];if(!_0x4d8f7f[_0xfb42('0x4b')][_0xfb42('0x6c')])return;return db['User']['find']({'where':{'id':_0x4d8f7f[_0xfb42('0x4b')][_0xfb42('0x6c')],'role':'agent'},'attributes':['id',_0xfb42('0x30'),_0xfb42('0x7e'),_0xfb42('0x7f'),_0xfb42('0x80')],'raw':!![]});}return null;})[_0xfb42('0x18')](function(_0x121b03){if(!_0x121b03)return null;_0x1150da[_0xfb42('0x21')]['agent']=_0x121b03;return _0x1150da;})[_0xfb42('0x18')](function(_0xe4763){if(_0xe4763){_0xe4763['message'][_0xfb42('0x83')]=util[_0xfb42('0x73')](_0xfb42('0x84'),_0x4d8f7f[_0xfb42('0x6d')][_0xfb42('0x30')]);_0xe4763[_0xfb42('0x21')][_0xfb42('0x85')]=_0x1150da['channel'];_0xe4763[_0xfb42('0x21')]['motionChannel']=_0x1150da[_0xfb42('0x85')];_0xe4763[_0xfb42('0x21')][_0xfb42('0x92')]='reject';_0xe4763[_0xfb42('0x21')][_0xfb42('0x4b')]=htmlToText[_0xfb42('0x88')](_0x1150da[_0xfb42('0x21')][_0xfb42('0x4b')]);respondWithRpcPromise(_0xfb42('0x89'),'EventManager',{'event':'rejectmessage','message':_0xe4763[_0xfb42('0x21')]},client9002);return _0x1150da;}return null;})[_0xfb42('0x18')](respondWithResult(_0x424cc0,null))[_0xfb42('0x23')](handleError(_0x424cc0,null));};exports[_0xfb42('0x93')]=function(_0x350c13,_0x2d010b){var _0x4c0cb7=moment()[_0xfb42('0x94')]()[_0xfb42('0x95')]();var _0x5753d1=path[_0xfb42('0x62')](config[_0xfb42('0x63')],_0xfb42('0x96'),_0xfb42('0x97'),_0xfb42('0x98'));var _0x350887=path['join'](config[_0xfb42('0x63')],'server',_0xfb42('0x97'),_0xfb42('0x60'));var _0x5c0d2a=util[_0xfb42('0x73')](_0xfb42('0x99'),_0x4c0cb7);return db[_0xfb42('0x33')]['find']({'where':{'id':_0x350c13[_0xfb42('0x4c')]['id']},'attributes':['id',_0xfb42('0x4b'),_0xfb42('0x9a'),'to','cc','bcc',_0xfb42('0x9b'),_0xfb42('0x9c')],'include':_0x350c13[_0xfb42('0x39')][_0xfb42('0x60')]?[{'model':db['Attachment'],'as':_0xfb42('0x9d')}]:undefined})[_0xfb42('0x18')](handleEntityNotFound(_0x2d010b,null))[_0xfb42('0x18')](function(_0x57f9f3){if(_0x57f9f3){var _0x33ce61=_0x57f9f3;var _0x1cb0f2={'from':_0x33ce61[_0xfb42('0x9a')],'to':_0x33ce61['to'],'subject':_0x33ce61[_0xfb42('0x9b')],'html':htmlEntities[_0xfb42('0x9e')](_0x33ce61[_0xfb42('0x4b')]),'headers':{'Date':moment(_0x33ce61['createdAt'])[_0xfb42('0x73')](_0xfb42('0x9f'))},'attachments':[]};if(_0x57f9f3['cc']){_0x1cb0f2['headers']['Cc']=_0x57f9f3['cc'];}if(_0x57f9f3[_0xfb42('0xa0')]){_0x1cb0f2[_0xfb42('0xa1')]['Bcc']=_0x57f9f3[_0xfb42('0xa0')];}return new BPromise(function(_0x666226,_0x4b0ebb){if(_0x57f9f3[_0xfb42('0x9d')]){for(var _0xb75f70=0x0;_0xb75f70<_0x57f9f3[_0xfb42('0x9d')][_0xfb42('0x61')];_0xb75f70++){var _0x366ddb=_0x57f9f3[_0xfb42('0x9d')][_0xb75f70];var _0x49e40a=path[_0xfb42('0x62')](_0x350887,_0x366ddb[_0xfb42('0x64')]);if(fs[_0xfb42('0xa2')](_0x49e40a)){_0x1cb0f2[_0xfb42('0x60')][_0xfb42('0xa3')]({'name':_0x366ddb[_0xfb42('0x30')],'contentType':_0x366ddb[_0xfb42('0x36')],'data':fs[_0xfb42('0xa4')](_0x49e40a)});}}}emlformat[_0xfb42('0xa5')](_0x1cb0f2,function(_0x2ed65a,_0x3003b5){if(_0x2ed65a)return _0x4b0ebb(_0x2ed65a);fs[_0xfb42('0xa6')](path[_0xfb42('0x62')](_0x5753d1,_0x5c0d2a),_0x3003b5);return _0x666226(_0x3003b5);});});}})[_0xfb42('0x18')](function(_0x3e0198){if(_0x3e0198){return _0x2d010b[_0xfb42('0x93')](path[_0xfb42('0x62')](_0x5753d1,_0x5c0d2a),_0x5c0d2a,function(_0x1dfb4e){if(!_0x1dfb4e)fs[_0xfb42('0xa7')](path[_0xfb42('0x62')](_0x5753d1,_0x5c0d2a));});}})['catch'](handleError(_0x2d010b,null));};