d2d2379aa34dcc86973a92510949e49bb37a22ce
[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 _0x867f=['root','basename','Contacts','create','defaults','ListId','List\x20not\x20found','SendMail','sendMail','format','YYYY-MM-DD\x20HH:mm:ss','mail-interactions','accept','mail','readAt','UserId','get','MailInteraction','MailInteractionId','interaction','role','null','AcceptMessage','manual','interface','SIP/%s','channel','EventManager','acceptmessage','reject','agent','[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','event','rejectmessage','download','unix','toString','files','message-%s.eml','from','bcc','Attachment','Attachments','subject','createdAt','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','push','readFileSync','build','log','err','html-pdf','eml-format','rimraf','fast-json-patch','request-promise','moment','mustache','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','localhost','socket.io-emitter','./mailMessage.socket','register','jayson/promise','client','http','then','info','MailMessage,\x20%s,\x20%s','request\x20sent','MailMessage,\x20%s,\x20%s,\x20%s','stringify','error','message','result','catch','status','end','count','offset','limit','set','Content-Range','json','update','sendStatus','stack','name','index','map','rawAttributes','fieldName','type','query','keys','filters','attributes','intersection','model','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options','MailMessage','includeAll','include','rows','params','filtears','body','destroy','find','describe','direction','out','secret','split','MailAccount','findOne','MailAccountId','List','CmContact','updatedAt','DESC','MailServerOut','Smtp','isNil','host','port','secure','authentication','auth','user','service','attachments','join'];(function(_0x1573ea,_0x434f02){var _0x51c0e2=function(_0x5c9b28){while(--_0x5c9b28){_0x1573ea['push'](_0x1573ea['shift']());}};_0x51c0e2(++_0x434f02);}(_0x867f,0xdc));var _0xf867=function(_0x151732,_0x82b764){_0x151732=_0x151732-0x0;var _0x1cda86=_0x867f[_0x151732];return _0x1cda86;};'use strict';var pdf=require(_0xf867('0x0'));var emlformat=require(_0xf867('0x1'));var rimraf=require(_0xf867('0x2'));var zipdir=require('zip-dir');var jsonpatch=require(_0xf867('0x3'));var rp=require(_0xf867('0x4'));var moment=require(_0xf867('0x5'));var BPromise=require('bluebird');var Mustache=require(_0xf867('0x6'));var util=require('util');var path=require(_0xf867('0x7'));var sox=require(_0xf867('0x8'));var csv=require(_0xf867('0x9'));var ejs=require(_0xf867('0xa'));var fs=require('fs');var _=require(_0xf867('0xb'));var squel=require(_0xf867('0xc'));var crypto=require(_0xf867('0xd'));var jsforce=require(_0xf867('0xe'));var deskjs=require(_0xf867('0xf'));var toCsv=require(_0xf867('0x9'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xf867('0x10'));var authService=require(_0xf867('0x11'));var qs=require(_0xf867('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0xf867('0x13'))(_0xf867('0x14'));var utils=require('../../config/utils');var config=require(_0xf867('0x15'));var licenseUtil=require(_0xf867('0x16'));var db=require(_0xf867('0x17'))['db'];config[_0xf867('0x18')]=_['defaults'](config[_0xf867('0x18')],{'host':_0xf867('0x19'),'port':0x18eb});var socket=require(_0xf867('0x1a'))(new Redis(config[_0xf867('0x18')]));require(_0xf867('0x1b'))[_0xf867('0x1c')](socket);var jayson=require(_0xf867('0x1d'));var client=jayson[_0xf867('0x1e')][_0xf867('0x1f')]({'port':0x232c});var client9003=jayson[_0xf867('0x1e')]['http']({'port':0x232b});var client9002=jayson['client']['http']({'port':0x232a});function respondWithRpcPromise(_0x46d844,_0x45541a,_0x42179b,_0x3bc89a){return new BPromise(function(_0x13f08f,_0x29b42f){var _0x2b98cd=_0x3bc89a||client;return _0x2b98cd['request'](_0x46d844,_0x42179b)[_0xf867('0x20')](function(_0x20586d){logger[_0xf867('0x21')](_0xf867('0x22'),_0x45541a,_0xf867('0x23'));logger['debug'](_0xf867('0x24'),_0x45541a,'request\x20sent',JSON[_0xf867('0x25')](_0x20586d));if(_0x20586d[_0xf867('0x26')]){if(_0x20586d[_0xf867('0x26')]['code']===0x1f4){logger[_0xf867('0x26')](_0xf867('0x22'),_0x45541a,_0x20586d[_0xf867('0x26')][_0xf867('0x27')]);return _0x29b42f(_0x20586d[_0xf867('0x26')][_0xf867('0x27')]);}logger[_0xf867('0x26')](_0xf867('0x22'),_0x45541a,_0x20586d['error'][_0xf867('0x27')]);return _0x13f08f(_0x20586d[_0xf867('0x26')][_0xf867('0x27')]);}else{logger['info'](_0xf867('0x22'),_0x45541a,_0xf867('0x23'));_0x13f08f(_0x20586d[_0xf867('0x28')][_0xf867('0x27')]);}})[_0xf867('0x29')](function(_0x550f54){logger[_0xf867('0x26')](_0xf867('0x22'),_0x45541a,_0x550f54);_0x29b42f(_0x550f54);});});}function respondWithStatusCode(_0x2c6e8e,_0x165e63){_0x165e63=_0x165e63||0xcc;return function(_0x16a903){if(_0x16a903){return _0x2c6e8e['sendStatus'](_0x165e63);}return _0x2c6e8e[_0xf867('0x2a')](_0x165e63)[_0xf867('0x2b')]();};}function respondWithResult(_0x13a202,_0x20594e){_0x20594e=_0x20594e||0xc8;return function(_0x2765cf){if(_0x2765cf){return _0x13a202[_0xf867('0x2a')](_0x20594e)['json'](_0x2765cf);}};}function respondWithFilteredResult(_0x3d4b18,_0x28fb90){return function(_0x169578){if(_0x169578){var _0x4c0f0c=_0x169578[_0xf867('0x2c')],_0x1553fc=_0x28fb90['offset'],_0x1b7509=_0x28fb90[_0xf867('0x2d')]+_0x28fb90[_0xf867('0x2e')],_0x298406;if(_0x1b7509>=_0x4c0f0c){_0x1b7509=_0x4c0f0c;_0x298406=0xc8;}else{_0x298406=0xce;}_0x3d4b18[_0xf867('0x2a')](_0x298406);return _0x3d4b18[_0xf867('0x2f')](_0xf867('0x30'),_0x1553fc+'-'+_0x1b7509+'/'+_0x4c0f0c)[_0xf867('0x31')](_0x169578);}return null;};}function patchUpdates(_0x3ec1fc){return function(_0x20321a){try{jsonpatch['apply'](_0x20321a,_0x3ec1fc,!![]);}catch(_0x1bfdcf){return BPromise['reject'](_0x1bfdcf);}return _0x20321a['save']();};}function saveUpdates(_0x4d7666,_0x1a48c4){return function(_0x8a7c99){if(_0x8a7c99){return _0x8a7c99[_0xf867('0x32')](_0x4d7666)[_0xf867('0x20')](function(_0x5da8bb){return _0x5da8bb;});}return null;};}function removeEntity(_0x390a74,_0x382309){return function(_0x574979){if(_0x574979){return _0x574979['destroy']()[_0xf867('0x20')](function(){_0x390a74['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x5ae8f6,_0x13cb27){return function(_0x531ee5){if(!_0x531ee5){_0x5ae8f6[_0xf867('0x33')](0x194);}return _0x531ee5;};}function handleError(_0x41bc1b,_0x5f9ab0){_0x5f9ab0=_0x5f9ab0||0x1f4;return function(_0x332ca5){logger[_0xf867('0x26')](_0x332ca5[_0xf867('0x34')]);if(_0x332ca5[_0xf867('0x35')]){delete _0x332ca5['name'];}_0x41bc1b[_0xf867('0x2a')](_0x5f9ab0)['send'](_0x332ca5);};}exports[_0xf867('0x36')]=function(_0x1cb4e1,_0x5919a8){var _0x33d428={},_0x57eff1={},_0x3321c9={'count':0x0,'rows':[]};var _0x6d0131=_[_0xf867('0x37')](db['MailMessage'][_0xf867('0x38')],function(_0x5f2980){return{'name':_0x5f2980[_0xf867('0x39')],'type':_0x5f2980[_0xf867('0x3a')]['key']};});_0x57eff1['model']=_['map'](_0x6d0131,_0xf867('0x35'));_0x57eff1[_0xf867('0x3b')]=_[_0xf867('0x3c')](_0x1cb4e1['query']);_0x57eff1[_0xf867('0x3d')]=_['intersection'](_0x57eff1['model'],_0x57eff1[_0xf867('0x3b')]);_0x33d428[_0xf867('0x3e')]=_[_0xf867('0x3f')](_0x57eff1[_0xf867('0x40')],qs[_0xf867('0x41')](_0x1cb4e1['query']['fields']));_0x33d428['attributes']=_0x33d428[_0xf867('0x3e')][_0xf867('0x42')]?_0x33d428[_0xf867('0x3e')]:_0x57eff1[_0xf867('0x40')];if(!_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x43')](_0xf867('0x44'))){_0x33d428[_0xf867('0x2e')]=qs['limit'](_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x2e')]);_0x33d428['offset']=qs[_0xf867('0x2d')](_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x2d')]);}_0x33d428[_0xf867('0x45')]=qs[_0xf867('0x46')](_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x46')]);_0x33d428[_0xf867('0x47')]=qs[_0xf867('0x3d')](_[_0xf867('0x48')](_0x1cb4e1[_0xf867('0x3b')],_0x57eff1[_0xf867('0x3d')]),_0x6d0131);if(_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x49')]){_0x33d428[_0xf867('0x47')]=_['merge'](_0x33d428[_0xf867('0x47')],{'$or':_[_0xf867('0x37')](_0x6d0131,function(_0x462764){if(_0x462764[_0xf867('0x3a')]!==_0xf867('0x4a')){var _0x350426={};_0x350426[_0x462764[_0xf867('0x35')]]={'$like':'%'+_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x49')]+'%'};return _0x350426;}})});}_0x33d428=_[_0xf867('0x4b')]({},_0x33d428,_0x1cb4e1[_0xf867('0x4c')]);var _0x83324c={'where':_0x33d428[_0xf867('0x47')]};return db[_0xf867('0x4d')][_0xf867('0x2c')](_0x83324c)[_0xf867('0x20')](function(_0x13d0b5){_0x3321c9['count']=_0x13d0b5;if(_0x1cb4e1[_0xf867('0x3b')][_0xf867('0x4e')]){_0x33d428[_0xf867('0x4f')]=[{'all':!![]}];}return db[_0xf867('0x4d')]['findAll'](_0x33d428);})[_0xf867('0x20')](function(_0x1931ae){_0x3321c9[_0xf867('0x50')]=_0x1931ae;return _0x3321c9;})[_0xf867('0x20')](respondWithFilteredResult(_0x5919a8,_0x33d428))[_0xf867('0x29')](handleError(_0x5919a8,null));};exports['show']=function(_0x53dba7,_0x18ed38){var _0x26275e={'raw':!![],'where':{'id':_0x53dba7[_0xf867('0x51')]['id']}},_0x1e0810={};_0x1e0810[_0xf867('0x40')]=_['keys'](db[_0xf867('0x4d')][_0xf867('0x38')]);_0x1e0810[_0xf867('0x3b')]=_['keys'](_0x53dba7['query']);_0x1e0810[_0xf867('0x52')]=_[_0xf867('0x3f')](_0x1e0810['model'],_0x1e0810['query']);_0x26275e['attributes']=_[_0xf867('0x3f')](_0x1e0810[_0xf867('0x40')],qs[_0xf867('0x41')](_0x53dba7[_0xf867('0x3b')]['fields']));_0x26275e[_0xf867('0x3e')]=_0x26275e[_0xf867('0x3e')][_0xf867('0x42')]?_0x26275e[_0xf867('0x3e')]:_0x1e0810[_0xf867('0x40')];if(_0x53dba7[_0xf867('0x3b')][_0xf867('0x4e')]){_0x26275e[_0xf867('0x4f')]=[{'all':!![]}];}_0x26275e=_[_0xf867('0x4b')]({},_0x26275e,_0x53dba7[_0xf867('0x4c')]);return db[_0xf867('0x4d')]['find'](_0x26275e)[_0xf867('0x20')](handleEntityNotFound(_0x18ed38,null))[_0xf867('0x20')](respondWithResult(_0x18ed38,null))['catch'](handleError(_0x18ed38,null));};exports[_0xf867('0x32')]=function(_0x279187,_0x5396b4){if(_0x279187[_0xf867('0x53')]['id']){delete _0x279187['body']['id'];}return db['MailMessage']['find']({'where':{'id':_0x279187[_0xf867('0x51')]['id']}})[_0xf867('0x20')](handleEntityNotFound(_0x5396b4,null))[_0xf867('0x20')](saveUpdates(_0x279187[_0xf867('0x53')],null))['then'](respondWithResult(_0x5396b4,null))[_0xf867('0x29')](handleError(_0x5396b4,null));};exports[_0xf867('0x54')]=function(_0xe5b12e,_0x317afc){return db[_0xf867('0x4d')][_0xf867('0x55')]({'where':{'id':_0xe5b12e[_0xf867('0x51')]['id']}})[_0xf867('0x20')](handleEntityNotFound(_0x317afc,null))['then'](removeEntity(_0x317afc,null))[_0xf867('0x29')](handleError(_0x317afc,null));};exports[_0xf867('0x56')]=function(_0x27906c,_0x5964ea){return db[_0xf867('0x4d')][_0xf867('0x56')]()[_0xf867('0x20')](respondWithResult(_0x5964ea,null))[_0xf867('0x29')](handleError(_0x5964ea,null));};exports['create']=function(_0x4bfa11,_0x246d40){return db['MailMessage']['create'](_0x4bfa11[_0xf867('0x53')])[_0xf867('0x20')](function(_0x5b7cbe){if(_0x5b7cbe){if(_0x5b7cbe[_0xf867('0x57')]===_0xf867('0x58')&&!_0x5b7cbe[_0xf867('0x59')]){var _0x33a70d,_0x307cbb,_0x544170;if(_0x4bfa11[_0xf867('0x53')]['to']){_0x307cbb=_0x4bfa11[_0xf867('0x53')]['to'][_0xf867('0x5a')](',')[0x0];}return db[_0xf867('0x5b')][_0xf867('0x5c')]({'where':{'id':_0x5b7cbe[_0xf867('0x5d')]},'include':[{'model':db['CmList'],'as':_0xf867('0x5e'),'include':[{'model':db[_0xf867('0x5f')],'as':'Contacts','where':{'email':_0x307cbb},'limit':0x1,'order':[[_0xf867('0x60'),_0xf867('0x61')]]}]},{'model':db[_0xf867('0x62')],'as':_0xf867('0x63'),'raw':!![]}]})[_0xf867('0x20')](function(_0x18baed){_0x33a70d=_0x18baed;if(_0x33a70d&&_0x33a70d[_0xf867('0x63')]){var _0x4800a0={'tls':{'rejectUnauthorized':![]}};if(_[_0xf867('0x64')](_0x33a70d[_0xf867('0x63')]['service'])){_0x4800a0[_0xf867('0x65')]=_0x33a70d[_0xf867('0x63')][_0xf867('0x65')];_0x4800a0['port']=_0x33a70d[_0xf867('0x63')][_0xf867('0x66')];_0x4800a0[_0xf867('0x67')]=_0x33a70d[_0xf867('0x63')][_0xf867('0x67')];if(_0x33a70d[_0xf867('0x63')][_0xf867('0x68')]){_0x4800a0[_0xf867('0x69')]={'user':_0x33a70d[_0xf867('0x63')][_0xf867('0x6a')],'pass':_0x33a70d[_0xf867('0x63')]['pass']};}}else{_0x4800a0[_0xf867('0x6b')]=_0x33a70d[_0xf867('0x63')][_0xf867('0x6b')];_0x4800a0[_0xf867('0x69')]={'user':_0x33a70d[_0xf867('0x63')][_0xf867('0x6a')],'pass':_0x33a70d[_0xf867('0x63')]['pass']};_0x4800a0[_0xf867('0x67')]=_0x33a70d['Smtp'][_0xf867('0x67')];}if(_0x4bfa11[_0xf867('0x53')][_0xf867('0x6c')]&&_0x4bfa11[_0xf867('0x53')][_0xf867('0x6c')][_0xf867('0x42')]){for(var _0x428a49=0x0;_0x428a49<_0x4bfa11[_0xf867('0x53')][_0xf867('0x6c')]['length'];_0x428a49+=0x1){_0x4bfa11[_0xf867('0x53')][_0xf867('0x6c')][_0x428a49]={'filename':_0x4bfa11[_0xf867('0x53')][_0xf867('0x6c')][_0x428a49][_0xf867('0x35')],'path':path[_0xf867('0x6d')](config[_0xf867('0x6e')],'server/files/attachments',_0x4bfa11[_0xf867('0x53')][_0xf867('0x6c')][_0x428a49][_0xf867('0x6f')])};}}_0x544170={'account':_0x4800a0,'message':_0x4bfa11[_0xf867('0x53')]};return;}})[_0xf867('0x20')](function(){if(_0x33a70d[_0xf867('0x5e')]){if(_0x33a70d[_0xf867('0x5e')][_0xf867('0x70')][_0xf867('0x42')]){return _0x33a70d[_0xf867('0x5e')]['Contacts'][0x0];}else{return db['CmContact'][_0xf867('0x71')](_[_0xf867('0x72')](_0x4bfa11[_0xf867('0x53')],{'firstName':_0x4bfa11[_0xf867('0x53')]['to'],'email':_0x4bfa11[_0xf867('0x53')]['to'],'phone':_0x4bfa11['body']['to'],'ListId':_0x33a70d[_0xf867('0x73')]}));}}else{throw new Error(_0xf867('0x74'));}})['then'](function(){if(_0x544170){return respondWithRpcPromise(_0xf867('0x75'),_0xf867('0x76'),_0x544170,client9003)[_0xf867('0x20')](function(_0x30c1b8){return _0x5b7cbe[_0xf867('0x32')]({'read':!![],'messageId':_0x30c1b8['messageId'],'sentAt':moment()[_0xf867('0x77')](_0xf867('0x78'))});})[_0xf867('0x29')](function(_0x10d51f){logger['error']('%s',JSON['stringify'](_0x10d51f));return _0x5b7cbe;});}return _0x5b7cbe;});}return _0x5b7cbe;}})['then'](respondWithResult(_0x246d40,0xc9))['catch'](handleError(_0x246d40,null));};var interaction_log=require(_0xf867('0x13'))(_0xf867('0x79'));exports[_0xf867('0x7a')]=function(_0x59d6c3,_0x26dc06,_0x3b4719){var _0x10cb95={'channel':_0xf867('0x7b')};if(_0x59d6c3['body']['id']){delete _0x59d6c3['body']['id'];}_0x59d6c3[_0xf867('0x53')]['read']=!![];_0x59d6c3[_0xf867('0x53')][_0xf867('0x7c')]=moment()[_0xf867('0x77')](_0xf867('0x78'));_0x59d6c3[_0xf867('0x53')]['UserId']=_0x59d6c3['body'][_0xf867('0x7d')]||_0x59d6c3[_0xf867('0x6a')]['id'];return db[_0xf867('0x4d')][_0xf867('0x55')]({'where':{'id':_0x59d6c3[_0xf867('0x51')]['id'],'UserId':null},'include':[{'model':db['CmContact'],'as':'Contact'}]})[_0xf867('0x20')](handleEntityNotFound(_0x26dc06,null))[_0xf867('0x20')](saveUpdates(_0x59d6c3[_0xf867('0x53')],null))[_0xf867('0x20')](function(_0x5bc6c0){if(_0x5bc6c0){_0x10cb95['message']=_0x5bc6c0[_0xf867('0x7e')]({'plain':!![]});return db[_0xf867('0x7f')]['find']({'where':{'id':_0x5bc6c0[_0xf867('0x80')]}});}return null;})[_0xf867('0x20')](handleEntityNotFound(_0x26dc06,null))['then'](function(_0x220568){if(_0x220568){return _0x220568[_0xf867('0x32')]({'UserId':_0x59d6c3[_0xf867('0x53')][_0xf867('0x7d')],'read1stAt':_[_0xf867('0x64')](_0x220568['read1stAt'])?moment()[_0xf867('0x77')](_0xf867('0x78')):undefined});}return null;})[_0xf867('0x20')](function(_0x4af735){if(_0x4af735){_0x10cb95[_0xf867('0x81')]=_0x4af735[_0xf867('0x7e')]({'plain':!![]});interaction_log[_0xf867('0x21')]('[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x59d6c3['user']['id'],_0x59d6c3['user'][_0xf867('0x35')],_0x59d6c3[_0xf867('0x6a')][_0xf867('0x82')],_0x10cb95['interaction']['id'],_0x59d6c3['body']?JSON['stringify'](_0x59d6c3['body']):_0xf867('0x83'));return respondWithRpcPromise(_0xf867('0x84'),'acceptMessage',_0x10cb95);}return null;})[_0xf867('0x20')](function(_0xbf4efd){if(_0xbf4efd){if(_0x59d6c3[_0xf867('0x53')][_0xf867('0x85')]){_0x10cb95['message'][_0xf867('0x86')]=util['format'](_0xf867('0x87'),_0x59d6c3['user'][_0xf867('0x35')]);_0x10cb95[_0xf867('0x27')][_0xf867('0x88')]=_0x10cb95[_0xf867('0x88')];_0x10cb95[_0xf867('0x27')]['event']=_0xf867('0x7a');return respondWithRpcPromise(_0xf867('0x89'),'EventManager',{'event':_0xf867('0x8a'),'message':_0x10cb95[_0xf867('0x27')]},client9002)[_0xf867('0x20')](function(){return _0x10cb95;});}return _0x10cb95;}return null;})[_0xf867('0x20')](respondWithResult(_0x26dc06,null))['catch'](handleError(_0x26dc06,null));};var interaction_log=require(_0xf867('0x13'))('mail-interactions');exports[_0xf867('0x8b')]=function(_0x4dff7d,_0x4d45ab,_0x1cb161){var _0x592c6d={'agent':{},'channel':'mail'};if(_0x4dff7d['body']['id']){delete _0x4dff7d[_0xf867('0x53')]['id'];}_0x4dff7d[_0xf867('0x53')][_0xf867('0x7d')]=_0x4dff7d['body'][_0xf867('0x7d')]||_0x4dff7d['user']['id'];_0x592c6d[_0xf867('0x8c')]['id']=_0x4dff7d['body']['UserId'];return db[_0xf867('0x4d')][_0xf867('0x55')]({'where':{'id':_0x4dff7d[_0xf867('0x51')]['id'],'UserId':null}})[_0xf867('0x20')](handleEntityNotFound(_0x4d45ab,null))[_0xf867('0x20')](function(_0x3910c4){if(_0x3910c4){_0x592c6d['message']=_0x3910c4['get']({'plain':!![]});return db[_0xf867('0x7f')][_0xf867('0x55')]({'where':{'id':_0x3910c4[_0xf867('0x80')]}});}return null;})[_0xf867('0x20')](handleEntityNotFound(_0x4d45ab,null))['then'](function(_0x3aef1b){if(_0x3aef1b){_0x592c6d['interaction']=_0x3aef1b[_0xf867('0x7e')]({'plain':!![]});interaction_log['info'](_0xf867('0x8d'),_0x4dff7d[_0xf867('0x6a')]['id'],_0x4dff7d[_0xf867('0x6a')][_0xf867('0x35')],_0x4dff7d['user'][_0xf867('0x82')],_0x592c6d['interaction']['id'],_0x4dff7d[_0xf867('0x53')]?JSON[_0xf867('0x25')](_0x4dff7d['body']):_0xf867('0x83'));return respondWithRpcPromise(_0xf867('0x8e'),_0xf867('0x8f'),_0x592c6d);}return null;})[_0xf867('0x20')](function(_0x1aa7cc){if(_0x1aa7cc){_0x1aa7cc[_0xf867('0x27')][_0xf867('0x86')]=util[_0xf867('0x77')](_0xf867('0x87'),_0x4dff7d[_0xf867('0x6a')]['name']);_0x1aa7cc[_0xf867('0x27')][_0xf867('0x88')]=_0x592c6d[_0xf867('0x88')];_0x1aa7cc[_0xf867('0x27')][_0xf867('0x90')]=_0xf867('0x8b');respondWithRpcPromise(_0xf867('0x89'),'EventManager',{'event':_0xf867('0x91'),'message':_0x1aa7cc[_0xf867('0x27')]},client9002);return _0x592c6d;}return null;})['then'](respondWithResult(_0x4d45ab,null))['catch'](handleError(_0x4d45ab,null));};exports[_0xf867('0x92')]=function(_0xb494b2,_0x58484e){var _0x54084d=moment()[_0xf867('0x93')]()[_0xf867('0x94')]();var _0x5c6ab5=path[_0xf867('0x6d')](config[_0xf867('0x6e')],'server',_0xf867('0x95'),'tmp');var _0x132848=path[_0xf867('0x6d')](config[_0xf867('0x6e')],'server',_0xf867('0x95'),_0xf867('0x6c'));var _0x159cb1=util['format'](_0xf867('0x96'),_0x54084d);return db[_0xf867('0x4d')]['find']({'where':{'id':_0xb494b2[_0xf867('0x51')]['id']},'attributes':['id',_0xf867('0x53'),_0xf867('0x97'),'to','cc',_0xf867('0x98'),'subject','createdAt'],'include':_0xb494b2[_0xf867('0x3b')]['attachments']?[{'model':db[_0xf867('0x99')],'as':_0xf867('0x9a')}]:undefined})[_0xf867('0x20')](handleEntityNotFound(_0x58484e,null))[_0xf867('0x20')](function(_0x1d6ac9){if(_0x1d6ac9){var _0xfd924f=_0x1d6ac9;var _0x48b523={'from':_0xfd924f[_0xf867('0x97')],'to':_0xfd924f['to'],'subject':_0xfd924f[_0xf867('0x9b')],'html':_0xfd924f[_0xf867('0x53')],'headers':{'Date':moment(_0xfd924f[_0xf867('0x9c')])[_0xf867('0x77')](_0xf867('0x9d'))},'attachments':[]};if(_0x1d6ac9['cc']){_0x48b523[_0xf867('0x9e')]['Cc']=_0x1d6ac9['cc'];}if(_0x1d6ac9[_0xf867('0x98')]){_0x48b523['headers'][_0xf867('0x9f')]=_0x1d6ac9[_0xf867('0x98')];}return new BPromise(function(_0x24c950,_0x765dbc){if(_0x1d6ac9['Attachments']){for(var _0x31fa81=0x0;_0x31fa81<_0x1d6ac9[_0xf867('0x9a')][_0xf867('0x42')];_0x31fa81++){var _0x353758=_0x1d6ac9['Attachments'][_0x31fa81];var _0x2c0103=path[_0xf867('0x6d')](_0x132848,_0x353758['basename']);if(fs['existsSync'](_0x2c0103)){_0x48b523[_0xf867('0x6c')][_0xf867('0xa0')]({'name':_0x353758[_0xf867('0x35')],'contentType':_0x353758[_0xf867('0x3a')],'data':fs[_0xf867('0xa1')](_0x2c0103)});}}}emlformat[_0xf867('0xa2')](_0x48b523,function(_0x1fc35a,_0x5f1fed){if(_0x1fc35a)return _0x765dbc(_0x1fc35a);fs['writeFileSync'](path[_0xf867('0x6d')](_0x5c6ab5,_0x159cb1),_0x5f1fed);return _0x24c950(_0x5f1fed);});});}})[_0xf867('0x20')](function(_0x55a559){if(_0x55a559){return _0x58484e[_0xf867('0x92')](path[_0xf867('0x6d')](_0x5c6ab5,_0x159cb1),_0x159cb1,function(_0xb28320){if(_0xb28320){console[_0xf867('0xa3')](_0xf867('0xa4'),_0xb28320);}else{fs['unlinkSync'](path[_0xf867('0x6d')](_0x5c6ab5,_0x159cb1));}});}})[_0xf867('0x29')](handleError(_0x58484e,null));};