9fa63e4bfe6034e83849a4cbcfb6b7d0a4fb2a2d
[motion2.git] / server / api / faxInteraction / faxInteraction.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 _0x0f07=['update','destroy','stack','name','send','index','FaxInteraction','rawAttributes','fieldName','type','key','getOptions','user','then','catch','model','keys','query','filters','intersection','attributes','fields','include','merge','options','find','create','body','params','describe','addMessage','omit','ids','getMessages','findOne','FaxMessage','length','hasOwnProperty','nolimit','order','sort','where','FaxInteractionId','filter','$gte','split','YYYY-MM-DD\x20HH:mm:ss','includeAll','findAll','rows','spread','Tag','color','download','unix','join','root','tmp','server','files','attachments','format','transcript-%d-%s.zip','Attachment','Messages','createdAt','get','mkdirSync','existsSync','createReadStream','pipe','createWriteStream','resolve','log','err','unlinkSync','rimraf','zip-dir','moment','bluebird','path','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','redis','localhost','socket.io-emitter','register','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range'];(function(_0x24cf91,_0x1a2645){var _0x2db5c9=function(_0xfccee9){while(--_0xfccee9){_0x24cf91['push'](_0x24cf91['shift']());}};_0x2db5c9(++_0x1a2645);}(_0x0f07,0x11a));var _0x70f0=function(_0x1663d9,_0x4fc07e){_0x1663d9=_0x1663d9-0x0;var _0xa7d532=_0x0f07[_0x1663d9];return _0xa7d532;};'use strict';var rimraf=require(_0x70f0('0x0'));var zipdir=require(_0x70f0('0x1'));var moment=require(_0x70f0('0x2'));var BPromise=require(_0x70f0('0x3'));var util=require('util');var path=require(_0x70f0('0x4'));var fs=require('fs');var _=require(_0x70f0('0x5'));var Redis=require(_0x70f0('0x6'));var interaction=require(_0x70f0('0x7'));var qs=require(_0x70f0('0x8'));var logger=require(_0x70f0('0x9'))(_0x70f0('0xa'));var config=require(_0x70f0('0xb'));var db=require(_0x70f0('0xc'))['db'];config[_0x70f0('0xd')]=_['defaults'](config['redis'],{'host':_0x70f0('0xe'),'port':0x18eb});var socket=require(_0x70f0('0xf'))(new Redis(config[_0x70f0('0xd')]));require('./faxInteraction.socket')[_0x70f0('0x10')](socket);function respondWithStatusCode(_0xe9d6f2,_0x3d9544){_0x3d9544=_0x3d9544||0xcc;return function(_0x3b5307){if(_0x3b5307){return _0xe9d6f2[_0x70f0('0x11')](_0x3d9544);}return _0xe9d6f2[_0x70f0('0x12')](_0x3d9544)[_0x70f0('0x13')]();};}function respondWithResult(_0x5af282,_0x1515fe){_0x1515fe=_0x1515fe||0xc8;return function(_0x281547){if(_0x281547){return _0x5af282[_0x70f0('0x12')](_0x1515fe)[_0x70f0('0x14')](_0x281547);}};}function respondWithFilteredResult(_0x1774d7,_0x490b63){return function(_0x1a104a){if(_0x1a104a){var _0x35d0c9=typeof _0x490b63[_0x70f0('0x15')]===_0x70f0('0x16')&&typeof _0x490b63['limit']===_0x70f0('0x16');var _0x376a25=_0x1a104a[_0x70f0('0x17')];var _0x48c192=_0x35d0c9?0x0:_0x490b63[_0x70f0('0x15')];var _0x20b938=_0x35d0c9?_0x1a104a[_0x70f0('0x17')]:_0x490b63[_0x70f0('0x15')]+_0x490b63[_0x70f0('0x18')];var _0x323802;if(_0x20b938>=_0x376a25){_0x20b938=_0x376a25;_0x323802=0xc8;}else{_0x323802=0xce;}_0x1774d7[_0x70f0('0x12')](_0x323802);return _0x1774d7[_0x70f0('0x19')](_0x70f0('0x1a'),_0x48c192+'-'+_0x20b938+'/'+_0x376a25)[_0x70f0('0x14')](_0x1a104a);}return null;};}function saveUpdates(_0x4f9824){return function(_0x25d21b){if(_0x25d21b){return _0x25d21b[_0x70f0('0x1b')](_0x4f9824)['then'](function(_0x10d7f8){return _0x10d7f8;});}return null;};}function removeEntity(_0x46face){return function(_0x2955c7){if(_0x2955c7){return _0x2955c7[_0x70f0('0x1c')]()['then'](function(){_0x46face[_0x70f0('0x12')](0xcc)[_0x70f0('0x13')]();});}};}function handleEntityNotFound(_0x481150){return function(_0x274829){if(!_0x274829){_0x481150['sendStatus'](0x194);}return _0x274829;};}function handleError(_0x576814,_0x5de30b){_0x5de30b=_0x5de30b||0x1f4;return function(_0x41d8f8){logger['error'](_0x41d8f8[_0x70f0('0x1d')]);if(_0x41d8f8[_0x70f0('0x1e')]){delete _0x41d8f8['name'];}_0x576814[_0x70f0('0x12')](_0x5de30b)[_0x70f0('0x1f')](_0x41d8f8);};}exports[_0x70f0('0x20')]=function(_0x4bf16e,_0x2e7e20){var _0x332432={};var _0x2321a2=_['map'](db[_0x70f0('0x21')][_0x70f0('0x22')],function(_0x7bce66){return{'name':_0x7bce66[_0x70f0('0x23')],'type':_0x7bce66[_0x70f0('0x24')][_0x70f0('0x25')]};});_0x332432=qs[_0x70f0('0x26')](_0x2321a2,_0x4bf16e);return interaction['findAll']('fax',_0x4bf16e['query'],_0x4bf16e[_0x70f0('0x27')],_0x332432,_0x2321a2)[_0x70f0('0x28')](respondWithFilteredResult(_0x2e7e20,_0x332432))[_0x70f0('0x29')](handleError(_0x2e7e20,null));};exports['show']=function(_0x4c9600,_0x32b1f1){var _0x3fe0b4={'raw':![],'where':{'id':_0x4c9600['params']['id']}},_0x3cde2d={};_0x3cde2d[_0x70f0('0x2a')]=_[_0x70f0('0x2b')](db[_0x70f0('0x21')][_0x70f0('0x22')]);_0x3cde2d['query']=_['keys'](_0x4c9600[_0x70f0('0x2c')]);_0x3cde2d[_0x70f0('0x2d')]=_[_0x70f0('0x2e')](_0x3cde2d[_0x70f0('0x2a')],_0x3cde2d[_0x70f0('0x2c')]);_0x3fe0b4[_0x70f0('0x2f')]=_[_0x70f0('0x2e')](_0x3cde2d[_0x70f0('0x2a')],qs[_0x70f0('0x30')](_0x4c9600[_0x70f0('0x2c')][_0x70f0('0x30')]));_0x3fe0b4['attributes']=_0x3fe0b4[_0x70f0('0x2f')]['length']?_0x3fe0b4['attributes']:_0x3cde2d['model'];if(_0x4c9600[_0x70f0('0x2c')]['includeAll']){_0x3fe0b4[_0x70f0('0x31')]=[{'all':!![]}];}_0x3fe0b4=_[_0x70f0('0x32')]({},_0x3fe0b4,_0x4c9600[_0x70f0('0x33')]);return db[_0x70f0('0x21')][_0x70f0('0x34')](_0x3fe0b4)['then'](handleEntityNotFound(_0x32b1f1,null))[_0x70f0('0x28')](respondWithResult(_0x32b1f1,null))[_0x70f0('0x29')](handleError(_0x32b1f1,null));};exports['create']=function(_0x723ebe,_0x468c5e){return db[_0x70f0('0x21')][_0x70f0('0x35')](_0x723ebe['body'],{})[_0x70f0('0x28')](respondWithResult(_0x468c5e,0xc9))['catch'](handleError(_0x468c5e,null));};exports[_0x70f0('0x1b')]=function(_0x4474b2,_0x761c54){if(_0x4474b2[_0x70f0('0x36')]['id']){delete _0x4474b2[_0x70f0('0x36')]['id'];}return db[_0x70f0('0x21')][_0x70f0('0x34')]({'where':{'id':_0x4474b2[_0x70f0('0x37')]['id']}})[_0x70f0('0x28')](handleEntityNotFound(_0x761c54,null))[_0x70f0('0x28')](saveUpdates(_0x4474b2['body'],null))[_0x70f0('0x28')](respondWithResult(_0x761c54,null))['catch'](handleError(_0x761c54,null));};exports[_0x70f0('0x1c')]=function(_0x3a447b,_0x327b39){return db[_0x70f0('0x21')][_0x70f0('0x34')]({'where':{'id':_0x3a447b[_0x70f0('0x37')]['id']}})[_0x70f0('0x28')](handleEntityNotFound(_0x327b39,null))['then'](removeEntity(_0x327b39,null))[_0x70f0('0x29')](handleError(_0x327b39,null));};exports['describe']=function(_0x41377d,_0x17e5fd){return db[_0x70f0('0x21')][_0x70f0('0x38')]()['then'](respondWithResult(_0x17e5fd,null))[_0x70f0('0x29')](handleError(_0x17e5fd,null));};exports[_0x70f0('0x39')]=function(_0x31e32d,_0x3d7334){return db['FaxMessage'][_0x70f0('0x34')]({'where':{'id':_0x31e32d[_0x70f0('0x37')]['id']}})[_0x70f0('0x28')](handleEntityNotFound(_0x3d7334,null))['then'](function(_0x5bcbad){if(_0x5bcbad){return _0x5bcbad[_0x70f0('0x39')](_0x31e32d['body']['ids'],_[_0x70f0('0x3a')](_0x31e32d[_0x70f0('0x36')],[_0x70f0('0x3b'),'id'])||{});}})[_0x70f0('0x28')](respondWithResult(_0x3d7334,null))['catch'](handleError(_0x3d7334,null));};exports[_0x70f0('0x3c')]=function(_0x525d0b,_0x5b878b){var _0x30fa72={'raw':![],'where':{}};var _0x45a2a0={};var _0x35578d={'count':0x0,'rows':[]};return db['FaxInteraction'][_0x70f0('0x3d')]({'where':{'id':_0x525d0b['params']['id']}})[_0x70f0('0x28')](handleEntityNotFound(_0x5b878b,null))[_0x70f0('0x28')](function(_0x5b2dad){if(_0x5b2dad){_0x45a2a0['model']=_[_0x70f0('0x2b')](db[_0x70f0('0x3e')][_0x70f0('0x22')]);_0x45a2a0[_0x70f0('0x2c')]=_['keys'](_0x525d0b[_0x70f0('0x2c')]);_0x45a2a0[_0x70f0('0x2d')]=_[_0x70f0('0x2e')](_0x45a2a0[_0x70f0('0x2a')],_0x45a2a0['query']);_0x30fa72[_0x70f0('0x2f')]=_[_0x70f0('0x2e')](_0x45a2a0[_0x70f0('0x2a')],qs[_0x70f0('0x30')](_0x525d0b['query'][_0x70f0('0x30')]));_0x30fa72[_0x70f0('0x2f')]=_0x30fa72[_0x70f0('0x2f')][_0x70f0('0x3f')]?_0x30fa72[_0x70f0('0x2f')]:_0x45a2a0['model'];if(!_0x525d0b[_0x70f0('0x2c')][_0x70f0('0x40')](_0x70f0('0x41'))){_0x30fa72[_0x70f0('0x18')]=qs['limit'](_0x525d0b[_0x70f0('0x2c')][_0x70f0('0x18')]);_0x30fa72[_0x70f0('0x15')]=qs[_0x70f0('0x15')](_0x525d0b['query'][_0x70f0('0x15')]);}_0x30fa72[_0x70f0('0x42')]=qs[_0x70f0('0x43')](_0x525d0b[_0x70f0('0x2c')]['sort']);_0x30fa72[_0x70f0('0x44')]=qs[_0x70f0('0x2d')](_['pick'](_0x525d0b['query'],_0x45a2a0[_0x70f0('0x2d')]));_0x30fa72[_0x70f0('0x44')][_0x70f0('0x45')]=_0x5b2dad['id'];if(_0x525d0b['query'][_0x70f0('0x46')]){_0x30fa72['where']=_[_0x70f0('0x32')](_0x30fa72['where'],{'$or':_['map'](_0x30fa72[_0x70f0('0x2f')],function(_0x484dfd){var _0x302828={};_0x302828[_0x484dfd]={'$like':'%'+_0x525d0b[_0x70f0('0x2c')][_0x70f0('0x46')]+'%'};return _0x302828;})});}if(_0x525d0b[_0x70f0('0x2c')][_0x70f0('0x47')]){var _0x331f0b=_0x525d0b['query'][_0x70f0('0x47')][_0x70f0('0x48')](',');var _0x2f19e7={};_0x2f19e7[_0x331f0b[0x0]]={'$gte':moment(_0x331f0b[0x1])['format'](_0x70f0('0x49'))};_0x30fa72[_0x70f0('0x44')]=_[_0x70f0('0x32')](_0x30fa72['where'],_0x2f19e7);}_0x30fa72=_[_0x70f0('0x32')]({},_0x30fa72,_0x525d0b['options']);return db[_0x70f0('0x3e')]['count']({'where':_0x30fa72[_0x70f0('0x44')]})[_0x70f0('0x28')](function(_0x2e8291){_0x35578d[_0x70f0('0x17')]=_0x2e8291;if(_0x525d0b[_0x70f0('0x2c')][_0x70f0('0x4a')]){_0x30fa72['include']=[{'all':!![]}];}return db[_0x70f0('0x3e')][_0x70f0('0x4b')](_0x30fa72);})[_0x70f0('0x28')](function(_0x1dceca){_0x35578d[_0x70f0('0x4c')]=_0x1dceca;return _0x35578d;});}})[_0x70f0('0x28')](respondWithFilteredResult(_0x5b878b,_0x30fa72))[_0x70f0('0x29')](handleError(_0x5b878b,null));};exports['addTags']=function(_0x45183a,_0x2cb713){return db[_0x70f0('0x21')][_0x70f0('0x34')]({'where':{'id':_0x45183a[_0x70f0('0x37')]['id']}})[_0x70f0('0x28')](handleEntityNotFound(_0x2cb713,null))['then'](function(_0x3943bc){if(_0x3943bc){return _0x3943bc['setTags'](_0x45183a[_0x70f0('0x36')][_0x70f0('0x3b')],_[_0x70f0('0x3a')](_0x45183a['body'],[_0x70f0('0x3b'),'id'])||{})[_0x70f0('0x4d')](function(){return db[_0x70f0('0x4e')][_0x70f0('0x4b')]({'attributes':['id',_0x70f0('0x1e'),_0x70f0('0x4f')],'where':{'id':_0x45183a[_0x70f0('0x36')][_0x70f0('0x3b')]}});})['then'](function(_0x487123){socket['emit']('faxInteractionTags:save',{'id':Number(_0x45183a[_0x70f0('0x37')]['id']),'tags':_0x487123||[]});return{'id':Number(_0x45183a[_0x70f0('0x37')]['id']),'tags':_0x487123||[]};});}})[_0x70f0('0x28')](respondWithResult(_0x2cb713,null))['catch'](handleError(_0x2cb713,null));};exports['removeTags']=function(_0x28bb80,_0x2bf031){return db[_0x70f0('0x21')]['find']({'where':{'id':_0x28bb80[_0x70f0('0x37')]['id']}})[_0x70f0('0x28')](handleEntityNotFound(_0x2bf031,null))['then'](function(_0x2c5773){if(_0x2c5773){return _0x2c5773['removeTags'](_0x28bb80[_0x70f0('0x2c')][_0x70f0('0x3b')]);}})[_0x70f0('0x28')](respondWithStatusCode(_0x2bf031,null))['catch'](handleError(_0x2bf031,null));};exports[_0x70f0('0x50')]=function(_0x263183,_0x4cbdf9){var _0x3332e6=moment()[_0x70f0('0x51')]()['toString']();var _0x5ba929=path[_0x70f0('0x52')](config[_0x70f0('0x53')],'server','files',_0x70f0('0x54'));var _0x43d71a=path[_0x70f0('0x52')](config[_0x70f0('0x53')],_0x70f0('0x55'),_0x70f0('0x56'),_0x70f0('0x57'));var _0x5f9c13=path[_0x70f0('0x52')](_0x5ba929,_0x3332e6);var _0x534baf=util[_0x70f0('0x58')](_0x70f0('0x59'),_0x263183[_0x70f0('0x37')]['id'],_0x3332e6);var _0x429002=path[_0x70f0('0x52')](_0x5ba929,_0x534baf);var _0x2eec6c=[];_0x2eec6c['push']({'model':db[_0x70f0('0x5a')],'as':_0x70f0('0x5a'),'raw':!![]});var _0x224427=[{'model':db[_0x70f0('0x3e')],'as':_0x70f0('0x5b'),'attributes':['id','body',_0x70f0('0x5c')],'include':_0x2eec6c}];return db[_0x70f0('0x21')][_0x70f0('0x34')]({'where':{'id':_0x263183[_0x70f0('0x37')]['id']},'include':_0x224427})[_0x70f0('0x28')](handleEntityNotFound(_0x4cbdf9,null))['then'](function(_0x15145c){if(_0x15145c&&_0x15145c[_0x70f0('0x5b')]){var _0x31983a=_0x15145c[_0x70f0('0x5d')]({'plain':!![]});fs[_0x70f0('0x5e')](_0x5f9c13);for(var _0x10efee=0x0;_0x10efee<_0x31983a[_0x70f0('0x5b')]['length'];_0x10efee++){var _0x303342=_0x31983a['Messages'][_0x10efee][_0x70f0('0x5a')];if(_0x303342){var _0x3e65d5=path['join'](_0x43d71a,_0x303342['basename']);if(fs[_0x70f0('0x5f')](_0x3e65d5)){fs[_0x70f0('0x60')](_0x3e65d5)[_0x70f0('0x61')](fs[_0x70f0('0x62')](path[_0x70f0('0x52')](_0x5f9c13,_0x303342['basename'])));}}}return BPromise[_0x70f0('0x63')]()[_0x70f0('0x28')](function(){return new BPromise(function(_0x5fc7b,_0x24b66f){zipdir(_0x5f9c13,{'saveTo':_0x429002},function(_0xb30b36,_0x50e417){if(_0xb30b36)return _0x24b66f(_0xb30b36);return _0x5fc7b(_0x50e417);});})[_0x70f0('0x28')](function(){return new BPromise(function(_0x4d244d,_0x27fe71){rimraf(_0x5f9c13,function(_0x5a749d){if(_0x5a749d)_0x27fe71(_0x5a749d);return _0x4d244d();});});})[_0x70f0('0x28')](function(){return _0x4cbdf9[_0x70f0('0x50')](_0x429002,_0x534baf,function(_0x212303){if(_0x212303){console[_0x70f0('0x64')](_0x70f0('0x65'),_0x212303);}else{fs[_0x70f0('0x66')](_0x429002);}});});});}else{return _0x4cbdf9[_0x70f0('0x11')](0xc8);}})[_0x70f0('0x29')](handleError(_0x4cbdf9,null));};