Built motion from commit eddce310.|2.6.12
[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 _0xbf89=['bluebird','util','path','ioredis','html-to-text','../../config/logger','../../config/environment','../../mysqldb','redis','defaults','socket.io-emitter','./mailMessage.socket','jayson/promise','client','http','request','then','info','MailMessage,\x20%s,\x20%s','request\x20sent','debug','MailMessage,\x20%s,\x20%s,\x20%s','error','message','result','catch','json','offset','undefined','limit','count','status','set','Content-Range','update','destroy','name','send','index','map','MailMessage','rawAttributes','fieldName','type','key','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','find','body','params','create','out','MailAccount','findOne','CmList','List','Contacts','updatedAt','DESC','MailServerOut','Smtp','CloudProvider','getSmtpOptions','attachments','join','root','server/files/attachments','basename','CmContact','ListId','SendMail','sendMail','format','YYYY-MM-DD\x20HH:mm:ss','stringify','accept','read','readAt','UserId','agent','get','MailInteraction','MailInteractionId','isNil','interaction','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','user','role','acceptMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','sequelize','Sequelize','QueryTypes','SELECT','contact','User','email','internal','manual','interface','SIP/%s','motionChannel','channel','event','EventManager','acceptmessage','reject','mail','[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','fullname','rejectmessage','download','toString','files','tmp','server','message-%s.eml','from','subject','createdAt','Attachment','decode','bcc','headers','Attachments','push','readFileSync','build','writeFileSync','eml-format'];(function(_0x160817,_0x3a7d36){var _0x260566=function(_0x1c337e){while(--_0x1c337e){_0x160817['push'](_0x160817['shift']());}};_0x260566(++_0x3a7d36);}(_0xbf89,0x131));var _0x9bf8=function(_0x4a00d0,_0x56d533){_0x4a00d0=_0x4a00d0-0x0;var _0x6e25bf=_0xbf89[_0x4a00d0];return _0x6e25bf;};'use strict';var emlformat=require(_0x9bf8('0x0'));var moment=require('moment');var BPromise=require(_0x9bf8('0x1'));var util=require(_0x9bf8('0x2'));var path=require(_0x9bf8('0x3'));var fs=require('fs');var _=require('lodash');var Redis=require(_0x9bf8('0x4'));var htmlToText=require(_0x9bf8('0x5'));var qs=require('../../components/parsers/qs');var logger=require(_0x9bf8('0x6'))('api');var interaction_log=require('../../config/logger')('mail-interactions');var config=require(_0x9bf8('0x7'));var db=require(_0x9bf8('0x8'))['db'];config[_0x9bf8('0x9')]=_[_0x9bf8('0xa')](config[_0x9bf8('0x9')],{'host':'localhost','port':0x18eb});var socket=require(_0x9bf8('0xb'))(new Redis(config[_0x9bf8('0x9')]));var htmlEntities=require('html-entities');require(_0x9bf8('0xc'))['register'](socket);var jayson=require(_0x9bf8('0xd'));var client=jayson[_0x9bf8('0xe')][_0x9bf8('0xf')]({'port':0x232c});var client9003=jayson[_0x9bf8('0xe')][_0x9bf8('0xf')]({'port':0x232b});var client9002=jayson[_0x9bf8('0xe')][_0x9bf8('0xf')]({'port':0x232a});function respondWithRpcPromise(_0x4df685,_0x3243a9,_0x129f96,_0x4334ac){return new BPromise(function(_0x208bc0,_0xeb042){var _0x7697f3=_0x4334ac||client;return _0x7697f3[_0x9bf8('0x10')](_0x4df685,_0x129f96)[_0x9bf8('0x11')](function(_0x5d94c0){logger[_0x9bf8('0x12')](_0x9bf8('0x13'),_0x3243a9,_0x9bf8('0x14'));logger[_0x9bf8('0x15')](_0x9bf8('0x16'),_0x3243a9,_0x9bf8('0x14'),JSON['stringify'](_0x5d94c0));if(_0x5d94c0[_0x9bf8('0x17')]){if(_0x5d94c0[_0x9bf8('0x17')]['code']===0x1f4){logger[_0x9bf8('0x17')](_0x9bf8('0x13'),_0x3243a9,_0x5d94c0['error'][_0x9bf8('0x18')]);return _0xeb042(_0x5d94c0[_0x9bf8('0x17')][_0x9bf8('0x18')]);}logger[_0x9bf8('0x17')]('MailMessage,\x20%s,\x20%s',_0x3243a9,_0x5d94c0[_0x9bf8('0x17')][_0x9bf8('0x18')]);return _0x208bc0(_0x5d94c0[_0x9bf8('0x17')][_0x9bf8('0x18')]);}else{logger['info'](_0x9bf8('0x13'),_0x3243a9,_0x9bf8('0x14'));_0x208bc0(_0x5d94c0[_0x9bf8('0x19')]['message']);}})[_0x9bf8('0x1a')](function(_0x55300e){logger[_0x9bf8('0x17')](_0x9bf8('0x13'),_0x3243a9,_0x55300e);_0xeb042(_0x55300e);});});}function respondWithResult(_0x1d6618,_0x145262){_0x145262=_0x145262||0xc8;return function(_0x41040f){if(_0x41040f){return _0x1d6618['status'](_0x145262)[_0x9bf8('0x1b')](_0x41040f);}};}function respondWithFilteredResult(_0x308aa2,_0x3cb69b){return function(_0x5561a7){if(_0x5561a7){var _0x43245a=typeof _0x3cb69b[_0x9bf8('0x1c')]===_0x9bf8('0x1d')&&typeof _0x3cb69b[_0x9bf8('0x1e')]===_0x9bf8('0x1d');var _0x5bd2f7=_0x5561a7[_0x9bf8('0x1f')];var _0x5c8fe7=_0x43245a?0x0:_0x3cb69b['offset'];var _0x5488b4=_0x43245a?_0x5561a7['count']:_0x3cb69b[_0x9bf8('0x1c')]+_0x3cb69b[_0x9bf8('0x1e')];var _0x42744f;if(_0x5488b4>=_0x5bd2f7){_0x5488b4=_0x5bd2f7;_0x42744f=0xc8;}else{_0x42744f=0xce;}_0x308aa2[_0x9bf8('0x20')](_0x42744f);return _0x308aa2[_0x9bf8('0x21')](_0x9bf8('0x22'),_0x5c8fe7+'-'+_0x5488b4+'/'+_0x5bd2f7)['json'](_0x5561a7);}return null;};}function saveUpdates(_0x3de61f){return function(_0x1958c6){if(_0x1958c6){return _0x1958c6[_0x9bf8('0x23')](_0x3de61f)[_0x9bf8('0x11')](function(_0x470736){return _0x470736;});}return null;};}function removeEntity(_0x4f7edd){return function(_0x504562){if(_0x504562){return _0x504562[_0x9bf8('0x24')]()[_0x9bf8('0x11')](function(){_0x4f7edd[_0x9bf8('0x20')](0xcc)['end']();});}};}function handleEntityNotFound(_0x3c9c1f){return function(_0x1087da){if(!_0x1087da){_0x3c9c1f['sendStatus'](0x194);}return _0x1087da;};}function handleError(_0x2db039,_0x215d0b){_0x215d0b=_0x215d0b||0x1f4;return function(_0x58fede){logger[_0x9bf8('0x17')](_0x58fede['stack']);if(_0x58fede['name']){delete _0x58fede[_0x9bf8('0x25')];}_0x2db039[_0x9bf8('0x20')](_0x215d0b)[_0x9bf8('0x26')](_0x58fede);};}exports[_0x9bf8('0x27')]=function(_0x598980,_0x4e7330){var _0x3a6779={},_0x46bd00={},_0x524e85={'count':0x0,'rows':[]};var _0x46c36d=_[_0x9bf8('0x28')](db[_0x9bf8('0x29')][_0x9bf8('0x2a')],function(_0x471320){return{'name':_0x471320[_0x9bf8('0x2b')],'type':_0x471320[_0x9bf8('0x2c')][_0x9bf8('0x2d')]};});_0x46bd00[_0x9bf8('0x2e')]=_[_0x9bf8('0x28')](_0x46c36d,_0x9bf8('0x25'));_0x46bd00[_0x9bf8('0x2f')]=_[_0x9bf8('0x30')](_0x598980[_0x9bf8('0x2f')]);_0x46bd00['filters']=_[_0x9bf8('0x31')](_0x46bd00[_0x9bf8('0x2e')],_0x46bd00[_0x9bf8('0x2f')]);_0x3a6779[_0x9bf8('0x32')]=_[_0x9bf8('0x31')](_0x46bd00[_0x9bf8('0x2e')],qs['fields'](_0x598980[_0x9bf8('0x2f')][_0x9bf8('0x33')]));_0x3a6779[_0x9bf8('0x32')]=_0x3a6779[_0x9bf8('0x32')][_0x9bf8('0x34')]?_0x3a6779[_0x9bf8('0x32')]:_0x46bd00[_0x9bf8('0x2e')];if(!_0x598980[_0x9bf8('0x2f')][_0x9bf8('0x35')](_0x9bf8('0x36'))){_0x3a6779[_0x9bf8('0x1e')]=qs['limit'](_0x598980['query'][_0x9bf8('0x1e')]);_0x3a6779['offset']=qs[_0x9bf8('0x1c')](_0x598980[_0x9bf8('0x2f')][_0x9bf8('0x1c')]);}_0x3a6779[_0x9bf8('0x37')]=qs[_0x9bf8('0x38')](_0x598980[_0x9bf8('0x2f')][_0x9bf8('0x38')]);_0x3a6779[_0x9bf8('0x39')]=qs[_0x9bf8('0x3a')](_[_0x9bf8('0x3b')](_0x598980[_0x9bf8('0x2f')],_0x46bd00['filters']),_0x46c36d);if(_0x598980[_0x9bf8('0x2f')][_0x9bf8('0x3c')]){_0x3a6779['where']=_[_0x9bf8('0x3d')](_0x3a6779[_0x9bf8('0x39')],{'$or':_[_0x9bf8('0x28')](_0x46c36d,function(_0x5003c8){if(_0x5003c8[_0x9bf8('0x2c')]!==_0x9bf8('0x3e')){var _0x2baee8={};_0x2baee8[_0x5003c8[_0x9bf8('0x25')]]={'$like':'%'+_0x598980[_0x9bf8('0x2f')]['filter']+'%'};return _0x2baee8;}})});}_0x3a6779=_[_0x9bf8('0x3d')]({},_0x3a6779,_0x598980[_0x9bf8('0x3f')]);var _0x37201d={'where':_0x3a6779[_0x9bf8('0x39')]};return db[_0x9bf8('0x29')][_0x9bf8('0x1f')](_0x37201d)[_0x9bf8('0x11')](function(_0x2fa444){_0x524e85[_0x9bf8('0x1f')]=_0x2fa444;if(_0x598980[_0x9bf8('0x2f')][_0x9bf8('0x40')]){_0x3a6779[_0x9bf8('0x41')]=[{'all':!![]}];}return db[_0x9bf8('0x29')][_0x9bf8('0x42')](_0x3a6779);})[_0x9bf8('0x11')](function(_0x1f4f0b){_0x524e85[_0x9bf8('0x43')]=_0x1f4f0b;return _0x524e85;})[_0x9bf8('0x11')](respondWithFilteredResult(_0x4e7330,_0x3a6779))[_0x9bf8('0x1a')](handleError(_0x4e7330,null));};exports['show']=function(_0x30ae19,_0xcba73a){var _0x4ce1f5={'raw':!![],'where':{'id':_0x30ae19['params']['id']}},_0x57e02c={};_0x57e02c['model']=_['keys'](db[_0x9bf8('0x29')][_0x9bf8('0x2a')]);_0x57e02c[_0x9bf8('0x2f')]=_[_0x9bf8('0x30')](_0x30ae19['query']);_0x57e02c['filters']=_[_0x9bf8('0x31')](_0x57e02c['model'],_0x57e02c[_0x9bf8('0x2f')]);_0x4ce1f5[_0x9bf8('0x32')]=_[_0x9bf8('0x31')](_0x57e02c[_0x9bf8('0x2e')],qs[_0x9bf8('0x33')](_0x30ae19[_0x9bf8('0x2f')][_0x9bf8('0x33')]));_0x4ce1f5[_0x9bf8('0x32')]=_0x4ce1f5['attributes']['length']?_0x4ce1f5[_0x9bf8('0x32')]:_0x57e02c[_0x9bf8('0x2e')];if(_0x30ae19['query'][_0x9bf8('0x40')]){_0x4ce1f5['include']=[{'all':!![]}];}_0x4ce1f5=_[_0x9bf8('0x3d')]({},_0x4ce1f5,_0x30ae19[_0x9bf8('0x3f')]);return db['MailMessage'][_0x9bf8('0x44')](_0x4ce1f5)[_0x9bf8('0x11')](handleEntityNotFound(_0xcba73a,null))[_0x9bf8('0x11')](respondWithResult(_0xcba73a,null))[_0x9bf8('0x1a')](handleError(_0xcba73a,null));};exports['update']=function(_0x4e2f95,_0x4c3729){if(_0x4e2f95[_0x9bf8('0x45')]['id']){delete _0x4e2f95[_0x9bf8('0x45')]['id'];}return db[_0x9bf8('0x29')][_0x9bf8('0x44')]({'where':{'id':_0x4e2f95[_0x9bf8('0x46')]['id']}})[_0x9bf8('0x11')](handleEntityNotFound(_0x4c3729,null))[_0x9bf8('0x11')](saveUpdates(_0x4e2f95[_0x9bf8('0x45')],null))['then'](respondWithResult(_0x4c3729,null))[_0x9bf8('0x1a')](handleError(_0x4c3729,null));};exports[_0x9bf8('0x24')]=function(_0x3232c8,_0xd31325){return db['MailMessage'][_0x9bf8('0x44')]({'where':{'id':_0x3232c8[_0x9bf8('0x46')]['id']}})['then'](handleEntityNotFound(_0xd31325,null))[_0x9bf8('0x11')](removeEntity(_0xd31325,null))[_0x9bf8('0x1a')](handleError(_0xd31325,null));};exports['describe']=function(_0x22a20b,_0x4ed0e9){return db['MailMessage']['describe']()[_0x9bf8('0x11')](respondWithResult(_0x4ed0e9,null))[_0x9bf8('0x1a')](handleError(_0x4ed0e9,null));};exports[_0x9bf8('0x47')]=function(_0x1907f9,_0x2d7bf9){return db['MailMessage'][_0x9bf8('0x47')](_0x1907f9[_0x9bf8('0x45')])[_0x9bf8('0x11')](function(_0x21aad3){if(_0x21aad3){if(_0x21aad3['direction']===_0x9bf8('0x48')&&!_0x21aad3['secret']){var _0x1a5a93,_0x23d669,_0x514748;if(_0x1907f9['body']['to']){_0x23d669=_0x1907f9[_0x9bf8('0x45')]['to']['split'](',')[0x0];}return db[_0x9bf8('0x49')][_0x9bf8('0x4a')]({'where':{'id':_0x21aad3['MailAccountId']},'include':[{'model':db[_0x9bf8('0x4b')],'as':_0x9bf8('0x4c'),'include':[{'model':db['CmContact'],'as':_0x9bf8('0x4d'),'where':{'email':_0x23d669},'limit':0x1,'order':[[_0x9bf8('0x4e'),_0x9bf8('0x4f')]]}]},{'model':db[_0x9bf8('0x50')],'as':_0x9bf8('0x51'),'raw':!![],'include':[{'model':db[_0x9bf8('0x52')],'as':_0x9bf8('0x52'),'raw':!![]}]}]})[_0x9bf8('0x11')](function(_0x425250){_0x1a5a93=_0x425250;if(!_0x1a5a93||!_0x1a5a93[_0x9bf8('0x51')])return;var _0x1e7d70=_0x1a5a93[_0x9bf8('0x53')]();if(_0x1907f9[_0x9bf8('0x45')][_0x9bf8('0x54')]&&_0x1907f9['body'][_0x9bf8('0x54')][_0x9bf8('0x34')]){for(var _0x5513d7=0x0;_0x5513d7<_0x1907f9[_0x9bf8('0x45')][_0x9bf8('0x54')][_0x9bf8('0x34')];_0x5513d7+=0x1){_0x1907f9[_0x9bf8('0x45')][_0x9bf8('0x54')][_0x5513d7]={'filename':_0x1907f9['body'][_0x9bf8('0x54')][_0x5513d7]['name'],'path':path[_0x9bf8('0x55')](config[_0x9bf8('0x56')],_0x9bf8('0x57'),_0x1907f9[_0x9bf8('0x45')]['attachments'][_0x5513d7][_0x9bf8('0x58')])};}}_0x514748={'account':_0x1e7d70,'message':_0x1907f9[_0x9bf8('0x45')]};return;})[_0x9bf8('0x11')](function(){if(_0x1a5a93[_0x9bf8('0x4c')]){if(_0x1a5a93[_0x9bf8('0x4c')][_0x9bf8('0x4d')]['length']){return _0x1a5a93[_0x9bf8('0x4c')]['Contacts'][0x0];}else{return db[_0x9bf8('0x59')][_0x9bf8('0x47')](_[_0x9bf8('0xa')](_0x1907f9['body'],{'firstName':_0x1907f9['body']['to'],'email':_0x1907f9['body']['to'],'phone':_0x1907f9[_0x9bf8('0x45')]['to'],'ListId':_0x1a5a93[_0x9bf8('0x5a')]}));}}else{throw new Error('List\x20not\x20found');}})['then'](function(){if(_0x514748){return respondWithRpcPromise(_0x9bf8('0x5b'),_0x9bf8('0x5c'),_0x514748,client9003)[_0x9bf8('0x11')](function(_0x52c921){return _0x21aad3[_0x9bf8('0x23')]({'read':!![],'messageId':_0x52c921['messageId'],'sentAt':moment()[_0x9bf8('0x5d')](_0x9bf8('0x5e'))});})[_0x9bf8('0x1a')](function(_0x49836f){logger[_0x9bf8('0x17')]('%s',JSON[_0x9bf8('0x5f')](_0x49836f));return _0x21aad3;});}return _0x21aad3;});}return _0x21aad3;}})[_0x9bf8('0x11')](respondWithResult(_0x2d7bf9,0xc9))['catch'](handleError(_0x2d7bf9,null));};exports[_0x9bf8('0x60')]=function(_0x25f636,_0x3a3213){var _0xee886c={'agent':{},'channel':'mail'};if(_0x25f636[_0x9bf8('0x45')]['id']){delete _0x25f636[_0x9bf8('0x45')]['id'];}_0x25f636[_0x9bf8('0x45')][_0x9bf8('0x61')]=!![];_0x25f636['body'][_0x9bf8('0x62')]=moment()['format'](_0x9bf8('0x5e'));_0x25f636[_0x9bf8('0x45')][_0x9bf8('0x63')]=_0x25f636['body'][_0x9bf8('0x63')]||_0x25f636['user']['id'];_0xee886c[_0x9bf8('0x64')]['id']=_0x25f636[_0x9bf8('0x45')][_0x9bf8('0x63')];return db['MailMessage'][_0x9bf8('0x44')]({'where':{'id':_0x25f636[_0x9bf8('0x46')]['id'],'UserId':null}})[_0x9bf8('0x11')](handleEntityNotFound(_0x3a3213,null))[_0x9bf8('0x11')](saveUpdates(_0x25f636[_0x9bf8('0x45')],null))[_0x9bf8('0x11')](function(_0x34d896){if(_0x34d896){_0xee886c[_0x9bf8('0x18')]=_0x34d896[_0x9bf8('0x65')]({'plain':!![]});return db[_0x9bf8('0x66')][_0x9bf8('0x44')]({'where':{'id':_0x34d896[_0x9bf8('0x67')]}});}return null;})[_0x9bf8('0x11')](handleEntityNotFound(_0x3a3213,null))[_0x9bf8('0x11')](function(_0x5650cb){if(_0x5650cb){return _0x5650cb[_0x9bf8('0x23')]({'UserId':_0x25f636[_0x9bf8('0x45')][_0x9bf8('0x63')],'read1stAt':_[_0x9bf8('0x68')](_0x5650cb['read1stAt'])?moment()[_0x9bf8('0x5d')](_0x9bf8('0x5e')):undefined});}return null;})[_0x9bf8('0x11')](function(_0x2c1545){if(_0x2c1545){_0xee886c[_0x9bf8('0x69')]=_0x2c1545['get']({'plain':!![]});interaction_log[_0x9bf8('0x12')](_0x9bf8('0x6a'),_0x25f636[_0x9bf8('0x6b')]['id'],_0x25f636['user'][_0x9bf8('0x25')],_0x25f636[_0x9bf8('0x6b')][_0x9bf8('0x6c')],_0xee886c[_0x9bf8('0x69')]['id'],_0x25f636['body']?JSON[_0x9bf8('0x5f')](_0x25f636[_0x9bf8('0x45')]):'null');return respondWithRpcPromise('AcceptMessage',_0x9bf8('0x6d'),_0xee886c);}return null;})[_0x9bf8('0x11')](function(_0xc754ad){var _0x124e6a=_0x9bf8('0x6e')+_0xc754ad[_0x9bf8('0x18')][_0x9bf8('0x6f')];return db[_0x9bf8('0x70')][_0x9bf8('0x2f')](_0x124e6a,{'type':db[_0x9bf8('0x71')][_0x9bf8('0x72')][_0x9bf8('0x73')],'raw':!![]})[_0x9bf8('0x11')](function(_0x33e926){return _0x33e926;});})[_0x9bf8('0x11')](function(_0xf7de12){if(_0xf7de12){_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x74')]=_0xf7de12[0x0];if(!_0x25f636[_0x9bf8('0x45')]['UserId'])return;return db[_0x9bf8('0x75')][_0x9bf8('0x44')]({'where':{'id':_0x25f636[_0x9bf8('0x45')][_0x9bf8('0x63')],'role':_0x9bf8('0x64')},'attributes':['id',_0x9bf8('0x25'),'fullname',_0x9bf8('0x76'),_0x9bf8('0x77')],'raw':!![]});}return null;})[_0x9bf8('0x11')](function(_0x54f3e6){if(!_0x54f3e6)return null;_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x64')]=_0x54f3e6;return _0xee886c;})['then'](function(_0x15e322){if(_0x15e322){if(_0x25f636[_0x9bf8('0x45')][_0x9bf8('0x78')]){_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x79')]=util[_0x9bf8('0x5d')](_0x9bf8('0x7a'),_0x25f636[_0x9bf8('0x6b')][_0x9bf8('0x25')]);_0xee886c['message']['channel']=_0xee886c['channel'];_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x7b')]=_0xee886c[_0x9bf8('0x7c')];_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x7d')]=_0x9bf8('0x60');_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x45')]=htmlToText['fromString'](_0xee886c[_0x9bf8('0x18')][_0x9bf8('0x45')]);return respondWithRpcPromise(_0x9bf8('0x7e'),_0x9bf8('0x7e'),{'event':_0x9bf8('0x7f'),'message':_0xee886c['message']},client9002)['then'](function(){return _0xee886c;});}return _0xee886c;}return null;})['then'](respondWithResult(_0x3a3213,null))[_0x9bf8('0x1a')](handleError(_0x3a3213,null));};exports[_0x9bf8('0x80')]=function(_0x4a52c6,_0x21aed7){var _0x1499ef={'agent':{},'channel':_0x9bf8('0x81')};if(_0x4a52c6[_0x9bf8('0x45')]['id']){delete _0x4a52c6[_0x9bf8('0x45')]['id'];}_0x4a52c6[_0x9bf8('0x45')][_0x9bf8('0x63')]=_0x4a52c6['body'][_0x9bf8('0x63')]||_0x4a52c6[_0x9bf8('0x6b')]['id'];_0x1499ef[_0x9bf8('0x64')]['id']=_0x4a52c6[_0x9bf8('0x45')][_0x9bf8('0x63')];return db[_0x9bf8('0x29')][_0x9bf8('0x44')]({'where':{'id':_0x4a52c6[_0x9bf8('0x46')]['id'],'UserId':null}})[_0x9bf8('0x11')](handleEntityNotFound(_0x21aed7,null))[_0x9bf8('0x11')](function(_0x3bd88f){if(_0x3bd88f){_0x1499ef['message']=_0x3bd88f[_0x9bf8('0x65')]({'plain':!![]});return db['MailInteraction'][_0x9bf8('0x44')]({'where':{'id':_0x3bd88f[_0x9bf8('0x67')]}});}return null;})[_0x9bf8('0x11')](handleEntityNotFound(_0x21aed7,null))[_0x9bf8('0x11')](function(_0x3b4d09){if(_0x3b4d09){_0x1499ef[_0x9bf8('0x69')]=_0x3b4d09[_0x9bf8('0x65')]({'plain':!![]});interaction_log[_0x9bf8('0x12')](_0x9bf8('0x82'),_0x4a52c6[_0x9bf8('0x6b')]['id'],_0x4a52c6[_0x9bf8('0x6b')]['name'],_0x4a52c6[_0x9bf8('0x6b')]['role'],_0x1499ef['interaction']['id'],_0x4a52c6[_0x9bf8('0x45')]?JSON[_0x9bf8('0x5f')](_0x4a52c6[_0x9bf8('0x45')]):'null');return respondWithRpcPromise(_0x9bf8('0x83'),_0x9bf8('0x84'),_0x1499ef);}return null;})['then'](function(_0x400f15){var _0x34fa35=_0x9bf8('0x6e')+_0x400f15[_0x9bf8('0x18')]['ContactId'];return db['sequelize'][_0x9bf8('0x2f')](_0x34fa35,{'type':db[_0x9bf8('0x71')][_0x9bf8('0x72')]['SELECT'],'raw':!![]})[_0x9bf8('0x11')](function(_0xd30698){return _0xd30698;});})[_0x9bf8('0x11')](function(_0x2407a7){if(_0x2407a7){_0x1499ef[_0x9bf8('0x18')][_0x9bf8('0x74')]=_0x2407a7[0x0];if(!_0x4a52c6[_0x9bf8('0x45')]['UserId'])return;return db[_0x9bf8('0x75')][_0x9bf8('0x44')]({'where':{'id':_0x4a52c6[_0x9bf8('0x45')][_0x9bf8('0x63')],'role':_0x9bf8('0x64')},'attributes':['id',_0x9bf8('0x25'),_0x9bf8('0x85'),_0x9bf8('0x76'),'internal'],'raw':!![]});}return null;})['then'](function(_0x44e329){if(!_0x44e329)return null;_0x1499ef[_0x9bf8('0x18')]['agent']=_0x44e329;return _0x1499ef;})[_0x9bf8('0x11')](function(_0x3e4cba){if(_0x3e4cba){_0x3e4cba['message']['interface']=util[_0x9bf8('0x5d')](_0x9bf8('0x7a'),_0x4a52c6[_0x9bf8('0x6b')][_0x9bf8('0x25')]);_0x3e4cba['message'][_0x9bf8('0x7c')]=_0x1499ef['channel'];_0x3e4cba[_0x9bf8('0x18')][_0x9bf8('0x7b')]=_0x1499ef['channel'];_0x3e4cba['message'][_0x9bf8('0x7d')]=_0x9bf8('0x80');_0x3e4cba['message'][_0x9bf8('0x45')]=htmlToText['fromString'](_0x1499ef[_0x9bf8('0x18')][_0x9bf8('0x45')]);respondWithRpcPromise(_0x9bf8('0x7e'),_0x9bf8('0x7e'),{'event':_0x9bf8('0x86'),'message':_0x3e4cba[_0x9bf8('0x18')]},client9002);return _0x1499ef;}return null;})[_0x9bf8('0x11')](respondWithResult(_0x21aed7,null))['catch'](handleError(_0x21aed7,null));};exports[_0x9bf8('0x87')]=function(_0x45ac1b,_0x141993){var _0x49e128=moment()['unix']()[_0x9bf8('0x88')]();var _0x3e05f7=path[_0x9bf8('0x55')](config[_0x9bf8('0x56')],'server',_0x9bf8('0x89'),_0x9bf8('0x8a'));var _0x5f20a0=path['join'](config['root'],_0x9bf8('0x8b'),'files','attachments');var _0x53767a=util[_0x9bf8('0x5d')](_0x9bf8('0x8c'),_0x49e128);return db[_0x9bf8('0x29')]['find']({'where':{'id':_0x45ac1b[_0x9bf8('0x46')]['id']},'attributes':['id','body',_0x9bf8('0x8d'),'to','cc','bcc',_0x9bf8('0x8e'),_0x9bf8('0x8f')],'include':_0x45ac1b[_0x9bf8('0x2f')][_0x9bf8('0x54')]?[{'model':db[_0x9bf8('0x90')],'as':'Attachments'}]:undefined})['then'](handleEntityNotFound(_0x141993,null))[_0x9bf8('0x11')](function(_0x34d497){if(_0x34d497){var _0x140da7=_0x34d497;var _0x36cdb2={'from':_0x140da7[_0x9bf8('0x8d')],'to':_0x140da7['to'],'subject':_0x140da7[_0x9bf8('0x8e')],'html':htmlEntities[_0x9bf8('0x91')](_0x140da7[_0x9bf8('0x45')]),'headers':{'Date':moment(_0x140da7['createdAt'])['format']('ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ')},'attachments':[]};if(_0x34d497['cc']){_0x36cdb2['headers']['Cc']=_0x34d497['cc'];}if(_0x34d497[_0x9bf8('0x92')]){_0x36cdb2[_0x9bf8('0x93')]['Bcc']=_0x34d497[_0x9bf8('0x92')];}return new BPromise(function(_0x1a1115,_0x42c7c7){if(_0x34d497[_0x9bf8('0x94')]){for(var _0x529fa0=0x0;_0x529fa0<_0x34d497['Attachments'][_0x9bf8('0x34')];_0x529fa0++){var _0x1744ea=_0x34d497[_0x9bf8('0x94')][_0x529fa0];var _0x2c7ab9=path['join'](_0x5f20a0,_0x1744ea[_0x9bf8('0x58')]);if(fs['existsSync'](_0x2c7ab9)){_0x36cdb2[_0x9bf8('0x54')][_0x9bf8('0x95')]({'name':_0x1744ea[_0x9bf8('0x25')],'contentType':_0x1744ea[_0x9bf8('0x2c')],'data':fs[_0x9bf8('0x96')](_0x2c7ab9)});}}}emlformat[_0x9bf8('0x97')](_0x36cdb2,function(_0x163fb0,_0x328f36){if(_0x163fb0)return _0x42c7c7(_0x163fb0);fs[_0x9bf8('0x98')](path[_0x9bf8('0x55')](_0x3e05f7,_0x53767a),_0x328f36);return _0x1a1115(_0x328f36);});});}})['then'](function(_0x486a1b){if(_0x486a1b){return _0x141993[_0x9bf8('0x87')](path['join'](_0x3e05f7,_0x53767a),_0x53767a,function(_0x1a2f93){if(!_0x1a2f93)fs['unlinkSync'](path[_0x9bf8('0x55')](_0x3e05f7,_0x53767a));});}})[_0x9bf8('0x1a')](handleError(_0x141993,null));};