Built motion from commit 97f2f211.|2.6.31
[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 _0x223f=['Attachment','Attachments','from','subject','createdAt','Messages','each','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','bcc','basename','type','readFileSync','writeFileSync','message%d.eml','unlinkSync','eml-format','rimraf','zip-dir','bluebird','util','path','lodash','juice','ioredis','../../components/parsers/qs','api','../../config/environment','../../mysqldb','redis','defaults','socket.io-emitter','./mailInteraction.socket','register','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','update','destroy','then','stack','name','send','index','map','MailInteraction','rawAttributes','fieldName','key','user','catch','params','query','intersection','attributes','model','fields','include','merge','find','create','body','describe','addMessage','ids','getMessages','findOne','keys','MailMessage','length','hasOwnProperty','nolimit','sort','filters','where','filter','$gte','split','YYYY-MM-DD\x20HH:mm:ss','options','includeAll','inlineCss','true','rows','addTags','setTags','omit','spread','Tag','color','emit','mailInteractionTags:save','removeTags','download','unix','toString','join','server','files','tmp','root','attachments','format','transcript-%d-%s.zip','push'];(function(_0x3ef13f,_0x40847c){var _0x81ad6f=function(_0x4a69e5){while(--_0x4a69e5){_0x3ef13f['push'](_0x3ef13f['shift']());}};_0x81ad6f(++_0x40847c);}(_0x223f,0x1d5));var _0xf223=function(_0x44aab6,_0x550e7c){_0x44aab6=_0x44aab6-0x0;var _0x2d2a51=_0x223f[_0x44aab6];return _0x2d2a51;};'use strict';var emlformat=require(_0xf223('0x0'));var rimraf=require(_0xf223('0x1'));var zipdir=require(_0xf223('0x2'));var moment=require('moment');var BPromise=require(_0xf223('0x3'));var util=require(_0xf223('0x4'));var path=require(_0xf223('0x5'));var fs=require('fs');var _=require(_0xf223('0x6'));var juice=require(_0xf223('0x7'));var Redis=require(_0xf223('0x8'));var interaction=require('../../components/interaction/service');var qs=require(_0xf223('0x9'));var logger=require('../../config/logger')(_0xf223('0xa'));var config=require(_0xf223('0xb'));var db=require(_0xf223('0xc'))['db'];var htmlEntities=require('html-entities');config[_0xf223('0xd')]=_[_0xf223('0xe')](config[_0xf223('0xd')],{'host':'localhost','port':0x18eb});var socket=require(_0xf223('0xf'))(new Redis(config[_0xf223('0xd')]));require(_0xf223('0x10'))[_0xf223('0x11')](socket);function respondWithStatusCode(_0x378e61,_0x4c9409){_0x4c9409=_0x4c9409||0xcc;return function(_0x279927){if(_0x279927){return _0x378e61[_0xf223('0x12')](_0x4c9409);}return _0x378e61[_0xf223('0x13')](_0x4c9409)[_0xf223('0x14')]();};}function respondWithResult(_0x35e5b2,_0x5742d9){_0x5742d9=_0x5742d9||0xc8;return function(_0x217853){if(_0x217853){return _0x35e5b2[_0xf223('0x13')](_0x5742d9)[_0xf223('0x15')](_0x217853);}};}function respondWithFilteredResult(_0x5a4444,_0x566d07){return function(_0x107270){if(_0x107270){var _0xf96491=typeof _0x566d07[_0xf223('0x16')]===_0xf223('0x17')&&typeof _0x566d07[_0xf223('0x18')]==='undefined';var _0x246186=_0x107270[_0xf223('0x19')];var _0x4ce802=_0xf96491?0x0:_0x566d07[_0xf223('0x16')];var _0x107cb4=_0xf96491?_0x107270['count']:_0x566d07['offset']+_0x566d07[_0xf223('0x18')];var _0xc71931;if(_0x107cb4>=_0x246186){_0x107cb4=_0x246186;_0xc71931=0xc8;}else{_0xc71931=0xce;}_0x5a4444['status'](_0xc71931);return _0x5a4444[_0xf223('0x1a')](_0xf223('0x1b'),_0x4ce802+'-'+_0x107cb4+'/'+_0x246186)['json'](_0x107270);}return null;};}function saveUpdates(_0x305de2){return function(_0x6b2aa2){if(_0x6b2aa2){return _0x6b2aa2[_0xf223('0x1c')](_0x305de2)['then'](function(_0x424dfb){return _0x424dfb;});}return null;};}function removeEntity(_0x37e0bd){return function(_0x290881){if(_0x290881){return _0x290881[_0xf223('0x1d')]()[_0xf223('0x1e')](function(){_0x37e0bd[_0xf223('0x13')](0xcc)[_0xf223('0x14')]();});}};}function handleEntityNotFound(_0x462eec){return function(_0x2b06b6){if(!_0x2b06b6){_0x462eec[_0xf223('0x12')](0x194);}return _0x2b06b6;};}function handleError(_0x27baad,_0x45f13f){_0x45f13f=_0x45f13f||0x1f4;return function(_0x21ac61){logger['error'](_0x21ac61[_0xf223('0x1f')]);if(_0x21ac61[_0xf223('0x20')]){delete _0x21ac61[_0xf223('0x20')];}_0x27baad[_0xf223('0x13')](_0x45f13f)[_0xf223('0x21')](_0x21ac61);};}exports[_0xf223('0x22')]=function(_0x1dd87f,_0x37c853){var _0x315668={};var _0x441e87=_[_0xf223('0x23')](db[_0xf223('0x24')][_0xf223('0x25')],function(_0x915524){return{'name':_0x915524[_0xf223('0x26')],'type':_0x915524['type'][_0xf223('0x27')]};});_0x315668=qs['getOptions'](_0x441e87,_0x1dd87f);return interaction['findAll']('mail',_0x1dd87f['query'],_0x1dd87f[_0xf223('0x28')],_0x315668,_0x441e87)[_0xf223('0x1e')](respondWithFilteredResult(_0x37c853,_0x315668))[_0xf223('0x29')](handleError(_0x37c853,null));};exports['show']=function(_0x4843a3,_0x1bf747){var _0x2b5d34={'raw':![],'where':{'id':_0x4843a3[_0xf223('0x2a')]['id']}},_0x2363ea={};_0x2363ea['model']=_['keys'](db[_0xf223('0x24')][_0xf223('0x25')]);_0x2363ea['query']=_['keys'](_0x4843a3[_0xf223('0x2b')]);_0x2363ea['filters']=_[_0xf223('0x2c')](_0x2363ea['model'],_0x2363ea[_0xf223('0x2b')]);_0x2b5d34[_0xf223('0x2d')]=_[_0xf223('0x2c')](_0x2363ea[_0xf223('0x2e')],qs[_0xf223('0x2f')](_0x4843a3[_0xf223('0x2b')][_0xf223('0x2f')]));_0x2b5d34[_0xf223('0x2d')]=_0x2b5d34[_0xf223('0x2d')]['length']?_0x2b5d34[_0xf223('0x2d')]:_0x2363ea[_0xf223('0x2e')];if(_0x4843a3[_0xf223('0x2b')]['includeAll']){_0x2b5d34[_0xf223('0x30')]=[{'all':!![]}];}_0x2b5d34=_[_0xf223('0x31')]({},_0x2b5d34,_0x4843a3['options']);return db[_0xf223('0x24')][_0xf223('0x32')](_0x2b5d34)[_0xf223('0x1e')](handleEntityNotFound(_0x1bf747,null))['then'](respondWithResult(_0x1bf747,null))['catch'](handleError(_0x1bf747,null));};exports[_0xf223('0x33')]=function(_0x3a4815,_0x174f03){return db[_0xf223('0x24')]['create'](_0x3a4815['body'],{})[_0xf223('0x1e')](respondWithResult(_0x174f03,0xc9))[_0xf223('0x29')](handleError(_0x174f03,null));};exports[_0xf223('0x1c')]=function(_0x30024d,_0x5d1f9e){if(_0x30024d[_0xf223('0x34')]['id']){delete _0x30024d[_0xf223('0x34')]['id'];}return db[_0xf223('0x24')][_0xf223('0x32')]({'where':{'id':_0x30024d[_0xf223('0x2a')]['id']}})[_0xf223('0x1e')](handleEntityNotFound(_0x5d1f9e,null))[_0xf223('0x1e')](saveUpdates(_0x30024d[_0xf223('0x34')],null))[_0xf223('0x1e')](respondWithResult(_0x5d1f9e,null))[_0xf223('0x29')](handleError(_0x5d1f9e,null));};exports[_0xf223('0x1d')]=function(_0x20e372,_0x2c5ec6){return db[_0xf223('0x24')]['find']({'where':{'id':_0x20e372[_0xf223('0x2a')]['id']}})['then'](handleEntityNotFound(_0x2c5ec6,null))['then'](removeEntity(_0x2c5ec6,null))[_0xf223('0x29')](handleError(_0x2c5ec6,null));};exports[_0xf223('0x35')]=function(_0x142fb5,_0xa8ebf3){return db['MailInteraction'][_0xf223('0x35')]()[_0xf223('0x1e')](respondWithResult(_0xa8ebf3,null))[_0xf223('0x29')](handleError(_0xa8ebf3,null));};exports[_0xf223('0x36')]=function(_0x532404,_0x4cdda5){return db['MailMessage'][_0xf223('0x32')]({'where':{'id':_0x532404[_0xf223('0x2a')]['id']}})[_0xf223('0x1e')](handleEntityNotFound(_0x4cdda5,null))['then'](function(_0x245838){if(_0x245838){return _0x245838[_0xf223('0x36')](_0x532404[_0xf223('0x34')][_0xf223('0x37')],_['omit'](_0x532404[_0xf223('0x34')],[_0xf223('0x37'),'id'])||{});}})[_0xf223('0x1e')](respondWithResult(_0x4cdda5,null))[_0xf223('0x29')](handleError(_0x4cdda5,null));};exports[_0xf223('0x38')]=function(_0x3e7862,_0x5fc6a){var _0x322979={'raw':![],'where':{}};var _0x4b6037={};var _0xe335f0={'count':0x0,'rows':[]};return db[_0xf223('0x24')][_0xf223('0x39')]({'where':{'id':_0x3e7862[_0xf223('0x2a')]['id']}})[_0xf223('0x1e')](handleEntityNotFound(_0x5fc6a,null))[_0xf223('0x1e')](function(_0x22ed23){if(_0x22ed23){_0x4b6037[_0xf223('0x2e')]=_[_0xf223('0x3a')](db[_0xf223('0x3b')][_0xf223('0x25')]);_0x4b6037[_0xf223('0x2b')]=_[_0xf223('0x3a')](_0x3e7862[_0xf223('0x2b')]);_0x4b6037['filters']=_[_0xf223('0x2c')](_0x4b6037[_0xf223('0x2e')],_0x4b6037[_0xf223('0x2b')]);_0x322979[_0xf223('0x2d')]=_['intersection'](_0x4b6037[_0xf223('0x2e')],qs[_0xf223('0x2f')](_0x3e7862['query']['fields']));_0x322979[_0xf223('0x2d')]=_0x322979[_0xf223('0x2d')][_0xf223('0x3c')]?_0x322979[_0xf223('0x2d')]:_0x4b6037[_0xf223('0x2e')];if(!_0x3e7862[_0xf223('0x2b')][_0xf223('0x3d')](_0xf223('0x3e'))){_0x322979['limit']=qs[_0xf223('0x18')](_0x3e7862[_0xf223('0x2b')][_0xf223('0x18')]);_0x322979['offset']=qs[_0xf223('0x16')](_0x3e7862[_0xf223('0x2b')][_0xf223('0x16')]);}_0x322979['order']=qs['sort'](_0x3e7862[_0xf223('0x2b')][_0xf223('0x3f')]);_0x322979['where']=qs['filters'](_['pick'](_0x3e7862[_0xf223('0x2b')],_0x4b6037[_0xf223('0x40')]));_0x322979[_0xf223('0x41')]['MailInteractionId']=_0x22ed23['id'];if(_0x3e7862[_0xf223('0x2b')][_0xf223('0x42')]){_0x322979[_0xf223('0x41')]=_['merge'](_0x322979[_0xf223('0x41')],{'$or':_[_0xf223('0x23')](_0x322979['attributes'],function(_0x46f807){var _0x389586={};_0x389586[_0x46f807]={'$like':'%'+_0x3e7862[_0xf223('0x2b')][_0xf223('0x42')]+'%'};return _0x389586;})});}if(_0x3e7862[_0xf223('0x2b')][_0xf223('0x43')]){var _0x1d948d=_0x3e7862[_0xf223('0x2b')][_0xf223('0x43')][_0xf223('0x44')](',');var _0x5e1b06={};_0x5e1b06[_0x1d948d[0x0]]={'$gte':moment(_0x1d948d[0x1])['format'](_0xf223('0x45'))};_0x322979[_0xf223('0x41')]=_['merge'](_0x322979[_0xf223('0x41')],_0x5e1b06);}_0x322979=_[_0xf223('0x31')]({},_0x322979,_0x3e7862[_0xf223('0x46')]);return db[_0xf223('0x3b')]['count']({'where':_0x322979[_0xf223('0x41')]})['then'](function(_0x52fb86){_0xe335f0[_0xf223('0x19')]=_0x52fb86;if(_0x3e7862[_0xf223('0x2b')][_0xf223('0x47')]){_0x322979[_0xf223('0x30')]=[{'all':!![]}];}return db[_0xf223('0x3b')]['findAll'](_0x322979);})[_0xf223('0x1e')](function(_0x5efb98){if(_0x3e7862[_0xf223('0x2b')][_0xf223('0x48')]===_0xf223('0x49')){for(var _0x4e89d6=0x0;_0x4e89d6<_0x5efb98['length'];_0x4e89d6++){_0x5efb98[_0x4e89d6][_0xf223('0x34')]=juice(_0x5efb98[_0x4e89d6][_0xf223('0x34')]);}}_0xe335f0[_0xf223('0x4a')]=_0x5efb98;return _0xe335f0;});}})[_0xf223('0x1e')](respondWithFilteredResult(_0x5fc6a,_0x322979))[_0xf223('0x29')](handleError(_0x5fc6a,null));};exports[_0xf223('0x4b')]=function(_0x36b829,_0x6d824e){return db[_0xf223('0x24')][_0xf223('0x32')]({'where':{'id':_0x36b829[_0xf223('0x2a')]['id']}})['then'](handleEntityNotFound(_0x6d824e,null))[_0xf223('0x1e')](function(_0x262f0e){if(_0x262f0e){return _0x262f0e[_0xf223('0x4c')](_0x36b829[_0xf223('0x34')][_0xf223('0x37')],_[_0xf223('0x4d')](_0x36b829[_0xf223('0x34')],[_0xf223('0x37'),'id'])||{})[_0xf223('0x4e')](function(){return db[_0xf223('0x4f')]['findAll']({'attributes':['id','name',_0xf223('0x50')],'where':{'id':_0x36b829[_0xf223('0x34')]['ids']}});})[_0xf223('0x1e')](function(_0x212019){socket[_0xf223('0x51')](_0xf223('0x52'),{'id':Number(_0x36b829[_0xf223('0x2a')]['id']),'tags':_0x212019||[]});return{'id':Number(_0x36b829[_0xf223('0x2a')]['id']),'tags':_0x212019||[]};});}})['then'](respondWithResult(_0x6d824e,null))[_0xf223('0x29')](handleError(_0x6d824e,null));};exports[_0xf223('0x53')]=function(_0x4399cf,_0x3d5b36){return db[_0xf223('0x24')][_0xf223('0x32')]({'where':{'id':_0x4399cf[_0xf223('0x2a')]['id']}})[_0xf223('0x1e')](handleEntityNotFound(_0x3d5b36,null))[_0xf223('0x1e')](function(_0x5774bf){if(_0x5774bf){return _0x5774bf['removeTags'](_0x4399cf[_0xf223('0x2b')]['ids']);}})['then'](respondWithStatusCode(_0x3d5b36,null))[_0xf223('0x29')](handleError(_0x3d5b36,null));};exports[_0xf223('0x54')]=function(_0x23f16b,_0x29ca71){var _0x2f8a41=moment()[_0xf223('0x55')]()[_0xf223('0x56')]();var _0x340de7=path[_0xf223('0x57')](config['root'],_0xf223('0x58'),_0xf223('0x59'),_0xf223('0x5a'));var _0xfe475c=path[_0xf223('0x57')](config[_0xf223('0x5b')],_0xf223('0x58'),_0xf223('0x59'),_0xf223('0x5c'));var _0x1e220b=path[_0xf223('0x57')](_0x340de7,_0x2f8a41);var _0x51d092=util[_0xf223('0x5d')](_0xf223('0x5e'),_0x23f16b[_0xf223('0x2a')]['id'],_0x2f8a41);var _0x2457ba=path[_0xf223('0x57')](_0x340de7,_0x51d092);var _0x28de17=[];if(_0x23f16b[_0xf223('0x2b')][_0xf223('0x5c')]){_0x28de17[_0xf223('0x5f')]({'model':db[_0xf223('0x60')],'as':_0xf223('0x61'),'raw':!![]});}var _0x2dc391=[{'model':db[_0xf223('0x3b')],'as':'Messages','attributes':['id',_0xf223('0x34'),_0xf223('0x62'),'to','cc','bcc',_0xf223('0x63'),_0xf223('0x64')],'where':{'secret':![]},'order':qs[_0xf223('0x3f')]('id'),'include':_0x28de17}];return db[_0xf223('0x24')][_0xf223('0x32')]({'where':{'id':_0x23f16b[_0xf223('0x2a')]['id']},'include':_0x2dc391})[_0xf223('0x1e')](handleEntityNotFound(_0x29ca71,null))[_0xf223('0x1e')](function(_0x5bab8a){if(_0x5bab8a&&_0x5bab8a[_0xf223('0x65')]){var _0xb01f0a=_0x5bab8a['get']({'plain':!![]});fs['mkdirSync'](_0x1e220b);return BPromise[_0xf223('0x66')](_0xb01f0a[_0xf223('0x65')],function(_0x21794e,_0x5116d6){var _0x3ded95={'from':_0x21794e['from'],'to':_0x21794e['to']||'missing.recipient@your.company.com','subject':_0x21794e[_0xf223('0x63')],'html':htmlEntities['decode'](_0x21794e[_0xf223('0x34')]),'headers':{'Date':moment(_0x21794e['createdAt'])['format'](_0xf223('0x67'))},'attachments':[]};if(_0x21794e['cc']){_0x3ded95[_0xf223('0x68')]['Cc']=_0x21794e['cc'];}if(_0x21794e['bcc']){_0x3ded95[_0xf223('0x68')][_0xf223('0x69')]=_0x21794e[_0xf223('0x6a')];}return new BPromise(function(_0x3d52d1,_0x3c1536){if(_0x21794e[_0xf223('0x61')]){for(var _0xe78f65=0x0;_0xe78f65<_0x21794e[_0xf223('0x61')][_0xf223('0x3c')];_0xe78f65++){var _0x1f5ea3=_0x21794e[_0xf223('0x61')][_0xe78f65];var _0x207e73=path[_0xf223('0x57')](_0xfe475c,_0x1f5ea3[_0xf223('0x6b')]);if(fs['existsSync'](_0x207e73)){_0x3ded95[_0xf223('0x5c')][_0xf223('0x5f')]({'name':_0x1f5ea3[_0xf223('0x20')],'contentType':_0x1f5ea3[_0xf223('0x6c')],'data':fs[_0xf223('0x6d')](_0x207e73)});}}}emlformat['build'](_0x3ded95,function(_0x2831ba,_0x4361d6){if(_0x2831ba)return _0x3c1536(_0x2831ba);fs[_0xf223('0x6e')](path[_0xf223('0x57')](_0x1e220b,util[_0xf223('0x5d')](_0xf223('0x6f'),_0x5116d6)),_0x4361d6);return _0x3d52d1(_0x4361d6);});});})[_0xf223('0x1e')](function(){return new BPromise(function(_0x13a61c,_0x5b6147){zipdir(_0x1e220b,{'saveTo':_0x2457ba},function(_0x5b2426,_0x23cd16){if(_0x5b2426)return _0x5b6147(_0x5b2426);return _0x13a61c(_0x23cd16);});})[_0xf223('0x1e')](function(){return new BPromise(function(_0x409fa5,_0x160f06){rimraf(_0x1e220b,function(_0x466f0b){if(_0x466f0b)_0x160f06(_0x466f0b);return _0x409fa5();});});})['then'](function(){return _0x29ca71['download'](_0x2457ba,_0x51d092,function(_0x4f808d){if(!_0x4f808d)fs[_0xf223('0x70')](_0x2457ba);});});});}else{return _0x29ca71['sendStatus'](0xc8);}})[_0xf223('0x29')](handleError(_0x29ca71,null));};