Built motion from commit (unavailable).|2.4.18
[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 _0xf491=['mail_messages','mail_interaction_has_tags','it.MailInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','search','parseSearch','sqlOperator','length','conditions','unreadMessages','value','having','`\x20=\x200','`\x20>\x200','Contact','operator','$substring','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','text','start','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','union','User','UserId','buildExpression','plainBody','some','compact','isEmpty','MailAccountId','where','i.MailAccountId\x20IN\x20?','concat','parse','$gte','$lte','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','null','\x20IS\x20NULL','read','`unreadMessages`\x20=\x200','`unreadMessages`\x20>\x200','tag','filter','\x27\x27\x27\x27','isNumeric','isEmail','MATCH\x20(i.to,\x20i.subject)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','i.id','Sequelize','QueryTypes','SELECT','clone','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','t.id\x20IN\x20?','journey','i.MailAccountId\x20IS\x20NOT\x20NULL','user','i.MailAccountId\x20IS\x20NULL','sequelize','toString','merge','Account.key','a.mandatoryDisposition','Account.mandatoryDisposition','i.createdAt','i.closedAt','i.attach','i.lastMsgDirection','i.subject','i.disposition','i.substatus','i.to','i.ContactId','i.MailAccountId','c.firstName','c.lastName','Contact.lastName','Contact.phone','c.mobile','c.fax','Contact.fax','Owner.fullname','o.internal','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','groupBy','enableEmailPreview','all','rows','show','params','rawAttributes','includeAll','include','options','body','describe','MailMessage','addMessage','ids','omit','getMessages','hasOwnProperty','nolimit','pick','MailInteractionId','format','YYYY-MM-DD\x20HH:mm:ss','addTags','setTags','Tag','findAll','emit','mailInteractionTags:save','removeTags','unix','root','server','tmp','join','files','attachments','transcript-%d-%s.zip','Attachment','Messages','subject','createdAt','get','mkdirSync','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','bcc','Bcc','Attachments','basename','existsSync','readFileSync','build','writeFileSync','download','log','html-pdf','eml-format','rimraf','request-promise','bluebird','util','path','sox','lodash','desk.js','to-csv','querystring','html-to-text','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','redis','defaults','localhost','socket.io-emitter','./mailInteraction.socket','register','end','count','offset','limit','set','json','apply','reject','save','update','destroy','status','error','stack','name','send','Users','map','TagIds','Tags','split','forEach','push','MailInteraction','findOne','lastMsgBody','lastMsgText','then','trimStart','fromString','catch','fieldName','type','model','query','keys','filters','intersection','attributes','fields','order','sort','select','field','color','from','tools_tags','u.id','u.fullname','ui.MailInteractionId','user_has_mail_interactions','left_join','users','u.id\x20=\x20ui.UserId','mail_interactions','cm_contacts','o.id\x20=\x20i.UserId','a.id\x20=\x20i.MailAccountId'];(function(_0x51dbba,_0x817f0d){var _0x34906d=function(_0x192a8b){while(--_0x192a8b){_0x51dbba['push'](_0x51dbba['shift']());}};_0x34906d(++_0x817f0d);}(_0xf491,0x178));var _0x1f49=function(_0x54ea4f,_0x472285){_0x54ea4f=_0x54ea4f-0x0;var _0x4315b5=_0xf491[_0x54ea4f];return _0x4315b5;};'use strict';var pdf=require(_0x1f49('0x0'));var emlformat=require(_0x1f49('0x1'));var rimraf=require(_0x1f49('0x2'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x1f49('0x3'));var moment=require('moment');var BPromise=require(_0x1f49('0x4'));var Mustache=require('mustache');var util=require(_0x1f49('0x5'));var path=require(_0x1f49('0x6'));var sox=require(_0x1f49('0x7'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0x1f49('0x8'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x1f49('0x9'));var toCsv=require(_0x1f49('0xa'));var querystring=require(_0x1f49('0xb'));var Papa=require('papaparse');var htmlToText=require(_0x1f49('0xc'));var Redis=require(_0x1f49('0xd'));var authService=require(_0x1f49('0xe'));var qs=require(_0x1f49('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x1f49('0x10'));var logger=require(_0x1f49('0x11'))(_0x1f49('0x12'));var utils=require(_0x1f49('0x13'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config[_0x1f49('0x14')]=_[_0x1f49('0x15')](config[_0x1f49('0x14')],{'host':_0x1f49('0x16'),'port':0x18eb});var socket=require(_0x1f49('0x17'))(new Redis(config[_0x1f49('0x14')]));require(_0x1f49('0x18'))[_0x1f49('0x19')](socket);function respondWithStatusCode(_0x1121a2,_0x403bfd){_0x403bfd=_0x403bfd||0xcc;return function(_0x4b1764){if(_0x4b1764){return _0x1121a2['sendStatus'](_0x403bfd);}return _0x1121a2['status'](_0x403bfd)[_0x1f49('0x1a')]();};}function respondWithResult(_0x37948a,_0x118397){_0x118397=_0x118397||0xc8;return function(_0x1bc49b){if(_0x1bc49b){return _0x37948a['status'](_0x118397)['json'](_0x1bc49b);}};}function respondWithFilteredResult(_0x8c2793,_0x210bd1){return function(_0x5acdd1){if(_0x5acdd1){var _0x5cac84=_0x5acdd1[_0x1f49('0x1b')],_0x3e21a7=_0x210bd1[_0x1f49('0x1c')],_0x3f909a=_0x210bd1['offset']+_0x210bd1[_0x1f49('0x1d')],_0x2bf3ca;if(_0x3f909a>=_0x5cac84){_0x3f909a=_0x5cac84;_0x2bf3ca=0xc8;}else{_0x2bf3ca=0xce;}_0x8c2793['status'](_0x2bf3ca);return _0x8c2793[_0x1f49('0x1e')]('Content-Range',_0x3e21a7+'-'+_0x3f909a+'/'+_0x5cac84)[_0x1f49('0x1f')](_0x5acdd1);}return null;};}function patchUpdates(_0x1c3c7e){return function(_0x51f8f7){try{jsonpatch[_0x1f49('0x20')](_0x51f8f7,_0x1c3c7e,!![]);}catch(_0x245844){return BPromise[_0x1f49('0x21')](_0x245844);}return _0x51f8f7[_0x1f49('0x22')]();};}function saveUpdates(_0x286785,_0x38ece0){return function(_0x506383){if(_0x506383){return _0x506383[_0x1f49('0x23')](_0x286785)['then'](function(_0x3a05b3){return _0x3a05b3;});}return null;};}function removeEntity(_0xbb97c,_0x372421){return function(_0x47e9e4){if(_0x47e9e4){return _0x47e9e4[_0x1f49('0x24')]()['then'](function(){_0xbb97c[_0x1f49('0x25')](0xcc)[_0x1f49('0x1a')]();});}};}function handleEntityNotFound(_0x1077ac,_0x2b8f27){return function(_0x374142){if(!_0x374142){_0x1077ac['sendStatus'](0x194);}return _0x374142;};}function handleError(_0x20c586,_0x316b20){_0x316b20=_0x316b20||0x1f4;return function(_0x54d574){logger[_0x1f49('0x26')](_0x54d574[_0x1f49('0x27')]);if(_0x54d574['name']){delete _0x54d574[_0x1f49('0x28')];}_0x20c586[_0x1f49('0x25')](_0x316b20)[_0x1f49('0x29')](_0x54d574);};}function getInteractionUsers(_0x480331,_0x1034d2){return new BPromise(function(_0x5e5134,_0x310763){try{if(_0x1034d2[_0x480331['id']]){_0x480331[_0x1f49('0x2a')]=_[_0x1f49('0x2b')](_0x1034d2[_0x480331['id']],function(_0x5bd85a){return{'id':_0x5bd85a['id'],'fullname':_0x5bd85a['fullname']};});}}catch(_0x270f08){_0x310763(_0x270f08);}_0x5e5134(_0x480331);});}function getInteractionTags(_0x25084c,_0x2a8b08){return new BPromise(function(_0x27c348,_0x4ff31d){try{if(_0x25084c[_0x1f49('0x2c')]){_0x25084c[_0x1f49('0x2d')]=[];_0x25084c[_0x1f49('0x2c')][_0x1f49('0x2e')](',')[_0x1f49('0x2f')](function(_0x37ebb7){_0x25084c[_0x1f49('0x2d')][_0x1f49('0x30')](_0x2a8b08[_0x37ebb7]);});}delete _0x25084c[_0x1f49('0x2c')];}catch(_0x10e4e9){_0x4ff31d(_0x10e4e9);}_0x27c348(_0x25084c);});}function getLastMessageText(_0x4bf1be){return new BPromise(function(_0x51a47e,_0x4269a7){return db[_0x1f49('0x31')][_0x1f49('0x32')]({'where':{'id':_0x4bf1be['id']},'attributes':[_0x1f49('0x33'),_0x1f49('0x34')],'raw':!![]})[_0x1f49('0x35')](function(_0x59e0e3){if(!_0x59e0e3[_0x1f49('0x34')]){_0x4bf1be[_0x1f49('0x34')]=_[_0x1f49('0x36')](htmlToText[_0x1f49('0x37')](_0x59e0e3[_0x1f49('0x33')],{'preserveNewlines':!![],'singleNewLineParagraphs':!![],'uppercaseHeadings':![],'noLinkBrackets':!![],'ignoreImage':!![]}));}else{_0x4bf1be[_0x1f49('0x34')]=_0x59e0e3['lastMsgText'];}_0x51a47e(_0x4bf1be);})['catch'](function(_0x448b92){_0x4269a7(_0x448b92);});});}function updateMailInteraction(_0x233db8,_0x4db279,_0x31ec20,_0x5b7676){return new BPromise(function(_0xe2c2dc,_0xc55c7d){return getInteractionUsers(_0x233db8,_0x31ec20)['then'](function(_0x5d18dc){return getInteractionTags(_0x5d18dc,_0x4db279);})[_0x1f49('0x35')](function(_0x50f6d6){if(_0x5b7676){_0xe2c2dc(getLastMessageText(_0x50f6d6));}else{_0xe2c2dc(_0x50f6d6);}})[_0x1f49('0x38')](function(_0x4215a7){_0xc55c7d(_0x4215a7);});});}exports['index']=function(_0xb8e272,_0xdf8725){var _0x2f7306={},_0x142487={},_0x4ab9a4={'count':0x0,'rows':[]};var _0x78515d=_[_0x1f49('0x2b')](db[_0x1f49('0x31')]['rawAttributes'],function(_0x5cd08b){return{'name':_0x5cd08b[_0x1f49('0x39')],'type':_0x5cd08b[_0x1f49('0x3a')]['key']};});_0x142487[_0x1f49('0x3b')]=_[_0x1f49('0x2b')](_0x78515d,_0x1f49('0x28'));_0x142487[_0x1f49('0x3c')]=_[_0x1f49('0x3d')](_0xb8e272[_0x1f49('0x3c')]);_0x142487[_0x1f49('0x3e')]=_[_0x1f49('0x3f')](_0x142487[_0x1f49('0x3b')],_0x142487[_0x1f49('0x3c')]);_0x2f7306[_0x1f49('0x40')]=_[_0x1f49('0x3f')](_0x142487[_0x1f49('0x3b')],qs[_0x1f49('0x41')](_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x41')]));_0x2f7306[_0x1f49('0x40')]=_0x2f7306[_0x1f49('0x40')]['length']?_0x2f7306[_0x1f49('0x40')]:_0x142487['model'];if(!_0xb8e272[_0x1f49('0x3c')]['hasOwnProperty']('nolimit')){_0x2f7306[_0x1f49('0x1d')]=qs['limit'](_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x1d')]);_0x2f7306['offset']=qs['offset'](_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x1c')]);}_0x2f7306[_0x1f49('0x42')]=qs[_0x1f49('0x43')](_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x43')]);_0x2f7306['where']=qs['filters'](_['pick'](_0xb8e272[_0x1f49('0x3c')],_0x142487[_0x1f49('0x3e')]));var _0xb3089e=[];var _0x560cfd=squel[_0x1f49('0x44')]()[_0x1f49('0x45')]('id')[_0x1f49('0x45')](_0x1f49('0x28'))['field'](_0x1f49('0x46'))[_0x1f49('0x47')](_0x1f49('0x48'));var _0x1e8951=squel[_0x1f49('0x44')]()[_0x1f49('0x45')](_0x1f49('0x49'))['field'](_0x1f49('0x4a'))[_0x1f49('0x45')](_0x1f49('0x4b'))[_0x1f49('0x47')](_0x1f49('0x4c'),'ui')[_0x1f49('0x4d')](_0x1f49('0x4e'),'u',_0x1f49('0x4f'));var _0x2a42af=squel[_0x1f49('0x44')]()[_0x1f49('0x47')](_0x1f49('0x50'),'i')[_0x1f49('0x4d')](_0x1f49('0x51'),'c','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL')[_0x1f49('0x4d')](_0x1f49('0x4e'),'o',_0x1f49('0x52'))[_0x1f49('0x4d')]('mail_accounts','a',_0x1f49('0x53'))['left_join'](_0x1f49('0x54'),'me','me.MailInteractionId\x20=\x20i.id')['left_join'](_0x1f49('0x55'),'it',_0x1f49('0x56'))[_0x1f49('0x4d')](_0x1f49('0x48'),'t',_0x1f49('0x57'));var _0x51d539=squel[_0x1f49('0x58')]();var _0x37035d=[];var _0x163cff=squel[_0x1f49('0x58')]();var _0x558f03;if(_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x59')]){_0x558f03=as[_0x1f49('0x5a')](_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x59')]);var _0x3aefd0=_0x558f03[_0x1f49('0x5b')];for(var _0x2b683c=0x0;_0x2b683c<_0x558f03['conditions'][_0x1f49('0x5c')];_0x2b683c++){var _0xf1472c=_0x558f03[_0x1f49('0x5d')][_0x2b683c];var _0x37e868='i';var _0x34e4e9=_['find'](_0x78515d,['name',_0xf1472c['field']]);if(!_0x34e4e9){switch(_0xf1472c[_0x1f49('0x45')]){case _0x1f49('0x5e'):if(_0xf1472c[_0x1f49('0x5f')]==0x1){_0x2a42af[_0x1f49('0x60')]('`'+_0xf1472c[_0x1f49('0x45')]+_0x1f49('0x61'));}else{_0x2a42af['having']('`'+_0xf1472c[_0x1f49('0x45')]+_0x1f49('0x62'));}break;case _0x1f49('0x63'):if(_0xf1472c[_0x1f49('0x64')]===_0x1f49('0x65')){if(_0xf1472c[_0x1f49('0x5f')][_0x1f49('0x2e')]('\x20')[_0x1f49('0x5c')]>0x1){_0x51d539[_0x3aefd0](_0x1f49('0x66'),qs[_0x1f49('0x67')](_0xf1472c[_0x1f49('0x5f')]),null);}else{var _0x294c28='%'+_0xf1472c[_0x1f49('0x5f')]+'%';_0x51d539[_0x3aefd0](_0x1f49('0x68'),_0x294c28,_0x294c28,_0x294c28);}}else{_0x34e4e9=_[_0x1f49('0x69')](_0x78515d,[_0x1f49('0x28'),_0x1f49('0x6a')]);_0xf1472c[_0x1f49('0x45')]=_0x1f49('0x6a');_0x5ce3e6=as['buildExpression'](_0x37e868,_0x34e4e9[_0x1f49('0x3a')],_0xf1472c);_0x51d539[_0x3aefd0](_0x5ce3e6[_0x1f49('0x6b')],_0x5ce3e6[_0x1f49('0x5f')][_0x1f49('0x6c')],_0x5ce3e6[_0x1f49('0x5f')][_0x1f49('0x1a')]);}break;case'Tag':var _0x3b5a6a=_0xf1472c[_0x1f49('0x5f')][_0x1f49('0x2e')](',')[_0x1f49('0x2b')](function(_0x4746e3){return Number(_0x4746e3);});_0x3b5a6a[_0x1f49('0x2f')](function(_0x189490){_0x163cff['or'](_0x1f49('0x6d'),'%,'+_0x189490+',%');});_0x37035d=_[_0x1f49('0x6e')](_0x37035d,_0x3b5a6a);break;case _0x1f49('0x6f'):if(_0xf1472c[_0x1f49('0x64')]==='$substring'){_0x51d539[_0x3aefd0]('CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','%'+_0xf1472c[_0x1f49('0x5f')]+'%',null);}else{_0x34e4e9=_[_0x1f49('0x69')](_0x78515d,[_0x1f49('0x28'),_0x1f49('0x70')]);_0xf1472c['field']=_0x1f49('0x70');_0x5ce3e6=as[_0x1f49('0x71')](_0x37e868,_0x34e4e9['type'],_0xf1472c);_0x51d539[_0x3aefd0](_0x5ce3e6['text'],_0x5ce3e6[_0x1f49('0x5f')][_0x1f49('0x6c')],_0x5ce3e6[_0x1f49('0x5f')]['end']);}break;case _0x1f49('0x72'):_0x5ce3e6=as[_0x1f49('0x71')]('me',null,_0xf1472c);_0x51d539[_0x3aefd0](_0x5ce3e6[_0x1f49('0x6b')],_0x5ce3e6[_0x1f49('0x5f')][_0x1f49('0x6c')],_0x5ce3e6[_0x1f49('0x5f')][_0x1f49('0x1a')]);break;}}else{var _0x5ce3e6=as[_0x1f49('0x71')](_0x37e868,_0x34e4e9[_0x1f49('0x3a')],_0xf1472c);_0x51d539[_0x3aefd0](_0x5ce3e6[_0x1f49('0x6b')],_0x5ce3e6['value'][_0x1f49('0x6c')],_0x5ce3e6[_0x1f49('0x5f')][_0x1f49('0x1a')]);}}}else{var _0x5a8602=_(_0xb8e272[_0x1f49('0x3c')])[_0x1f49('0x3d')]()[_0x1f49('0x2b')](function(_0x36e014){return _[_0x1f49('0x73')](_0x78515d,[_0x1f49('0x28'),_0x36e014])?_0x36e014:undefined;})[_0x1f49('0x74')]()[_0x1f49('0x5f')]();if(!_[_0x1f49('0x75')](_0x5a8602)){_0x5a8602[_0x1f49('0x2f')](function(_0x29445b){if(_0x29445b===_0x1f49('0x76')){_0x2a42af[_0x1f49('0x77')](_0x1f49('0x78'),[][_0x1f49('0x79')](_0xb8e272[_0x1f49('0x3c')][_0x29445b]));}else if(_0x29445b==='createdAt'){var _0x2e25c0=JSON[_0x1f49('0x7a')](_0xb8e272['query'][_0x29445b])[_0x1f49('0x7b')];var _0x355aa9=JSON[_0x1f49('0x7a')](_0xb8e272['query'][_0x29445b])[_0x1f49('0x7c')];_0x2a42af[_0x1f49('0x77')](_0x1f49('0x7d'),_0x2e25c0,_0x355aa9);}else{if(_0xb8e272[_0x1f49('0x3c')][_0x29445b]===_0x1f49('0x7e')){_0x2a42af['where']('i.'+_0x29445b+_0x1f49('0x7f'));}else{_0x2a42af[_0x1f49('0x77')]('i.'+_0x29445b+'\x20=\x20?',_0xb8e272[_0x1f49('0x3c')][_0x29445b]);}}});}if(_0xb8e272['query'][_0x1f49('0x80')]){if(_0xb8e272['query'][_0x1f49('0x80')]==0x1){_0x2a42af[_0x1f49('0x60')](_0x1f49('0x81'));}else{_0x2a42af[_0x1f49('0x60')](_0x1f49('0x82'));}}if(_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x83')]){_0x37035d=_['isArray'](_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x83')])?_0xb8e272['query'][_0x1f49('0x83')]:new Array(_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x83')]);_0x37035d[_0x1f49('0x2f')](function(_0x1cba6f){_0x163cff['or'](_0x1f49('0x6d'),'%,'+_0x1cba6f+',%');});}if(_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x84')]){var _0x2d78e4=_0xb8e272['query']['filter']['replace']('\x5c','\x5c\x5c')['replace']('\x27\x27',_0x1f49('0x85'));if(qs[_0x1f49('0x86')](_0x2d78e4)){_0x51d539['or']('i.id\x20LIKE\x20?',_0x2d78e4+'%');}else if(qs[_0x1f49('0x87')](_0x2d78e4)){_0x51d539['or']('c.email\x20LIKE\x20?',_0x2d78e4+'%');}else{_0x51d539['or'](_0x1f49('0x88'),qs[_0x1f49('0x67')](_0x2d78e4))['or']('MATCH\x20(me.plainBody,\x20me.subject)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0x1f49('0x67')](_0x2d78e4));}}}_0x2a42af[_0x1f49('0x77')](_0x51d539);_0x2a42af['group'](_0x1f49('0x89'));var _0x1ed077={'type':db[_0x1f49('0x8a')][_0x1f49('0x8b')][_0x1f49('0x8c')],'raw':!![]};var _0x435cb2=_0x2a42af[_0x1f49('0x8d')]();_0x435cb2['field']('i.id');_0x435cb2[_0x1f49('0x45')](_0x1f49('0x8e'),_0x1f49('0x5e'));if(_0x2f7306[_0x1f49('0x42')]){_0x2f7306[_0x1f49('0x42')]['forEach'](function(_0x3fe2ff){_0x2a42af[_0x1f49('0x42')]('i.'+_0x3fe2ff[0x0],_0x3fe2ff[0x1]==='DESC'?![]:!![]);});}if(!_[_0x1f49('0x75')](_0x37035d)){_0x2a42af[_0x1f49('0x60')](_0x163cff);_0x435cb2['where'](_0x1f49('0x8f'),_0x37035d);}BPromise['resolve']()[_0x1f49('0x35')](function(){if(!_0x558f03){if(_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x76')]||_0xb8e272[_0x1f49('0x3c')][_0x1f49('0x90')])return;_0x435cb2[_0x1f49('0x77')]('i.MailAccountId\x20IS\x20NOT\x20NULL');_0x2a42af[_0x1f49('0x77')](_0x1f49('0x91'));return;}return _0xb8e272[_0x1f49('0x92')]['getMailAccounts']({'attributes':['id'],'raw':!![]})[_0x1f49('0x35')](function(_0x3f2508){if(_['isEmpty'](_0x3f2508)){_0x435cb2['where'](_0x1f49('0x93'));_0x2a42af['where'](_0x1f49('0x93'));}else{_0x435cb2[_0x1f49('0x77')]('i.MailAccountId\x20IN\x20?',_[_0x1f49('0x2b')](_0x3f2508,'id'));_0x2a42af['where'](_0x1f49('0x78'),_[_0x1f49('0x2b')](_0x3f2508,'id'));}});})[_0x1f49('0x35')](function(){db[_0x1f49('0x94')]['query'](_0x435cb2[_0x1f49('0x95')](),_0x1ed077)['then'](function(_0x43d138){_0x4ab9a4[_0x1f49('0x1b')]=_0x43d138[_0x1f49('0x5c')];if(_0x4ab9a4[_0x1f49('0x1b')]===0x0)return[];return db['sequelize'][_0x1f49('0x3c')](_0x560cfd['toString'](),_0x1ed077)['then'](function(_0x376de2){_0xb3089e=_['keyBy'](_0x376de2,'id');_0x1ed077=_[_0x1f49('0x96')](_0x1ed077,{'model':db[_0x1f49('0x31')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0xb8e272['query'][_0x1f49('0x41')]){_0x2f7306[_0x1f49('0x40')]['forEach'](function(_0x34cf7d){_0x2a42af[_0x1f49('0x45')]('i.'+_0x34cf7d);});}else{_0x2a42af['field'](_0x1f49('0x8e'),'unreadMessages');_0x2a42af[_0x1f49('0x45')]('a.key',_0x1f49('0x97'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0x98'),_0x1f49('0x99'));_0x2a42af['field']('i.id');_0x2a42af[_0x1f49('0x45')](_0x1f49('0x9a'));_0x2a42af[_0x1f49('0x45')]('i.updatedAt');_0x2a42af[_0x1f49('0x45')]('i.closed');_0x2a42af[_0x1f49('0x45')](_0x1f49('0x9b'));_0x2a42af['field'](_0x1f49('0x9c'));_0x2a42af[_0x1f49('0x45')]('i.lastMsgAt');_0x2a42af[_0x1f49('0x45')](_0x1f49('0x9d'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0x9e'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0x9f'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa0'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa1'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa2'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa3'));_0x2a42af[_0x1f49('0x45')]('i.UserId');_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa4'),'Contact.firstName');_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa5'),_0x1f49('0xa6'));_0x2a42af[_0x1f49('0x45')]('c.email','Contact.email');_0x2a42af[_0x1f49('0x45')]('c.phone',_0x1f49('0xa7'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa8'),'Contact.mobile');_0x2a42af[_0x1f49('0x45')](_0x1f49('0xa9'),_0x1f49('0xaa'));_0x2a42af[_0x1f49('0x45')]('o.fullname',_0x1f49('0xab'));_0x2a42af['field'](_0x1f49('0xac'),_0x1f49('0xad'));_0x2a42af[_0x1f49('0x45')](_0x1f49('0xae'),_0x1f49('0x2c'));}_0x2a42af[_0x1f49('0x1d')](_0x2f7306[_0x1f49('0x1d')]);_0x2a42af[_0x1f49('0x1c')](_0x2f7306[_0x1f49('0x1c')]);return db[_0x1f49('0x94')]['query'](_0x2a42af['toString'](),_0x1ed077);})['then'](function(_0x44ca27){_0x1e8951[_0x1f49('0x77')]('ui.MailInteractionId\x20IN\x20?',_[_0x1f49('0x2b')](_0x44ca27,'id'));return db[_0x1f49('0x94')][_0x1f49('0x3c')](_0x1e8951['toString'](),_0x1ed077)['then'](function(_0x5e6d72){var _0x4e7470=_[_0x1f49('0xaf')](_0x5e6d72,'MailInteractionId');var _0x2b43b7=[];return db['Setting'][_0x1f49('0x32')]({'attributes':[_0x1f49('0xb0')],'raw':!![]})[_0x1f49('0x35')](function(_0x3e7984){var _0x1414fc=_0x3e7984[_0x1f49('0xb0')];_0x44ca27[_0x1f49('0x2f')](function(_0x558209){_0x2b43b7[_0x1f49('0x30')](updateMailInteraction(_0x558209,_0xb3089e,_0x4e7470,_0x1414fc));});return BPromise[_0x1f49('0xb1')](_0x2b43b7);});});});})['then'](function(_0x24f9f7){_0x4ab9a4[_0x1f49('0xb2')]=_0x24f9f7;return _0x4ab9a4;})['then'](respondWithFilteredResult(_0xdf8725,_0x2f7306));})[_0x1f49('0x38')](handleError(_0xdf8725,null));};exports[_0x1f49('0xb3')]=function(_0x2d2424,_0x246c61){var _0x96b970={'raw':![],'where':{'id':_0x2d2424[_0x1f49('0xb4')]['id']}},_0x3d2760={};_0x3d2760[_0x1f49('0x3b')]=_[_0x1f49('0x3d')](db['MailInteraction'][_0x1f49('0xb5')]);_0x3d2760['query']=_[_0x1f49('0x3d')](_0x2d2424[_0x1f49('0x3c')]);_0x3d2760[_0x1f49('0x3e')]=_[_0x1f49('0x3f')](_0x3d2760[_0x1f49('0x3b')],_0x3d2760[_0x1f49('0x3c')]);_0x96b970[_0x1f49('0x40')]=_['intersection'](_0x3d2760[_0x1f49('0x3b')],qs['fields'](_0x2d2424['query'][_0x1f49('0x41')]));_0x96b970[_0x1f49('0x40')]=_0x96b970[_0x1f49('0x40')]['length']?_0x96b970[_0x1f49('0x40')]:_0x3d2760['model'];if(_0x2d2424[_0x1f49('0x3c')][_0x1f49('0xb6')]){_0x96b970[_0x1f49('0xb7')]=[{'all':!![]}];}_0x96b970=_[_0x1f49('0x96')]({},_0x96b970,_0x2d2424[_0x1f49('0xb8')]);return db[_0x1f49('0x31')][_0x1f49('0x69')](_0x96b970)[_0x1f49('0x35')](handleEntityNotFound(_0x246c61,null))[_0x1f49('0x35')](respondWithResult(_0x246c61,null))[_0x1f49('0x38')](handleError(_0x246c61,null));};exports['create']=function(_0x10ea1e,_0x3f9310){return db[_0x1f49('0x31')]['create'](_0x10ea1e[_0x1f49('0xb9')],{})[_0x1f49('0x35')](respondWithResult(_0x3f9310,0xc9))[_0x1f49('0x38')](handleError(_0x3f9310,null));};exports[_0x1f49('0x23')]=function(_0xd1acb1,_0x3dc3b4){if(_0xd1acb1[_0x1f49('0xb9')]['id']){delete _0xd1acb1[_0x1f49('0xb9')]['id'];}return db[_0x1f49('0x31')][_0x1f49('0x69')]({'where':{'id':_0xd1acb1[_0x1f49('0xb4')]['id']}})['then'](handleEntityNotFound(_0x3dc3b4,null))['then'](saveUpdates(_0xd1acb1[_0x1f49('0xb9')],null))['then'](respondWithResult(_0x3dc3b4,null))[_0x1f49('0x38')](handleError(_0x3dc3b4,null));};exports[_0x1f49('0x24')]=function(_0x482c05,_0xf15fce){return db[_0x1f49('0x31')]['find']({'where':{'id':_0x482c05[_0x1f49('0xb4')]['id']}})[_0x1f49('0x35')](handleEntityNotFound(_0xf15fce,null))[_0x1f49('0x35')](removeEntity(_0xf15fce,null))['catch'](handleError(_0xf15fce,null));};exports[_0x1f49('0xba')]=function(_0x590a30,_0x55c1db){return db[_0x1f49('0x31')][_0x1f49('0xba')]()[_0x1f49('0x35')](respondWithResult(_0x55c1db,null))[_0x1f49('0x38')](handleError(_0x55c1db,null));};exports['addMessage']=function(_0x542340,_0x244504,_0x2fd3cf){return db[_0x1f49('0xbb')][_0x1f49('0x69')]({'where':{'id':_0x542340[_0x1f49('0xb4')]['id']}})[_0x1f49('0x35')](handleEntityNotFound(_0x244504,null))['then'](function(_0x2dddfe){if(_0x2dddfe){return _0x2dddfe[_0x1f49('0xbc')](_0x542340['body'][_0x1f49('0xbd')],_[_0x1f49('0xbe')](_0x542340[_0x1f49('0xb9')],[_0x1f49('0xbd'),'id'])||{});}})[_0x1f49('0x35')](respondWithResult(_0x244504,null))[_0x1f49('0x38')](handleError(_0x244504,null));};exports[_0x1f49('0xbf')]=function(_0x5cd8c4,_0x536112,_0x36d5ad){var _0xa2a21c={'raw':![],'where':{}};var _0xd9fa14={};var _0x1066d5={'count':0x0,'rows':[]};return db[_0x1f49('0x31')][_0x1f49('0x32')]({'where':{'id':_0x5cd8c4['params']['id']}})[_0x1f49('0x35')](handleEntityNotFound(_0x536112,null))['then'](function(_0x5b2f77){if(_0x5b2f77){_0xd9fa14[_0x1f49('0x3b')]=_[_0x1f49('0x3d')](db[_0x1f49('0xbb')]['rawAttributes']);_0xd9fa14['query']=_[_0x1f49('0x3d')](_0x5cd8c4['query']);_0xd9fa14[_0x1f49('0x3e')]=_[_0x1f49('0x3f')](_0xd9fa14['model'],_0xd9fa14['query']);_0xa2a21c[_0x1f49('0x40')]=_[_0x1f49('0x3f')](_0xd9fa14['model'],qs[_0x1f49('0x41')](_0x5cd8c4[_0x1f49('0x3c')][_0x1f49('0x41')]));_0xa2a21c[_0x1f49('0x40')]=_0xa2a21c[_0x1f49('0x40')]['length']?_0xa2a21c[_0x1f49('0x40')]:_0xd9fa14[_0x1f49('0x3b')];if(!_0x5cd8c4['query'][_0x1f49('0xc0')](_0x1f49('0xc1'))){_0xa2a21c[_0x1f49('0x1d')]=qs[_0x1f49('0x1d')](_0x5cd8c4[_0x1f49('0x3c')]['limit']);_0xa2a21c[_0x1f49('0x1c')]=qs[_0x1f49('0x1c')](_0x5cd8c4[_0x1f49('0x3c')][_0x1f49('0x1c')]);}_0xa2a21c[_0x1f49('0x42')]=qs[_0x1f49('0x43')](_0x5cd8c4['query'][_0x1f49('0x43')]);_0xa2a21c[_0x1f49('0x77')]=qs[_0x1f49('0x3e')](_[_0x1f49('0xc2')](_0x5cd8c4['query'],_0xd9fa14[_0x1f49('0x3e')]));_0xa2a21c[_0x1f49('0x77')][_0x1f49('0xc3')]=_0x5b2f77['id'];if(_0x5cd8c4[_0x1f49('0x3c')]['filter']){_0xa2a21c[_0x1f49('0x77')]=_[_0x1f49('0x96')](_0xa2a21c['where'],{'$or':_['map'](_0xa2a21c[_0x1f49('0x40')],function(_0x2bb1f5){var _0x16305f={};_0x16305f[_0x2bb1f5]={'$like':'%'+_0x5cd8c4['query'][_0x1f49('0x84')]+'%'};return _0x16305f;})});}if(_0x5cd8c4[_0x1f49('0x3c')][_0x1f49('0x7b')]){var _0x4991dc=_0x5cd8c4[_0x1f49('0x3c')][_0x1f49('0x7b')][_0x1f49('0x2e')](',');var _0x140455={};_0x140455[_0x4991dc[0x0]]={'$gte':moment(_0x4991dc[0x1])[_0x1f49('0xc4')](_0x1f49('0xc5'))};_0xa2a21c[_0x1f49('0x77')]=_['merge'](_0xa2a21c[_0x1f49('0x77')],_0x140455);}_0xa2a21c=_['merge']({},_0xa2a21c,_0x5cd8c4[_0x1f49('0xb8')]);return db[_0x1f49('0xbb')]['count']({'where':_0xa2a21c[_0x1f49('0x77')]})[_0x1f49('0x35')](function(_0x557a1c){_0x1066d5[_0x1f49('0x1b')]=_0x557a1c;if(_0x5cd8c4[_0x1f49('0x3c')][_0x1f49('0xb6')]){_0xa2a21c[_0x1f49('0xb7')]=[{'all':!![]}];}return db[_0x1f49('0xbb')]['findAll'](_0xa2a21c);})[_0x1f49('0x35')](function(_0x1ecb78){_0x1066d5[_0x1f49('0xb2')]=_0x1ecb78;return _0x1066d5;});}})[_0x1f49('0x35')](respondWithFilteredResult(_0x536112,_0xa2a21c))[_0x1f49('0x38')](handleError(_0x536112,null));};exports[_0x1f49('0xc6')]=function(_0x3fd769,_0x5a425c,_0x2211fb){return db[_0x1f49('0x31')][_0x1f49('0x69')]({'where':{'id':_0x3fd769['params']['id']}})[_0x1f49('0x35')](handleEntityNotFound(_0x5a425c,null))[_0x1f49('0x35')](function(_0x13df67){if(_0x13df67){return _0x13df67[_0x1f49('0xc7')](_0x3fd769[_0x1f49('0xb9')][_0x1f49('0xbd')],_[_0x1f49('0xbe')](_0x3fd769[_0x1f49('0xb9')],['ids','id'])||{})['spread'](function(){return db[_0x1f49('0xc8')][_0x1f49('0xc9')]({'attributes':['id','name',_0x1f49('0x46')],'where':{'id':_0x3fd769[_0x1f49('0xb9')][_0x1f49('0xbd')]}});})[_0x1f49('0x35')](function(_0x453f14){socket[_0x1f49('0xca')](_0x1f49('0xcb'),{'id':Number(_0x3fd769[_0x1f49('0xb4')]['id']),'tags':_0x453f14||[]});return{'id':Number(_0x3fd769[_0x1f49('0xb4')]['id']),'tags':_0x453f14||[]};});}})[_0x1f49('0x35')](respondWithResult(_0x5a425c,null))[_0x1f49('0x38')](handleError(_0x5a425c,null));};exports['removeTags']=function(_0x385630,_0x1a81d3,_0x2f586a){return db[_0x1f49('0x31')][_0x1f49('0x69')]({'where':{'id':_0x385630[_0x1f49('0xb4')]['id']}})['then'](handleEntityNotFound(_0x1a81d3,null))[_0x1f49('0x35')](function(_0x5e1edc){if(_0x5e1edc){return _0x5e1edc[_0x1f49('0xcc')](_0x385630[_0x1f49('0x3c')][_0x1f49('0xbd')]);}})[_0x1f49('0x35')](respondWithStatusCode(_0x1a81d3,null))[_0x1f49('0x38')](handleError(_0x1a81d3,null));};exports['download']=function(_0x1ce8c8,_0x46a33e){var _0x2c73f7=moment()[_0x1f49('0xcd')]()[_0x1f49('0x95')]();var _0x1f5f7a=path['join'](config[_0x1f49('0xce')],_0x1f49('0xcf'),'files',_0x1f49('0xd0'));var _0x3bbd55=path[_0x1f49('0xd1')](config[_0x1f49('0xce')],_0x1f49('0xcf'),_0x1f49('0xd2'),_0x1f49('0xd3'));var _0x5a8ab2=path['join'](_0x1f5f7a,_0x2c73f7);var _0x2fdfae=util[_0x1f49('0xc4')](_0x1f49('0xd4'),_0x1ce8c8['params']['id'],_0x2c73f7);var _0x2f8ff4=path['join'](_0x1f5f7a,_0x2fdfae);var _0x1aa07b=[];if(_0x1ce8c8[_0x1f49('0x3c')][_0x1f49('0xd3')]){_0x1aa07b[_0x1f49('0x30')]({'model':db[_0x1f49('0xd5')],'as':'Attachments','raw':!![]});}var _0x4e53d2=[{'model':db[_0x1f49('0xbb')],'as':_0x1f49('0xd6'),'attributes':['id',_0x1f49('0xb9'),_0x1f49('0x47'),'to','cc','bcc',_0x1f49('0xd7'),_0x1f49('0xd8')],'where':{'secret':![]},'order':qs[_0x1f49('0x43')]('id'),'include':_0x1aa07b}];return db[_0x1f49('0x31')][_0x1f49('0x69')]({'where':{'id':_0x1ce8c8[_0x1f49('0xb4')]['id']},'include':_0x4e53d2})[_0x1f49('0x35')](handleEntityNotFound(_0x46a33e,null))[_0x1f49('0x35')](function(_0x16f44f){if(_0x16f44f&&_0x16f44f[_0x1f49('0xd6')]){var _0x58cf16=_0x16f44f[_0x1f49('0xd9')]({'plain':!![]});fs[_0x1f49('0xda')](_0x5a8ab2);return BPromise['each'](_0x58cf16['Messages'],function(_0x147af0,_0x4376e1){var _0x2e77cd={'from':_0x147af0[_0x1f49('0x47')],'to':_0x147af0['to'],'subject':_0x147af0['subject'],'html':_0x147af0[_0x1f49('0xb9')],'headers':{'Date':moment(_0x147af0['createdAt'])[_0x1f49('0xc4')](_0x1f49('0xdb'))},'attachments':[]};if(_0x147af0['cc']){_0x2e77cd['headers']['Cc']=_0x147af0['cc'];}if(_0x147af0[_0x1f49('0xdc')]){_0x2e77cd['headers'][_0x1f49('0xdd')]=_0x147af0['bcc'];}return new BPromise(function(_0x1c8291,_0x1df10e){if(_0x147af0[_0x1f49('0xde')]){for(var _0x8bf9e5=0x0;_0x8bf9e5<_0x147af0[_0x1f49('0xde')][_0x1f49('0x5c')];_0x8bf9e5++){var _0x4df179=_0x147af0[_0x1f49('0xde')][_0x8bf9e5];var _0x3e0915=path['join'](_0x3bbd55,_0x4df179[_0x1f49('0xdf')]);if(fs[_0x1f49('0xe0')](_0x3e0915)){_0x2e77cd[_0x1f49('0xd3')]['push']({'name':_0x4df179[_0x1f49('0x28')],'contentType':_0x4df179['type'],'data':fs[_0x1f49('0xe1')](_0x3e0915)});}}}emlformat[_0x1f49('0xe2')](_0x2e77cd,function(_0x382e58,_0x5cd6d5){if(_0x382e58)return _0x1df10e(_0x382e58);fs[_0x1f49('0xe3')](path[_0x1f49('0xd1')](_0x5a8ab2,util['format']('message%d.eml',_0x4376e1)),_0x5cd6d5);return _0x1c8291(_0x5cd6d5);});});})[_0x1f49('0x35')](function(){return new BPromise(function(_0x4878f6,_0x577123){zipdir(_0x5a8ab2,{'saveTo':_0x2f8ff4},function(_0x4174fa,_0x470f5a){if(_0x4174fa)return _0x577123(_0x4174fa);return _0x4878f6(_0x470f5a);});})['then'](function(){return new BPromise(function(_0x470d4d,_0x5bcb50){rimraf(_0x5a8ab2,function(_0x2a6a33){if(_0x2a6a33)_0x5bcb50(_0x2a6a33);return _0x470d4d();});});})[_0x1f49('0x35')](function(){return _0x46a33e[_0x1f49('0xe4')](_0x2f8ff4,_0x2fdfae,function(_0x1f57ef){if(_0x1f57ef){console[_0x1f49('0xe5')]('err',_0x1f57ef);}else{fs['unlinkSync'](_0x2f8ff4);}});});});}else{return _0x46a33e['sendStatus'](0xc8);}})[_0x1f49('0x38')](handleError(_0x46a33e,null));};