ddecfeedf6e4dccf48424d7e1d3c50fb392d24fd
[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 _0xed3c=['key','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','field','color','from','tools_tags','select','u.fullname','ui.MailInteractionId','user_has_mail_interactions','left_join','users','u.id\x20=\x20ui.UserId','cm_contacts','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','o.id\x20=\x20i.UserId','mail_accounts','a.id\x20=\x20i.MailAccountId','mail_messages','me.MailInteractionId\x20=\x20i.id','mail_interaction_has_tags','it.MailInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','search','sqlOperator','conditions','unreadMessages','value','having','`\x20=\x200','`\x20>\x200','Contact','operator','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?','find','ContactId','buildExpression','text','start','Tag','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','union','User','$substring','UserId','plainBody','end','some','i.MailAccountId\x20IN\x20?','$gte','$lte','null','\x20IS\x20NULL','read','tag','isArray','filter','replace','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','order','contactName','DESC','t.id\x20IN\x20?','resolve','i.MailAccountId\x20IS\x20NOT\x20NULL','user','role','agent','i.MailAccountId\x20IS\x20NULL','MailAccountId','sequelize','toString','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','Account.key','a.mandatoryDisposition','Account.mandatoryDisposition','CASE\x20WHEN\x20i.to\x20IS\x20NOT\x20NULL\x20AND\x20i.to\x20<>\x20\x27\x27\x20AND\x20i.to\x20NOT\x20LIKE\x20(\x27%\x27\x20+\x20a.email\x20+\x20\x27%\x27)\x20THEN\x20SUBSTRING_INDEX(i.to,\x20\x27<\x27,\x201)\x20WHEN\x20(c.firstName\x20IS\x20NOT\x20NULL\x20AND\x20c.firstName\x20<>\x20\x27\x27)\x20OR\x20(c.lastName\x20IS\x20NOT\x20NULL\x20AND\x20c.lastName\x20<>\x20\x27\x27)\x20THEN\x20CONCAT(IFNULL(c.firstName,\x20\x27\x27),\x20\x27\x20\x27,\x20IFNULL(c.lastName,\x20\x27\x27))\x20ELSE\x20\x27Unknown\x27\x20END','i.createdAt','i.updatedAt','i.closed','i.closedAt','i.attach','i.lastMsgDirection','i.subject','i.disposition','i.thirdDisposition','i.to','i.ContactId','i.MailAccountId','i.UserId','c.firstName','Contact.firstName','c.email','Contact.email','c.phone','Contact.phone','c.mobile','Contact.mobile','c.fax','Contact.fax','o.fullname','Owner.fullname','o.internal','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','ui.MailInteractionId\x20IN\x20?','groupBy','MailInteractionId','Setting','enableEmailPreview','all','rows','show','includeAll','include','merge','create','body','describe','addMessage','params','ids','getMessages','MailMessage','rawAttributes','findAll','inlineCss','true','addTags','omit','spread','mailInteractionTags:save','unix','join','root','server','files','tmp','format','attachments','Attachment','Messages','bcc','subject','mkdirSync','each','createdAt','headers','Bcc','Attachments','readFileSync','writeFileSync','message%d.eml','download','log','err','unlinkSync','html-pdf','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','path','sox','to-csv','ejs','jsforce','papaparse','html-to-text','juice','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','socket.io-emitter','./mailInteraction.socket','register','sendStatus','status','count','offset','limit','set','apply','save','update','then','destroy','error','name','send','Users','map','fullname','TagIds','Tags','split','forEach','push','MailInteraction','findOne','lastMsgBody','lastMsgText','trimStart','fromString','catch','fieldName','type'];(function(_0x55309a,_0x316d40){var _0x12d419=function(_0x15ba8d){while(--_0x15ba8d){_0x55309a['push'](_0x55309a['shift']());}};_0x12d419(++_0x316d40);}(_0xed3c,0x1a6));var _0xced3=function(_0x564821,_0x4d037e){_0x564821=_0x564821-0x0;var _0x2b604d=_0xed3c[_0x564821];return _0x2b604d;};'use strict';var pdf=require(_0xced3('0x0'));var emlformat=require(_0xced3('0x1'));var rimraf=require(_0xced3('0x2'));var zipdir=require(_0xced3('0x3'));var jsonpatch=require('fast-json-patch');var rp=require(_0xced3('0x4'));var moment=require(_0xced3('0x5'));var BPromise=require(_0xced3('0x6'));var Mustache=require('mustache');var util=require('util');var path=require(_0xced3('0x7'));var sox=require(_0xced3('0x8'));var csv=require(_0xced3('0x9'));var ejs=require(_0xced3('0xa'));var fs=require('fs');var _=require('lodash');var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0xced3('0xb'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0xced3('0xc'));var htmlToText=require(_0xced3('0xd'));var juice=require(_0xced3('0xe'));var Redis=require(_0xced3('0xf'));var authService=require(_0xced3('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0xced3('0x11'));var hardwareService=require(_0xced3('0x12'));var logger=require(_0xced3('0x13'))('api');var utils=require(_0xced3('0x14'));var config=require(_0xced3('0x15'));var licenseUtil=require(_0xced3('0x16'));var db=require(_0xced3('0x17'))['db'];config[_0xced3('0x18')]=_[_0xced3('0x19')](config[_0xced3('0x18')],{'host':'localhost','port':0x18eb});var socket=require(_0xced3('0x1a'))(new Redis(config['redis']));require(_0xced3('0x1b'))[_0xced3('0x1c')](socket);function respondWithStatusCode(_0x20d099,_0x39e4aa){_0x39e4aa=_0x39e4aa||0xcc;return function(_0x4dd4c3){if(_0x4dd4c3){return _0x20d099[_0xced3('0x1d')](_0x39e4aa);}return _0x20d099[_0xced3('0x1e')](_0x39e4aa)['end']();};}function respondWithResult(_0x3b5962,_0x37e2d5){_0x37e2d5=_0x37e2d5||0xc8;return function(_0x43a14f){if(_0x43a14f){return _0x3b5962[_0xced3('0x1e')](_0x37e2d5)['json'](_0x43a14f);}};}function respondWithFilteredResult(_0x4569af,_0x4a3afe){return function(_0x353580){if(_0x353580){var _0x4480a6=_0x353580[_0xced3('0x1f')],_0x32c402=_0x4a3afe[_0xced3('0x20')],_0x1fb300=_0x4a3afe[_0xced3('0x20')]+_0x4a3afe[_0xced3('0x21')],_0x44143f;if(_0x1fb300>=_0x4480a6){_0x1fb300=_0x4480a6;_0x44143f=0xc8;}else{_0x44143f=0xce;}_0x4569af[_0xced3('0x1e')](_0x44143f);return _0x4569af[_0xced3('0x22')]('Content-Range',_0x32c402+'-'+_0x1fb300+'/'+_0x4480a6)['json'](_0x353580);}return null;};}function patchUpdates(_0x5e8c44){return function(_0x1e8450){try{jsonpatch[_0xced3('0x23')](_0x1e8450,_0x5e8c44,!![]);}catch(_0x21e074){return BPromise['reject'](_0x21e074);}return _0x1e8450[_0xced3('0x24')]();};}function saveUpdates(_0x30d984,_0x526854){return function(_0x559fa2){if(_0x559fa2){return _0x559fa2[_0xced3('0x25')](_0x30d984)[_0xced3('0x26')](function(_0x219fa8){return _0x219fa8;});}return null;};}function removeEntity(_0x15c6c5,_0x50f7c1){return function(_0x1905a3){if(_0x1905a3){return _0x1905a3[_0xced3('0x27')]()['then'](function(){_0x15c6c5[_0xced3('0x1e')](0xcc)['end']();});}};}function handleEntityNotFound(_0x1dcc52,_0x4123ba){return function(_0x3b728d){if(!_0x3b728d){_0x1dcc52[_0xced3('0x1d')](0x194);}return _0x3b728d;};}function handleError(_0x2ec2c4,_0x502cf1){_0x502cf1=_0x502cf1||0x1f4;return function(_0x5454c0){logger[_0xced3('0x28')](_0x5454c0['stack']);if(_0x5454c0[_0xced3('0x29')]){delete _0x5454c0[_0xced3('0x29')];}_0x2ec2c4['status'](_0x502cf1)[_0xced3('0x2a')](_0x5454c0);};}function getInteractionUsers(_0x4024fa,_0x196956){return new BPromise(function(_0x1032d6,_0x4389f8){try{if(_0x196956[_0x4024fa['id']]){_0x4024fa[_0xced3('0x2b')]=_[_0xced3('0x2c')](_0x196956[_0x4024fa['id']],function(_0x41d003){return{'id':_0x41d003['id'],'fullname':_0x41d003[_0xced3('0x2d')]};});}}catch(_0x559b8e){_0x4389f8(_0x559b8e);}_0x1032d6(_0x4024fa);});}function getInteractionTags(_0xa78899,_0x1b8baa){return new BPromise(function(_0x101769,_0x5867af){try{if(_0xa78899[_0xced3('0x2e')]){_0xa78899[_0xced3('0x2f')]=[];_0xa78899[_0xced3('0x2e')][_0xced3('0x30')](',')[_0xced3('0x31')](function(_0x30cd7f){_0xa78899[_0xced3('0x2f')][_0xced3('0x32')](_0x1b8baa[_0x30cd7f]);});}delete _0xa78899[_0xced3('0x2e')];}catch(_0x407a24){_0x5867af(_0x407a24);}_0x101769(_0xa78899);});}function getLastMessageText(_0x3048d0){return new BPromise(function(_0x19509c,_0x45897d){return db[_0xced3('0x33')][_0xced3('0x34')]({'where':{'id':_0x3048d0['id']},'attributes':[_0xced3('0x35'),_0xced3('0x36')],'raw':!![]})['then'](function(_0x5b92fc){if(!_0x5b92fc['lastMsgText']){_0x3048d0[_0xced3('0x36')]=_[_0xced3('0x37')](htmlToText[_0xced3('0x38')](_0x5b92fc[_0xced3('0x35')],{'preserveNewlines':!![],'singleNewLineParagraphs':!![],'uppercaseHeadings':![],'noLinkBrackets':!![],'ignoreImage':!![]}));}else{_0x3048d0[_0xced3('0x36')]=_0x5b92fc[_0xced3('0x36')];}_0x19509c(_0x3048d0);})[_0xced3('0x39')](function(_0x340be1){_0x45897d(_0x340be1);});});}function updateMailInteraction(_0x464c5f,_0x310e10,_0x5c290d,_0x342c11){return new BPromise(function(_0x26a435,_0x53e401){return getInteractionUsers(_0x464c5f,_0x5c290d)[_0xced3('0x26')](function(_0x3bf5e5){return getInteractionTags(_0x3bf5e5,_0x310e10);})[_0xced3('0x26')](function(_0x4f6ada){if(_0x342c11){_0x26a435(getLastMessageText(_0x4f6ada));}else{_0x26a435(_0x4f6ada);}})['catch'](function(_0x197ab3){_0x53e401(_0x197ab3);});});}exports['index']=function(_0x410357,_0x413da3){var _0x5f0ab8={},_0x180b65={},_0x15ca03={'count':0x0,'rows':[]};var _0x2a6dbf=_['map'](db[_0xced3('0x33')]['rawAttributes'],function(_0x55cf74){return{'name':_0x55cf74[_0xced3('0x3a')],'type':_0x55cf74[_0xced3('0x3b')][_0xced3('0x3c')]};});_0x180b65['model']=_[_0xced3('0x2c')](_0x2a6dbf,_0xced3('0x29'));_0x180b65[_0xced3('0x3d')]=_[_0xced3('0x3e')](_0x410357[_0xced3('0x3d')]);_0x180b65[_0xced3('0x3f')]=_[_0xced3('0x40')](_0x180b65[_0xced3('0x41')],_0x180b65[_0xced3('0x3d')]);_0x5f0ab8[_0xced3('0x42')]=_[_0xced3('0x40')](_0x180b65[_0xced3('0x41')],qs[_0xced3('0x43')](_0x410357[_0xced3('0x3d')][_0xced3('0x43')]));_0x5f0ab8['attributes']=_0x5f0ab8[_0xced3('0x42')][_0xced3('0x44')]?_0x5f0ab8[_0xced3('0x42')]:_0x180b65[_0xced3('0x41')];if(!_0x410357[_0xced3('0x3d')][_0xced3('0x45')](_0xced3('0x46'))){_0x5f0ab8[_0xced3('0x21')]=qs[_0xced3('0x21')](_0x410357['query'][_0xced3('0x21')]);_0x5f0ab8['offset']=qs[_0xced3('0x20')](_0x410357[_0xced3('0x3d')][_0xced3('0x20')]);}_0x5f0ab8['order']=qs[_0xced3('0x47')](_0x410357[_0xced3('0x3d')][_0xced3('0x47')]);_0x5f0ab8[_0xced3('0x48')]=qs['filters'](_[_0xced3('0x49')](_0x410357[_0xced3('0x3d')],_0x180b65[_0xced3('0x3f')]),_0x2a6dbf);var _0x26d16c=[];var _0x198aad=squel['select']()[_0xced3('0x4a')]('id')[_0xced3('0x4a')](_0xced3('0x29'))[_0xced3('0x4a')](_0xced3('0x4b'))[_0xced3('0x4c')](_0xced3('0x4d'));var _0x532db9=squel[_0xced3('0x4e')]()[_0xced3('0x4a')]('u.id')['field'](_0xced3('0x4f'))['field'](_0xced3('0x50'))[_0xced3('0x4c')](_0xced3('0x51'),'ui')[_0xced3('0x52')](_0xced3('0x53'),'u',_0xced3('0x54'));var _0x3700ed=squel[_0xced3('0x4e')]()[_0xced3('0x4c')]('mail_interactions','i')[_0xced3('0x52')](_0xced3('0x55'),'c',_0xced3('0x56'))[_0xced3('0x52')](_0xced3('0x53'),'o',_0xced3('0x57'))['left_join'](_0xced3('0x58'),'a',_0xced3('0x59'))[_0xced3('0x52')](_0xced3('0x5a'),'me',_0xced3('0x5b'))[_0xced3('0x52')](_0xced3('0x5c'),'it',_0xced3('0x5d'))[_0xced3('0x52')](_0xced3('0x4d'),'t',_0xced3('0x5e'));var _0x3e2a19=squel['expr']();var _0x1c69df=[];var _0x4bd3cb=squel[_0xced3('0x5f')]();var _0x27ce0b;if(_0x410357[_0xced3('0x3d')]['search']){_0x27ce0b=as['parseSearch'](_0x410357[_0xced3('0x3d')][_0xced3('0x60')]);var _0x53724a=_0x27ce0b[_0xced3('0x61')];for(var _0x4ae67d=0x0;_0x4ae67d<_0x27ce0b[_0xced3('0x62')][_0xced3('0x44')];_0x4ae67d++){var _0x4f1860=_0x27ce0b[_0xced3('0x62')][_0x4ae67d];var _0x11eb76='i';var _0xf4d7bf=_['find'](_0x2a6dbf,[_0xced3('0x29'),_0x4f1860['field']]);if(!_0xf4d7bf){switch(_0x4f1860[_0xced3('0x4a')]){case _0xced3('0x63'):if(_0x4f1860[_0xced3('0x64')]==0x1){_0x3700ed[_0xced3('0x65')]('`'+_0x4f1860[_0xced3('0x4a')]+_0xced3('0x66'));}else{_0x3700ed[_0xced3('0x65')]('`'+_0x4f1860[_0xced3('0x4a')]+_0xced3('0x67'));}break;case _0xced3('0x68'):if(_0x4f1860[_0xced3('0x69')]==='$substring'){if(_0x4f1860[_0xced3('0x64')][_0xced3('0x30')]('\x20')[_0xced3('0x44')]>0x1){_0x3e2a19[_0x53724a](_0xced3('0x6a'),qs[_0xced3('0x6b')](_0x4f1860[_0xced3('0x64')]),null);}else{var _0x4d0916='%'+_0x4f1860[_0xced3('0x64')]+'%';_0x3e2a19[_0x53724a](_0xced3('0x6c'),_0x4d0916,_0x4d0916,_0x4d0916);}}else{_0xf4d7bf=_[_0xced3('0x6d')](_0x2a6dbf,[_0xced3('0x29'),_0xced3('0x6e')]);_0x4f1860[_0xced3('0x4a')]='ContactId';_0x58d7fc=as[_0xced3('0x6f')](_0x11eb76,_0xf4d7bf['type'],_0x4f1860);_0x3e2a19[_0x53724a](_0x58d7fc[_0xced3('0x70')],_0x58d7fc['value'][_0xced3('0x71')],_0x58d7fc[_0xced3('0x64')]['end']);}break;case _0xced3('0x72'):var _0xce41d9=_0x4f1860[_0xced3('0x64')][_0xced3('0x30')](',')[_0xced3('0x2c')](function(_0x21eeec){return Number(_0x21eeec);});_0xce41d9[_0xced3('0x31')](function(_0x206dd3){_0x4bd3cb['or'](_0xced3('0x73'),'%,'+_0x206dd3+',%');});_0x1c69df=_[_0xced3('0x74')](_0x1c69df,_0xce41d9);break;case _0xced3('0x75'):if(_0x4f1860[_0xced3('0x69')]===_0xced3('0x76')){_0x3e2a19[_0x53724a]('CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','%'+_0x4f1860[_0xced3('0x64')]+'%',null);}else{_0xf4d7bf=_[_0xced3('0x6d')](_0x2a6dbf,[_0xced3('0x29'),_0xced3('0x77')]);_0x4f1860['field']=_0xced3('0x77');_0x58d7fc=as[_0xced3('0x6f')](_0x11eb76,_0xf4d7bf[_0xced3('0x3b')],_0x4f1860);_0x3e2a19[_0x53724a](_0x58d7fc[_0xced3('0x70')],_0x58d7fc[_0xced3('0x64')][_0xced3('0x71')],_0x58d7fc[_0xced3('0x64')]['end']);}break;case _0xced3('0x78'):_0x58d7fc=as[_0xced3('0x6f')]('me',null,_0x4f1860);_0x3e2a19[_0x53724a](_0x58d7fc['text'],_0x58d7fc[_0xced3('0x64')][_0xced3('0x71')],_0x58d7fc[_0xced3('0x64')][_0xced3('0x79')]);break;}}else{var _0x58d7fc=as[_0xced3('0x6f')](_0x11eb76,_0xf4d7bf[_0xced3('0x3b')],_0x4f1860);_0x3e2a19[_0x53724a](_0x58d7fc[_0xced3('0x70')],_0x58d7fc[_0xced3('0x64')][_0xced3('0x71')],_0x58d7fc[_0xced3('0x64')][_0xced3('0x79')]);}}}else{var _0x3ad0e7=_(_0x410357[_0xced3('0x3d')])[_0xced3('0x3e')]()['map'](function(_0x3d39de){return _[_0xced3('0x7a')](_0x2a6dbf,['name',_0x3d39de])?_0x3d39de:undefined;})['compact']()[_0xced3('0x64')]();if(!_['isEmpty'](_0x3ad0e7)){_0x3ad0e7[_0xced3('0x31')](function(_0x256216){if(_0x256216==='MailAccountId'){_0x3700ed[_0xced3('0x48')](_0xced3('0x7b'),[]['concat'](_0x410357[_0xced3('0x3d')][_0x256216]));}else if(_0x256216==='createdAt'){var _0x39eead=JSON['parse'](_0x410357[_0xced3('0x3d')][_0x256216])[_0xced3('0x7c')];var _0x11f393=JSON['parse'](_0x410357[_0xced3('0x3d')][_0x256216])[_0xced3('0x7d')];_0x3700ed[_0xced3('0x48')]('i.createdAt\x20BETWEEN\x20?\x20AND\x20?',_0x39eead,_0x11f393);}else{if(_0x410357[_0xced3('0x3d')][_0x256216]===_0xced3('0x7e')){_0x3700ed[_0xced3('0x48')]('i.'+_0x256216+_0xced3('0x7f'));}else{_0x3700ed['where']('i.'+_0x256216+'\x20=\x20?',_0x410357[_0xced3('0x3d')][_0x256216]);}}});}if(_0x410357[_0xced3('0x3d')][_0xced3('0x80')]){if(_0x410357[_0xced3('0x3d')][_0xced3('0x80')]==0x1){_0x3700ed[_0xced3('0x65')]('`unreadMessages`\x20=\x200');}else{_0x3700ed[_0xced3('0x65')]('`unreadMessages`\x20>\x200');}}if(_0x410357[_0xced3('0x3d')][_0xced3('0x81')]){_0x1c69df=_[_0xced3('0x82')](_0x410357[_0xced3('0x3d')]['tag'])?_0x410357[_0xced3('0x3d')][_0xced3('0x81')]:new Array(_0x410357['query']['tag']);_0x1c69df[_0xced3('0x31')](function(_0x14818f){_0x4bd3cb['or'](_0xced3('0x73'),'%,'+_0x14818f+',%');});}if(_0x410357[_0xced3('0x3d')][_0xced3('0x83')]){var _0x56c948=_0x410357[_0xced3('0x3d')][_0xced3('0x83')][_0xced3('0x84')]('\x5c','\x5c\x5c')[_0xced3('0x84')](/'/g,'\x27\x27');if(qs['isNumeric'](_0x56c948)){_0x3e2a19['or'](_0xced3('0x85'),_0x56c948+'%');}else if(qs[_0xced3('0x86')](_0x56c948)){_0x3e2a19['or'](_0xced3('0x87'),_0x56c948+'%');}else{_0x3e2a19['or'](_0xced3('0x88'),qs[_0xced3('0x6b')](_0x56c948))['or'](_0xced3('0x89'),qs[_0xced3('0x6b')](_0x56c948));}}}_0x3700ed[_0xced3('0x48')](_0x3e2a19);_0x3700ed[_0xced3('0x8a')](_0xced3('0x8b'));var _0x4d8196={'type':db[_0xced3('0x8c')][_0xced3('0x8d')]['SELECT'],'raw':!![]};var _0x284cf2=_0x3700ed[_0xced3('0x8e')]();_0x284cf2[_0xced3('0x4a')](_0xced3('0x8b'));_0x284cf2[_0xced3('0x4a')]('SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)',_0xced3('0x63'));if(_0x5f0ab8[_0xced3('0x8f')]){_0x5f0ab8['order'][_0xced3('0x31')](function(_0xab85c0){var _0x2c8929=_0xab85c0[0x0]===_0xced3('0x90')?_0xab85c0[0x0]:'i.'+_0xab85c0[0x0];_0x3700ed[_0xced3('0x8f')](_0x2c8929,_0xab85c0[0x1]===_0xced3('0x91')?![]:!![]);});}if(!_['isEmpty'](_0x1c69df)){_0x3700ed[_0xced3('0x65')](_0x4bd3cb);_0x284cf2[_0xced3('0x48')](_0xced3('0x92'),_0x1c69df);}BPromise[_0xced3('0x93')]()[_0xced3('0x26')](function(){if(!_0x27ce0b){if(_0x410357[_0xced3('0x3d')]['MailAccountId'])return;_0x284cf2['where'](_0xced3('0x94'));_0x3700ed[_0xced3('0x48')](_0xced3('0x94'));return;}if(_0x410357[_0xced3('0x95')][_0xced3('0x96')]!==_0xced3('0x97'))return;return _0x410357[_0xced3('0x95')]['getMailAccounts']({'attributes':['id'],'raw':!![]})[_0xced3('0x26')](function(_0x290ca3){if(_['isEmpty'](_0x290ca3)){_0x284cf2['where'](_0xced3('0x98'));_0x3700ed[_0xced3('0x48')](_0xced3('0x98'));}else{_0x284cf2[_0xced3('0x48')](_0xced3('0x7b'),_['map'](_0x290ca3,'id'));_0x3700ed[_0xced3('0x48')]('i.MailAccountId\x20IN\x20?',_[_0xced3('0x2c')](_0x290ca3,'id'));}});})[_0xced3('0x26')](function(){if(_0x410357[_0xced3('0x95')][_0xced3('0x96')]===_0xced3('0x97')&&!_0x27ce0b&&!_0x410357['query'][_0xced3('0x99')])return[];return db[_0xced3('0x9a')][_0xced3('0x3d')](_0x284cf2[_0xced3('0x9b')](),_0x4d8196);})['then'](function(_0x52df3c){_0x15ca03[_0xced3('0x1f')]=_0x52df3c[_0xced3('0x44')];if(_0x15ca03[_0xced3('0x1f')]===0x0)return[];return db[_0xced3('0x9a')][_0xced3('0x3d')](_0x198aad[_0xced3('0x9b')](),_0x4d8196)[_0xced3('0x26')](function(_0x1036c2){_0x26d16c=_['keyBy'](_0x1036c2,'id');_0x4d8196=_['merge'](_0x4d8196,{'model':db[_0xced3('0x33')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x410357[_0xced3('0x3d')][_0xced3('0x43')]){_0x5f0ab8[_0xced3('0x42')][_0xced3('0x31')](function(_0x2ba321){_0x3700ed[_0xced3('0x4a')]('i.'+_0x2ba321);});}else{_0x3700ed[_0xced3('0x4a')](_0xced3('0x9c'),_0xced3('0x63'));_0x3700ed[_0xced3('0x4a')]('a.key',_0xced3('0x9d'));_0x3700ed['field'](_0xced3('0x9e'),_0xced3('0x9f'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa0'),_0xced3('0x90'));_0x3700ed[_0xced3('0x4a')](_0xced3('0x8b'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa1'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa2'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa3'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa4'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa5'));_0x3700ed['field']('i.lastMsgAt');_0x3700ed['field'](_0xced3('0xa6'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa7'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xa8'));_0x3700ed[_0xced3('0x4a')]('i.secondDisposition');_0x3700ed[_0xced3('0x4a')](_0xced3('0xa9'));_0x3700ed['field']('i.substatus');_0x3700ed[_0xced3('0x4a')](_0xced3('0xaa'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xab'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xac'));_0x3700ed['field'](_0xced3('0xad'));_0x3700ed['field'](_0xced3('0xae'),_0xced3('0xaf'));_0x3700ed[_0xced3('0x4a')]('c.lastName','Contact.lastName');_0x3700ed['field'](_0xced3('0xb0'),_0xced3('0xb1'));_0x3700ed['field'](_0xced3('0xb2'),_0xced3('0xb3'));_0x3700ed['field'](_0xced3('0xb4'),_0xced3('0xb5'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xb6'),_0xced3('0xb7'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xb8'),_0xced3('0xb9'));_0x3700ed['field'](_0xced3('0xba'),_0xced3('0xbb'));_0x3700ed[_0xced3('0x4a')](_0xced3('0xbc'),_0xced3('0x2e'));}if(_0x5f0ab8[_0xced3('0x21')])_0x3700ed[_0xced3('0x21')](_0x5f0ab8[_0xced3('0x21')]);if(_0x5f0ab8[_0xced3('0x20')])_0x3700ed[_0xced3('0x20')](_0x5f0ab8[_0xced3('0x20')]);return db[_0xced3('0x9a')][_0xced3('0x3d')](_0x3700ed[_0xced3('0x9b')](),_0x4d8196);})[_0xced3('0x26')](function(_0x58c587){_0x532db9[_0xced3('0x48')](_0xced3('0xbd'),_[_0xced3('0x2c')](_0x58c587,'id'));return db[_0xced3('0x9a')][_0xced3('0x3d')](_0x532db9['toString'](),_0x4d8196)['then'](function(_0x9c4096){var _0x1f9354=_[_0xced3('0xbe')](_0x9c4096,_0xced3('0xbf'));var _0x81b3bc=[];return db[_0xced3('0xc0')][_0xced3('0x34')]({'attributes':['enableEmailPreview'],'raw':!![]})['then'](function(_0x394416){var _0x4a720b=_0x394416[_0xced3('0xc1')];_0x58c587[_0xced3('0x31')](function(_0x236a53){_0x81b3bc[_0xced3('0x32')](updateMailInteraction(_0x236a53,_0x26d16c,_0x1f9354,_0x4a720b));});return BPromise[_0xced3('0xc2')](_0x81b3bc);});});});})[_0xced3('0x26')](function(_0xaec7bf){_0x15ca03[_0xced3('0xc3')]=_0xaec7bf;return _0x15ca03;})[_0xced3('0x26')](respondWithFilteredResult(_0x413da3,_0x5f0ab8))[_0xced3('0x39')](handleError(_0x413da3,null));};exports[_0xced3('0xc4')]=function(_0x28a497,_0x1994d3){var _0xbd91ef={'raw':![],'where':{'id':_0x28a497['params']['id']}},_0x561aca={};_0x561aca[_0xced3('0x41')]=_['keys'](db[_0xced3('0x33')]['rawAttributes']);_0x561aca['query']=_[_0xced3('0x3e')](_0x28a497[_0xced3('0x3d')]);_0x561aca[_0xced3('0x3f')]=_[_0xced3('0x40')](_0x561aca['model'],_0x561aca['query']);_0xbd91ef[_0xced3('0x42')]=_[_0xced3('0x40')](_0x561aca[_0xced3('0x41')],qs['fields'](_0x28a497[_0xced3('0x3d')]['fields']));_0xbd91ef['attributes']=_0xbd91ef[_0xced3('0x42')][_0xced3('0x44')]?_0xbd91ef[_0xced3('0x42')]:_0x561aca[_0xced3('0x41')];if(_0x28a497[_0xced3('0x3d')][_0xced3('0xc5')]){_0xbd91ef[_0xced3('0xc6')]=[{'all':!![]}];}_0xbd91ef=_[_0xced3('0xc7')]({},_0xbd91ef,_0x28a497['options']);return db['MailInteraction'][_0xced3('0x6d')](_0xbd91ef)['then'](handleEntityNotFound(_0x1994d3,null))[_0xced3('0x26')](respondWithResult(_0x1994d3,null))[_0xced3('0x39')](handleError(_0x1994d3,null));};exports['create']=function(_0x2e7c5c,_0x2e79d0){return db[_0xced3('0x33')][_0xced3('0xc8')](_0x2e7c5c[_0xced3('0xc9')],{})[_0xced3('0x26')](respondWithResult(_0x2e79d0,0xc9))['catch'](handleError(_0x2e79d0,null));};exports[_0xced3('0x25')]=function(_0x3e265,_0x3887a5){if(_0x3e265[_0xced3('0xc9')]['id']){delete _0x3e265[_0xced3('0xc9')]['id'];}return db[_0xced3('0x33')]['find']({'where':{'id':_0x3e265['params']['id']}})['then'](handleEntityNotFound(_0x3887a5,null))[_0xced3('0x26')](saveUpdates(_0x3e265[_0xced3('0xc9')],null))[_0xced3('0x26')](respondWithResult(_0x3887a5,null))[_0xced3('0x39')](handleError(_0x3887a5,null));};exports[_0xced3('0x27')]=function(_0x4af496,_0x5e31dd){return db[_0xced3('0x33')][_0xced3('0x6d')]({'where':{'id':_0x4af496['params']['id']}})['then'](handleEntityNotFound(_0x5e31dd,null))[_0xced3('0x26')](removeEntity(_0x5e31dd,null))[_0xced3('0x39')](handleError(_0x5e31dd,null));};exports[_0xced3('0xca')]=function(_0xbeffe6,_0x2211ed){return db[_0xced3('0x33')][_0xced3('0xca')]()[_0xced3('0x26')](respondWithResult(_0x2211ed,null))[_0xced3('0x39')](handleError(_0x2211ed,null));};exports[_0xced3('0xcb')]=function(_0x54d810,_0x7cfba,_0x212446){return db['MailMessage'][_0xced3('0x6d')]({'where':{'id':_0x54d810[_0xced3('0xcc')]['id']}})[_0xced3('0x26')](handleEntityNotFound(_0x7cfba,null))[_0xced3('0x26')](function(_0x4173eb){if(_0x4173eb){return _0x4173eb[_0xced3('0xcb')](_0x54d810[_0xced3('0xc9')][_0xced3('0xcd')],_['omit'](_0x54d810['body'],[_0xced3('0xcd'),'id'])||{});}})[_0xced3('0x26')](respondWithResult(_0x7cfba,null))['catch'](handleError(_0x7cfba,null));};exports[_0xced3('0xce')]=function(_0x445dff,_0x1d174e,_0x22b801){var _0x438582={'raw':![],'where':{}};var _0x477619={};var _0x5b53cb={'count':0x0,'rows':[]};return db[_0xced3('0x33')]['findOne']({'where':{'id':_0x445dff['params']['id']}})[_0xced3('0x26')](handleEntityNotFound(_0x1d174e,null))[_0xced3('0x26')](function(_0x21f013){if(_0x21f013){_0x477619[_0xced3('0x41')]=_['keys'](db[_0xced3('0xcf')][_0xced3('0xd0')]);_0x477619['query']=_['keys'](_0x445dff['query']);_0x477619[_0xced3('0x3f')]=_[_0xced3('0x40')](_0x477619[_0xced3('0x41')],_0x477619[_0xced3('0x3d')]);_0x438582[_0xced3('0x42')]=_[_0xced3('0x40')](_0x477619[_0xced3('0x41')],qs[_0xced3('0x43')](_0x445dff[_0xced3('0x3d')][_0xced3('0x43')]));_0x438582['attributes']=_0x438582['attributes'][_0xced3('0x44')]?_0x438582[_0xced3('0x42')]:_0x477619['model'];if(!_0x445dff['query'][_0xced3('0x45')](_0xced3('0x46'))){_0x438582[_0xced3('0x21')]=qs['limit'](_0x445dff[_0xced3('0x3d')]['limit']);_0x438582[_0xced3('0x20')]=qs['offset'](_0x445dff[_0xced3('0x3d')]['offset']);}_0x438582[_0xced3('0x8f')]=qs[_0xced3('0x47')](_0x445dff[_0xced3('0x3d')]['sort']);_0x438582['where']=qs[_0xced3('0x3f')](_[_0xced3('0x49')](_0x445dff[_0xced3('0x3d')],_0x477619[_0xced3('0x3f')]));_0x438582['where'][_0xced3('0xbf')]=_0x21f013['id'];if(_0x445dff[_0xced3('0x3d')][_0xced3('0x83')]){_0x438582[_0xced3('0x48')]=_['merge'](_0x438582[_0xced3('0x48')],{'$or':_[_0xced3('0x2c')](_0x438582[_0xced3('0x42')],function(_0x4073b9){var _0x404b7d={};_0x404b7d[_0x4073b9]={'$like':'%'+_0x445dff[_0xced3('0x3d')][_0xced3('0x83')]+'%'};return _0x404b7d;})});}if(_0x445dff[_0xced3('0x3d')]['$gte']){var _0x1aaa1c=_0x445dff[_0xced3('0x3d')][_0xced3('0x7c')][_0xced3('0x30')](',');var _0x2bac8f={};_0x2bac8f[_0x1aaa1c[0x0]]={'$gte':moment(_0x1aaa1c[0x1])['format']('YYYY-MM-DD\x20HH:mm:ss')};_0x438582[_0xced3('0x48')]=_[_0xced3('0xc7')](_0x438582[_0xced3('0x48')],_0x2bac8f);}_0x438582=_[_0xced3('0xc7')]({},_0x438582,_0x445dff['options']);return db[_0xced3('0xcf')]['count']({'where':_0x438582['where']})[_0xced3('0x26')](function(_0x473560){_0x5b53cb['count']=_0x473560;if(_0x445dff[_0xced3('0x3d')]['includeAll']){_0x438582[_0xced3('0xc6')]=[{'all':!![]}];}return db['MailMessage'][_0xced3('0xd1')](_0x438582);})[_0xced3('0x26')](function(_0x38e79e){if(_0x445dff[_0xced3('0x3d')][_0xced3('0xd2')]===_0xced3('0xd3')){for(var _0xd6e93c=0x0;_0xd6e93c<_0x38e79e[_0xced3('0x44')];_0xd6e93c++){_0x38e79e[_0xd6e93c]['body']=juice(_0x38e79e[_0xd6e93c][_0xced3('0xc9')]);}}_0x5b53cb[_0xced3('0xc3')]=_0x38e79e;return _0x5b53cb;});}})[_0xced3('0x26')](respondWithFilteredResult(_0x1d174e,_0x438582))[_0xced3('0x39')](handleError(_0x1d174e,null));};exports[_0xced3('0xd4')]=function(_0x48343e,_0x3dadb4,_0x35bb1c){return db[_0xced3('0x33')][_0xced3('0x6d')]({'where':{'id':_0x48343e[_0xced3('0xcc')]['id']}})[_0xced3('0x26')](handleEntityNotFound(_0x3dadb4,null))[_0xced3('0x26')](function(_0x178619){if(_0x178619){return _0x178619['setTags'](_0x48343e[_0xced3('0xc9')]['ids'],_[_0xced3('0xd5')](_0x48343e[_0xced3('0xc9')],[_0xced3('0xcd'),'id'])||{})[_0xced3('0xd6')](function(){return db[_0xced3('0x72')]['findAll']({'attributes':['id',_0xced3('0x29'),'color'],'where':{'id':_0x48343e[_0xced3('0xc9')][_0xced3('0xcd')]}});})[_0xced3('0x26')](function(_0x29db96){socket['emit'](_0xced3('0xd7'),{'id':Number(_0x48343e[_0xced3('0xcc')]['id']),'tags':_0x29db96||[]});return{'id':Number(_0x48343e[_0xced3('0xcc')]['id']),'tags':_0x29db96||[]};});}})['then'](respondWithResult(_0x3dadb4,null))[_0xced3('0x39')](handleError(_0x3dadb4,null));};exports['removeTags']=function(_0x2a3216,_0x5d25b7,_0x4568e4){return db[_0xced3('0x33')][_0xced3('0x6d')]({'where':{'id':_0x2a3216[_0xced3('0xcc')]['id']}})[_0xced3('0x26')](handleEntityNotFound(_0x5d25b7,null))['then'](function(_0x1052ea){if(_0x1052ea){return _0x1052ea['removeTags'](_0x2a3216[_0xced3('0x3d')][_0xced3('0xcd')]);}})[_0xced3('0x26')](respondWithStatusCode(_0x5d25b7,null))[_0xced3('0x39')](handleError(_0x5d25b7,null));};exports['download']=function(_0x2c2efe,_0x35dfd4){var _0x5ca892=moment()[_0xced3('0xd8')]()[_0xced3('0x9b')]();var _0x6ae24f=path[_0xced3('0xd9')](config[_0xced3('0xda')],_0xced3('0xdb'),_0xced3('0xdc'),_0xced3('0xdd'));var _0x4d3a09=path['join'](config['root'],_0xced3('0xdb'),'files','attachments');var _0x2efdf6=path['join'](_0x6ae24f,_0x5ca892);var _0x4b93df=util[_0xced3('0xde')]('transcript-%d-%s.zip',_0x2c2efe['params']['id'],_0x5ca892);var _0x297523=path['join'](_0x6ae24f,_0x4b93df);var _0x29b3f4=[];if(_0x2c2efe['query'][_0xced3('0xdf')]){_0x29b3f4[_0xced3('0x32')]({'model':db[_0xced3('0xe0')],'as':'Attachments','raw':!![]});}var _0x5a4a32=[{'model':db[_0xced3('0xcf')],'as':_0xced3('0xe1'),'attributes':['id',_0xced3('0xc9'),_0xced3('0x4c'),'to','cc',_0xced3('0xe2'),_0xced3('0xe3'),'createdAt'],'where':{'secret':![]},'order':qs[_0xced3('0x47')]('id'),'include':_0x29b3f4}];return db[_0xced3('0x33')][_0xced3('0x6d')]({'where':{'id':_0x2c2efe[_0xced3('0xcc')]['id']},'include':_0x5a4a32})['then'](handleEntityNotFound(_0x35dfd4,null))['then'](function(_0x2e511e){if(_0x2e511e&&_0x2e511e[_0xced3('0xe1')]){var _0x329e5e=_0x2e511e['get']({'plain':!![]});fs[_0xced3('0xe4')](_0x2efdf6);return BPromise[_0xced3('0xe5')](_0x329e5e[_0xced3('0xe1')],function(_0x34a909,_0x54f62f){var _0x196276={'from':_0x34a909[_0xced3('0x4c')],'to':_0x34a909['to'],'subject':_0x34a909[_0xced3('0xe3')],'html':_0x34a909[_0xced3('0xc9')],'headers':{'Date':moment(_0x34a909[_0xced3('0xe6')])['format']('ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ')},'attachments':[]};if(_0x34a909['cc']){_0x196276[_0xced3('0xe7')]['Cc']=_0x34a909['cc'];}if(_0x34a909[_0xced3('0xe2')]){_0x196276[_0xced3('0xe7')][_0xced3('0xe8')]=_0x34a909[_0xced3('0xe2')];}return new BPromise(function(_0x903ab8,_0xefa174){if(_0x34a909[_0xced3('0xe9')]){for(var _0x23f168=0x0;_0x23f168<_0x34a909['Attachments'][_0xced3('0x44')];_0x23f168++){var _0x10f5f8=_0x34a909[_0xced3('0xe9')][_0x23f168];var _0x87545=path[_0xced3('0xd9')](_0x4d3a09,_0x10f5f8['basename']);if(fs['existsSync'](_0x87545)){_0x196276[_0xced3('0xdf')][_0xced3('0x32')]({'name':_0x10f5f8['name'],'contentType':_0x10f5f8['type'],'data':fs[_0xced3('0xea')](_0x87545)});}}}emlformat['build'](_0x196276,function(_0x4ef82a,_0x404108){if(_0x4ef82a)return _0xefa174(_0x4ef82a);fs[_0xced3('0xeb')](path[_0xced3('0xd9')](_0x2efdf6,util[_0xced3('0xde')](_0xced3('0xec'),_0x54f62f)),_0x404108);return _0x903ab8(_0x404108);});});})[_0xced3('0x26')](function(){return new BPromise(function(_0x37a7e9,_0x560c00){zipdir(_0x2efdf6,{'saveTo':_0x297523},function(_0x26b3e7,_0x111aa8){if(_0x26b3e7)return _0x560c00(_0x26b3e7);return _0x37a7e9(_0x111aa8);});})[_0xced3('0x26')](function(){return new BPromise(function(_0x311789,_0xbfc8ab){rimraf(_0x2efdf6,function(_0x14df8c){if(_0x14df8c)_0xbfc8ab(_0x14df8c);return _0x311789();});});})[_0xced3('0x26')](function(){return _0x35dfd4[_0xced3('0xed')](_0x297523,_0x4b93df,function(_0x3cbbb3){if(_0x3cbbb3){console[_0xced3('0xee')](_0xced3('0xef'),_0x3cbbb3);}else{fs[_0xced3('0xf0')](_0x297523);}});});});}else{return _0x35dfd4[_0xced3('0x1d')](0xc8);}})[_0xced3('0x39')](handleError(_0x35dfd4,null));};