50d47623e5c7fbda355f11d6e0e1ba8b4b2edad6
[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 _0x0baf=['authentication','auth','user','service','pass','attachments','root','server/files/attachments','CmContact','defaults','ListId','sendMail','messageId','format','YYYY-MM-DD\x20HH:mm:ss','mail','read','UserId','agent','get','MailInteraction','read1stAt','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','interaction','null','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','sequelize','Sequelize','QueryTypes','SELECT','User','fullname','email','internal','manual','interface','SIP/%s','channel','motionChannel','event','fromString','EventManager','acceptmessage','reject','MailInteractionId','[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','contact','rejectmessage','download','unix','toString','server','files','tmp','join','message-%s.eml','from','createdAt','Attachments','subject','decode','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','bcc','Bcc','existsSync','push','readFileSync','build','writeFileSync','unlinkSync','eml-format','moment','bluebird','util','path','lodash','ioredis','html-to-text','../../components/parsers/qs','../../config/logger','api','mail-interactions','../../config/environment','../../mysqldb','localhost','socket.io-emitter','redis','./mailMessage.socket','register','jayson/promise','client','http','then','info','request\x20sent','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','sendStatus','stack','name','index','map','MailMessage','fieldName','type','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','VIRTUAL','options','includeAll','include','findAll','params','merge','find','body','describe','create','out','secret','MailAccount','findOne','MailAccountId','CmList','List','Contacts','updatedAt','DESC','Smtp','isNil','host','port','secure'];(function(_0x478874,_0x36d2a5){var _0x53e9d9=function(_0x32f00e){while(--_0x32f00e){_0x478874['push'](_0x478874['shift']());}};_0x53e9d9(++_0x36d2a5);}(_0x0baf,0xf3));var _0xf0ba=function(_0x55c979,_0x36c5f0){_0x55c979=_0x55c979-0x0;var _0x690107=_0x0baf[_0x55c979];return _0x690107;};'use strict';var emlformat=require(_0xf0ba('0x0'));var moment=require(_0xf0ba('0x1'));var BPromise=require(_0xf0ba('0x2'));var util=require(_0xf0ba('0x3'));var path=require(_0xf0ba('0x4'));var fs=require('fs');var _=require(_0xf0ba('0x5'));var Redis=require(_0xf0ba('0x6'));var htmlToText=require(_0xf0ba('0x7'));var qs=require(_0xf0ba('0x8'));var logger=require(_0xf0ba('0x9'))(_0xf0ba('0xa'));var interaction_log=require('../../config/logger')(_0xf0ba('0xb'));var config=require(_0xf0ba('0xc'));var db=require(_0xf0ba('0xd'))['db'];config['redis']=_['defaults'](config['redis'],{'host':_0xf0ba('0xe'),'port':0x18eb});var socket=require(_0xf0ba('0xf'))(new Redis(config[_0xf0ba('0x10')]));var htmlEntities=require('html-entities');require(_0xf0ba('0x11'))[_0xf0ba('0x12')](socket);var jayson=require(_0xf0ba('0x13'));var client=jayson[_0xf0ba('0x14')]['http']({'port':0x232c});var client9003=jayson[_0xf0ba('0x14')][_0xf0ba('0x15')]({'port':0x232b});var client9002=jayson[_0xf0ba('0x14')][_0xf0ba('0x15')]({'port':0x232a});function respondWithRpcPromise(_0x3832a9,_0x5b3d13,_0x15936c,_0x47d8c7){return new BPromise(function(_0x39fbe0,_0x191f41){var _0x49bb2c=_0x47d8c7||client;return _0x49bb2c['request'](_0x3832a9,_0x15936c)[_0xf0ba('0x16')](function(_0x52a8bc){logger[_0xf0ba('0x17')]('MailMessage,\x20%s,\x20%s',_0x5b3d13,_0xf0ba('0x18'));logger['debug'](_0xf0ba('0x19'),_0x5b3d13,_0xf0ba('0x18'),JSON[_0xf0ba('0x1a')](_0x52a8bc));if(_0x52a8bc[_0xf0ba('0x1b')]){if(_0x52a8bc[_0xf0ba('0x1b')][_0xf0ba('0x1c')]===0x1f4){logger[_0xf0ba('0x1b')](_0xf0ba('0x1d'),_0x5b3d13,_0x52a8bc[_0xf0ba('0x1b')][_0xf0ba('0x1e')]);return _0x191f41(_0x52a8bc[_0xf0ba('0x1b')][_0xf0ba('0x1e')]);}logger[_0xf0ba('0x1b')](_0xf0ba('0x1d'),_0x5b3d13,_0x52a8bc[_0xf0ba('0x1b')][_0xf0ba('0x1e')]);return _0x39fbe0(_0x52a8bc[_0xf0ba('0x1b')]['message']);}else{logger[_0xf0ba('0x17')](_0xf0ba('0x1d'),_0x5b3d13,_0xf0ba('0x18'));_0x39fbe0(_0x52a8bc[_0xf0ba('0x1f')]['message']);}})[_0xf0ba('0x20')](function(_0x18a5a3){logger['error']('MailMessage,\x20%s,\x20%s',_0x5b3d13,_0x18a5a3);_0x191f41(_0x18a5a3);});});}function respondWithResult(_0x20a98e,_0x28e6cd){_0x28e6cd=_0x28e6cd||0xc8;return function(_0x27f04a){if(_0x27f04a){return _0x20a98e[_0xf0ba('0x21')](_0x28e6cd)[_0xf0ba('0x22')](_0x27f04a);}};}function respondWithFilteredResult(_0xeb2f8f,_0x3875ec){return function(_0x65a7c6){if(_0x65a7c6){var _0x543c2a=typeof _0x3875ec[_0xf0ba('0x23')]===_0xf0ba('0x24')&&typeof _0x3875ec[_0xf0ba('0x25')]==='undefined';var _0x5851cc=_0x65a7c6[_0xf0ba('0x26')];var _0x32b139=_0x543c2a?0x0:_0x3875ec[_0xf0ba('0x23')];var _0x2d21bf=_0x543c2a?_0x65a7c6[_0xf0ba('0x26')]:_0x3875ec[_0xf0ba('0x23')]+_0x3875ec[_0xf0ba('0x25')];var _0x44e9ac;if(_0x2d21bf>=_0x5851cc){_0x2d21bf=_0x5851cc;_0x44e9ac=0xc8;}else{_0x44e9ac=0xce;}_0xeb2f8f[_0xf0ba('0x21')](_0x44e9ac);return _0xeb2f8f[_0xf0ba('0x27')](_0xf0ba('0x28'),_0x32b139+'-'+_0x2d21bf+'/'+_0x5851cc)[_0xf0ba('0x22')](_0x65a7c6);}return null;};}function saveUpdates(_0x4c8b12){return function(_0x397306){if(_0x397306){return _0x397306[_0xf0ba('0x29')](_0x4c8b12)[_0xf0ba('0x16')](function(_0x397e17){return _0x397e17;});}return null;};}function removeEntity(_0x69fc25){return function(_0x31b3b6){if(_0x31b3b6){return _0x31b3b6[_0xf0ba('0x2a')]()[_0xf0ba('0x16')](function(){_0x69fc25[_0xf0ba('0x21')](0xcc)[_0xf0ba('0x2b')]();});}};}function handleEntityNotFound(_0x1b3225){return function(_0x5c468d){if(!_0x5c468d){_0x1b3225[_0xf0ba('0x2c')](0x194);}return _0x5c468d;};}function handleError(_0x809db3,_0x490e4a){_0x490e4a=_0x490e4a||0x1f4;return function(_0xecd299){logger['error'](_0xecd299[_0xf0ba('0x2d')]);if(_0xecd299[_0xf0ba('0x2e')]){delete _0xecd299[_0xf0ba('0x2e')];}_0x809db3['status'](_0x490e4a)['send'](_0xecd299);};}exports[_0xf0ba('0x2f')]=function(_0x3e9287,_0x314bc8){var _0x5a5d71={},_0x4b9afe={},_0x3c5180={'count':0x0,'rows':[]};var _0x34d2e7=_[_0xf0ba('0x30')](db[_0xf0ba('0x31')]['rawAttributes'],function(_0x56c814){return{'name':_0x56c814[_0xf0ba('0x32')],'type':_0x56c814[_0xf0ba('0x33')]['key']};});_0x4b9afe[_0xf0ba('0x34')]=_[_0xf0ba('0x30')](_0x34d2e7,_0xf0ba('0x2e'));_0x4b9afe[_0xf0ba('0x35')]=_[_0xf0ba('0x36')](_0x3e9287[_0xf0ba('0x35')]);_0x4b9afe[_0xf0ba('0x37')]=_[_0xf0ba('0x38')](_0x4b9afe['model'],_0x4b9afe[_0xf0ba('0x35')]);_0x5a5d71['attributes']=_[_0xf0ba('0x38')](_0x4b9afe[_0xf0ba('0x34')],qs[_0xf0ba('0x39')](_0x3e9287[_0xf0ba('0x35')]['fields']));_0x5a5d71['attributes']=_0x5a5d71[_0xf0ba('0x3a')][_0xf0ba('0x3b')]?_0x5a5d71[_0xf0ba('0x3a')]:_0x4b9afe[_0xf0ba('0x34')];if(!_0x3e9287[_0xf0ba('0x35')][_0xf0ba('0x3c')](_0xf0ba('0x3d'))){_0x5a5d71[_0xf0ba('0x25')]=qs[_0xf0ba('0x25')](_0x3e9287['query'][_0xf0ba('0x25')]);_0x5a5d71[_0xf0ba('0x23')]=qs['offset'](_0x3e9287[_0xf0ba('0x35')]['offset']);}_0x5a5d71[_0xf0ba('0x3e')]=qs['sort'](_0x3e9287[_0xf0ba('0x35')][_0xf0ba('0x3f')]);_0x5a5d71['where']=qs[_0xf0ba('0x37')](_[_0xf0ba('0x40')](_0x3e9287[_0xf0ba('0x35')],_0x4b9afe[_0xf0ba('0x37')]),_0x34d2e7);if(_0x3e9287[_0xf0ba('0x35')][_0xf0ba('0x41')]){_0x5a5d71[_0xf0ba('0x42')]=_['merge'](_0x5a5d71['where'],{'$or':_[_0xf0ba('0x30')](_0x34d2e7,function(_0x56a25e){if(_0x56a25e[_0xf0ba('0x33')]!==_0xf0ba('0x43')){var _0x5b5c4a={};_0x5b5c4a[_0x56a25e['name']]={'$like':'%'+_0x3e9287[_0xf0ba('0x35')][_0xf0ba('0x41')]+'%'};return _0x5b5c4a;}})});}_0x5a5d71=_['merge']({},_0x5a5d71,_0x3e9287[_0xf0ba('0x44')]);var _0x5d83f6={'where':_0x5a5d71[_0xf0ba('0x42')]};return db['MailMessage'][_0xf0ba('0x26')](_0x5d83f6)[_0xf0ba('0x16')](function(_0x1a9f5e){_0x3c5180[_0xf0ba('0x26')]=_0x1a9f5e;if(_0x3e9287[_0xf0ba('0x35')][_0xf0ba('0x45')]){_0x5a5d71[_0xf0ba('0x46')]=[{'all':!![]}];}return db[_0xf0ba('0x31')][_0xf0ba('0x47')](_0x5a5d71);})[_0xf0ba('0x16')](function(_0x36323e){_0x3c5180['rows']=_0x36323e;return _0x3c5180;})['then'](respondWithFilteredResult(_0x314bc8,_0x5a5d71))[_0xf0ba('0x20')](handleError(_0x314bc8,null));};exports['show']=function(_0x50b7f4,_0x494b15){var _0x3f1547={'raw':!![],'where':{'id':_0x50b7f4[_0xf0ba('0x48')]['id']}},_0x37f004={};_0x37f004[_0xf0ba('0x34')]=_['keys'](db[_0xf0ba('0x31')]['rawAttributes']);_0x37f004[_0xf0ba('0x35')]=_[_0xf0ba('0x36')](_0x50b7f4[_0xf0ba('0x35')]);_0x37f004[_0xf0ba('0x37')]=_[_0xf0ba('0x38')](_0x37f004[_0xf0ba('0x34')],_0x37f004[_0xf0ba('0x35')]);_0x3f1547['attributes']=_[_0xf0ba('0x38')](_0x37f004[_0xf0ba('0x34')],qs['fields'](_0x50b7f4[_0xf0ba('0x35')][_0xf0ba('0x39')]));_0x3f1547[_0xf0ba('0x3a')]=_0x3f1547['attributes'][_0xf0ba('0x3b')]?_0x3f1547[_0xf0ba('0x3a')]:_0x37f004['model'];if(_0x50b7f4['query'][_0xf0ba('0x45')]){_0x3f1547['include']=[{'all':!![]}];}_0x3f1547=_[_0xf0ba('0x49')]({},_0x3f1547,_0x50b7f4[_0xf0ba('0x44')]);return db['MailMessage'][_0xf0ba('0x4a')](_0x3f1547)['then'](handleEntityNotFound(_0x494b15,null))[_0xf0ba('0x16')](respondWithResult(_0x494b15,null))[_0xf0ba('0x20')](handleError(_0x494b15,null));};exports['update']=function(_0x18a39c,_0x3852e7){if(_0x18a39c['body']['id']){delete _0x18a39c[_0xf0ba('0x4b')]['id'];}return db['MailMessage'][_0xf0ba('0x4a')]({'where':{'id':_0x18a39c[_0xf0ba('0x48')]['id']}})[_0xf0ba('0x16')](handleEntityNotFound(_0x3852e7,null))[_0xf0ba('0x16')](saveUpdates(_0x18a39c['body'],null))[_0xf0ba('0x16')](respondWithResult(_0x3852e7,null))[_0xf0ba('0x20')](handleError(_0x3852e7,null));};exports[_0xf0ba('0x2a')]=function(_0x4bd601,_0x5b57f8){return db[_0xf0ba('0x31')][_0xf0ba('0x4a')]({'where':{'id':_0x4bd601['params']['id']}})[_0xf0ba('0x16')](handleEntityNotFound(_0x5b57f8,null))[_0xf0ba('0x16')](removeEntity(_0x5b57f8,null))['catch'](handleError(_0x5b57f8,null));};exports[_0xf0ba('0x4c')]=function(_0x126dbb,_0x2b7e5b){return db['MailMessage'][_0xf0ba('0x4c')]()['then'](respondWithResult(_0x2b7e5b,null))[_0xf0ba('0x20')](handleError(_0x2b7e5b,null));};exports['create']=function(_0x55065c,_0x146629){return db[_0xf0ba('0x31')][_0xf0ba('0x4d')](_0x55065c[_0xf0ba('0x4b')])[_0xf0ba('0x16')](function(_0x5290b1){if(_0x5290b1){if(_0x5290b1['direction']===_0xf0ba('0x4e')&&!_0x5290b1[_0xf0ba('0x4f')]){var _0x2c1f79,_0x2d4cbc,_0x3b9ee0;if(_0x55065c[_0xf0ba('0x4b')]['to']){_0x2d4cbc=_0x55065c[_0xf0ba('0x4b')]['to']['split'](',')[0x0];}return db[_0xf0ba('0x50')][_0xf0ba('0x51')]({'where':{'id':_0x5290b1[_0xf0ba('0x52')]},'include':[{'model':db[_0xf0ba('0x53')],'as':_0xf0ba('0x54'),'include':[{'model':db['CmContact'],'as':_0xf0ba('0x55'),'where':{'email':_0x2d4cbc},'limit':0x1,'order':[[_0xf0ba('0x56'),_0xf0ba('0x57')]]}]},{'model':db['MailServerOut'],'as':_0xf0ba('0x58'),'raw':!![]}]})[_0xf0ba('0x16')](function(_0x3582be){_0x2c1f79=_0x3582be;if(_0x2c1f79&&_0x2c1f79[_0xf0ba('0x58')]){var _0xa14c80={'tls':{'rejectUnauthorized':![]}};if(_[_0xf0ba('0x59')](_0x2c1f79[_0xf0ba('0x58')]['service'])){_0xa14c80[_0xf0ba('0x5a')]=_0x2c1f79['Smtp'][_0xf0ba('0x5a')];_0xa14c80[_0xf0ba('0x5b')]=_0x2c1f79['Smtp'][_0xf0ba('0x5b')];_0xa14c80[_0xf0ba('0x5c')]=_0x2c1f79[_0xf0ba('0x58')][_0xf0ba('0x5c')];if(_0x2c1f79['Smtp'][_0xf0ba('0x5d')]){_0xa14c80[_0xf0ba('0x5e')]={'user':_0x2c1f79['Smtp'][_0xf0ba('0x5f')],'pass':_0x2c1f79[_0xf0ba('0x58')]['pass']};}}else{_0xa14c80[_0xf0ba('0x60')]=_0x2c1f79[_0xf0ba('0x58')][_0xf0ba('0x60')];_0xa14c80[_0xf0ba('0x5e')]={'user':_0x2c1f79[_0xf0ba('0x58')]['user'],'pass':_0x2c1f79['Smtp'][_0xf0ba('0x61')]};_0xa14c80['secure']=_0x2c1f79[_0xf0ba('0x58')]['secure'];}if(_0x55065c[_0xf0ba('0x4b')][_0xf0ba('0x62')]&&_0x55065c['body'][_0xf0ba('0x62')][_0xf0ba('0x3b')]){for(var _0x3cb42f=0x0;_0x3cb42f<_0x55065c[_0xf0ba('0x4b')][_0xf0ba('0x62')][_0xf0ba('0x3b')];_0x3cb42f+=0x1){_0x55065c[_0xf0ba('0x4b')]['attachments'][_0x3cb42f]={'filename':_0x55065c[_0xf0ba('0x4b')][_0xf0ba('0x62')][_0x3cb42f][_0xf0ba('0x2e')],'path':path['join'](config[_0xf0ba('0x63')],_0xf0ba('0x64'),_0x55065c['body'][_0xf0ba('0x62')][_0x3cb42f]['basename'])};}}_0x3b9ee0={'account':_0xa14c80,'message':_0x55065c[_0xf0ba('0x4b')]};return;}})[_0xf0ba('0x16')](function(){if(_0x2c1f79[_0xf0ba('0x54')]){if(_0x2c1f79['List'][_0xf0ba('0x55')]['length']){return _0x2c1f79[_0xf0ba('0x54')][_0xf0ba('0x55')][0x0];}else{return db[_0xf0ba('0x65')][_0xf0ba('0x4d')](_[_0xf0ba('0x66')](_0x55065c[_0xf0ba('0x4b')],{'firstName':_0x55065c[_0xf0ba('0x4b')]['to'],'email':_0x55065c[_0xf0ba('0x4b')]['to'],'phone':_0x55065c[_0xf0ba('0x4b')]['to'],'ListId':_0x2c1f79[_0xf0ba('0x67')]}));}}else{throw new Error('List\x20not\x20found');}})['then'](function(){if(_0x3b9ee0){return respondWithRpcPromise('SendMail',_0xf0ba('0x68'),_0x3b9ee0,client9003)['then'](function(_0x4268bc){return _0x5290b1[_0xf0ba('0x29')]({'read':!![],'messageId':_0x4268bc[_0xf0ba('0x69')],'sentAt':moment()[_0xf0ba('0x6a')](_0xf0ba('0x6b'))});})[_0xf0ba('0x20')](function(_0x3518fa){logger[_0xf0ba('0x1b')]('%s',JSON[_0xf0ba('0x1a')](_0x3518fa));return _0x5290b1;});}return _0x5290b1;});}return _0x5290b1;}})[_0xf0ba('0x16')](respondWithResult(_0x146629,0xc9))['catch'](handleError(_0x146629,null));};exports['accept']=function(_0xc84012,_0xd33726){var _0x9f0620={'agent':{},'channel':_0xf0ba('0x6c')};if(_0xc84012[_0xf0ba('0x4b')]['id']){delete _0xc84012['body']['id'];}_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x6d')]=!![];_0xc84012[_0xf0ba('0x4b')]['readAt']=moment()[_0xf0ba('0x6a')](_0xf0ba('0x6b'));_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x6e')]=_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x6e')]||_0xc84012['user']['id'];_0x9f0620[_0xf0ba('0x6f')]['id']=_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x6e')];return db[_0xf0ba('0x31')][_0xf0ba('0x4a')]({'where':{'id':_0xc84012[_0xf0ba('0x48')]['id'],'UserId':null}})[_0xf0ba('0x16')](handleEntityNotFound(_0xd33726,null))['then'](saveUpdates(_0xc84012['body'],null))[_0xf0ba('0x16')](function(_0x301d2d){if(_0x301d2d){_0x9f0620['message']=_0x301d2d[_0xf0ba('0x70')]({'plain':!![]});return db[_0xf0ba('0x71')]['find']({'where':{'id':_0x301d2d['MailInteractionId']}});}return null;})[_0xf0ba('0x16')](handleEntityNotFound(_0xd33726,null))[_0xf0ba('0x16')](function(_0x3a250){if(_0x3a250){return _0x3a250[_0xf0ba('0x29')]({'UserId':_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x6e')],'read1stAt':_[_0xf0ba('0x59')](_0x3a250[_0xf0ba('0x72')])?moment()[_0xf0ba('0x6a')]('YYYY-MM-DD\x20HH:mm:ss'):undefined});}return null;})['then'](function(_0x1a3398){if(_0x1a3398){_0x9f0620['interaction']=_0x1a3398[_0xf0ba('0x70')]({'plain':!![]});interaction_log[_0xf0ba('0x17')](_0xf0ba('0x73'),_0xc84012[_0xf0ba('0x5f')]['id'],_0xc84012[_0xf0ba('0x5f')][_0xf0ba('0x2e')],_0xc84012[_0xf0ba('0x5f')][_0xf0ba('0x74')],_0x9f0620[_0xf0ba('0x75')]['id'],_0xc84012[_0xf0ba('0x4b')]?JSON[_0xf0ba('0x1a')](_0xc84012['body']):_0xf0ba('0x76'));return respondWithRpcPromise('AcceptMessage','acceptMessage',_0x9f0620);}return null;})[_0xf0ba('0x16')](function(_0x1720ed){var _0x60ca0b=_0xf0ba('0x77')+_0x1720ed[_0xf0ba('0x1e')][_0xf0ba('0x78')];return db[_0xf0ba('0x79')][_0xf0ba('0x35')](_0x60ca0b,{'type':db[_0xf0ba('0x7a')][_0xf0ba('0x7b')][_0xf0ba('0x7c')],'raw':!![]})['then'](function(_0xa5fc1a){return _0xa5fc1a;});})[_0xf0ba('0x16')](function(_0x2400c9){if(_0x2400c9){_0x9f0620['message']['contact']=_0x2400c9[0x0];if(!_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x6e')])return;return db[_0xf0ba('0x7d')][_0xf0ba('0x4a')]({'where':{'id':_0xc84012['body'][_0xf0ba('0x6e')],'role':'agent'},'attributes':['id',_0xf0ba('0x2e'),_0xf0ba('0x7e'),_0xf0ba('0x7f'),_0xf0ba('0x80')],'raw':!![]});}return null;})[_0xf0ba('0x16')](function(_0x232db6){if(!_0x232db6)return null;_0x9f0620[_0xf0ba('0x1e')][_0xf0ba('0x6f')]=_0x232db6;return _0x9f0620;})[_0xf0ba('0x16')](function(_0x518dcb){if(_0x518dcb){if(_0xc84012[_0xf0ba('0x4b')][_0xf0ba('0x81')]){_0x9f0620[_0xf0ba('0x1e')][_0xf0ba('0x82')]=util[_0xf0ba('0x6a')](_0xf0ba('0x83'),_0xc84012['user'][_0xf0ba('0x2e')]);_0x9f0620['message'][_0xf0ba('0x84')]=_0x9f0620[_0xf0ba('0x84')];_0x9f0620[_0xf0ba('0x1e')][_0xf0ba('0x85')]=_0x9f0620['channel'];_0x9f0620[_0xf0ba('0x1e')][_0xf0ba('0x86')]='accept';_0x9f0620['message'][_0xf0ba('0x4b')]=htmlToText[_0xf0ba('0x87')](_0x9f0620[_0xf0ba('0x1e')]['body']);return respondWithRpcPromise(_0xf0ba('0x88'),'EventManager',{'event':_0xf0ba('0x89'),'message':_0x9f0620[_0xf0ba('0x1e')]},client9002)[_0xf0ba('0x16')](function(){return _0x9f0620;});}return _0x9f0620;}return null;})[_0xf0ba('0x16')](respondWithResult(_0xd33726,null))['catch'](handleError(_0xd33726,null));};exports[_0xf0ba('0x8a')]=function(_0x52b8fe,_0x20a5c8){var _0x8211f0={'agent':{},'channel':'mail'};if(_0x52b8fe[_0xf0ba('0x4b')]['id']){delete _0x52b8fe[_0xf0ba('0x4b')]['id'];}_0x52b8fe[_0xf0ba('0x4b')][_0xf0ba('0x6e')]=_0x52b8fe[_0xf0ba('0x4b')][_0xf0ba('0x6e')]||_0x52b8fe[_0xf0ba('0x5f')]['id'];_0x8211f0[_0xf0ba('0x6f')]['id']=_0x52b8fe[_0xf0ba('0x4b')][_0xf0ba('0x6e')];return db[_0xf0ba('0x31')][_0xf0ba('0x4a')]({'where':{'id':_0x52b8fe[_0xf0ba('0x48')]['id'],'UserId':null}})[_0xf0ba('0x16')](handleEntityNotFound(_0x20a5c8,null))[_0xf0ba('0x16')](function(_0x25aa2a){if(_0x25aa2a){_0x8211f0['message']=_0x25aa2a['get']({'plain':!![]});return db[_0xf0ba('0x71')][_0xf0ba('0x4a')]({'where':{'id':_0x25aa2a[_0xf0ba('0x8b')]}});}return null;})['then'](handleEntityNotFound(_0x20a5c8,null))[_0xf0ba('0x16')](function(_0x47e04e){if(_0x47e04e){_0x8211f0[_0xf0ba('0x75')]=_0x47e04e[_0xf0ba('0x70')]({'plain':!![]});interaction_log[_0xf0ba('0x17')](_0xf0ba('0x8c'),_0x52b8fe[_0xf0ba('0x5f')]['id'],_0x52b8fe[_0xf0ba('0x5f')][_0xf0ba('0x2e')],_0x52b8fe[_0xf0ba('0x5f')][_0xf0ba('0x74')],_0x8211f0[_0xf0ba('0x75')]['id'],_0x52b8fe[_0xf0ba('0x4b')]?JSON[_0xf0ba('0x1a')](_0x52b8fe['body']):_0xf0ba('0x76'));return respondWithRpcPromise(_0xf0ba('0x8d'),_0xf0ba('0x8e'),_0x8211f0);}return null;})['then'](function(_0x253bc3){var _0x14842a='SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20'+_0x253bc3[_0xf0ba('0x1e')][_0xf0ba('0x78')];return db['sequelize'][_0xf0ba('0x35')](_0x14842a,{'type':db[_0xf0ba('0x7a')][_0xf0ba('0x7b')][_0xf0ba('0x7c')],'raw':!![]})[_0xf0ba('0x16')](function(_0x435cf9){return _0x435cf9;});})[_0xf0ba('0x16')](function(_0x3ea486){if(_0x3ea486){_0x8211f0[_0xf0ba('0x1e')][_0xf0ba('0x8f')]=_0x3ea486[0x0];if(!_0x52b8fe[_0xf0ba('0x4b')]['UserId'])return;return db[_0xf0ba('0x7d')][_0xf0ba('0x4a')]({'where':{'id':_0x52b8fe[_0xf0ba('0x4b')]['UserId'],'role':'agent'},'attributes':['id',_0xf0ba('0x2e'),_0xf0ba('0x7e'),'email',_0xf0ba('0x80')],'raw':!![]});}return null;})[_0xf0ba('0x16')](function(_0x29153d){if(!_0x29153d)return null;_0x8211f0[_0xf0ba('0x1e')]['agent']=_0x29153d;return _0x8211f0;})[_0xf0ba('0x16')](function(_0x459347){if(_0x459347){_0x459347[_0xf0ba('0x1e')][_0xf0ba('0x82')]=util['format'](_0xf0ba('0x83'),_0x52b8fe['user']['name']);_0x459347[_0xf0ba('0x1e')][_0xf0ba('0x84')]=_0x8211f0[_0xf0ba('0x84')];_0x459347['message'][_0xf0ba('0x85')]=_0x8211f0['channel'];_0x459347[_0xf0ba('0x1e')][_0xf0ba('0x86')]='reject';_0x459347[_0xf0ba('0x1e')][_0xf0ba('0x4b')]=htmlToText[_0xf0ba('0x87')](_0x8211f0[_0xf0ba('0x1e')][_0xf0ba('0x4b')]);respondWithRpcPromise(_0xf0ba('0x88'),'EventManager',{'event':_0xf0ba('0x90'),'message':_0x459347['message']},client9002);return _0x8211f0;}return null;})[_0xf0ba('0x16')](respondWithResult(_0x20a5c8,null))['catch'](handleError(_0x20a5c8,null));};exports[_0xf0ba('0x91')]=function(_0x16f6ea,_0x33f033){var _0x3140d0=moment()[_0xf0ba('0x92')]()[_0xf0ba('0x93')]();var _0x1ea6f8=path['join'](config['root'],_0xf0ba('0x94'),_0xf0ba('0x95'),_0xf0ba('0x96'));var _0xdff4d6=path[_0xf0ba('0x97')](config[_0xf0ba('0x63')],_0xf0ba('0x94'),_0xf0ba('0x95'),_0xf0ba('0x62'));var _0x31081b=util[_0xf0ba('0x6a')](_0xf0ba('0x98'),_0x3140d0);return db[_0xf0ba('0x31')][_0xf0ba('0x4a')]({'where':{'id':_0x16f6ea[_0xf0ba('0x48')]['id']},'attributes':['id',_0xf0ba('0x4b'),_0xf0ba('0x99'),'to','cc','bcc','subject',_0xf0ba('0x9a')],'include':_0x16f6ea['query'][_0xf0ba('0x62')]?[{'model':db['Attachment'],'as':_0xf0ba('0x9b')}]:undefined})[_0xf0ba('0x16')](handleEntityNotFound(_0x33f033,null))[_0xf0ba('0x16')](function(_0x599efd){if(_0x599efd){var _0x363724=_0x599efd;var _0x18ca2e={'from':_0x363724[_0xf0ba('0x99')],'to':_0x363724['to'],'subject':_0x363724[_0xf0ba('0x9c')],'html':htmlEntities[_0xf0ba('0x9d')](_0x363724[_0xf0ba('0x4b')]),'headers':{'Date':moment(_0x363724[_0xf0ba('0x9a')])[_0xf0ba('0x6a')](_0xf0ba('0x9e'))},'attachments':[]};if(_0x599efd['cc']){_0x18ca2e[_0xf0ba('0x9f')]['Cc']=_0x599efd['cc'];}if(_0x599efd[_0xf0ba('0xa0')]){_0x18ca2e[_0xf0ba('0x9f')][_0xf0ba('0xa1')]=_0x599efd[_0xf0ba('0xa0')];}return new BPromise(function(_0xa474ac,_0x3f7626){if(_0x599efd[_0xf0ba('0x9b')]){for(var _0xd521b1=0x0;_0xd521b1<_0x599efd[_0xf0ba('0x9b')][_0xf0ba('0x3b')];_0xd521b1++){var _0x169b23=_0x599efd['Attachments'][_0xd521b1];var _0x5015b1=path[_0xf0ba('0x97')](_0xdff4d6,_0x169b23['basename']);if(fs[_0xf0ba('0xa2')](_0x5015b1)){_0x18ca2e[_0xf0ba('0x62')][_0xf0ba('0xa3')]({'name':_0x169b23[_0xf0ba('0x2e')],'contentType':_0x169b23[_0xf0ba('0x33')],'data':fs[_0xf0ba('0xa4')](_0x5015b1)});}}}emlformat[_0xf0ba('0xa5')](_0x18ca2e,function(_0x14a6e6,_0x154c4e){if(_0x14a6e6)return _0x3f7626(_0x14a6e6);fs[_0xf0ba('0xa6')](path[_0xf0ba('0x97')](_0x1ea6f8,_0x31081b),_0x154c4e);return _0xa474ac(_0x154c4e);});});}})[_0xf0ba('0x16')](function(_0x4e5709){if(_0x4e5709){return _0x33f033[_0xf0ba('0x91')](path[_0xf0ba('0x97')](_0x1ea6f8,_0x31081b),_0x31081b,function(_0x3bea55){if(!_0x3bea55)fs[_0xf0ba('0xa7')](path[_0xf0ba('0x97')](_0x1ea6f8,_0x31081b));});}})[_0xf0ba('0x20')](handleError(_0x33f033,null));};