4e5630bb1ba1958a45806da9e5b4451ae84de263
[motion2.git] / server / api / mailAccount / mailAccount.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 _0xfc1f=['join','basename','getSmtpOptions','SendMail','List\x20not\x20found','messageId','subject','attach','out','Messages','VerifySmtp','verifySmtp','Sequelize','ValidationError','spread','emit','userMailAccount:save','removeAgents','isArray','userMailAccount:remove','getAgents','bluebird','util','path','juice','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','socket.io-emitter','./mailAccount.socket','register','jayson/promise','client','then','info','MailAccount,\x20%s,\x20%s','request\x20sent','debug','MailAccount,\x20%s,\x20%s,\x20%s','error','message','result','catch','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','update','destroy','get','MailAccounts','UserProfileResource','stack','name','gmail','yahoo','imap.mail.yahoo.com','outlook365','outlook.office365.com','smtp.gmail.com','hotmail','smtp-mail.outlook.com','smtp.office365.com','Unknown\x20email\x20service\x20provider','user','username','password','modernAuthentication','CloudProviderId','pass','host','port','secure','service','authentication','Imap','Smtp','index','MailServerIn','MailServerOut','Pause','mandatoryDispositionPause','map','MailAccount','rawAttributes','fieldName','type','model','query','keys','filters','intersection','attributes','fields','order','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','findAll','params','length','include','find','body','provider','Mail\x20Account\x20not\x20found','describe','MailAccountId','Disposition','hasOwnProperty','nolimit','rows','removeDispositions','ids','addAnswer','create','getAnswers','CannedAnswer','removeAnswers','key','custom','email','ListId','isNil','active','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','getImap','findOne','addImap','removeImap','setImap','getSmtp','removeSmtp','MailInteraction','addInteraction','omit','getInteractions','CmContact','Contact','User','Owner','fullname','internal','Tag','color','tag','addApplications','sequelize','transaction','Transaction','READ_COMMITTED','MailApplication','bulkCreate','findAndCountAll','priority','finally','send','getApplications','getMessages','MailMessage','$gte','split','YYYY-MM-DD\x20HH:mm:ss','inlineCss','CmList','List','Contacts','updatedAt','DESC','CloudProvider','from','format','%s\x20<%s>','attachments'];(function(_0x1e21a6,_0x3b097c){var _0x483f55=function(_0x462e1b){while(--_0x462e1b){_0x1e21a6['push'](_0x1e21a6['shift']());}};_0x483f55(++_0x3b097c);}(_0xfc1f,0xd6));var _0xffc1=function(_0x1ee909,_0x50ec30){_0x1ee909=_0x1ee909-0x0;var _0x256cde=_0xfc1f[_0x1ee909];return _0x256cde;};'use strict';var moment=require('moment');var BPromise=require(_0xffc1('0x0'));var util=require(_0xffc1('0x1'));var path=require(_0xffc1('0x2'));var _=require('lodash');var juice=require(_0xffc1('0x3'));var Redis=require(_0xffc1('0x4'));var qs=require(_0xffc1('0x5'));var logger=require(_0xffc1('0x6'))(_0xffc1('0x7'));var config=require(_0xffc1('0x8'));var db=require(_0xffc1('0x9'))['db'];config[_0xffc1('0xa')]=_[_0xffc1('0xb')](config[_0xffc1('0xa')],{'host':'localhost','port':0x18eb});var socket=require(_0xffc1('0xc'))(new Redis(config[_0xffc1('0xa')]));require(_0xffc1('0xd'))[_0xffc1('0xe')](socket);var jayson=require(_0xffc1('0xf'));var client=jayson[_0xffc1('0x10')]['http']({'port':0x232b});function respondWithRpcPromise(_0x5d444f,_0x3e9ca2,_0x3f8b63,_0x37447c){return new BPromise(function(_0x4fc2ee,_0x1770c0){var _0x5e5086=_0x37447c||client;return _0x5e5086['request'](_0x5d444f,_0x3f8b63)[_0xffc1('0x11')](function(_0xd54200){logger[_0xffc1('0x12')](_0xffc1('0x13'),_0x3e9ca2,_0xffc1('0x14'));logger[_0xffc1('0x15')](_0xffc1('0x16'),_0x3e9ca2,_0xffc1('0x14'),JSON['stringify'](_0xd54200));if(_0xd54200[_0xffc1('0x17')]){if(_0xd54200[_0xffc1('0x17')]['code']===0x1f4){logger[_0xffc1('0x17')](_0xffc1('0x13'),_0x3e9ca2,_0xd54200[_0xffc1('0x17')]['message']);return _0x1770c0(_0xd54200[_0xffc1('0x17')]['message']);}logger['error'](_0xffc1('0x13'),_0x3e9ca2,_0xd54200[_0xffc1('0x17')][_0xffc1('0x18')]);return _0x4fc2ee(_0xd54200['error'][_0xffc1('0x18')]);}else{logger[_0xffc1('0x12')](_0xffc1('0x13'),_0x3e9ca2,_0xffc1('0x14'));_0x4fc2ee(_0xd54200[_0xffc1('0x19')][_0xffc1('0x18')]);}})[_0xffc1('0x1a')](function(_0x2607ab){logger[_0xffc1('0x17')](_0xffc1('0x13'),_0x3e9ca2,_0x2607ab);_0x1770c0(_0x2607ab);});});}function respondWithStatusCode(_0x22da22,_0x4f950e){_0x4f950e=_0x4f950e||0xcc;return function(_0x358423){if(_0x358423){return _0x22da22[_0xffc1('0x1b')](_0x4f950e);}return _0x22da22[_0xffc1('0x1c')](_0x4f950e)[_0xffc1('0x1d')]();};}function respondWithResult(_0x269c04,_0x1956b1){_0x1956b1=_0x1956b1||0xc8;return function(_0x50ba24){if(_0x50ba24){return _0x269c04['status'](_0x1956b1)[_0xffc1('0x1e')](_0x50ba24);}};}function respondWithFilteredResult(_0x459088,_0x2cf6e4){return function(_0x31d3f1){if(_0x31d3f1){var _0x216071=typeof _0x2cf6e4[_0xffc1('0x1f')]===_0xffc1('0x20')&&typeof _0x2cf6e4[_0xffc1('0x21')]===_0xffc1('0x20');var _0x34fabd=_0x31d3f1[_0xffc1('0x22')];var _0x73f815=_0x216071?0x0:_0x2cf6e4['offset'];var _0x2b2222=_0x216071?_0x31d3f1[_0xffc1('0x22')]:_0x2cf6e4['offset']+_0x2cf6e4[_0xffc1('0x21')];var _0x579cd4;if(_0x2b2222>=_0x34fabd){_0x2b2222=_0x34fabd;_0x579cd4=0xc8;}else{_0x579cd4=0xce;}_0x459088[_0xffc1('0x1c')](_0x579cd4);return _0x459088[_0xffc1('0x23')](_0xffc1('0x24'),_0x73f815+'-'+_0x2b2222+'/'+_0x34fabd)['json'](_0x31d3f1);}return null;};}function saveUpdates(_0x3f5150){return function(_0xcf7cd7){if(_0xcf7cd7){return _0xcf7cd7[_0xffc1('0x25')](_0x3f5150)[_0xffc1('0x11')](function(_0x2a71a9){return _0x2a71a9;});}return null;};}function removeEntity(_0x224f52){return function(_0x371c5c){if(_0x371c5c){return _0x371c5c[_0xffc1('0x26')]()[_0xffc1('0x11')](function(){var _0x2fe675=_0x371c5c[_0xffc1('0x27')]({'plain':!![]});var _0x3e32e7=_0xffc1('0x28');return db[_0xffc1('0x29')][_0xffc1('0x26')]({'where':{'type':_0x3e32e7,'resourceId':_0x2fe675['id']}})['then'](function(){return _0x371c5c;});})[_0xffc1('0x11')](function(){_0x224f52[_0xffc1('0x1c')](0xcc)['end']();});}};}function handleEntityNotFound(_0x3a78bc){return function(_0x88dbe1){if(!_0x88dbe1){_0x3a78bc[_0xffc1('0x1b')](0x194);}return _0x88dbe1;};}function handleError(_0x496e03,_0x14e375){_0x14e375=_0x14e375||0x1f4;return function(_0x4202dc){logger[_0xffc1('0x17')](_0x4202dc[_0xffc1('0x2a')]);if(_0x4202dc[_0xffc1('0x2b')]){delete _0x4202dc[_0xffc1('0x2b')];}_0x496e03['status'](_0x14e375)['send'](_0x4202dc);};}function getImapHost(_0x5eaf10){switch(_0x5eaf10){case _0xffc1('0x2c'):return'imap.gmail.com';case _0xffc1('0x2d'):return _0xffc1('0x2e');case'hotmail':return'imap-mail.outlook.com';case _0xffc1('0x2f'):return _0xffc1('0x30');}throw new Error('Unknown\x20email\x20service\x20provider',_0x5eaf10);}function getSmtpHost(_0x5a13c4){switch(_0x5a13c4){case _0xffc1('0x2c'):return _0xffc1('0x31');case'yahoo':return'smtp.mail.yahoo.com';case _0xffc1('0x32'):return _0xffc1('0x33');case'outlook365':return _0xffc1('0x34');}throw new Error(_0xffc1('0x35'),_0x5a13c4);}function getAuthenticationOptions(_0x4cf25d,_0x338c5f){var _0xd725da={};var _0x1f0e57={};if(_0x4cf25d){_0xd725da[_0xffc1('0x36')]=_0x1f0e57[_0xffc1('0x36')]=_0x338c5f[_0xffc1('0x37')];_0xd725da[_0xffc1('0x38')]=_0x1f0e57['pass']=_0x338c5f[_0xffc1('0x38')];if(_0x338c5f[_0xffc1('0x39')]){_0xd725da[_0xffc1('0x39')]=_0x1f0e57[_0xffc1('0x39')]=!![];_0xd725da[_0xffc1('0x3a')]=_0x1f0e57[_0xffc1('0x3a')]=_0x338c5f[_0xffc1('0x3a')];_0xd725da[_0xffc1('0x38')]=_0x1f0e57[_0xffc1('0x3b')]=null;}else{_0xd725da[_0xffc1('0x3a')]=_0x1f0e57[_0xffc1('0x3a')]=null;}_0xd725da[_0xffc1('0x3c')]=getImapHost(_0x4cf25d);_0xd725da['port']=0x3e1;_0x1f0e57[_0xffc1('0x3c')]=getSmtpHost(_0x4cf25d);_0x1f0e57[_0xffc1('0x3d')]=0x24b;_0x1f0e57[_0xffc1('0x3e')]=![];_0xd725da[_0xffc1('0x3f')]=_0x1f0e57[_0xffc1('0x3f')]=null;}else{if(_0x338c5f[_0xffc1('0x40')]){_0xd725da['user']=_0x1f0e57[_0xffc1('0x36')]=_0x338c5f['username'];_0xd725da['password']=_0x1f0e57[_0xffc1('0x3b')]=_0x338c5f[_0xffc1('0x38')];}else{_0xd725da['authentication']=_0x1f0e57[_0xffc1('0x40')]=![];_0xd725da[_0xffc1('0x36')]=_0x1f0e57[_0xffc1('0x36')]=null;_0xd725da[_0xffc1('0x38')]=_0x1f0e57[_0xffc1('0x3b')]=null;}_0xd725da[_0xffc1('0x3c')]=_0x338c5f[_0xffc1('0x41')]['host'];_0xd725da[_0xffc1('0x3d')]=_0x338c5f[_0xffc1('0x41')]['port'];_0x1f0e57[_0xffc1('0x3c')]=_0x338c5f['Smtp']['host'];_0x1f0e57[_0xffc1('0x3d')]=_0x338c5f[_0xffc1('0x42')][_0xffc1('0x3d')];}return{'Imap':_0xd725da,'Smtp':_0x1f0e57};}exports[_0xffc1('0x43')]=function(_0xae40f5,_0x24d6d3){var _0x2b252f={'include':[{'model':db[_0xffc1('0x44')],'as':_0xffc1('0x41')},{'model':db[_0xffc1('0x45')],'as':'Smtp'},{'model':db[_0xffc1('0x46')],'as':_0xffc1('0x47')}]},_0x52578d={},_0x59959e={'count':0x0,'rows':[]};var _0x506c9f=_[_0xffc1('0x48')](db[_0xffc1('0x49')][_0xffc1('0x4a')],function(_0xf6454b){return{'name':_0xf6454b[_0xffc1('0x4b')],'type':_0xf6454b[_0xffc1('0x4c')]['key']};});_0x52578d[_0xffc1('0x4d')]=_[_0xffc1('0x48')](_0x506c9f,_0xffc1('0x2b'));_0x52578d[_0xffc1('0x4e')]=_[_0xffc1('0x4f')](_0xae40f5[_0xffc1('0x4e')]);_0x52578d[_0xffc1('0x50')]=_[_0xffc1('0x51')](_0x52578d['model'],_0x52578d[_0xffc1('0x4e')]);_0x2b252f[_0xffc1('0x52')]=_['intersection'](_0x52578d[_0xffc1('0x4d')],qs[_0xffc1('0x53')](_0xae40f5['query']['fields']));_0x2b252f[_0xffc1('0x52')]=_0x2b252f['attributes']['length']?_0x2b252f[_0xffc1('0x52')]:_0x52578d[_0xffc1('0x4d')];if(!_0xae40f5[_0xffc1('0x4e')]['hasOwnProperty']('nolimit')){_0x2b252f[_0xffc1('0x21')]=qs[_0xffc1('0x21')](_0xae40f5['query'][_0xffc1('0x21')]);_0x2b252f[_0xffc1('0x1f')]=qs['offset'](_0xae40f5[_0xffc1('0x4e')]['offset']);}_0x2b252f[_0xffc1('0x54')]=qs['sort'](_0xae40f5[_0xffc1('0x4e')][_0xffc1('0x55')]);_0x2b252f[_0xffc1('0x56')]=qs[_0xffc1('0x50')](_[_0xffc1('0x57')](_0xae40f5[_0xffc1('0x4e')],_0x52578d['filters']),_0x506c9f);if(_0xae40f5[_0xffc1('0x4e')][_0xffc1('0x58')]){_0x2b252f[_0xffc1('0x56')]=_[_0xffc1('0x59')](_0x2b252f[_0xffc1('0x56')],{'$or':_[_0xffc1('0x48')](_0x506c9f,function(_0x151376){if(_0x151376[_0xffc1('0x4c')]!==_0xffc1('0x5a')){var _0x453ee1={};_0x453ee1[_0x151376[_0xffc1('0x2b')]]={'$like':'%'+_0xae40f5[_0xffc1('0x4e')][_0xffc1('0x58')]+'%'};return _0x453ee1;}})});}_0x2b252f=_['merge']({},_0x2b252f,_0xae40f5[_0xffc1('0x5b')]);var _0x4dbf9c={'where':_0x2b252f['where']};return db[_0xffc1('0x49')][_0xffc1('0x22')](_0x4dbf9c)['then'](function(_0x106a64){_0x59959e[_0xffc1('0x22')]=_0x106a64;if(_0xae40f5[_0xffc1('0x4e')][_0xffc1('0x5c')]){_0x2b252f['include']=[{'all':!![]}];}return db[_0xffc1('0x49')][_0xffc1('0x5d')](_0x2b252f);})[_0xffc1('0x11')](function(_0x543bd8){_0x59959e['rows']=_0x543bd8;return _0x59959e;})['then'](respondWithFilteredResult(_0x24d6d3,_0x2b252f))[_0xffc1('0x1a')](handleError(_0x24d6d3,null));};exports['show']=function(_0x42314d,_0x254325){var _0x1c4f38={'raw':![],'where':{'id':_0x42314d[_0xffc1('0x5e')]['id']},'include':[{'model':db[_0xffc1('0x44')],'as':_0xffc1('0x41')},{'model':db[_0xffc1('0x45')],'as':_0xffc1('0x42')},{'model':db[_0xffc1('0x46')],'as':_0xffc1('0x47')}]},_0x505d76={};_0x505d76[_0xffc1('0x4d')]=_[_0xffc1('0x4f')](db[_0xffc1('0x49')][_0xffc1('0x4a')]);_0x505d76[_0xffc1('0x4e')]=_['keys'](_0x42314d['query']);_0x505d76['filters']=_[_0xffc1('0x51')](_0x505d76[_0xffc1('0x4d')],_0x505d76[_0xffc1('0x4e')]);_0x1c4f38[_0xffc1('0x52')]=_[_0xffc1('0x51')](_0x505d76[_0xffc1('0x4d')],qs[_0xffc1('0x53')](_0x42314d[_0xffc1('0x4e')][_0xffc1('0x53')]));_0x1c4f38[_0xffc1('0x52')]=_0x1c4f38['attributes'][_0xffc1('0x5f')]?_0x1c4f38[_0xffc1('0x52')]:_0x505d76['model'];if(_0x42314d[_0xffc1('0x4e')][_0xffc1('0x5c')]){_0x1c4f38[_0xffc1('0x60')]=[{'all':!![]}];}_0x1c4f38=_['merge']({},_0x1c4f38,_0x42314d['options']);return db['MailAccount'][_0xffc1('0x61')](_0x1c4f38)[_0xffc1('0x11')](handleEntityNotFound(_0x254325,null))[_0xffc1('0x11')](respondWithResult(_0x254325,null))[_0xffc1('0x1a')](handleError(_0x254325,null));};exports[_0xffc1('0x25')]=function(_0x265fff,_0x105a27){if(_0x265fff['body']['id']){delete _0x265fff[_0xffc1('0x62')]['id'];}var _0x25206b=_0x265fff[_0xffc1('0x62')]['provider']==='custom'?null:_0x265fff[_0xffc1('0x62')][_0xffc1('0x63')];var _0x10fb0c=_0x265fff[_0xffc1('0x62')][_0xffc1('0x41')]||{};var _0x1e0fc4=_0x265fff['body']['Smtp']||{};if(_0x25206b){_0x10fb0c[_0xffc1('0x3c')]=getImapHost(_0x25206b);_0x10fb0c['port']=0x3e1;_0x1e0fc4[_0xffc1('0x3c')]=getSmtpHost(_0x25206b);_0x1e0fc4['port']=0x24b;_0x1e0fc4[_0xffc1('0x3e')]=![];}if(_0x1e0fc4[_0xffc1('0x39')]){_0x1e0fc4['secure']=![];}else{_0x1e0fc4[_0xffc1('0x3a')]=null;}if(!_0x10fb0c[_0xffc1('0x39')])_0x10fb0c['CloudProviderId']=null;_0x265fff[_0xffc1('0x62')][_0xffc1('0x41')]=_0x10fb0c;_0x265fff['body'][_0xffc1('0x42')]=_0x1e0fc4;return db['MailAccount'][_0xffc1('0x61')]({'where':{'id':_0x265fff[_0xffc1('0x5e')]['id']},'include':[{'model':db[_0xffc1('0x44')],'as':_0xffc1('0x41')},{'model':db['MailServerOut'],'as':_0xffc1('0x42')},{'model':db[_0xffc1('0x46')],'as':_0xffc1('0x47')}]})['then'](handleEntityNotFound(_0x105a27,null))[_0xffc1('0x11')](function(_0x49de2f){if(!_0x49de2f)throw new Error(_0xffc1('0x64'));if(_0x49de2f[_0xffc1('0x3f')])delete _0x265fff[_0xffc1('0x62')]['Imap'];return _0x49de2f;})['then'](saveUpdates(_0x265fff[_0xffc1('0x62')],null))[_0xffc1('0x11')](function(_0x30c2ba){if(!_0x265fff['body'][_0xffc1('0x41')])return _0x30c2ba;return db[_0xffc1('0x44')][_0xffc1('0x25')](_0x265fff[_0xffc1('0x62')][_0xffc1('0x41')],{'where':{'MailAccountId':_0x30c2ba['id']}})[_0xffc1('0x11')](function(){return _0x30c2ba;});})[_0xffc1('0x11')](function(_0x29dd1e){if(!_0x265fff[_0xffc1('0x62')]['Smtp'])return _0x29dd1e;return db[_0xffc1('0x45')]['update'](_0x265fff['body'][_0xffc1('0x42')],{'where':{'MailAccountId':_0x29dd1e['id']}})[_0xffc1('0x11')](function(){return _0x29dd1e;});})['then'](respondWithResult(_0x105a27,null))[_0xffc1('0x1a')](handleError(_0x105a27,null));};exports[_0xffc1('0x65')]=function(_0x658daf,_0x4be944){return db[_0xffc1('0x49')]['describe']()[_0xffc1('0x11')](respondWithResult(_0x4be944,null))[_0xffc1('0x1a')](handleError(_0x4be944,null));};exports['addDisposition']=function(_0x492166,_0x20dbcd){if(_0x492166[_0xffc1('0x62')]['id']){delete _0x492166['body']['id'];}return db[_0xffc1('0x49')]['find']({'where':{'id':_0x492166[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x20dbcd,null))[_0xffc1('0x11')](function(_0xb00e74){if(_0xb00e74){_0x492166[_0xffc1('0x62')][_0xffc1('0x66')]=_0xb00e74['id'];return db['Disposition']['create'](_0x492166[_0xffc1('0x62')]);}})[_0xffc1('0x11')](respondWithResult(_0x20dbcd,null))[_0xffc1('0x1a')](handleError(_0x20dbcd,null));};exports['getDispositions']=function(_0x5f2b50,_0x155f15){var _0xad16f2={'raw':![],'where':{}};var _0x3546a8={};var _0x270ba6={'count':0x0,'rows':[]};return db['MailAccount']['findOne']({'where':{'id':_0x5f2b50[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x155f15,null))['then'](function(_0x649ef1){if(_0x649ef1){_0x3546a8[_0xffc1('0x4d')]=_[_0xffc1('0x4f')](db[_0xffc1('0x67')][_0xffc1('0x4a')]);_0x3546a8[_0xffc1('0x4e')]=_['keys'](_0x5f2b50[_0xffc1('0x4e')]);_0x3546a8[_0xffc1('0x50')]=_[_0xffc1('0x51')](_0x3546a8[_0xffc1('0x4d')],_0x3546a8['query']);_0xad16f2[_0xffc1('0x52')]=_[_0xffc1('0x51')](_0x3546a8['model'],qs['fields'](_0x5f2b50[_0xffc1('0x4e')]['fields']));_0xad16f2[_0xffc1('0x52')]=_0xad16f2['attributes']['length']?_0xad16f2[_0xffc1('0x52')]:_0x3546a8[_0xffc1('0x4d')];if(!_0x5f2b50[_0xffc1('0x4e')][_0xffc1('0x68')](_0xffc1('0x69'))){_0xad16f2[_0xffc1('0x21')]=qs[_0xffc1('0x21')](_0x5f2b50['query']['limit']);_0xad16f2[_0xffc1('0x1f')]=qs['offset'](_0x5f2b50[_0xffc1('0x4e')][_0xffc1('0x1f')]);}_0xad16f2[_0xffc1('0x54')]=qs[_0xffc1('0x55')](_0x5f2b50['query'][_0xffc1('0x55')]);_0xad16f2[_0xffc1('0x56')]=qs[_0xffc1('0x50')](_[_0xffc1('0x57')](_0x5f2b50['query'],_0x3546a8['filters']));_0xad16f2['where'][_0xffc1('0x66')]=_0x649ef1['id'];if(_0x5f2b50[_0xffc1('0x4e')][_0xffc1('0x58')]){_0xad16f2['where']=_[_0xffc1('0x59')](_0xad16f2['where'],{'$or':_['map'](_0xad16f2[_0xffc1('0x52')],function(_0x4e1a9c){var _0x40666f={};_0x40666f[_0x4e1a9c]={'$like':'%'+_0x5f2b50[_0xffc1('0x4e')][_0xffc1('0x58')]+'%'};return _0x40666f;})});}_0xad16f2=_[_0xffc1('0x59')]({},_0xad16f2,_0x5f2b50[_0xffc1('0x5b')]);return db[_0xffc1('0x67')]['count']({'where':_0xad16f2['where']})['then'](function(_0x363bad){_0x270ba6['count']=_0x363bad;if(_0x5f2b50[_0xffc1('0x4e')][_0xffc1('0x5c')]){_0xad16f2['include']=[{'all':!![]}];}return db['Disposition'][_0xffc1('0x5d')](_0xad16f2);})[_0xffc1('0x11')](function(_0x45552e){_0x270ba6[_0xffc1('0x6a')]=_0x45552e;return _0x270ba6;});}})[_0xffc1('0x11')](respondWithFilteredResult(_0x155f15,_0xad16f2))[_0xffc1('0x1a')](handleError(_0x155f15,null));};exports[_0xffc1('0x6b')]=function(_0x2ded21,_0xb32b9b){return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0x2ded21[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0xb32b9b,null))['then'](function(_0xc60a12){if(_0xc60a12){return _0xc60a12['removeDispositions'](_0x2ded21[_0xffc1('0x4e')][_0xffc1('0x6c')]);}})[_0xffc1('0x11')](respondWithStatusCode(_0xb32b9b,null))['catch'](handleError(_0xb32b9b,null));};exports[_0xffc1('0x6d')]=function(_0x4bddd7,_0x4a5764){if(_0x4bddd7[_0xffc1('0x62')]['id']){delete _0x4bddd7[_0xffc1('0x62')]['id'];}return db['MailAccount'][_0xffc1('0x61')]({'where':{'id':_0x4bddd7[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x4a5764,null))['then'](function(_0x291077){if(_0x291077){_0x4bddd7['body'][_0xffc1('0x66')]=_0x291077['id'];return db['CannedAnswer'][_0xffc1('0x6e')](_0x4bddd7[_0xffc1('0x62')]);}})['then'](respondWithResult(_0x4a5764,null))[_0xffc1('0x1a')](handleError(_0x4a5764,null));};exports[_0xffc1('0x6f')]=function(_0x419244,_0x5eb03f){var _0x4f1f2a={'raw':![],'where':{}};var _0xde9c9d={};var _0x39e750={'count':0x0,'rows':[]};return db['MailAccount']['findOne']({'where':{'id':_0x419244[_0xffc1('0x5e')]['id']}})['then'](handleEntityNotFound(_0x5eb03f,null))['then'](function(_0x4d40e7){if(_0x4d40e7){_0xde9c9d[_0xffc1('0x4d')]=_[_0xffc1('0x4f')](db[_0xffc1('0x70')][_0xffc1('0x4a')]);_0xde9c9d[_0xffc1('0x4e')]=_['keys'](_0x419244[_0xffc1('0x4e')]);_0xde9c9d['filters']=_['intersection'](_0xde9c9d[_0xffc1('0x4d')],_0xde9c9d[_0xffc1('0x4e')]);_0x4f1f2a[_0xffc1('0x52')]=_['intersection'](_0xde9c9d[_0xffc1('0x4d')],qs['fields'](_0x419244[_0xffc1('0x4e')][_0xffc1('0x53')]));_0x4f1f2a[_0xffc1('0x52')]=_0x4f1f2a[_0xffc1('0x52')][_0xffc1('0x5f')]?_0x4f1f2a[_0xffc1('0x52')]:_0xde9c9d[_0xffc1('0x4d')];if(!_0x419244['query'][_0xffc1('0x68')](_0xffc1('0x69'))){_0x4f1f2a[_0xffc1('0x21')]=qs[_0xffc1('0x21')](_0x419244[_0xffc1('0x4e')]['limit']);_0x4f1f2a['offset']=qs['offset'](_0x419244['query']['offset']);}_0x4f1f2a[_0xffc1('0x54')]=qs[_0xffc1('0x55')](_0x419244['query']['sort']);_0x4f1f2a[_0xffc1('0x56')]=qs[_0xffc1('0x50')](_['pick'](_0x419244[_0xffc1('0x4e')],_0xde9c9d['filters']));_0x4f1f2a[_0xffc1('0x56')]['MailAccountId']=_0x4d40e7['id'];if(_0x419244['query']['filter']){_0x4f1f2a[_0xffc1('0x56')]=_[_0xffc1('0x59')](_0x4f1f2a[_0xffc1('0x56')],{'$or':_[_0xffc1('0x48')](_0x4f1f2a[_0xffc1('0x52')],function(_0x5cdab6){var _0x178916={};_0x178916[_0x5cdab6]={'$like':'%'+_0x419244['query'][_0xffc1('0x58')]+'%'};return _0x178916;})});}_0x4f1f2a=_[_0xffc1('0x59')]({},_0x4f1f2a,_0x419244[_0xffc1('0x5b')]);return db['CannedAnswer']['count']({'where':_0x4f1f2a['where']})[_0xffc1('0x11')](function(_0x4d1a6c){_0x39e750[_0xffc1('0x22')]=_0x4d1a6c;if(_0x419244[_0xffc1('0x4e')]['includeAll']){_0x4f1f2a['include']=[{'all':!![]}];}return db[_0xffc1('0x70')][_0xffc1('0x5d')](_0x4f1f2a);})[_0xffc1('0x11')](function(_0x4840ca){_0x39e750[_0xffc1('0x6a')]=_0x4840ca;return _0x39e750;});}})[_0xffc1('0x11')](respondWithFilteredResult(_0x5eb03f,_0x4f1f2a))['catch'](handleError(_0x5eb03f,null));};exports[_0xffc1('0x71')]=function(_0xd9cc3b,_0xc5de9a){return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0xd9cc3b[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0xc5de9a,null))[_0xffc1('0x11')](function(_0x2396a9){if(_0x2396a9){return _0x2396a9[_0xffc1('0x71')](_0xd9cc3b[_0xffc1('0x4e')][_0xffc1('0x6c')]);}})['then'](respondWithStatusCode(_0xc5de9a,null))['catch'](handleError(_0xc5de9a,null));};exports[_0xffc1('0x6e')]=function(_0x5002d3,_0x3ac490){var _0x55b8fd={'name':_0x5002d3[_0xffc1('0x62')]['name'],'key':_0x5002d3[_0xffc1('0x62')][_0xffc1('0x72')],'provider':_0x5002d3[_0xffc1('0x62')][_0xffc1('0x63')]===_0xffc1('0x73')?null:_0x5002d3[_0xffc1('0x62')][_0xffc1('0x63')],'email':_0x5002d3['body'][_0xffc1('0x74')],'ListId':_0x5002d3[_0xffc1('0x62')][_0xffc1('0x75')],'active':!_[_0xffc1('0x76')](_0x5002d3['body'][_0xffc1('0x77')])?_0x5002d3[_0xffc1('0x62')][_0xffc1('0x77')]:!![],'description':_0x5002d3[_0xffc1('0x62')]['description']||null};var _0x4a334b=getAuthenticationOptions(_0x55b8fd[_0xffc1('0x63')],_0x5002d3[_0xffc1('0x62')]);_0x55b8fd['Imap']=_0x4a334b['Imap'];_0x55b8fd[_0xffc1('0x42')]=_0x4a334b[_0xffc1('0x42')];return db[_0xffc1('0x49')][_0xffc1('0x6e')](_0x55b8fd,{'include':[{'model':db[_0xffc1('0x45')],'as':_0xffc1('0x42')},{'model':db['MailServerIn'],'as':_0xffc1('0x41')}]})['then'](function(_0x48ba5e){var _0x39e9be=_0x5002d3['user'][_0xffc1('0x27')]({'plain':!![]});if(!_0x39e9be)throw new Error(_0xffc1('0x78'));if(_0x39e9be['role']===_0xffc1('0x36')){var _0x4f56a7=_0x48ba5e[_0xffc1('0x27')]({'plain':!![]});return db[_0xffc1('0x79')][_0xffc1('0x61')]({'where':{'name':_0xffc1('0x28'),'userProfileId':_0x39e9be['userProfileId']},'raw':!![]})['then'](function(_0x654dd7){if(_0x654dd7&&_0x654dd7[_0xffc1('0x7a')]===0x0){return db[_0xffc1('0x29')][_0xffc1('0x6e')]({'name':_0x4f56a7[_0xffc1('0x2b')],'resourceId':_0x4f56a7['id'],'type':_0x654dd7[_0xffc1('0x2b')],'sectionId':_0x654dd7['id']},{})[_0xffc1('0x11')](function(){return _0x48ba5e;});}else{return _0x48ba5e;}})[_0xffc1('0x1a')](function(_0x56ea6e){logger[_0xffc1('0x17')](_0xffc1('0x7b'),_0x56ea6e);throw _0x56ea6e;});}return _0x48ba5e;})[_0xffc1('0x11')](respondWithResult(_0x3ac490,0xc9))['catch'](handleError(_0x3ac490,null));};exports[_0xffc1('0x7c')]=function(_0x49978c,_0x4695eb){return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x49978c[_0xffc1('0x5e')]['id']}})['then'](handleEntityNotFound(_0x4695eb,null))['then'](function(_0x182dcd){if(_0x182dcd){return _0x182dcd['getImap']();}return null;})[_0xffc1('0x11')](respondWithResult(_0x4695eb,null))['catch'](handleError(_0x4695eb,null));};exports[_0xffc1('0x7e')]=function(_0x3effe4,_0x497593){if(_0x3effe4[_0xffc1('0x62')]['id']){delete _0x3effe4['body']['id'];}return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0x3effe4[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x497593,null))[_0xffc1('0x11')](function(_0x442d2d){if(_0x442d2d){_0x3effe4[_0xffc1('0x62')][_0xffc1('0x66')]=_0x442d2d['id'];return db[_0xffc1('0x44')][_0xffc1('0x6e')](_0x3effe4[_0xffc1('0x62')]);}})[_0xffc1('0x11')](respondWithResult(_0x497593,null))[_0xffc1('0x1a')](handleError(_0x497593,null));};exports[_0xffc1('0x7f')]=function(_0x1aa38b,_0x53015b){return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x1aa38b[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x53015b,null))[_0xffc1('0x11')](function(_0x57951d){if(_0x57951d){return _0x57951d[_0xffc1('0x80')](null);}return null;})[_0xffc1('0x11')](respondWithStatusCode(_0x53015b,null))[_0xffc1('0x1a')](handleError(_0x53015b,null));};exports[_0xffc1('0x81')]=function(_0x3a4438,_0x22a04d){return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x3a4438[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x22a04d,null))[_0xffc1('0x11')](function(_0x4b55f1){if(_0x4b55f1){return _0x4b55f1[_0xffc1('0x81')]();}return null;})[_0xffc1('0x11')](respondWithResult(_0x22a04d,null))['catch'](handleError(_0x22a04d,null));};exports['addSmtp']=function(_0x492fe2,_0x38dc1e){if(_0x492fe2[_0xffc1('0x62')]['id']){delete _0x492fe2[_0xffc1('0x62')]['id'];}return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0x492fe2[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x38dc1e,null))['then'](function(_0x283ff6){if(_0x283ff6){_0x492fe2[_0xffc1('0x62')][_0xffc1('0x66')]=_0x283ff6['id'];return db[_0xffc1('0x45')][_0xffc1('0x6e')](_0x492fe2['body']);}})[_0xffc1('0x11')](respondWithResult(_0x38dc1e,null))[_0xffc1('0x1a')](handleError(_0x38dc1e,null));};exports[_0xffc1('0x82')]=function(_0x7ab2ce,_0x225292){return db['MailAccount'][_0xffc1('0x7d')]({'where':{'id':_0x7ab2ce[_0xffc1('0x5e')]['id']}})['then'](handleEntityNotFound(_0x225292,null))[_0xffc1('0x11')](function(_0x43fb3c){if(_0x43fb3c){return _0x43fb3c['setSmtp'](null);}return null;})[_0xffc1('0x11')](respondWithStatusCode(_0x225292,null))[_0xffc1('0x1a')](handleError(_0x225292,null));};exports['addInteraction']=function(_0x5a6ff2,_0x3be7f0){return db[_0xffc1('0x83')][_0xffc1('0x61')]({'where':{'id':_0x5a6ff2[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x3be7f0,null))[_0xffc1('0x11')](function(_0x24fd73){if(_0x24fd73){return _0x24fd73[_0xffc1('0x84')](_0x5a6ff2[_0xffc1('0x62')]['ids'],_[_0xffc1('0x85')](_0x5a6ff2[_0xffc1('0x62')],[_0xffc1('0x6c'),'id'])||{});}})[_0xffc1('0x11')](respondWithResult(_0x3be7f0,null))[_0xffc1('0x1a')](handleError(_0x3be7f0,null));};exports[_0xffc1('0x86')]=function(_0x3dedda,_0x3abe2d){var _0xd3d8bd={'raw':![],'where':{}};var _0x4e6da5={};var _0x48d706={'count':0x0,'rows':[]};return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x3dedda[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x3abe2d,null))['then'](function(_0x2b21a7){if(_0x2b21a7){_0x4e6da5[_0xffc1('0x4d')]=_[_0xffc1('0x4f')](db[_0xffc1('0x83')][_0xffc1('0x4a')]);_0x4e6da5[_0xffc1('0x4e')]=_[_0xffc1('0x4f')](_0x3dedda[_0xffc1('0x4e')]);_0x4e6da5[_0xffc1('0x50')]=_[_0xffc1('0x51')](_0x4e6da5[_0xffc1('0x4d')],_0x4e6da5[_0xffc1('0x4e')]);_0xd3d8bd['attributes']=_[_0xffc1('0x51')](_0x4e6da5[_0xffc1('0x4d')],qs[_0xffc1('0x53')](_0x3dedda[_0xffc1('0x4e')][_0xffc1('0x53')]));_0xd3d8bd[_0xffc1('0x52')]=_0xd3d8bd[_0xffc1('0x52')][_0xffc1('0x5f')]?_0xd3d8bd[_0xffc1('0x52')]:_0x4e6da5[_0xffc1('0x4d')];if(!_0x3dedda[_0xffc1('0x4e')][_0xffc1('0x68')](_0xffc1('0x69'))){_0xd3d8bd[_0xffc1('0x21')]=qs['limit'](_0x3dedda[_0xffc1('0x4e')][_0xffc1('0x21')]);_0xd3d8bd[_0xffc1('0x1f')]=qs[_0xffc1('0x1f')](_0x3dedda['query']['offset']);}_0xd3d8bd[_0xffc1('0x54')]=qs[_0xffc1('0x55')](_0x3dedda['query'][_0xffc1('0x55')]);_0xd3d8bd[_0xffc1('0x56')]=qs[_0xffc1('0x50')](_['pick'](_0x3dedda['query'],_0x4e6da5[_0xffc1('0x50')]));_0xd3d8bd['where'][_0xffc1('0x66')]=_0x2b21a7['id'];if(_0x3dedda['query']['filter']){_0xd3d8bd[_0xffc1('0x56')]=_[_0xffc1('0x59')](_0xd3d8bd[_0xffc1('0x56')],{'$or':_[_0xffc1('0x48')](_0xd3d8bd['attributes'],function(_0x19d9b3){var _0x48111d={};_0x48111d[_0x19d9b3]={'$like':'%'+_0x3dedda['query'][_0xffc1('0x58')]+'%'};return _0x48111d;})});}_0xd3d8bd=_['merge']({},_0xd3d8bd,_0x3dedda[_0xffc1('0x5b')]);return db['MailInteraction'][_0xffc1('0x22')]({'where':_0xd3d8bd[_0xffc1('0x56')]})[_0xffc1('0x11')](function(_0x260938){_0x48d706[_0xffc1('0x22')]=_0x260938;if(_0x3dedda[_0xffc1('0x4e')][_0xffc1('0x5c')]){_0xd3d8bd[_0xffc1('0x60')]=[{'model':db[_0xffc1('0x87')],'as':_0xffc1('0x88'),'required':![]},{'model':db[_0xffc1('0x89')],'as':_0xffc1('0x8a'),'attributes':[_0xffc1('0x2b'),_0xffc1('0x8b'),_0xffc1('0x8c')],'required':![]},{'model':db[_0xffc1('0x8d')],'as':'Tags','attributes':['id',_0xffc1('0x2b'),_0xffc1('0x8e')],'where':_0x3dedda[_0xffc1('0x4e')][_0xffc1('0x8f')]?{'id':_0x3dedda[_0xffc1('0x4e')][_0xffc1('0x8f')]}:undefined,'required':_0x3dedda['query'][_0xffc1('0x8f')]?!![]:![]}];}return db[_0xffc1('0x83')][_0xffc1('0x5d')](_0xd3d8bd);})[_0xffc1('0x11')](function(_0x3d3aa9){_0x48d706[_0xffc1('0x6a')]=_0x3d3aa9;return _0x48d706;});}})['then'](respondWithFilteredResult(_0x3abe2d,_0xd3d8bd))[_0xffc1('0x1a')](handleError(_0x3abe2d,null));};exports[_0xffc1('0x90')]=function addApplications(_0x334fdd,_0x4ebdd9){var _0x50c6bd=_0x334fdd['params']['id'];var _0x245de6=_0x334fdd[_0xffc1('0x62')];var _0x29f98a=0xc8;var _0x55d1eb=null;return db[_0xffc1('0x91')][_0xffc1('0x92')]({'isolationLevel':db[_0xffc1('0x91')][_0xffc1('0x93')]['ISOLATION_LEVELS'][_0xffc1('0x94')]},function(_0x48214e){return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x50c6bd},'transaction':_0x48214e})[_0xffc1('0x11')](function(_0x122830){if(_0x122830){return db[_0xffc1('0x95')]['destroy']({'where':{'MailAccountId':_0x50c6bd},'transaction':_0x48214e})[_0xffc1('0x11')](function(){var _0x44e8e1=_[_0xffc1('0x48')](_0x245de6,function(_0xd3427b){_0xd3427b[_0xffc1('0x66')]=_0x50c6bd;return _0xd3427b;});return db['MailApplication'][_0xffc1('0x96')](_0x44e8e1,{'transaction':_0x48214e});});}else{_0x29f98a=0x194;_0x55d1eb=[];}});})[_0xffc1('0x11')](function(){if(_0x29f98a!==0x194){return db['MailApplication'][_0xffc1('0x97')]({'where':{'MailAccountId':_0x50c6bd},'order':_0xffc1('0x98')})[_0xffc1('0x11')](function(_0x48d0c2){_0x55d1eb=_0x48d0c2;});}})['catch'](function(_0x5b0901){_0x29f98a=0x1f4;logger[_0xffc1('0x17')](_0x5b0901[_0xffc1('0x2a')]);if(_0x5b0901[_0xffc1('0x2b')]){delete _0x5b0901[_0xffc1('0x2b')];}_0x55d1eb=_0x5b0901;})[_0xffc1('0x99')](function(){if(_0x55d1eb===null){_0x4ebdd9[_0xffc1('0x1b')](_0x29f98a);}else{if(_0x29f98a===0x1f4){_0x4ebdd9[_0xffc1('0x1c')](_0x29f98a)[_0xffc1('0x9a')](_0x55d1eb);}else{_0x4ebdd9[_0xffc1('0x1c')](_0x29f98a)[_0xffc1('0x1e')](_0x55d1eb);}}});};exports[_0xffc1('0x9b')]=function(_0x1f9e4b,_0x3823c3){var _0x5598ec={};var _0x3bc6f1={};var _0x199f86;var _0x365cfd;return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x1f9e4b[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x3823c3,null))[_0xffc1('0x11')](function(_0x161d44){if(_0x161d44){_0x199f86=_0x161d44;_0x3bc6f1['model']=_[_0xffc1('0x4f')](db['MailApplication']['rawAttributes']);_0x3bc6f1[_0xffc1('0x4e')]=_['keys'](_0x1f9e4b[_0xffc1('0x4e')]);_0x3bc6f1[_0xffc1('0x50')]=_[_0xffc1('0x51')](_0x3bc6f1[_0xffc1('0x4d')],_0x3bc6f1[_0xffc1('0x4e')]);_0x5598ec[_0xffc1('0x52')]=_['intersection'](_0x3bc6f1[_0xffc1('0x4d')],qs['fields'](_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x53')]));_0x5598ec['attributes']=_0x5598ec[_0xffc1('0x52')][_0xffc1('0x5f')]?_0x5598ec[_0xffc1('0x52')]:_0x3bc6f1[_0xffc1('0x4d')];_0x5598ec[_0xffc1('0x54')]=qs[_0xffc1('0x55')](_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x55')]);_0x5598ec[_0xffc1('0x56')]=qs['filters'](_[_0xffc1('0x57')](_0x1f9e4b[_0xffc1('0x4e')],_0x3bc6f1['filters']));if(_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x58')]){_0x5598ec['where']=_[_0xffc1('0x59')](_0x5598ec[_0xffc1('0x56')],{'$or':_[_0xffc1('0x48')](_0x5598ec[_0xffc1('0x52')],function(_0x1ef123){var _0x5a0ace={};_0x5a0ace[_0x1ef123]={'$like':'%'+_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x58')]+'%'};return _0x5a0ace;})});}_0x5598ec=_[_0xffc1('0x59')]({},_0x5598ec,_0x1f9e4b['options']);return _0x199f86['getApplications'](_0x5598ec);}})[_0xffc1('0x11')](function(_0x4454b5){if(_0x4454b5){_0x365cfd=_0x4454b5[_0xffc1('0x5f')];if(!_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x68')](_0xffc1('0x69'))){_0x5598ec[_0xffc1('0x21')]=qs['limit'](_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x21')]);_0x5598ec[_0xffc1('0x1f')]=qs['offset'](_0x1f9e4b[_0xffc1('0x4e')][_0xffc1('0x1f')]);}return _0x199f86[_0xffc1('0x9b')](_0x5598ec);}})[_0xffc1('0x11')](function(_0x351f6a){if(_0x351f6a){return _0x351f6a?{'count':_0x365cfd,'rows':_0x351f6a}:null;}})[_0xffc1('0x11')](respondWithResult(_0x3823c3,null))[_0xffc1('0x1a')](handleError(_0x3823c3,null));};exports[_0xffc1('0x9c')]=function(_0x1594f0,_0x5b65e5){var _0x1d5f71={'raw':!![],'where':{}};var _0x5eecd0={};var _0x3e4a48={'count':0x0,'rows':[]};return db['MailAccount']['findOne']({'where':{'id':_0x1594f0[_0xffc1('0x5e')]['id']}})['then'](handleEntityNotFound(_0x5b65e5,null))['then'](function(_0x3f6961){if(_0x3f6961){_0x5eecd0[_0xffc1('0x4d')]=_[_0xffc1('0x4f')](db[_0xffc1('0x9d')][_0xffc1('0x4a')]);_0x5eecd0['query']=_['keys'](_0x1594f0[_0xffc1('0x4e')]);_0x5eecd0[_0xffc1('0x50')]=_['intersection'](_0x5eecd0['model'],_0x5eecd0['query']);_0x1d5f71['attributes']=_['intersection'](_0x5eecd0['model'],qs[_0xffc1('0x53')](_0x1594f0['query'][_0xffc1('0x53')]));_0x1d5f71[_0xffc1('0x52')]=_0x1d5f71['attributes'][_0xffc1('0x5f')]?_0x1d5f71[_0xffc1('0x52')]:_0x5eecd0['model'];if(!_0x1594f0[_0xffc1('0x4e')][_0xffc1('0x68')]('nolimit')){_0x1d5f71[_0xffc1('0x21')]=qs['limit'](_0x1594f0[_0xffc1('0x4e')][_0xffc1('0x21')]);_0x1d5f71[_0xffc1('0x1f')]=qs[_0xffc1('0x1f')](_0x1594f0[_0xffc1('0x4e')][_0xffc1('0x1f')]);}_0x1d5f71['order']=qs['sort'](_0x1594f0[_0xffc1('0x4e')][_0xffc1('0x55')]);_0x1d5f71[_0xffc1('0x56')]=qs['filters'](_[_0xffc1('0x57')](_0x1594f0[_0xffc1('0x4e')],_0x5eecd0['filters']));_0x1d5f71[_0xffc1('0x56')][_0xffc1('0x66')]=_0x3f6961['id'];if(_0x1594f0[_0xffc1('0x4e')]['filter']){_0x1d5f71[_0xffc1('0x56')]=_['merge'](_0x1d5f71[_0xffc1('0x56')],{'$or':_[_0xffc1('0x48')](_0x1d5f71[_0xffc1('0x52')],function(_0xf804d5){var _0x356dec={};_0x356dec[_0xf804d5]={'$like':'%'+_0x1594f0['query'][_0xffc1('0x58')]+'%'};return _0x356dec;})});}if(_0x1594f0[_0xffc1('0x4e')]['$gte']){var _0x1b0e37=_0x1594f0[_0xffc1('0x4e')][_0xffc1('0x9e')][_0xffc1('0x9f')](',');var _0x14f895={};_0x14f895[_0x1b0e37[0x0]]={'$gte':moment(_0x1b0e37[0x1])['format'](_0xffc1('0xa0'))};_0x1d5f71['where']=_[_0xffc1('0x59')](_0x1d5f71[_0xffc1('0x56')],_0x14f895);}_0x1d5f71=_['merge']({},_0x1d5f71,_0x1594f0[_0xffc1('0x5b')]);return db[_0xffc1('0x9d')][_0xffc1('0x22')]({'where':_0x1d5f71[_0xffc1('0x56')]})[_0xffc1('0x11')](function(_0x6d051b){_0x3e4a48['count']=_0x6d051b;if(_0x1594f0['query'][_0xffc1('0x5c')]){_0x1d5f71['include']=[{'all':!![]}];}return db[_0xffc1('0x9d')][_0xffc1('0x5d')](_0x1d5f71);})['then'](function(_0x1f5ad8){if(_0x1594f0[_0xffc1('0x4e')][_0xffc1('0xa1')]==='true'){for(var _0x251bd2=0x0;_0x251bd2<_0x1f5ad8['length'];_0x251bd2++){_0x1f5ad8[_0x251bd2][_0xffc1('0x62')]=juice(_0x1f5ad8[_0x251bd2]['body']);}}_0x3e4a48[_0xffc1('0x6a')]=_0x1f5ad8;return _0x3e4a48;});}})[_0xffc1('0x11')](respondWithFilteredResult(_0x5b65e5,_0x1d5f71))['catch'](handleError(_0x5b65e5,null));};exports['send']=function(_0x135d37,_0x196681){var _0xd21c6a,_0x281398={},_0x44f99d;if(_0x135d37['body']['to']){_0x44f99d=_0x135d37[_0xffc1('0x62')]['to'][_0xffc1('0x9f')](',')[0x0];}return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0x135d37[_0xffc1('0x5e')]['id']},'include':[{'model':db[_0xffc1('0xa2')],'as':_0xffc1('0xa3'),'include':[{'model':db['CmContact'],'as':_0xffc1('0xa4'),'where':{'email':_0x44f99d},'limit':0x1,'order':[[_0xffc1('0xa5'),_0xffc1('0xa6')]]}]},{'model':db[_0xffc1('0x45')],'as':_0xffc1('0x42'),'raw':!![],'include':[{'model':db[_0xffc1('0xa7')],'as':_0xffc1('0xa7'),'raw':!![]}]}]})[_0xffc1('0x11')](handleEntityNotFound(_0x196681,null))[_0xffc1('0x11')](function(_0x355166){if(!_0x355166)return;_0xd21c6a=_0x355166;if(!_0x355166[_0xffc1('0x42')])throw new Error('Smtp\x20configuration\x20not\x20found');if(_[_0xffc1('0x76')](_0x135d37[_0xffc1('0x62')]['from'])){_0x135d37[_0xffc1('0x62')][_0xffc1('0xa8')]=util[_0xffc1('0xa9')](_0xffc1('0xaa'),_0x355166[_0xffc1('0x2b')],_0x355166[_0xffc1('0x74')]||_0x355166[_0xffc1('0x42')][_0xffc1('0x36')]);}if(_0x135d37[_0xffc1('0x62')][_0xffc1('0xab')]&&_0x135d37['body']['attachments'][_0xffc1('0x5f')]){for(var _0x32b8d7=0x0;_0x32b8d7<_0x135d37[_0xffc1('0x62')][_0xffc1('0xab')][_0xffc1('0x5f')];_0x32b8d7+=0x1){_0x135d37[_0xffc1('0x62')]['attachments'][_0x32b8d7]={'filename':_0x135d37[_0xffc1('0x62')][_0xffc1('0xab')][_0x32b8d7][_0xffc1('0x2b')],'path':path[_0xffc1('0xac')](config['root'],'server/files/attachments',_0x135d37[_0xffc1('0x62')][_0xffc1('0xab')][_0x32b8d7][_0xffc1('0xad')])};}}var _0x454676=_0x355166[_0xffc1('0xae')]();return respondWithRpcPromise(_0xffc1('0xaf'),'sendMail',{'account':_0x454676,'message':_0x135d37[_0xffc1('0x62')]});})[_0xffc1('0x11')](function(_0x29c956){if(!_0x29c956)return;_0x281398=_0x29c956;if(!_0xd21c6a['List'])throw new Error(_0xffc1('0xb0'));if(_0xd21c6a[_0xffc1('0xa3')][_0xffc1('0xa4')][_0xffc1('0x5f')])return _0xd21c6a[_0xffc1('0xa3')][_0xffc1('0xa4')][0x0];return db[_0xffc1('0x87')]['create'](_[_0xffc1('0xb')](_0x135d37['body'],{'firstName':_0x135d37[_0xffc1('0x62')]['to'],'email':_0x135d37[_0xffc1('0x62')]['to'],'phone':_0x135d37[_0xffc1('0x62')]['to'],'ListId':_0xd21c6a[_0xffc1('0x75')]}));})['then'](function(_0x51b455){if(!_0x51b455)return;var _0x52ec23={'UserId':_0x135d37[_0xffc1('0x36')]['id'],'ContactId':_0x51b455['id'],'MailAccountId':_0xd21c6a['id'],'inReplyTo':_0x281398[_0xffc1('0xb1')],'to':_0x135d37['body'][_0xffc1('0xa8')],'cc':_0x135d37[_0xffc1('0x62')]['cc'],'subject':_0x135d37[_0xffc1('0x62')][_0xffc1('0xb2')],'attach':_0x135d37[_0xffc1('0x62')][_0xffc1('0xb3')],'firstMsgDirection':'out','lastMsgAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'lastMsgDirection':_0xffc1('0xb4'),'Messages':[_[_0xffc1('0x59')](_0x135d37['body'],{'messageId':_0x281398['messageId'],'sentAt':moment()[_0xffc1('0xa9')](_0xffc1('0xa0')),'read':!![],'MailAccountId':_0xd21c6a['id'],'UserId':_0x135d37[_0xffc1('0x36')]['id'],'ContactId':_0x51b455['id']})]};if(_0x135d37[_0xffc1('0x62')]['Interaction']){_0x52ec23=_[_0xffc1('0x59')](_0x52ec23,_0x135d37[_0xffc1('0x62')]['Interaction']);}return db['MailInteraction']['create'](_0x52ec23,{'include':[{'model':db[_0xffc1('0x9d')],'as':_0xffc1('0xb5')}]});})[_0xffc1('0x11')](respondWithResult(_0x196681,null))[_0xffc1('0x1a')](handleError(_0x196681,null));};exports['verifySmtp']=function(_0x459ebd,_0x3f2c41){return db['MailAccount']['find']({'where':{'id':_0x459ebd[_0xffc1('0x5e')]['id']},'include':[{'model':db['MailServerOut'],'as':_0xffc1('0x42'),'raw':!![],'include':[{'model':db[_0xffc1('0xa7')],'as':_0xffc1('0xa7'),'raw':!![]}]}]})['then'](handleEntityNotFound(_0x3f2c41,null))['then'](function(_0x4a6fc8){if(!_0x4a6fc8[_0xffc1('0x42')])return;var _0x5ea7de=_0x4a6fc8[_0xffc1('0xae')]();return respondWithRpcPromise(_0xffc1('0xb6'),_0xffc1('0xb7'),{'account':_0x5ea7de});})[_0xffc1('0x11')](respondWithResult(_0x3f2c41,null))[_0xffc1('0x1a')](handleError(_0x3f2c41,null));};exports[_0xffc1('0x26')]=function(_0x26cdef,_0x531654){return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0x26cdef[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x531654,null))[_0xffc1('0x11')](function(_0x872a41){if(_0x872a41&&_0x872a41[_0xffc1('0x3f')]){throw new db[(_0xffc1('0xb8'))][(_0xffc1('0xb9'))]('You\x20can\x27t\x20remove\x20the\x20mail\x20service\x20account');}return _0x872a41;})[_0xffc1('0x11')](removeEntity(_0x531654,null))['catch'](handleError(_0x531654,null));};exports['addAgents']=function(_0x3f7089,_0x169e9b){return db['MailAccount'][_0xffc1('0x61')]({'where':{'id':_0x3f7089['params']['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x169e9b,null))[_0xffc1('0x11')](function(_0xf5bd36){if(_0xf5bd36){return _0xf5bd36['addAgents'](_0x3f7089[_0xffc1('0x62')][_0xffc1('0x6c')],_[_0xffc1('0x85')](_0x3f7089[_0xffc1('0x62')],[_0xffc1('0x6c'),'id'])||{})[_0xffc1('0xba')](function(_0xe60b22){for(var _0x34eb4d=0x0;_0x34eb4d<_0x3f7089[_0xffc1('0x62')][_0xffc1('0x6c')][_0xffc1('0x5f')];_0x34eb4d+=0x1){socket[_0xffc1('0xbb')](_0xffc1('0xbc'),{'UserId':Number(_0x3f7089[_0xffc1('0x62')][_0xffc1('0x6c')][_0x34eb4d]),'MailAccountId':Number(_0x3f7089[_0xffc1('0x5e')]['id'])});}return _0xe60b22;});}})['then'](respondWithResult(_0x169e9b,null))['catch'](handleError(_0x169e9b,null));};exports[_0xffc1('0xbd')]=function(_0x278181,_0x3f2c93){return db[_0xffc1('0x49')][_0xffc1('0x61')]({'where':{'id':_0x278181[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x3f2c93,null))[_0xffc1('0x11')](function(_0x5ac8e6){if(_0x5ac8e6){return _0x5ac8e6[_0xffc1('0xbd')](_0x278181['query'][_0xffc1('0x6c')])[_0xffc1('0x11')](function(){if(_[_0xffc1('0xbe')](_0x278181[_0xffc1('0x4e')][_0xffc1('0x6c')])){for(var _0x587f3b=0x0;_0x587f3b<_0x278181[_0xffc1('0x4e')][_0xffc1('0x6c')][_0xffc1('0x5f')];_0x587f3b+=0x1){socket[_0xffc1('0xbb')](_0xffc1('0xbf'),{'UserId':Number(_0x278181[_0xffc1('0x4e')]['ids'][_0x587f3b]),'MailAccountId':Number(_0x278181['params']['id'])});}}else{socket[_0xffc1('0xbb')](_0xffc1('0xbf'),{'UserId':Number(_0x278181[_0xffc1('0x4e')][_0xffc1('0x6c')]),'MailAccountId':Number(_0x278181[_0xffc1('0x5e')]['id'])});}});}})[_0xffc1('0x11')](respondWithStatusCode(_0x3f2c93,null))[_0xffc1('0x1a')](handleError(_0x3f2c93,null));};exports['getAgents']=function(_0x57f054,_0x73ae71){var _0x4c45f7={};var _0x3ca59d={};var _0xcc2f17;var _0x21a81a;return db[_0xffc1('0x49')][_0xffc1('0x7d')]({'where':{'id':_0x57f054[_0xffc1('0x5e')]['id']}})[_0xffc1('0x11')](handleEntityNotFound(_0x73ae71,null))['then'](function(_0x3e3ff8){if(_0x3e3ff8){_0xcc2f17=_0x3e3ff8;_0x3ca59d[_0xffc1('0x4d')]=_[_0xffc1('0x4f')](db[_0xffc1('0x89')][_0xffc1('0x4a')]);_0x3ca59d['query']=_[_0xffc1('0x4f')](_0x57f054[_0xffc1('0x4e')]);_0x3ca59d['filters']=_[_0xffc1('0x51')](_0x3ca59d[_0xffc1('0x4d')],_0x3ca59d[_0xffc1('0x4e')]);_0x4c45f7['attributes']=_[_0xffc1('0x51')](_0x3ca59d[_0xffc1('0x4d')],qs[_0xffc1('0x53')](_0x57f054[_0xffc1('0x4e')]['fields']));_0x4c45f7[_0xffc1('0x52')]=_0x4c45f7[_0xffc1('0x52')]['length']?_0x4c45f7['attributes']:_0x3ca59d[_0xffc1('0x4d')];_0x4c45f7[_0xffc1('0x54')]=qs[_0xffc1('0x55')](_0x57f054['query']['sort']);_0x4c45f7[_0xffc1('0x56')]=qs[_0xffc1('0x50')](_[_0xffc1('0x57')](_0x57f054['query'],_0x3ca59d[_0xffc1('0x50')]));if(_0x57f054[_0xffc1('0x4e')][_0xffc1('0x58')]){_0x4c45f7[_0xffc1('0x56')]=_[_0xffc1('0x59')](_0x4c45f7[_0xffc1('0x56')],{'$or':_[_0xffc1('0x48')](_0x4c45f7['attributes'],function(_0x45776a){var _0x4d6364={};_0x4d6364[_0x45776a]={'$like':'%'+_0x57f054[_0xffc1('0x4e')][_0xffc1('0x58')]+'%'};return _0x4d6364;})});}_0x4c45f7=_[_0xffc1('0x59')]({},_0x4c45f7,_0x57f054['options']);return _0xcc2f17[_0xffc1('0xc0')](_0x4c45f7);}})[_0xffc1('0x11')](function(_0x136e46){if(_0x136e46){_0x21a81a=_0x136e46[_0xffc1('0x5f')];if(!_0x57f054[_0xffc1('0x4e')]['hasOwnProperty']('nolimit')){_0x4c45f7[_0xffc1('0x21')]=qs[_0xffc1('0x21')](_0x57f054[_0xffc1('0x4e')][_0xffc1('0x21')]);_0x4c45f7['offset']=qs['offset'](_0x57f054['query']['offset']);}return _0xcc2f17['getAgents'](_0x4c45f7);}})[_0xffc1('0x11')](function(_0xf9775b){if(_0xf9775b){return _0xf9775b?{'count':_0x21a81a,'rows':_0xf9775b}:null;}})['then'](respondWithResult(_0x73ae71,null))[_0xffc1('0x1a')](handleError(_0x73ae71,null));};