ccd75cb38c659f502927f86225cd3b9441cfc9d6
[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 _0x875f=['register','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','then','destroy','error','stack','name','send','index','map','rawAttributes','fieldName','type','key','findAll','mail','user','catch','show','model','keys','MailInteraction','query','intersection','fields','attributes','length','includeAll','merge','options','create','body','find','params','describe','MailMessage','ids','omit','getMessages','findOne','filters','hasOwnProperty','nolimit','order','sort','where','pick','MailInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','include','inlineCss','true','rows','addTags','spread','Tag','color','emit','mailInteractionTags:save','removeTags','unix','toString','server','files','root','attachments','join','transcript-%d-%s.zip','push','Attachment','Attachments','bcc','subject','Messages','get','each','from','missing.recipient@your.company.com','decode','createdAt','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','Bcc','basename','existsSync','build','unlinkSync','eml-format','rimraf','zip-dir','moment','bluebird','util','lodash','juice','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','../../config/environment','html-entities','defaults','redis','localhost','socket.io-emitter','./mailInteraction.socket'];(function(_0x531f6e,_0x330129){var _0x5b99a3=function(_0x374827){while(--_0x374827){_0x531f6e['push'](_0x531f6e['shift']());}};_0x5b99a3(++_0x330129);}(_0x875f,0x1c9));var _0xf875=function(_0x1ed898,_0x42431b){_0x1ed898=_0x1ed898-0x0;var _0x65e8cf=_0x875f[_0x1ed898];return _0x65e8cf;};'use strict';var emlformat=require(_0xf875('0x0'));var rimraf=require(_0xf875('0x1'));var zipdir=require(_0xf875('0x2'));var moment=require(_0xf875('0x3'));var BPromise=require(_0xf875('0x4'));var util=require(_0xf875('0x5'));var path=require('path');var fs=require('fs');var _=require(_0xf875('0x6'));var juice=require(_0xf875('0x7'));var Redis=require(_0xf875('0x8'));var interaction=require(_0xf875('0x9'));var qs=require(_0xf875('0xa'));var logger=require(_0xf875('0xb'))('api');var config=require(_0xf875('0xc'));var db=require('../../mysqldb')['db'];var htmlEntities=require(_0xf875('0xd'));config['redis']=_[_0xf875('0xe')](config[_0xf875('0xf')],{'host':_0xf875('0x10'),'port':0x18eb});var socket=require(_0xf875('0x11'))(new Redis(config[_0xf875('0xf')]));require(_0xf875('0x12'))[_0xf875('0x13')](socket);function respondWithStatusCode(_0x134883,_0x10d97f){_0x10d97f=_0x10d97f||0xcc;return function(_0x5b5b1c){if(_0x5b5b1c){return _0x134883[_0xf875('0x14')](_0x10d97f);}return _0x134883[_0xf875('0x15')](_0x10d97f)[_0xf875('0x16')]();};}function respondWithResult(_0xf6d8f2,_0x104a3a){_0x104a3a=_0x104a3a||0xc8;return function(_0xb38b4f){if(_0xb38b4f){return _0xf6d8f2[_0xf875('0x15')](_0x104a3a)[_0xf875('0x17')](_0xb38b4f);}};}function respondWithFilteredResult(_0x1a44fd,_0x3e5fdf){return function(_0x5bcee2){if(_0x5bcee2){var _0x4d4cb8=typeof _0x3e5fdf[_0xf875('0x18')]===_0xf875('0x19')&&typeof _0x3e5fdf[_0xf875('0x1a')]==='undefined';var _0x1f7054=_0x5bcee2[_0xf875('0x1b')];var _0x4f03eb=_0x4d4cb8?0x0:_0x3e5fdf[_0xf875('0x18')];var _0x2b22ed=_0x4d4cb8?_0x5bcee2[_0xf875('0x1b')]:_0x3e5fdf[_0xf875('0x18')]+_0x3e5fdf[_0xf875('0x1a')];var _0x5af150;if(_0x2b22ed>=_0x1f7054){_0x2b22ed=_0x1f7054;_0x5af150=0xc8;}else{_0x5af150=0xce;}_0x1a44fd[_0xf875('0x15')](_0x5af150);return _0x1a44fd[_0xf875('0x1c')](_0xf875('0x1d'),_0x4f03eb+'-'+_0x2b22ed+'/'+_0x1f7054)['json'](_0x5bcee2);}return null;};}function saveUpdates(_0x34aec4){return function(_0x2b1806){if(_0x2b1806){return _0x2b1806['update'](_0x34aec4)[_0xf875('0x1e')](function(_0x3def94){return _0x3def94;});}return null;};}function removeEntity(_0x228788){return function(_0x1a2c92){if(_0x1a2c92){return _0x1a2c92[_0xf875('0x1f')]()['then'](function(){_0x228788[_0xf875('0x15')](0xcc)[_0xf875('0x16')]();});}};}function handleEntityNotFound(_0x15c143){return function(_0x4534ab){if(!_0x4534ab){_0x15c143['sendStatus'](0x194);}return _0x4534ab;};}function handleError(_0x37ce86,_0x323195){_0x323195=_0x323195||0x1f4;return function(_0x203f2e){logger[_0xf875('0x20')](_0x203f2e[_0xf875('0x21')]);if(_0x203f2e[_0xf875('0x22')]){delete _0x203f2e['name'];}_0x37ce86[_0xf875('0x15')](_0x323195)[_0xf875('0x23')](_0x203f2e);};}exports[_0xf875('0x24')]=function(_0x4e0d73,_0x34db3c){var _0x248e12={};var _0x43d0aa=_[_0xf875('0x25')](db['MailInteraction'][_0xf875('0x26')],function(_0x481652){return{'name':_0x481652[_0xf875('0x27')],'type':_0x481652[_0xf875('0x28')][_0xf875('0x29')]};});_0x248e12=qs['getOptions'](_0x43d0aa,_0x4e0d73);return interaction[_0xf875('0x2a')](_0xf875('0x2b'),_0x4e0d73['query'],_0x4e0d73[_0xf875('0x2c')],_0x248e12,_0x43d0aa)[_0xf875('0x1e')](respondWithFilteredResult(_0x34db3c,_0x248e12))[_0xf875('0x2d')](handleError(_0x34db3c,null));};exports[_0xf875('0x2e')]=function(_0x3a1f35,_0x5ee494){var _0x24ef49={'raw':![],'where':{'id':_0x3a1f35['params']['id']}},_0xdc7e93={};_0xdc7e93[_0xf875('0x2f')]=_[_0xf875('0x30')](db[_0xf875('0x31')][_0xf875('0x26')]);_0xdc7e93[_0xf875('0x32')]=_['keys'](_0x3a1f35[_0xf875('0x32')]);_0xdc7e93['filters']=_[_0xf875('0x33')](_0xdc7e93[_0xf875('0x2f')],_0xdc7e93[_0xf875('0x32')]);_0x24ef49['attributes']=_[_0xf875('0x33')](_0xdc7e93[_0xf875('0x2f')],qs[_0xf875('0x34')](_0x3a1f35[_0xf875('0x32')][_0xf875('0x34')]));_0x24ef49[_0xf875('0x35')]=_0x24ef49[_0xf875('0x35')][_0xf875('0x36')]?_0x24ef49[_0xf875('0x35')]:_0xdc7e93[_0xf875('0x2f')];if(_0x3a1f35[_0xf875('0x32')][_0xf875('0x37')]){_0x24ef49['include']=[{'all':!![]}];}_0x24ef49=_[_0xf875('0x38')]({},_0x24ef49,_0x3a1f35[_0xf875('0x39')]);return db['MailInteraction']['find'](_0x24ef49)[_0xf875('0x1e')](handleEntityNotFound(_0x5ee494,null))[_0xf875('0x1e')](respondWithResult(_0x5ee494,null))[_0xf875('0x2d')](handleError(_0x5ee494,null));};exports[_0xf875('0x3a')]=function(_0x1b1715,_0x2125c5){return db[_0xf875('0x31')][_0xf875('0x3a')](_0x1b1715[_0xf875('0x3b')],{})['then'](respondWithResult(_0x2125c5,0xc9))['catch'](handleError(_0x2125c5,null));};exports['update']=function(_0x568f81,_0xe2cea4){if(_0x568f81['body']['id']){delete _0x568f81[_0xf875('0x3b')]['id'];}return db['MailInteraction'][_0xf875('0x3c')]({'where':{'id':_0x568f81[_0xf875('0x3d')]['id']}})['then'](handleEntityNotFound(_0xe2cea4,null))[_0xf875('0x1e')](saveUpdates(_0x568f81[_0xf875('0x3b')],null))[_0xf875('0x1e')](respondWithResult(_0xe2cea4,null))[_0xf875('0x2d')](handleError(_0xe2cea4,null));};exports[_0xf875('0x1f')]=function(_0x22bcab,_0x18364d){return db[_0xf875('0x31')][_0xf875('0x3c')]({'where':{'id':_0x22bcab['params']['id']}})[_0xf875('0x1e')](handleEntityNotFound(_0x18364d,null))['then'](removeEntity(_0x18364d,null))['catch'](handleError(_0x18364d,null));};exports[_0xf875('0x3e')]=function(_0x321931,_0xa1e0bb){return db[_0xf875('0x31')]['describe']()[_0xf875('0x1e')](respondWithResult(_0xa1e0bb,null))['catch'](handleError(_0xa1e0bb,null));};exports['addMessage']=function(_0x1036b6,_0x2c9a69){return db[_0xf875('0x3f')][_0xf875('0x3c')]({'where':{'id':_0x1036b6[_0xf875('0x3d')]['id']}})[_0xf875('0x1e')](handleEntityNotFound(_0x2c9a69,null))['then'](function(_0x31420f){if(_0x31420f){return _0x31420f['addMessage'](_0x1036b6[_0xf875('0x3b')][_0xf875('0x40')],_[_0xf875('0x41')](_0x1036b6[_0xf875('0x3b')],[_0xf875('0x40'),'id'])||{});}})['then'](respondWithResult(_0x2c9a69,null))[_0xf875('0x2d')](handleError(_0x2c9a69,null));};exports[_0xf875('0x42')]=function(_0xcb36ae,_0x1fa9c5){var _0x5fb953={'raw':![],'where':{}};var _0x31813b={};var _0x36b378={'count':0x0,'rows':[]};return db[_0xf875('0x31')][_0xf875('0x43')]({'where':{'id':_0xcb36ae[_0xf875('0x3d')]['id']}})['then'](handleEntityNotFound(_0x1fa9c5,null))[_0xf875('0x1e')](function(_0x444184){if(_0x444184){_0x31813b['model']=_[_0xf875('0x30')](db['MailMessage'][_0xf875('0x26')]);_0x31813b[_0xf875('0x32')]=_['keys'](_0xcb36ae[_0xf875('0x32')]);_0x31813b[_0xf875('0x44')]=_['intersection'](_0x31813b['model'],_0x31813b[_0xf875('0x32')]);_0x5fb953['attributes']=_[_0xf875('0x33')](_0x31813b['model'],qs[_0xf875('0x34')](_0xcb36ae['query']['fields']));_0x5fb953[_0xf875('0x35')]=_0x5fb953[_0xf875('0x35')][_0xf875('0x36')]?_0x5fb953[_0xf875('0x35')]:_0x31813b['model'];if(!_0xcb36ae[_0xf875('0x32')][_0xf875('0x45')](_0xf875('0x46'))){_0x5fb953[_0xf875('0x1a')]=qs[_0xf875('0x1a')](_0xcb36ae[_0xf875('0x32')][_0xf875('0x1a')]);_0x5fb953[_0xf875('0x18')]=qs['offset'](_0xcb36ae['query']['offset']);}_0x5fb953[_0xf875('0x47')]=qs['sort'](_0xcb36ae[_0xf875('0x32')][_0xf875('0x48')]);_0x5fb953[_0xf875('0x49')]=qs['filters'](_[_0xf875('0x4a')](_0xcb36ae[_0xf875('0x32')],_0x31813b[_0xf875('0x44')]));_0x5fb953[_0xf875('0x49')][_0xf875('0x4b')]=_0x444184['id'];if(_0xcb36ae[_0xf875('0x32')][_0xf875('0x4c')]){_0x5fb953[_0xf875('0x49')]=_[_0xf875('0x38')](_0x5fb953[_0xf875('0x49')],{'$or':_[_0xf875('0x25')](_0x5fb953[_0xf875('0x35')],function(_0xe94774){var _0x5db055={};_0x5db055[_0xe94774]={'$like':'%'+_0xcb36ae['query'][_0xf875('0x4c')]+'%'};return _0x5db055;})});}if(_0xcb36ae[_0xf875('0x32')][_0xf875('0x4d')]){var _0x5e7338=_0xcb36ae[_0xf875('0x32')][_0xf875('0x4d')][_0xf875('0x4e')](',');var _0x40c5fa={};_0x40c5fa[_0x5e7338[0x0]]={'$gte':moment(_0x5e7338[0x1])[_0xf875('0x4f')](_0xf875('0x50'))};_0x5fb953[_0xf875('0x49')]=_[_0xf875('0x38')](_0x5fb953[_0xf875('0x49')],_0x40c5fa);}_0x5fb953=_[_0xf875('0x38')]({},_0x5fb953,_0xcb36ae[_0xf875('0x39')]);return db[_0xf875('0x3f')]['count']({'where':_0x5fb953[_0xf875('0x49')]})['then'](function(_0x4f691e){_0x36b378[_0xf875('0x1b')]=_0x4f691e;if(_0xcb36ae[_0xf875('0x32')]['includeAll']){_0x5fb953[_0xf875('0x51')]=[{'all':!![]}];}return db[_0xf875('0x3f')]['findAll'](_0x5fb953);})['then'](function(_0x370f34){if(_0xcb36ae[_0xf875('0x32')][_0xf875('0x52')]===_0xf875('0x53')){for(var _0x6de2f8=0x0;_0x6de2f8<_0x370f34[_0xf875('0x36')];_0x6de2f8++){_0x370f34[_0x6de2f8]['body']=juice(_0x370f34[_0x6de2f8][_0xf875('0x3b')]);}}_0x36b378[_0xf875('0x54')]=_0x370f34;return _0x36b378;});}})[_0xf875('0x1e')](respondWithFilteredResult(_0x1fa9c5,_0x5fb953))['catch'](handleError(_0x1fa9c5,null));};exports[_0xf875('0x55')]=function(_0x5ec59a,_0x209a35){return db['MailInteraction'][_0xf875('0x3c')]({'where':{'id':_0x5ec59a[_0xf875('0x3d')]['id']}})['then'](handleEntityNotFound(_0x209a35,null))['then'](function(_0x30799f){if(_0x30799f){return _0x30799f['setTags'](_0x5ec59a[_0xf875('0x3b')]['ids'],_[_0xf875('0x41')](_0x5ec59a['body'],[_0xf875('0x40'),'id'])||{})[_0xf875('0x56')](function(){return db[_0xf875('0x57')][_0xf875('0x2a')]({'attributes':['id',_0xf875('0x22'),_0xf875('0x58')],'where':{'id':_0x5ec59a[_0xf875('0x3b')]['ids']}});})['then'](function(_0x1e2ebb){socket[_0xf875('0x59')](_0xf875('0x5a'),{'id':Number(_0x5ec59a[_0xf875('0x3d')]['id']),'tags':_0x1e2ebb||[]});return{'id':Number(_0x5ec59a[_0xf875('0x3d')]['id']),'tags':_0x1e2ebb||[]};});}})[_0xf875('0x1e')](respondWithResult(_0x209a35,null))['catch'](handleError(_0x209a35,null));};exports['removeTags']=function(_0x14f643,_0x212a37){return db[_0xf875('0x31')][_0xf875('0x3c')]({'where':{'id':_0x14f643['params']['id']}})['then'](handleEntityNotFound(_0x212a37,null))['then'](function(_0x284567){if(_0x284567){return _0x284567[_0xf875('0x5b')](_0x14f643[_0xf875('0x32')][_0xf875('0x40')]);}})[_0xf875('0x1e')](respondWithStatusCode(_0x212a37,null))[_0xf875('0x2d')](handleError(_0x212a37,null));};exports['download']=function(_0x5c6d79,_0xbe2dd8){var _0xfeeabe=moment()[_0xf875('0x5c')]()[_0xf875('0x5d')]();var _0x432953=path['join'](config['root'],_0xf875('0x5e'),_0xf875('0x5f'),'tmp');var _0x5e48bd=path['join'](config[_0xf875('0x60')],_0xf875('0x5e'),'files',_0xf875('0x61'));var _0x4cb511=path[_0xf875('0x62')](_0x432953,_0xfeeabe);var _0x5ea605=util['format'](_0xf875('0x63'),_0x5c6d79[_0xf875('0x3d')]['id'],_0xfeeabe);var _0x34450a=path['join'](_0x432953,_0x5ea605);var _0x1dd26d=[];if(_0x5c6d79[_0xf875('0x32')][_0xf875('0x61')]){_0x1dd26d[_0xf875('0x64')]({'model':db[_0xf875('0x65')],'as':_0xf875('0x66'),'raw':!![]});}var _0x4a2c52=[{'model':db[_0xf875('0x3f')],'as':'Messages','attributes':['id',_0xf875('0x3b'),'from','to','cc',_0xf875('0x67'),_0xf875('0x68'),'createdAt'],'where':{'secret':![]},'order':qs['sort']('id'),'include':_0x1dd26d}];return db[_0xf875('0x31')][_0xf875('0x3c')]({'where':{'id':_0x5c6d79['params']['id']},'include':_0x4a2c52})[_0xf875('0x1e')](handleEntityNotFound(_0xbe2dd8,null))[_0xf875('0x1e')](function(_0x508fbb){if(_0x508fbb&&_0x508fbb[_0xf875('0x69')]){var _0x41fb2c=_0x508fbb[_0xf875('0x6a')]({'plain':!![]});fs['mkdirSync'](_0x4cb511);return BPromise[_0xf875('0x6b')](_0x41fb2c[_0xf875('0x69')],function(_0x544365,_0x18f955){var _0x559e86={'from':_0x544365[_0xf875('0x6c')],'to':_0x544365['to']||_0xf875('0x6d'),'subject':_0x544365[_0xf875('0x68')],'html':htmlEntities[_0xf875('0x6e')](_0x544365[_0xf875('0x3b')]),'headers':{'Date':moment(_0x544365[_0xf875('0x6f')])['format'](_0xf875('0x70'))},'attachments':[]};if(_0x544365['cc']){_0x559e86['headers']['Cc']=_0x544365['cc'];}if(_0x544365['bcc']){_0x559e86[_0xf875('0x71')][_0xf875('0x72')]=_0x544365[_0xf875('0x67')];}return new BPromise(function(_0x1a6cab,_0x16f7cf){if(_0x544365[_0xf875('0x66')]){for(var _0x54d58d=0x0;_0x54d58d<_0x544365[_0xf875('0x66')][_0xf875('0x36')];_0x54d58d++){var _0x18a2f5=_0x544365['Attachments'][_0x54d58d];var _0x281ebb=path[_0xf875('0x62')](_0x5e48bd,_0x18a2f5[_0xf875('0x73')]);if(fs[_0xf875('0x74')](_0x281ebb)){_0x559e86[_0xf875('0x61')][_0xf875('0x64')]({'name':_0x18a2f5[_0xf875('0x22')],'contentType':_0x18a2f5['type'],'data':fs['readFileSync'](_0x281ebb)});}}}emlformat[_0xf875('0x75')](_0x559e86,function(_0x515eb0,_0x256af4){if(_0x515eb0)return _0x16f7cf(_0x515eb0);fs['writeFileSync'](path[_0xf875('0x62')](_0x4cb511,util[_0xf875('0x4f')]('message%d.eml',_0x18f955)),_0x256af4);return _0x1a6cab(_0x256af4);});});})[_0xf875('0x1e')](function(){return new BPromise(function(_0x2471eb,_0x11577e){zipdir(_0x4cb511,{'saveTo':_0x34450a},function(_0x1c18ff,_0x269c67){if(_0x1c18ff)return _0x11577e(_0x1c18ff);return _0x2471eb(_0x269c67);});})['then'](function(){return new BPromise(function(_0x223cfa,_0x12911b){rimraf(_0x4cb511,function(_0x3641b2){if(_0x3641b2)_0x12911b(_0x3641b2);return _0x223cfa();});});})[_0xf875('0x1e')](function(){return _0xbe2dd8['download'](_0x34450a,_0x5ea605,function(_0x184d70){if(!_0x184d70)fs[_0xf875('0x76')](_0x34450a);});});});}else{return _0xbe2dd8[_0xf875('0x14')](0xc8);}})[_0xf875('0x2d')](handleError(_0xbe2dd8,null));};