7c78451a46a2dbadc6c5cadd199329ac02cd23fd
[motion2.git] / server / api / mailInteraction / mailInteraction.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 _0x66cf=['intersection','fields','length','hasOwnProperty','nolimit','order','sort','pick','field','color','from','tools_tags','select','u.id','ui.MailInteractionId','left_join','users','u.id\x20=\x20ui.UserId','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','o.id\x20=\x20i.UserId','mail_accounts','a.id\x20=\x20i.MailAccountId','mail_messages','it.MailInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','search','parseSearch','sqlOperator','conditions','find','having','`\x20=\x200','`\x20>\x200','Contact','operator','$substring','value','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','getFullTextValue','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','ContactId','buildExpression','text','start','union','User','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','UserId','plainBody','some','compact','isEmpty','MailAccountId','where','i.MailAccountId\x20IN\x20?','concat','createdAt','parse','$gte','$lte','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','null','\x20IS\x20NULL','read','`unreadMessages`\x20=\x200','tag','isArray','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','filter','replace','\x27\x27\x27\x27','isNumeric','i.id\x20LIKE\x20?','isEmail','c.email\x20LIKE\x20?','MATCH\x20(i.to,\x20i.subject)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','MATCH\x20(me.plainBody,\x20me.subject)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','group','i.id','Sequelize','QueryTypes','clone','unreadMessages','DESC','t.id\x20IN\x20?','resolve','journey','i.MailAccountId\x20IS\x20NOT\x20NULL','getMailAccounts','i.MailAccountId\x20IS\x20NULL','sequelize','toString','keyBy','merge','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','a.key','Account.key','a.mandatoryDisposition','Account.mandatoryDisposition','i.createdAt','i.closed','i.attach','i.lastMsgDirection','i.subject','i.disposition','i.substatus','i.to','i.MailAccountId','i.UserId','Contact.firstName','c.lastName','Contact.lastName','Contact.email','Contact.phone','c.mobile','Contact.mobile','c.fax','o.internal','GROUP_CONCAT(DISTINCT\x20t.id)','ui.MailInteractionId\x20IN\x20?','groupBy','MailInteractionId','enableEmailPreview','all','rows','show','params','includeAll','include','body','describe','MailMessage','addMessage','ids','omit','YYYY-MM-DD\x20HH:mm:ss','findAll','addTags','setTags','Tag','emit','mailInteractionTags:save','removeTags','join','root','server','files','tmp','attachments','format','transcript-%d-%s.zip','Attachment','Attachments','Messages','bcc','subject','get','mkdirSync','each','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','basename','existsSync','readFileSync','build','writeFileSync','message%d.eml','download','log','err','unlinkSync','html-pdf','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','path','sox','lodash','squel','crypto','to-csv','querystring','html-to-text','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./mailInteraction.socket','register','status','end','count','offset','limit','set','Content-Range','json','apply','reject','save','update','then','destroy','sendStatus','stack','name','send','Users','map','fullname','TagIds','Tags','split','forEach','push','MailInteraction','findOne','lastMsgBody','lastMsgText','trimStart','fromString','catch','index','rawAttributes','fieldName','type','key','model','keys','query','filters','attributes'];(function(_0x24c1d0,_0x4dec43){var _0x388583=function(_0x213304){while(--_0x213304){_0x24c1d0['push'](_0x24c1d0['shift']());}};_0x388583(++_0x4dec43);}(_0x66cf,0x1a2));var _0xf66c=function(_0x5bf7e5,_0x4fae8d){_0x5bf7e5=_0x5bf7e5-0x0;var _0x314966=_0x66cf[_0x5bf7e5];return _0x314966;};'use strict';var pdf=require(_0xf66c('0x0'));var emlformat=require('eml-format');var rimraf=require(_0xf66c('0x1'));var zipdir=require(_0xf66c('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf66c('0x3'));var moment=require(_0xf66c('0x4'));var BPromise=require(_0xf66c('0x5'));var Mustache=require(_0xf66c('0x6'));var util=require(_0xf66c('0x7'));var path=require(_0xf66c('0x8'));var sox=require(_0xf66c('0x9'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0xf66c('0xa'));var squel=require(_0xf66c('0xb'));var crypto=require(_0xf66c('0xc'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xf66c('0xd'));var querystring=require(_0xf66c('0xe'));var Papa=require('papaparse');var htmlToText=require(_0xf66c('0xf'));var Redis=require(_0xf66c('0x10'));var authService=require(_0xf66c('0x11'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf66c('0x12'));var logger=require(_0xf66c('0x13'))('api');var utils=require(_0xf66c('0x14'));var config=require(_0xf66c('0x15'));var db=require(_0xf66c('0x16'))['db'];config[_0xf66c('0x17')]=_[_0xf66c('0x18')](config[_0xf66c('0x17')],{'host':_0xf66c('0x19'),'port':0x18eb});var socket=require(_0xf66c('0x1a'))(new Redis(config[_0xf66c('0x17')]));require(_0xf66c('0x1b'))[_0xf66c('0x1c')](socket);function respondWithStatusCode(_0x5536df,_0x377948){_0x377948=_0x377948||0xcc;return function(_0x3c3d90){if(_0x3c3d90){return _0x5536df['sendStatus'](_0x377948);}return _0x5536df[_0xf66c('0x1d')](_0x377948)[_0xf66c('0x1e')]();};}function respondWithResult(_0x475397,_0x18c6bb){_0x18c6bb=_0x18c6bb||0xc8;return function(_0x3d48c9){if(_0x3d48c9){return _0x475397[_0xf66c('0x1d')](_0x18c6bb)['json'](_0x3d48c9);}};}function respondWithFilteredResult(_0x1d5694,_0x1ddc13){return function(_0x2e288a){if(_0x2e288a){var _0x2a6da5=_0x2e288a[_0xf66c('0x1f')],_0x150fb7=_0x1ddc13[_0xf66c('0x20')],_0x3d3ea1=_0x1ddc13[_0xf66c('0x20')]+_0x1ddc13[_0xf66c('0x21')],_0x1f45c6;if(_0x3d3ea1>=_0x2a6da5){_0x3d3ea1=_0x2a6da5;_0x1f45c6=0xc8;}else{_0x1f45c6=0xce;}_0x1d5694[_0xf66c('0x1d')](_0x1f45c6);return _0x1d5694[_0xf66c('0x22')](_0xf66c('0x23'),_0x150fb7+'-'+_0x3d3ea1+'/'+_0x2a6da5)[_0xf66c('0x24')](_0x2e288a);}return null;};}function patchUpdates(_0x343c33){return function(_0x1759a6){try{jsonpatch[_0xf66c('0x25')](_0x1759a6,_0x343c33,!![]);}catch(_0x438a87){return BPromise[_0xf66c('0x26')](_0x438a87);}return _0x1759a6[_0xf66c('0x27')]();};}function saveUpdates(_0x374bc9,_0x1f44ea){return function(_0x5c7d0f){if(_0x5c7d0f){return _0x5c7d0f[_0xf66c('0x28')](_0x374bc9)[_0xf66c('0x29')](function(_0x37a16a){return _0x37a16a;});}return null;};}function removeEntity(_0xd16e48,_0x283b61){return function(_0x1da1a2){if(_0x1da1a2){return _0x1da1a2[_0xf66c('0x2a')]()[_0xf66c('0x29')](function(){_0xd16e48[_0xf66c('0x1d')](0xcc)[_0xf66c('0x1e')]();});}};}function handleEntityNotFound(_0x356b73,_0x16222f){return function(_0x51daf1){if(!_0x51daf1){_0x356b73[_0xf66c('0x2b')](0x194);}return _0x51daf1;};}function handleError(_0xf2f5,_0x441ae7){_0x441ae7=_0x441ae7||0x1f4;return function(_0x24c79e){logger['error'](_0x24c79e[_0xf66c('0x2c')]);if(_0x24c79e[_0xf66c('0x2d')]){delete _0x24c79e[_0xf66c('0x2d')];}_0xf2f5[_0xf66c('0x1d')](_0x441ae7)[_0xf66c('0x2e')](_0x24c79e);};}function getInteractionUsers(_0x33a60c,_0x32b3b7){return new BPromise(function(_0x54f807,_0x963722){try{if(_0x32b3b7[_0x33a60c['id']]){_0x33a60c[_0xf66c('0x2f')]=_[_0xf66c('0x30')](_0x32b3b7[_0x33a60c['id']],function(_0x38b7a6){return{'id':_0x38b7a6['id'],'fullname':_0x38b7a6[_0xf66c('0x31')]};});}}catch(_0x10853b){_0x963722(_0x10853b);}_0x54f807(_0x33a60c);});}function getInteractionTags(_0x474d9f,_0x3c7e76){return new BPromise(function(_0x13ac02,_0x53ddda){try{if(_0x474d9f[_0xf66c('0x32')]){_0x474d9f[_0xf66c('0x33')]=[];_0x474d9f[_0xf66c('0x32')][_0xf66c('0x34')](',')[_0xf66c('0x35')](function(_0x5a799a){_0x474d9f[_0xf66c('0x33')][_0xf66c('0x36')](_0x3c7e76[_0x5a799a]);});}delete _0x474d9f[_0xf66c('0x32')];}catch(_0x1b2e4a){_0x53ddda(_0x1b2e4a);}_0x13ac02(_0x474d9f);});}function getLastMessageText(_0x3f1ab7){return new BPromise(function(_0x47c6aa,_0x502907){return db[_0xf66c('0x37')][_0xf66c('0x38')]({'where':{'id':_0x3f1ab7['id']},'attributes':[_0xf66c('0x39'),_0xf66c('0x3a')],'raw':!![]})['then'](function(_0x3a5259){if(!_0x3a5259[_0xf66c('0x3a')]){_0x3f1ab7[_0xf66c('0x3a')]=_[_0xf66c('0x3b')](htmlToText[_0xf66c('0x3c')](_0x3a5259[_0xf66c('0x39')],{'preserveNewlines':!![],'singleNewLineParagraphs':!![],'uppercaseHeadings':![],'noLinkBrackets':!![],'ignoreImage':!![]}));}else{_0x3f1ab7['lastMsgText']=_0x3a5259[_0xf66c('0x3a')];}_0x47c6aa(_0x3f1ab7);})[_0xf66c('0x3d')](function(_0x50afb3){_0x502907(_0x50afb3);});});}function updateMailInteraction(_0xc059c7,_0x4e4dc1,_0x68ff4d,_0x3eb01e){return new BPromise(function(_0x414cb4,_0xbd5b21){return getInteractionUsers(_0xc059c7,_0x68ff4d)[_0xf66c('0x29')](function(_0x519650){return getInteractionTags(_0x519650,_0x4e4dc1);})[_0xf66c('0x29')](function(_0x1b2249){if(_0x3eb01e){_0x414cb4(getLastMessageText(_0x1b2249));}else{_0x414cb4(_0x1b2249);}})[_0xf66c('0x3d')](function(_0xb79363){_0xbd5b21(_0xb79363);});});}exports[_0xf66c('0x3e')]=function(_0x1359af,_0x54064f){var _0x1c0097={},_0x20c7fd={},_0x87a4f7={'count':0x0,'rows':[]};var _0x188a36=_['map'](db[_0xf66c('0x37')][_0xf66c('0x3f')],function(_0x41a078){return{'name':_0x41a078[_0xf66c('0x40')],'type':_0x41a078[_0xf66c('0x41')][_0xf66c('0x42')]};});_0x20c7fd[_0xf66c('0x43')]=_['map'](_0x188a36,'name');_0x20c7fd['query']=_[_0xf66c('0x44')](_0x1359af[_0xf66c('0x45')]);_0x20c7fd[_0xf66c('0x46')]=_['intersection'](_0x20c7fd[_0xf66c('0x43')],_0x20c7fd[_0xf66c('0x45')]);_0x1c0097[_0xf66c('0x47')]=_[_0xf66c('0x48')](_0x20c7fd[_0xf66c('0x43')],qs[_0xf66c('0x49')](_0x1359af[_0xf66c('0x45')]['fields']));_0x1c0097['attributes']=_0x1c0097[_0xf66c('0x47')][_0xf66c('0x4a')]?_0x1c0097[_0xf66c('0x47')]:_0x20c7fd[_0xf66c('0x43')];if(!_0x1359af[_0xf66c('0x45')][_0xf66c('0x4b')](_0xf66c('0x4c'))){_0x1c0097[_0xf66c('0x21')]=qs['limit'](_0x1359af['query'][_0xf66c('0x21')]);_0x1c0097[_0xf66c('0x20')]=qs[_0xf66c('0x20')](_0x1359af[_0xf66c('0x45')]['offset']);}_0x1c0097[_0xf66c('0x4d')]=qs[_0xf66c('0x4e')](_0x1359af[_0xf66c('0x45')][_0xf66c('0x4e')]);_0x1c0097['where']=qs[_0xf66c('0x46')](_[_0xf66c('0x4f')](_0x1359af[_0xf66c('0x45')],_0x20c7fd['filters']));var _0x5a7922=[];var _0x2bbf1a=squel['select']()[_0xf66c('0x50')]('id')['field']('name')[_0xf66c('0x50')](_0xf66c('0x51'))[_0xf66c('0x52')](_0xf66c('0x53'));var _0xa8f1b0=squel[_0xf66c('0x54')]()[_0xf66c('0x50')](_0xf66c('0x55'))[_0xf66c('0x50')]('u.fullname')['field'](_0xf66c('0x56'))[_0xf66c('0x52')]('user_has_mail_interactions','ui')[_0xf66c('0x57')](_0xf66c('0x58'),'u',_0xf66c('0x59'));var _0x330263=squel[_0xf66c('0x54')]()[_0xf66c('0x52')]('mail_interactions','i')[_0xf66c('0x57')]('cm_contacts','c',_0xf66c('0x5a'))[_0xf66c('0x57')](_0xf66c('0x58'),'o',_0xf66c('0x5b'))[_0xf66c('0x57')](_0xf66c('0x5c'),'a',_0xf66c('0x5d'))[_0xf66c('0x57')](_0xf66c('0x5e'),'me','me.MailInteractionId\x20=\x20i.id')[_0xf66c('0x57')]('mail_interaction_has_tags','it',_0xf66c('0x5f'))[_0xf66c('0x57')](_0xf66c('0x53'),'t',_0xf66c('0x60'));var _0xc37f66=squel['expr']();var _0x45bfad=[];var _0x28bdd7=squel[_0xf66c('0x61')]();var _0x3c720d;if(_0x1359af[_0xf66c('0x45')][_0xf66c('0x62')]){_0x3c720d=as[_0xf66c('0x63')](_0x1359af[_0xf66c('0x45')][_0xf66c('0x62')]);var _0x1dbaaa=_0x3c720d[_0xf66c('0x64')];for(var _0x5a12ac=0x0;_0x5a12ac<_0x3c720d[_0xf66c('0x65')][_0xf66c('0x4a')];_0x5a12ac++){var _0x57f5cb=_0x3c720d[_0xf66c('0x65')][_0x5a12ac];var _0x34968b='i';var _0x38eb72=_[_0xf66c('0x66')](_0x188a36,[_0xf66c('0x2d'),_0x57f5cb['field']]);if(!_0x38eb72){switch(_0x57f5cb[_0xf66c('0x50')]){case'unreadMessages':if(_0x57f5cb['value']==0x1){_0x330263[_0xf66c('0x67')]('`'+_0x57f5cb[_0xf66c('0x50')]+_0xf66c('0x68'));}else{_0x330263['having']('`'+_0x57f5cb[_0xf66c('0x50')]+_0xf66c('0x69'));}break;case _0xf66c('0x6a'):if(_0x57f5cb[_0xf66c('0x6b')]===_0xf66c('0x6c')){if(_0x57f5cb[_0xf66c('0x6d')][_0xf66c('0x34')]('\x20')[_0xf66c('0x4a')]>0x1){_0xc37f66[_0x1dbaaa](_0xf66c('0x6e'),qs[_0xf66c('0x6f')](_0x57f5cb[_0xf66c('0x6d')]),null);}else{var _0x501def='%'+_0x57f5cb['value']+'%';_0xc37f66[_0x1dbaaa](_0xf66c('0x70'),_0x501def,_0x501def,_0x501def);}}else{_0x38eb72=_[_0xf66c('0x66')](_0x188a36,[_0xf66c('0x2d'),_0xf66c('0x71')]);_0x57f5cb[_0xf66c('0x50')]=_0xf66c('0x71');_0x31e35e=as[_0xf66c('0x72')](_0x34968b,_0x38eb72[_0xf66c('0x41')],_0x57f5cb);_0xc37f66[_0x1dbaaa](_0x31e35e[_0xf66c('0x73')],_0x31e35e[_0xf66c('0x6d')][_0xf66c('0x74')],_0x31e35e['value']['end']);}break;case'Tag':var _0x4b2691=_0x57f5cb[_0xf66c('0x6d')]['split'](',')[_0xf66c('0x30')](function(_0x39c902){return Number(_0x39c902);});_0x4b2691['forEach'](function(_0x3e1ff6){_0x28bdd7['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x3e1ff6+',%');});_0x45bfad=_[_0xf66c('0x75')](_0x45bfad,_0x4b2691);break;case _0xf66c('0x76'):if(_0x57f5cb[_0xf66c('0x6b')]===_0xf66c('0x6c')){_0xc37f66[_0x1dbaaa](_0xf66c('0x77'),'%'+_0x57f5cb['value']+'%',null);}else{_0x38eb72=_[_0xf66c('0x66')](_0x188a36,[_0xf66c('0x2d'),_0xf66c('0x78')]);_0x57f5cb[_0xf66c('0x50')]=_0xf66c('0x78');_0x31e35e=as[_0xf66c('0x72')](_0x34968b,_0x38eb72[_0xf66c('0x41')],_0x57f5cb);_0xc37f66[_0x1dbaaa](_0x31e35e[_0xf66c('0x73')],_0x31e35e[_0xf66c('0x6d')]['start'],_0x31e35e[_0xf66c('0x6d')][_0xf66c('0x1e')]);}break;case _0xf66c('0x79'):_0x31e35e=as[_0xf66c('0x72')]('me',null,_0x57f5cb);_0xc37f66[_0x1dbaaa](_0x31e35e[_0xf66c('0x73')],_0x31e35e['value'][_0xf66c('0x74')],_0x31e35e[_0xf66c('0x6d')][_0xf66c('0x1e')]);break;}}else{var _0x31e35e=as[_0xf66c('0x72')](_0x34968b,_0x38eb72[_0xf66c('0x41')],_0x57f5cb);_0xc37f66[_0x1dbaaa](_0x31e35e['text'],_0x31e35e[_0xf66c('0x6d')][_0xf66c('0x74')],_0x31e35e[_0xf66c('0x6d')]['end']);}}}else{var _0x47453c=_(_0x1359af[_0xf66c('0x45')])[_0xf66c('0x44')]()[_0xf66c('0x30')](function(_0x1b6432){return _[_0xf66c('0x7a')](_0x188a36,[_0xf66c('0x2d'),_0x1b6432])?_0x1b6432:undefined;})[_0xf66c('0x7b')]()['value']();if(!_[_0xf66c('0x7c')](_0x47453c)){_0x47453c[_0xf66c('0x35')](function(_0x196fdb){if(_0x196fdb===_0xf66c('0x7d')){_0x330263[_0xf66c('0x7e')](_0xf66c('0x7f'),[][_0xf66c('0x80')](_0x1359af[_0xf66c('0x45')][_0x196fdb]));}else if(_0x196fdb===_0xf66c('0x81')){var _0x125f4a=JSON[_0xf66c('0x82')](_0x1359af[_0xf66c('0x45')][_0x196fdb])[_0xf66c('0x83')];var _0x3cddbb=JSON['parse'](_0x1359af[_0xf66c('0x45')][_0x196fdb])[_0xf66c('0x84')];_0x330263[_0xf66c('0x7e')](_0xf66c('0x85'),_0x125f4a,_0x3cddbb);}else{if(_0x1359af[_0xf66c('0x45')][_0x196fdb]===_0xf66c('0x86')){_0x330263['where']('i.'+_0x196fdb+_0xf66c('0x87'));}else{_0x330263[_0xf66c('0x7e')]('i.'+_0x196fdb+'\x20=\x20?',_0x1359af[_0xf66c('0x45')][_0x196fdb]);}}});}if(_0x1359af['query'][_0xf66c('0x88')]){if(_0x1359af[_0xf66c('0x45')][_0xf66c('0x88')]==0x1){_0x330263[_0xf66c('0x67')](_0xf66c('0x89'));}else{_0x330263[_0xf66c('0x67')]('`unreadMessages`\x20>\x200');}}if(_0x1359af[_0xf66c('0x45')][_0xf66c('0x8a')]){_0x45bfad=_[_0xf66c('0x8b')](_0x1359af['query'][_0xf66c('0x8a')])?_0x1359af['query'][_0xf66c('0x8a')]:new Array(_0x1359af[_0xf66c('0x45')]['tag']);_0x45bfad[_0xf66c('0x35')](function(_0x10e78c){_0x28bdd7['or'](_0xf66c('0x8c'),'%,'+_0x10e78c+',%');});}if(_0x1359af['query'][_0xf66c('0x8d')]){var _0xf4498=_0x1359af['query']['filter'][_0xf66c('0x8e')]('\x5c','\x5c\x5c')[_0xf66c('0x8e')]('\x27\x27',_0xf66c('0x8f'));if(qs[_0xf66c('0x90')](_0xf4498)){_0xc37f66['or'](_0xf66c('0x91'),_0xf4498+'%');}else if(qs[_0xf66c('0x92')](_0xf4498)){_0xc37f66['or'](_0xf66c('0x93'),_0xf4498+'%');}else{_0xc37f66['or'](_0xf66c('0x94'),qs[_0xf66c('0x6f')](_0xf4498))['or'](_0xf66c('0x95'),qs[_0xf66c('0x6f')](_0xf4498));}}}_0x330263['where'](_0xc37f66);_0x330263[_0xf66c('0x96')](_0xf66c('0x97'));var _0x434a51={'type':db[_0xf66c('0x98')][_0xf66c('0x99')]['SELECT'],'raw':!![]};var _0x5ea604=_0x330263[_0xf66c('0x9a')]();_0x5ea604[_0xf66c('0x50')](_0xf66c('0x97'));_0x5ea604[_0xf66c('0x50')]('SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)',_0xf66c('0x9b'));if(_0x1c0097['order']){_0x1c0097[_0xf66c('0x4d')]['forEach'](function(_0x1d2c69){_0x330263['order']('i.'+_0x1d2c69[0x0],_0x1d2c69[0x1]===_0xf66c('0x9c')?![]:!![]);});}if(!_['isEmpty'](_0x45bfad)){_0x330263[_0xf66c('0x67')](_0x28bdd7);_0x5ea604[_0xf66c('0x7e')](_0xf66c('0x9d'),_0x45bfad);}BPromise[_0xf66c('0x9e')]()[_0xf66c('0x29')](function(){if(!_0x3c720d){if(_0x1359af[_0xf66c('0x45')][_0xf66c('0x7d')]||_0x1359af[_0xf66c('0x45')][_0xf66c('0x9f')])return;_0x5ea604['where'](_0xf66c('0xa0'));_0x330263[_0xf66c('0x7e')](_0xf66c('0xa0'));return;}return _0x1359af['user'][_0xf66c('0xa1')]({'attributes':['id'],'raw':!![]})[_0xf66c('0x29')](function(_0x5aeaef){if(_[_0xf66c('0x7c')](_0x5aeaef)){_0x5ea604[_0xf66c('0x7e')](_0xf66c('0xa2'));_0x330263[_0xf66c('0x7e')]('i.MailAccountId\x20IS\x20NULL');}else{_0x5ea604[_0xf66c('0x7e')](_0xf66c('0x7f'),_[_0xf66c('0x30')](_0x5aeaef,'id'));_0x330263[_0xf66c('0x7e')](_0xf66c('0x7f'),_[_0xf66c('0x30')](_0x5aeaef,'id'));}});})[_0xf66c('0x29')](function(){db[_0xf66c('0xa3')][_0xf66c('0x45')](_0x5ea604[_0xf66c('0xa4')](),_0x434a51)['then'](function(_0x5ebab7){_0x87a4f7[_0xf66c('0x1f')]=_0x5ebab7[_0xf66c('0x4a')];if(_0x87a4f7[_0xf66c('0x1f')]===0x0)return[];return db[_0xf66c('0xa3')][_0xf66c('0x45')](_0x2bbf1a[_0xf66c('0xa4')](),_0x434a51)[_0xf66c('0x29')](function(_0x3cc14c){_0x5a7922=_[_0xf66c('0xa5')](_0x3cc14c,'id');_0x434a51=_[_0xf66c('0xa6')](_0x434a51,{'model':db[_0xf66c('0x37')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x1359af[_0xf66c('0x45')]['fields']){_0x1c0097[_0xf66c('0x47')]['forEach'](function(_0x410194){_0x330263[_0xf66c('0x50')]('i.'+_0x410194);});}else{_0x330263[_0xf66c('0x50')](_0xf66c('0xa7'),_0xf66c('0x9b'));_0x330263[_0xf66c('0x50')](_0xf66c('0xa8'),_0xf66c('0xa9'));_0x330263[_0xf66c('0x50')](_0xf66c('0xaa'),_0xf66c('0xab'));_0x330263[_0xf66c('0x50')](_0xf66c('0x97'));_0x330263[_0xf66c('0x50')](_0xf66c('0xac'));_0x330263[_0xf66c('0x50')]('i.updatedAt');_0x330263[_0xf66c('0x50')](_0xf66c('0xad'));_0x330263[_0xf66c('0x50')]('i.closedAt');_0x330263['field'](_0xf66c('0xae'));_0x330263[_0xf66c('0x50')]('i.lastMsgAt');_0x330263['field'](_0xf66c('0xaf'));_0x330263[_0xf66c('0x50')](_0xf66c('0xb0'));_0x330263[_0xf66c('0x50')](_0xf66c('0xb1'));_0x330263['field'](_0xf66c('0xb2'));_0x330263['field'](_0xf66c('0xb3'));_0x330263[_0xf66c('0x50')]('i.ContactId');_0x330263[_0xf66c('0x50')](_0xf66c('0xb4'));_0x330263['field'](_0xf66c('0xb5'));_0x330263[_0xf66c('0x50')]('c.firstName',_0xf66c('0xb6'));_0x330263[_0xf66c('0x50')](_0xf66c('0xb7'),_0xf66c('0xb8'));_0x330263[_0xf66c('0x50')]('c.email',_0xf66c('0xb9'));_0x330263['field']('c.phone',_0xf66c('0xba'));_0x330263[_0xf66c('0x50')](_0xf66c('0xbb'),_0xf66c('0xbc'));_0x330263[_0xf66c('0x50')](_0xf66c('0xbd'),'Contact.fax');_0x330263[_0xf66c('0x50')]('o.fullname','Owner.fullname');_0x330263[_0xf66c('0x50')](_0xf66c('0xbe'),'Owner.internal');_0x330263[_0xf66c('0x50')](_0xf66c('0xbf'),'TagIds');}_0x330263[_0xf66c('0x21')](_0x1c0097[_0xf66c('0x21')]);_0x330263[_0xf66c('0x20')](_0x1c0097['offset']);return db[_0xf66c('0xa3')]['query'](_0x330263[_0xf66c('0xa4')](),_0x434a51);})[_0xf66c('0x29')](function(_0x7dd161){_0xa8f1b0[_0xf66c('0x7e')](_0xf66c('0xc0'),_['map'](_0x7dd161,'id'));return db[_0xf66c('0xa3')][_0xf66c('0x45')](_0xa8f1b0['toString'](),_0x434a51)[_0xf66c('0x29')](function(_0x45eca6){var _0x245231=_[_0xf66c('0xc1')](_0x45eca6,_0xf66c('0xc2'));var _0x5aa86a=[];return db['Setting'][_0xf66c('0x38')]({'attributes':[_0xf66c('0xc3')],'raw':!![]})['then'](function(_0x42e94e){var _0x150d6e=_0x42e94e[_0xf66c('0xc3')];_0x7dd161[_0xf66c('0x35')](function(_0x4a4585){_0x5aa86a[_0xf66c('0x36')](updateMailInteraction(_0x4a4585,_0x5a7922,_0x245231,_0x150d6e));});return BPromise[_0xf66c('0xc4')](_0x5aa86a);});});});})[_0xf66c('0x29')](function(_0x15d1fb){_0x87a4f7[_0xf66c('0xc5')]=_0x15d1fb;return _0x87a4f7;})[_0xf66c('0x29')](respondWithFilteredResult(_0x54064f,_0x1c0097));})[_0xf66c('0x3d')](handleError(_0x54064f,null));};exports[_0xf66c('0xc6')]=function(_0x17f2ea,_0x228be2){var _0x182117={'raw':![],'where':{'id':_0x17f2ea[_0xf66c('0xc7')]['id']}},_0x5f0d58={};_0x5f0d58[_0xf66c('0x43')]=_[_0xf66c('0x44')](db[_0xf66c('0x37')][_0xf66c('0x3f')]);_0x5f0d58[_0xf66c('0x45')]=_[_0xf66c('0x44')](_0x17f2ea[_0xf66c('0x45')]);_0x5f0d58['filters']=_['intersection'](_0x5f0d58['model'],_0x5f0d58['query']);_0x182117['attributes']=_['intersection'](_0x5f0d58[_0xf66c('0x43')],qs[_0xf66c('0x49')](_0x17f2ea[_0xf66c('0x45')][_0xf66c('0x49')]));_0x182117[_0xf66c('0x47')]=_0x182117[_0xf66c('0x47')]['length']?_0x182117[_0xf66c('0x47')]:_0x5f0d58[_0xf66c('0x43')];if(_0x17f2ea[_0xf66c('0x45')][_0xf66c('0xc8')]){_0x182117[_0xf66c('0xc9')]=[{'all':!![]}];}_0x182117=_[_0xf66c('0xa6')]({},_0x182117,_0x17f2ea['options']);return db['MailInteraction'][_0xf66c('0x66')](_0x182117)[_0xf66c('0x29')](handleEntityNotFound(_0x228be2,null))[_0xf66c('0x29')](respondWithResult(_0x228be2,null))['catch'](handleError(_0x228be2,null));};exports['create']=function(_0x5ee216,_0x1d12c8){return db[_0xf66c('0x37')]['create'](_0x5ee216[_0xf66c('0xca')],{})[_0xf66c('0x29')](respondWithResult(_0x1d12c8,0xc9))[_0xf66c('0x3d')](handleError(_0x1d12c8,null));};exports[_0xf66c('0x28')]=function(_0x2ec48f,_0xc4480c){if(_0x2ec48f[_0xf66c('0xca')]['id']){delete _0x2ec48f['body']['id'];}return db[_0xf66c('0x37')]['find']({'where':{'id':_0x2ec48f[_0xf66c('0xc7')]['id']}})[_0xf66c('0x29')](handleEntityNotFound(_0xc4480c,null))[_0xf66c('0x29')](saveUpdates(_0x2ec48f[_0xf66c('0xca')],null))['then'](respondWithResult(_0xc4480c,null))[_0xf66c('0x3d')](handleError(_0xc4480c,null));};exports[_0xf66c('0x2a')]=function(_0x536b65,_0x2e8d75){return db['MailInteraction'][_0xf66c('0x66')]({'where':{'id':_0x536b65[_0xf66c('0xc7')]['id']}})[_0xf66c('0x29')](handleEntityNotFound(_0x2e8d75,null))['then'](removeEntity(_0x2e8d75,null))['catch'](handleError(_0x2e8d75,null));};exports['describe']=function(_0x596527,_0x3deb89){return db[_0xf66c('0x37')][_0xf66c('0xcb')]()[_0xf66c('0x29')](respondWithResult(_0x3deb89,null))[_0xf66c('0x3d')](handleError(_0x3deb89,null));};exports['addMessage']=function(_0x19c9f5,_0x62279,_0x1668e8){return db[_0xf66c('0xcc')][_0xf66c('0x66')]({'where':{'id':_0x19c9f5['params']['id']}})['then'](handleEntityNotFound(_0x62279,null))[_0xf66c('0x29')](function(_0x28b88d){if(_0x28b88d){return _0x28b88d[_0xf66c('0xcd')](_0x19c9f5[_0xf66c('0xca')][_0xf66c('0xce')],_[_0xf66c('0xcf')](_0x19c9f5[_0xf66c('0xca')],[_0xf66c('0xce'),'id'])||{});}})['then'](respondWithResult(_0x62279,null))['catch'](handleError(_0x62279,null));};exports['getMessages']=function(_0x2fdfa9,_0x559442,_0x10121b){var _0x4e70fe={'raw':![],'where':{}};var _0x468e0d={};var _0x42d858={'count':0x0,'rows':[]};return db[_0xf66c('0x37')][_0xf66c('0x38')]({'where':{'id':_0x2fdfa9[_0xf66c('0xc7')]['id']}})['then'](handleEntityNotFound(_0x559442,null))[_0xf66c('0x29')](function(_0x2611b5){if(_0x2611b5){_0x468e0d[_0xf66c('0x43')]=_['keys'](db[_0xf66c('0xcc')][_0xf66c('0x3f')]);_0x468e0d['query']=_[_0xf66c('0x44')](_0x2fdfa9['query']);_0x468e0d[_0xf66c('0x46')]=_[_0xf66c('0x48')](_0x468e0d['model'],_0x468e0d[_0xf66c('0x45')]);_0x4e70fe['attributes']=_[_0xf66c('0x48')](_0x468e0d[_0xf66c('0x43')],qs[_0xf66c('0x49')](_0x2fdfa9['query'][_0xf66c('0x49')]));_0x4e70fe['attributes']=_0x4e70fe[_0xf66c('0x47')]['length']?_0x4e70fe[_0xf66c('0x47')]:_0x468e0d['model'];if(!_0x2fdfa9[_0xf66c('0x45')][_0xf66c('0x4b')](_0xf66c('0x4c'))){_0x4e70fe[_0xf66c('0x21')]=qs[_0xf66c('0x21')](_0x2fdfa9['query'][_0xf66c('0x21')]);_0x4e70fe[_0xf66c('0x20')]=qs[_0xf66c('0x20')](_0x2fdfa9[_0xf66c('0x45')][_0xf66c('0x20')]);}_0x4e70fe['order']=qs[_0xf66c('0x4e')](_0x2fdfa9[_0xf66c('0x45')][_0xf66c('0x4e')]);_0x4e70fe[_0xf66c('0x7e')]=qs['filters'](_[_0xf66c('0x4f')](_0x2fdfa9[_0xf66c('0x45')],_0x468e0d[_0xf66c('0x46')]));_0x4e70fe[_0xf66c('0x7e')]['MailInteractionId']=_0x2611b5['id'];if(_0x2fdfa9['query']['filter']){_0x4e70fe[_0xf66c('0x7e')]=_['merge'](_0x4e70fe['where'],{'$or':_['map'](_0x4e70fe[_0xf66c('0x47')],function(_0x11fdd5){var _0x40f8b0={};_0x40f8b0[_0x11fdd5]={'$like':'%'+_0x2fdfa9[_0xf66c('0x45')][_0xf66c('0x8d')]+'%'};return _0x40f8b0;})});}if(_0x2fdfa9[_0xf66c('0x45')][_0xf66c('0x83')]){var _0x23cf01=_0x2fdfa9['query'][_0xf66c('0x83')][_0xf66c('0x34')](',');var _0x3d75d9={};_0x3d75d9[_0x23cf01[0x0]]={'$gte':moment(_0x23cf01[0x1])['format'](_0xf66c('0xd0'))};_0x4e70fe[_0xf66c('0x7e')]=_[_0xf66c('0xa6')](_0x4e70fe['where'],_0x3d75d9);}_0x4e70fe=_[_0xf66c('0xa6')]({},_0x4e70fe,_0x2fdfa9['options']);return db[_0xf66c('0xcc')][_0xf66c('0x1f')]({'where':_0x4e70fe[_0xf66c('0x7e')]})[_0xf66c('0x29')](function(_0x3c4c30){_0x42d858[_0xf66c('0x1f')]=_0x3c4c30;if(_0x2fdfa9[_0xf66c('0x45')][_0xf66c('0xc8')]){_0x4e70fe[_0xf66c('0xc9')]=[{'all':!![]}];}return db[_0xf66c('0xcc')][_0xf66c('0xd1')](_0x4e70fe);})[_0xf66c('0x29')](function(_0x586d88){_0x42d858['rows']=_0x586d88;return _0x42d858;});}})[_0xf66c('0x29')](respondWithFilteredResult(_0x559442,_0x4e70fe))[_0xf66c('0x3d')](handleError(_0x559442,null));};exports[_0xf66c('0xd2')]=function(_0x2b104c,_0x2e3d05,_0x12a133){return db[_0xf66c('0x37')][_0xf66c('0x66')]({'where':{'id':_0x2b104c[_0xf66c('0xc7')]['id']}})[_0xf66c('0x29')](handleEntityNotFound(_0x2e3d05,null))['then'](function(_0x37a448){if(_0x37a448){return _0x37a448[_0xf66c('0xd3')](_0x2b104c['body']['ids'],_['omit'](_0x2b104c[_0xf66c('0xca')],[_0xf66c('0xce'),'id'])||{})['spread'](function(){return db[_0xf66c('0xd4')][_0xf66c('0xd1')]({'attributes':['id',_0xf66c('0x2d'),_0xf66c('0x51')],'where':{'id':_0x2b104c['body']['ids']}});})[_0xf66c('0x29')](function(_0x4b64ef){socket[_0xf66c('0xd5')](_0xf66c('0xd6'),{'id':Number(_0x2b104c[_0xf66c('0xc7')]['id']),'tags':_0x4b64ef||[]});return{'id':Number(_0x2b104c[_0xf66c('0xc7')]['id']),'tags':_0x4b64ef||[]};});}})[_0xf66c('0x29')](respondWithResult(_0x2e3d05,null))[_0xf66c('0x3d')](handleError(_0x2e3d05,null));};exports[_0xf66c('0xd7')]=function(_0x3bd7cc,_0x3cd932,_0xc78367){return db['MailInteraction'][_0xf66c('0x66')]({'where':{'id':_0x3bd7cc['params']['id']}})[_0xf66c('0x29')](handleEntityNotFound(_0x3cd932,null))[_0xf66c('0x29')](function(_0x49bcaf){if(_0x49bcaf){return _0x49bcaf[_0xf66c('0xd7')](_0x3bd7cc[_0xf66c('0x45')]['ids']);}})['then'](respondWithStatusCode(_0x3cd932,null))[_0xf66c('0x3d')](handleError(_0x3cd932,null));};exports['download']=function(_0x3c5ab5,_0x342f2b){var _0x12794e=moment()['unix']()[_0xf66c('0xa4')]();var _0x5d4777=path[_0xf66c('0xd8')](config[_0xf66c('0xd9')],_0xf66c('0xda'),_0xf66c('0xdb'),_0xf66c('0xdc'));var _0x214337=path['join'](config[_0xf66c('0xd9')],_0xf66c('0xda'),_0xf66c('0xdb'),_0xf66c('0xdd'));var _0x29c9bc=path[_0xf66c('0xd8')](_0x5d4777,_0x12794e);var _0x4e1f46=util[_0xf66c('0xde')](_0xf66c('0xdf'),_0x3c5ab5[_0xf66c('0xc7')]['id'],_0x12794e);var _0x393468=path[_0xf66c('0xd8')](_0x5d4777,_0x4e1f46);var _0x488bc2=[];if(_0x3c5ab5['query'][_0xf66c('0xdd')]){_0x488bc2[_0xf66c('0x36')]({'model':db[_0xf66c('0xe0')],'as':_0xf66c('0xe1'),'raw':!![]});}var _0x209a4c=[{'model':db[_0xf66c('0xcc')],'as':_0xf66c('0xe2'),'attributes':['id',_0xf66c('0xca'),_0xf66c('0x52'),'to','cc',_0xf66c('0xe3'),_0xf66c('0xe4'),_0xf66c('0x81')],'where':{'secret':![]},'order':qs['sort']('id'),'include':_0x488bc2}];return db['MailInteraction'][_0xf66c('0x66')]({'where':{'id':_0x3c5ab5[_0xf66c('0xc7')]['id']},'include':_0x209a4c})['then'](handleEntityNotFound(_0x342f2b,null))[_0xf66c('0x29')](function(_0x293ea4){if(_0x293ea4&&_0x293ea4[_0xf66c('0xe2')]){var _0xa4098d=_0x293ea4[_0xf66c('0xe5')]({'plain':!![]});fs[_0xf66c('0xe6')](_0x29c9bc);return BPromise[_0xf66c('0xe7')](_0xa4098d[_0xf66c('0xe2')],function(_0x3e6ec7,_0x4f090f){var _0x2e23ed={'from':_0x3e6ec7['from'],'to':_0x3e6ec7['to'],'subject':_0x3e6ec7[_0xf66c('0xe4')],'html':_0x3e6ec7[_0xf66c('0xca')],'headers':{'Date':moment(_0x3e6ec7[_0xf66c('0x81')])[_0xf66c('0xde')](_0xf66c('0xe8'))},'attachments':[]};if(_0x3e6ec7['cc']){_0x2e23ed[_0xf66c('0xe9')]['Cc']=_0x3e6ec7['cc'];}if(_0x3e6ec7[_0xf66c('0xe3')]){_0x2e23ed[_0xf66c('0xe9')][_0xf66c('0xea')]=_0x3e6ec7[_0xf66c('0xe3')];}return new BPromise(function(_0x212a8d,_0x163b5e){if(_0x3e6ec7[_0xf66c('0xe1')]){for(var _0xcd4a86=0x0;_0xcd4a86<_0x3e6ec7[_0xf66c('0xe1')][_0xf66c('0x4a')];_0xcd4a86++){var _0x3b3fc2=_0x3e6ec7[_0xf66c('0xe1')][_0xcd4a86];var _0x3059fa=path[_0xf66c('0xd8')](_0x214337,_0x3b3fc2[_0xf66c('0xeb')]);if(fs[_0xf66c('0xec')](_0x3059fa)){_0x2e23ed[_0xf66c('0xdd')][_0xf66c('0x36')]({'name':_0x3b3fc2[_0xf66c('0x2d')],'contentType':_0x3b3fc2['type'],'data':fs[_0xf66c('0xed')](_0x3059fa)});}}}emlformat[_0xf66c('0xee')](_0x2e23ed,function(_0x44fe1e,_0x1880bd){if(_0x44fe1e)return _0x163b5e(_0x44fe1e);fs[_0xf66c('0xef')](path[_0xf66c('0xd8')](_0x29c9bc,util[_0xf66c('0xde')](_0xf66c('0xf0'),_0x4f090f)),_0x1880bd);return _0x212a8d(_0x1880bd);});});})[_0xf66c('0x29')](function(){return new BPromise(function(_0x4ff5c3,_0x4f1e43){zipdir(_0x29c9bc,{'saveTo':_0x393468},function(_0x2cffa8,_0x288ffd){if(_0x2cffa8)return _0x4f1e43(_0x2cffa8);return _0x4ff5c3(_0x288ffd);});})['then'](function(){return new BPromise(function(_0x4b9cc5,_0x533060){rimraf(_0x29c9bc,function(_0xddbae0){if(_0xddbae0)_0x533060(_0xddbae0);return _0x4b9cc5();});});})[_0xf66c('0x29')](function(){return _0x342f2b[_0xf66c('0xf1')](_0x393468,_0x4e1f46,function(_0x594e3b){if(_0x594e3b){console[_0xf66c('0xf2')](_0xf66c('0xf3'),_0x594e3b);}else{fs[_0xf66c('0xf4')](_0x393468);}});});});}else{return _0x342f2b['sendStatus'](0xc8);}})[_0xf66c('0x3d')](handleError(_0x342f2b,null));};