736e3e3430ae1198fff6d69bb07be0efe4e7e4a5
[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 _0xdc80=['include','merge','create','catch','body','find','describe','addMessage','omit','getMessages','findOne','MailMessage','filters','nolimit','order','sort','where','pick','MailInteractionId','filter','$gte','format','options','inlineCss','addTags','setTags','ids','emit','mailInteractionTags:save','removeTags','unix','toString','join','root','server','files','attachments','transcript-%d-%s.zip','push','Attachment','Attachments','Messages','bcc','subject','get','mkdirSync','each','from','decode','headers','Bcc','existsSync','writeFileSync','message%d.eml','unlinkSync','rimraf','moment','bluebird','lodash','juice','../../components/interaction/service','../../components/parsers/qs','../../config/environment','html-entities','defaults','redis','localhost','./mailInteraction.socket','register','sendStatus','end','status','json','undefined','limit','count','offset','set','Content-Range','update','then','destroy','error','stack','name','send','map','MailInteraction','fieldName','type','getOptions','findAll','mail','query','user','show','params','model','keys','rawAttributes','intersection','attributes','fields','length','includeAll'];(function(_0x2eb06c,_0x33ba5b){var _0x2d983d=function(_0x5addf5){while(--_0x5addf5){_0x2eb06c['push'](_0x2eb06c['shift']());}};_0x2d983d(++_0x33ba5b);}(_0xdc80,0xa0));var _0x0dc8=function(_0x4239bd,_0x1f310b){_0x4239bd=_0x4239bd-0x0;var _0x21f061=_0xdc80[_0x4239bd];return _0x21f061;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x0dc8('0x0'));var zipdir=require('zip-dir');var moment=require(_0x0dc8('0x1'));var BPromise=require(_0x0dc8('0x2'));var util=require('util');var path=require('path');var fs=require('fs');var _=require(_0x0dc8('0x3'));var juice=require(_0x0dc8('0x4'));var Redis=require('ioredis');var interaction=require(_0x0dc8('0x5'));var qs=require(_0x0dc8('0x6'));var logger=require('../../config/logger')('api');var config=require(_0x0dc8('0x7'));var db=require('../../mysqldb')['db'];var htmlEntities=require(_0x0dc8('0x8'));config['redis']=_[_0x0dc8('0x9')](config[_0x0dc8('0xa')],{'host':_0x0dc8('0xb'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x0dc8('0xa')]));require(_0x0dc8('0xc'))[_0x0dc8('0xd')](socket);function respondWithStatusCode(_0x3683c3,_0x4aba5d){_0x4aba5d=_0x4aba5d||0xcc;return function(_0x577d71){if(_0x577d71){return _0x3683c3[_0x0dc8('0xe')](_0x4aba5d);}return _0x3683c3['status'](_0x4aba5d)[_0x0dc8('0xf')]();};}function respondWithResult(_0x4d1b78,_0x55ab91){_0x55ab91=_0x55ab91||0xc8;return function(_0x222f68){if(_0x222f68){return _0x4d1b78[_0x0dc8('0x10')](_0x55ab91)[_0x0dc8('0x11')](_0x222f68);}};}function respondWithFilteredResult(_0x8cbf13,_0x410b7f){return function(_0x182ce4){if(_0x182ce4){var _0x3a824d=typeof _0x410b7f['offset']===_0x0dc8('0x12')&&typeof _0x410b7f[_0x0dc8('0x13')]===_0x0dc8('0x12');var _0x29658e=_0x182ce4[_0x0dc8('0x14')];var _0x2d85ac=_0x3a824d?0x0:_0x410b7f[_0x0dc8('0x15')];var _0x5c405b=_0x3a824d?_0x182ce4[_0x0dc8('0x14')]:_0x410b7f['offset']+_0x410b7f[_0x0dc8('0x13')];var _0x307573;if(_0x5c405b>=_0x29658e){_0x5c405b=_0x29658e;_0x307573=0xc8;}else{_0x307573=0xce;}_0x8cbf13[_0x0dc8('0x10')](_0x307573);return _0x8cbf13[_0x0dc8('0x16')](_0x0dc8('0x17'),_0x2d85ac+'-'+_0x5c405b+'/'+_0x29658e)[_0x0dc8('0x11')](_0x182ce4);}return null;};}function saveUpdates(_0x145636){return function(_0x5d9221){if(_0x5d9221){return _0x5d9221[_0x0dc8('0x18')](_0x145636)[_0x0dc8('0x19')](function(_0x466ac1){return _0x466ac1;});}return null;};}function removeEntity(_0x4d97f0){return function(_0x3a218f){if(_0x3a218f){return _0x3a218f[_0x0dc8('0x1a')]()[_0x0dc8('0x19')](function(){_0x4d97f0[_0x0dc8('0x10')](0xcc)[_0x0dc8('0xf')]();});}};}function handleEntityNotFound(_0xb8c9f4){return function(_0x25b789){if(!_0x25b789){_0xb8c9f4[_0x0dc8('0xe')](0x194);}return _0x25b789;};}function handleError(_0x486ddc,_0x5120b4){_0x5120b4=_0x5120b4||0x1f4;return function(_0x115886){logger[_0x0dc8('0x1b')](_0x115886[_0x0dc8('0x1c')]);if(_0x115886[_0x0dc8('0x1d')]){delete _0x115886[_0x0dc8('0x1d')];}_0x486ddc[_0x0dc8('0x10')](_0x5120b4)[_0x0dc8('0x1e')](_0x115886);};}exports['index']=function(_0x2fbceb,_0x2e18d6){var _0xc5dfdf={};var _0x1d613a=_[_0x0dc8('0x1f')](db[_0x0dc8('0x20')]['rawAttributes'],function(_0x9a7fed){return{'name':_0x9a7fed[_0x0dc8('0x21')],'type':_0x9a7fed[_0x0dc8('0x22')]['key']};});_0xc5dfdf=qs[_0x0dc8('0x23')](_0x1d613a,_0x2fbceb);return interaction[_0x0dc8('0x24')](_0x0dc8('0x25'),_0x2fbceb[_0x0dc8('0x26')],_0x2fbceb[_0x0dc8('0x27')],_0xc5dfdf,_0x1d613a)[_0x0dc8('0x19')](respondWithFilteredResult(_0x2e18d6,_0xc5dfdf))['catch'](handleError(_0x2e18d6,null));};exports[_0x0dc8('0x28')]=function(_0x1b00ff,_0x2ecd92){var _0x5f19fd={'raw':![],'where':{'id':_0x1b00ff[_0x0dc8('0x29')]['id']}},_0x55e287={};_0x55e287[_0x0dc8('0x2a')]=_[_0x0dc8('0x2b')](db[_0x0dc8('0x20')][_0x0dc8('0x2c')]);_0x55e287['query']=_[_0x0dc8('0x2b')](_0x1b00ff[_0x0dc8('0x26')]);_0x55e287['filters']=_[_0x0dc8('0x2d')](_0x55e287[_0x0dc8('0x2a')],_0x55e287[_0x0dc8('0x26')]);_0x5f19fd[_0x0dc8('0x2e')]=_['intersection'](_0x55e287[_0x0dc8('0x2a')],qs[_0x0dc8('0x2f')](_0x1b00ff[_0x0dc8('0x26')]['fields']));_0x5f19fd[_0x0dc8('0x2e')]=_0x5f19fd['attributes'][_0x0dc8('0x30')]?_0x5f19fd['attributes']:_0x55e287[_0x0dc8('0x2a')];if(_0x1b00ff[_0x0dc8('0x26')][_0x0dc8('0x31')]){_0x5f19fd[_0x0dc8('0x32')]=[{'all':!![]}];}_0x5f19fd=_[_0x0dc8('0x33')]({},_0x5f19fd,_0x1b00ff['options']);return db['MailInteraction']['find'](_0x5f19fd)[_0x0dc8('0x19')](handleEntityNotFound(_0x2ecd92,null))[_0x0dc8('0x19')](respondWithResult(_0x2ecd92,null))['catch'](handleError(_0x2ecd92,null));};exports['create']=function(_0x452002,_0x4debfb){return db['MailInteraction'][_0x0dc8('0x34')](_0x452002['body'],{})['then'](respondWithResult(_0x4debfb,0xc9))[_0x0dc8('0x35')](handleError(_0x4debfb,null));};exports[_0x0dc8('0x18')]=function(_0x30fd1c,_0xf2dab8){if(_0x30fd1c[_0x0dc8('0x36')]['id']){delete _0x30fd1c[_0x0dc8('0x36')]['id'];}return db[_0x0dc8('0x20')][_0x0dc8('0x37')]({'where':{'id':_0x30fd1c[_0x0dc8('0x29')]['id']}})[_0x0dc8('0x19')](handleEntityNotFound(_0xf2dab8,null))[_0x0dc8('0x19')](saveUpdates(_0x30fd1c['body'],null))[_0x0dc8('0x19')](respondWithResult(_0xf2dab8,null))['catch'](handleError(_0xf2dab8,null));};exports[_0x0dc8('0x1a')]=function(_0x246e04,_0x45dae6){return db[_0x0dc8('0x20')][_0x0dc8('0x37')]({'where':{'id':_0x246e04['params']['id']}})[_0x0dc8('0x19')](handleEntityNotFound(_0x45dae6,null))['then'](removeEntity(_0x45dae6,null))['catch'](handleError(_0x45dae6,null));};exports[_0x0dc8('0x38')]=function(_0x51e8d5,_0x20559f){return db[_0x0dc8('0x20')][_0x0dc8('0x38')]()[_0x0dc8('0x19')](respondWithResult(_0x20559f,null))[_0x0dc8('0x35')](handleError(_0x20559f,null));};exports['addMessage']=function(_0x4b1a6b,_0x49264b){return db['MailMessage'][_0x0dc8('0x37')]({'where':{'id':_0x4b1a6b[_0x0dc8('0x29')]['id']}})[_0x0dc8('0x19')](handleEntityNotFound(_0x49264b,null))[_0x0dc8('0x19')](function(_0x554a6a){if(_0x554a6a){return _0x554a6a[_0x0dc8('0x39')](_0x4b1a6b[_0x0dc8('0x36')]['ids'],_[_0x0dc8('0x3a')](_0x4b1a6b['body'],['ids','id'])||{});}})[_0x0dc8('0x19')](respondWithResult(_0x49264b,null))[_0x0dc8('0x35')](handleError(_0x49264b,null));};exports[_0x0dc8('0x3b')]=function(_0x46c4b1,_0x5852e3){var _0x1ef020={'raw':![],'where':{}};var _0x2577ec={};var _0x4b0590={'count':0x0,'rows':[]};return db[_0x0dc8('0x20')][_0x0dc8('0x3c')]({'where':{'id':_0x46c4b1['params']['id']}})[_0x0dc8('0x19')](handleEntityNotFound(_0x5852e3,null))[_0x0dc8('0x19')](function(_0x404457){if(_0x404457){_0x2577ec['model']=_[_0x0dc8('0x2b')](db[_0x0dc8('0x3d')][_0x0dc8('0x2c')]);_0x2577ec[_0x0dc8('0x26')]=_[_0x0dc8('0x2b')](_0x46c4b1['query']);_0x2577ec[_0x0dc8('0x3e')]=_[_0x0dc8('0x2d')](_0x2577ec[_0x0dc8('0x2a')],_0x2577ec[_0x0dc8('0x26')]);_0x1ef020['attributes']=_[_0x0dc8('0x2d')](_0x2577ec[_0x0dc8('0x2a')],qs[_0x0dc8('0x2f')](_0x46c4b1[_0x0dc8('0x26')]['fields']));_0x1ef020[_0x0dc8('0x2e')]=_0x1ef020[_0x0dc8('0x2e')][_0x0dc8('0x30')]?_0x1ef020[_0x0dc8('0x2e')]:_0x2577ec[_0x0dc8('0x2a')];if(!_0x46c4b1[_0x0dc8('0x26')]['hasOwnProperty'](_0x0dc8('0x3f'))){_0x1ef020['limit']=qs[_0x0dc8('0x13')](_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x13')]);_0x1ef020['offset']=qs[_0x0dc8('0x15')](_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x15')]);}_0x1ef020[_0x0dc8('0x40')]=qs['sort'](_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x41')]);_0x1ef020[_0x0dc8('0x42')]=qs[_0x0dc8('0x3e')](_[_0x0dc8('0x43')](_0x46c4b1[_0x0dc8('0x26')],_0x2577ec['filters']));_0x1ef020['where'][_0x0dc8('0x44')]=_0x404457['id'];if(_0x46c4b1[_0x0dc8('0x26')]['filter']){_0x1ef020[_0x0dc8('0x42')]=_['merge'](_0x1ef020[_0x0dc8('0x42')],{'$or':_['map'](_0x1ef020[_0x0dc8('0x2e')],function(_0x4a4a79){var _0x161f18={};_0x161f18[_0x4a4a79]={'$like':'%'+_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x45')]+'%'};return _0x161f18;})});}if(_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x46')]){var _0x451ce6=_0x46c4b1['query'][_0x0dc8('0x46')]['split'](',');var _0x165778={};_0x165778[_0x451ce6[0x0]]={'$gte':moment(_0x451ce6[0x1])[_0x0dc8('0x47')]('YYYY-MM-DD\x20HH:mm:ss')};_0x1ef020[_0x0dc8('0x42')]=_[_0x0dc8('0x33')](_0x1ef020['where'],_0x165778);}_0x1ef020=_[_0x0dc8('0x33')]({},_0x1ef020,_0x46c4b1[_0x0dc8('0x48')]);return db[_0x0dc8('0x3d')][_0x0dc8('0x14')]({'where':_0x1ef020[_0x0dc8('0x42')]})[_0x0dc8('0x19')](function(_0x5a5c5e){_0x4b0590[_0x0dc8('0x14')]=_0x5a5c5e;if(_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x31')]){_0x1ef020[_0x0dc8('0x32')]=[{'all':!![]}];}return db[_0x0dc8('0x3d')][_0x0dc8('0x24')](_0x1ef020);})[_0x0dc8('0x19')](function(_0x3eb6d6){if(_0x46c4b1[_0x0dc8('0x26')][_0x0dc8('0x49')]==='true'){for(var _0x484d88=0x0;_0x484d88<_0x3eb6d6[_0x0dc8('0x30')];_0x484d88++){_0x3eb6d6[_0x484d88][_0x0dc8('0x36')]=juice(_0x3eb6d6[_0x484d88][_0x0dc8('0x36')]);}}_0x4b0590['rows']=_0x3eb6d6;return _0x4b0590;});}})['then'](respondWithFilteredResult(_0x5852e3,_0x1ef020))[_0x0dc8('0x35')](handleError(_0x5852e3,null));};exports[_0x0dc8('0x4a')]=function(_0x543f0e,_0x462a7e){return db[_0x0dc8('0x20')][_0x0dc8('0x37')]({'where':{'id':_0x543f0e[_0x0dc8('0x29')]['id']}})[_0x0dc8('0x19')](handleEntityNotFound(_0x462a7e,null))[_0x0dc8('0x19')](function(_0x24cf17){if(_0x24cf17){return _0x24cf17[_0x0dc8('0x4b')](_0x543f0e[_0x0dc8('0x36')][_0x0dc8('0x4c')],_[_0x0dc8('0x3a')](_0x543f0e[_0x0dc8('0x36')],[_0x0dc8('0x4c'),'id'])||{})['spread'](function(){return db['Tag'][_0x0dc8('0x24')]({'attributes':['id',_0x0dc8('0x1d'),'color'],'where':{'id':_0x543f0e[_0x0dc8('0x36')]['ids']}});})[_0x0dc8('0x19')](function(_0x21e067){socket[_0x0dc8('0x4d')](_0x0dc8('0x4e'),{'id':Number(_0x543f0e['params']['id']),'tags':_0x21e067||[]});return{'id':Number(_0x543f0e[_0x0dc8('0x29')]['id']),'tags':_0x21e067||[]};});}})[_0x0dc8('0x19')](respondWithResult(_0x462a7e,null))[_0x0dc8('0x35')](handleError(_0x462a7e,null));};exports[_0x0dc8('0x4f')]=function(_0x2f0e4f,_0x585d6c){return db['MailInteraction'][_0x0dc8('0x37')]({'where':{'id':_0x2f0e4f[_0x0dc8('0x29')]['id']}})['then'](handleEntityNotFound(_0x585d6c,null))['then'](function(_0x356248){if(_0x356248){return _0x356248[_0x0dc8('0x4f')](_0x2f0e4f[_0x0dc8('0x26')][_0x0dc8('0x4c')]);}})[_0x0dc8('0x19')](respondWithStatusCode(_0x585d6c,null))[_0x0dc8('0x35')](handleError(_0x585d6c,null));};exports['download']=function(_0x2b724d,_0x5a0f40){var _0x5db3b9=moment()[_0x0dc8('0x50')]()[_0x0dc8('0x51')]();var _0x106e41=path[_0x0dc8('0x52')](config[_0x0dc8('0x53')],_0x0dc8('0x54'),_0x0dc8('0x55'),'tmp');var _0x370186=path[_0x0dc8('0x52')](config[_0x0dc8('0x53')],_0x0dc8('0x54'),_0x0dc8('0x55'),_0x0dc8('0x56'));var _0x638787=path[_0x0dc8('0x52')](_0x106e41,_0x5db3b9);var _0x10abf8=util['format'](_0x0dc8('0x57'),_0x2b724d[_0x0dc8('0x29')]['id'],_0x5db3b9);var _0x500f58=path[_0x0dc8('0x52')](_0x106e41,_0x10abf8);var _0x93c9f1=[];if(_0x2b724d[_0x0dc8('0x26')][_0x0dc8('0x56')]){_0x93c9f1[_0x0dc8('0x58')]({'model':db[_0x0dc8('0x59')],'as':_0x0dc8('0x5a'),'raw':!![]});}var _0x2e80ff=[{'model':db[_0x0dc8('0x3d')],'as':_0x0dc8('0x5b'),'attributes':['id',_0x0dc8('0x36'),'from','to','cc',_0x0dc8('0x5c'),_0x0dc8('0x5d'),'createdAt'],'where':{'secret':![]},'order':qs[_0x0dc8('0x41')]('id'),'include':_0x93c9f1}];return db[_0x0dc8('0x20')][_0x0dc8('0x37')]({'where':{'id':_0x2b724d[_0x0dc8('0x29')]['id']},'include':_0x2e80ff})[_0x0dc8('0x19')](handleEntityNotFound(_0x5a0f40,null))[_0x0dc8('0x19')](function(_0x5576c2){if(_0x5576c2&&_0x5576c2[_0x0dc8('0x5b')]){var _0x1486f9=_0x5576c2[_0x0dc8('0x5e')]({'plain':!![]});fs[_0x0dc8('0x5f')](_0x638787);return BPromise[_0x0dc8('0x60')](_0x1486f9[_0x0dc8('0x5b')],function(_0x5abbe0,_0x5203d0){var _0x5cab73={'from':_0x5abbe0[_0x0dc8('0x61')],'to':_0x5abbe0['to']||'missing.recipient@your.company.com','subject':_0x5abbe0['subject'],'html':htmlEntities[_0x0dc8('0x62')](_0x5abbe0['body']),'headers':{'Date':moment(_0x5abbe0['createdAt'])['format']('ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ')},'attachments':[]};if(_0x5abbe0['cc']){_0x5cab73[_0x0dc8('0x63')]['Cc']=_0x5abbe0['cc'];}if(_0x5abbe0[_0x0dc8('0x5c')]){_0x5cab73['headers'][_0x0dc8('0x64')]=_0x5abbe0['bcc'];}return new BPromise(function(_0x318c9c,_0x3363f7){if(_0x5abbe0[_0x0dc8('0x5a')]){for(var _0x3edb82=0x0;_0x3edb82<_0x5abbe0[_0x0dc8('0x5a')][_0x0dc8('0x30')];_0x3edb82++){var _0x4cb0a3=_0x5abbe0[_0x0dc8('0x5a')][_0x3edb82];var _0x400032=path[_0x0dc8('0x52')](_0x370186,_0x4cb0a3['basename']);if(fs[_0x0dc8('0x65')](_0x400032)){_0x5cab73[_0x0dc8('0x56')][_0x0dc8('0x58')]({'name':_0x4cb0a3[_0x0dc8('0x1d')],'contentType':_0x4cb0a3[_0x0dc8('0x22')],'data':fs['readFileSync'](_0x400032)});}}}emlformat['build'](_0x5cab73,function(_0x265d5b,_0x1bb875){if(_0x265d5b)return _0x3363f7(_0x265d5b);fs[_0x0dc8('0x66')](path[_0x0dc8('0x52')](_0x638787,util[_0x0dc8('0x47')](_0x0dc8('0x67'),_0x5203d0)),_0x1bb875);return _0x318c9c(_0x1bb875);});});})[_0x0dc8('0x19')](function(){return new BPromise(function(_0x55abfa,_0x5ef449){zipdir(_0x638787,{'saveTo':_0x500f58},function(_0x3d153c,_0x30d025){if(_0x3d153c)return _0x5ef449(_0x3d153c);return _0x55abfa(_0x30d025);});})[_0x0dc8('0x19')](function(){return new BPromise(function(_0x3d2111,_0x488ce2){rimraf(_0x638787,function(_0x2f0c24){if(_0x2f0c24)_0x488ce2(_0x2f0c24);return _0x3d2111();});});})[_0x0dc8('0x19')](function(){return _0x5a0f40['download'](_0x500f58,_0x10abf8,function(_0x45bf9c){if(!_0x45bf9c)fs[_0x0dc8('0x68')](_0x500f58);});});});}else{return _0x5a0f40[_0x0dc8('0xe')](0xc8);}})[_0x0dc8('0x35')](handleError(_0x5a0f40,null));};