7e7f3db75c5dab68825dbfc456528cfff3097f68
[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 _0x81cf=['undefined','limit','set','Content-Range','update','then','destroy','stack','name','send','index','FaxInteraction','rawAttributes','fieldName','type','key','fax','query','user','show','model','keys','filters','intersection','attributes','fields','length','includeAll','include','merge','options','catch','create','body','find','params','describe','addMessage','FaxMessage','ids','findOne','hasOwnProperty','nolimit','order','sort','pick','where','map','$gte','split','count','findAll','rows','addTags','setTags','omit','spread','Tag','color','faxInteractionTags:save','download','unix','join','root','server','files','tmp','attachments','format','transcript-%d-%s.zip','push','Attachment','createdAt','get','mkdirSync','Messages','basename','existsSync','createReadStream','createWriteStream','resolve','log','err','unlinkSync','rimraf','moment','util','path','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','../../config/environment','defaults','localhost','socket.io-emitter','./faxInteraction.socket','sendStatus','end','status','json','offset'];(function(_0x41a3af,_0x35694d){var _0x2075ec=function(_0x36dffb){while(--_0x36dffb){_0x41a3af['push'](_0x41a3af['shift']());}};_0x2075ec(++_0x35694d);}(_0x81cf,0x1f0));var _0xf81c=function(_0x423edc,_0x1ec185){_0x423edc=_0x423edc-0x0;var _0x3d6101=_0x81cf[_0x423edc];return _0x3d6101;};'use strict';var rimraf=require(_0xf81c('0x0'));var zipdir=require('zip-dir');var moment=require(_0xf81c('0x1'));var BPromise=require('bluebird');var util=require(_0xf81c('0x2'));var path=require(_0xf81c('0x3'));var fs=require('fs');var _=require(_0xf81c('0x4'));var Redis=require(_0xf81c('0x5'));var interaction=require(_0xf81c('0x6'));var qs=require(_0xf81c('0x7'));var logger=require(_0xf81c('0x8'))('api');var config=require(_0xf81c('0x9'));var db=require('../../mysqldb')['db'];config['redis']=_[_0xf81c('0xa')](config['redis'],{'host':_0xf81c('0xb'),'port':0x18eb});var socket=require(_0xf81c('0xc'))(new Redis(config['redis']));require(_0xf81c('0xd'))['register'](socket);function respondWithStatusCode(_0x21a2b6,_0x5ae3db){_0x5ae3db=_0x5ae3db||0xcc;return function(_0x1fafb6){if(_0x1fafb6){return _0x21a2b6[_0xf81c('0xe')](_0x5ae3db);}return _0x21a2b6['status'](_0x5ae3db)[_0xf81c('0xf')]();};}function respondWithResult(_0x2adebb,_0x59ee9b){_0x59ee9b=_0x59ee9b||0xc8;return function(_0x40230b){if(_0x40230b){return _0x2adebb[_0xf81c('0x10')](_0x59ee9b)[_0xf81c('0x11')](_0x40230b);}};}function respondWithFilteredResult(_0x3470f0,_0x32e454){return function(_0x4756dd){if(_0x4756dd){var _0x594d4d=typeof _0x32e454[_0xf81c('0x12')]===_0xf81c('0x13')&&typeof _0x32e454[_0xf81c('0x14')]===_0xf81c('0x13');var _0x22ce73=_0x4756dd['count'];var _0x5528d5=_0x594d4d?0x0:_0x32e454[_0xf81c('0x12')];var _0x72f842=_0x594d4d?_0x4756dd['count']:_0x32e454[_0xf81c('0x12')]+_0x32e454[_0xf81c('0x14')];var _0x2ca4e2;if(_0x72f842>=_0x22ce73){_0x72f842=_0x22ce73;_0x2ca4e2=0xc8;}else{_0x2ca4e2=0xce;}_0x3470f0[_0xf81c('0x10')](_0x2ca4e2);return _0x3470f0[_0xf81c('0x15')](_0xf81c('0x16'),_0x5528d5+'-'+_0x72f842+'/'+_0x22ce73)[_0xf81c('0x11')](_0x4756dd);}return null;};}function saveUpdates(_0x2b339c){return function(_0x1fe6ad){if(_0x1fe6ad){return _0x1fe6ad[_0xf81c('0x17')](_0x2b339c)[_0xf81c('0x18')](function(_0x34f2a5){return _0x34f2a5;});}return null;};}function removeEntity(_0x2ce548){return function(_0x4dffa0){if(_0x4dffa0){return _0x4dffa0[_0xf81c('0x19')]()['then'](function(){_0x2ce548[_0xf81c('0x10')](0xcc)[_0xf81c('0xf')]();});}};}function handleEntityNotFound(_0x18df54){return function(_0x5aee2b){if(!_0x5aee2b){_0x18df54[_0xf81c('0xe')](0x194);}return _0x5aee2b;};}function handleError(_0x12eb16,_0x2f3b2f){_0x2f3b2f=_0x2f3b2f||0x1f4;return function(_0x4904ab){logger['error'](_0x4904ab[_0xf81c('0x1a')]);if(_0x4904ab[_0xf81c('0x1b')]){delete _0x4904ab[_0xf81c('0x1b')];}_0x12eb16[_0xf81c('0x10')](_0x2f3b2f)[_0xf81c('0x1c')](_0x4904ab);};}exports[_0xf81c('0x1d')]=function(_0x115265,_0x16dacd){var _0x270aaa={};var _0x207251=_['map'](db[_0xf81c('0x1e')][_0xf81c('0x1f')],function(_0x2438f4){return{'name':_0x2438f4[_0xf81c('0x20')],'type':_0x2438f4[_0xf81c('0x21')][_0xf81c('0x22')]};});_0x270aaa=qs['getOptions'](_0x207251,_0x115265);return interaction['findAll'](_0xf81c('0x23'),_0x115265[_0xf81c('0x24')],_0x115265[_0xf81c('0x25')],_0x270aaa,_0x207251)[_0xf81c('0x18')](respondWithFilteredResult(_0x16dacd,_0x270aaa))['catch'](handleError(_0x16dacd,null));};exports[_0xf81c('0x26')]=function(_0x5e0996,_0x118190){var _0x391501={'raw':![],'where':{'id':_0x5e0996['params']['id']}},_0x382a3b={};_0x382a3b[_0xf81c('0x27')]=_[_0xf81c('0x28')](db['FaxInteraction'][_0xf81c('0x1f')]);_0x382a3b['query']=_[_0xf81c('0x28')](_0x5e0996[_0xf81c('0x24')]);_0x382a3b[_0xf81c('0x29')]=_[_0xf81c('0x2a')](_0x382a3b[_0xf81c('0x27')],_0x382a3b['query']);_0x391501[_0xf81c('0x2b')]=_['intersection'](_0x382a3b[_0xf81c('0x27')],qs[_0xf81c('0x2c')](_0x5e0996[_0xf81c('0x24')][_0xf81c('0x2c')]));_0x391501[_0xf81c('0x2b')]=_0x391501[_0xf81c('0x2b')][_0xf81c('0x2d')]?_0x391501[_0xf81c('0x2b')]:_0x382a3b[_0xf81c('0x27')];if(_0x5e0996[_0xf81c('0x24')][_0xf81c('0x2e')]){_0x391501[_0xf81c('0x2f')]=[{'all':!![]}];}_0x391501=_[_0xf81c('0x30')]({},_0x391501,_0x5e0996[_0xf81c('0x31')]);return db[_0xf81c('0x1e')]['find'](_0x391501)[_0xf81c('0x18')](handleEntityNotFound(_0x118190,null))[_0xf81c('0x18')](respondWithResult(_0x118190,null))[_0xf81c('0x32')](handleError(_0x118190,null));};exports[_0xf81c('0x33')]=function(_0x40b77c,_0x37ced1){return db[_0xf81c('0x1e')][_0xf81c('0x33')](_0x40b77c[_0xf81c('0x34')],{})[_0xf81c('0x18')](respondWithResult(_0x37ced1,0xc9))[_0xf81c('0x32')](handleError(_0x37ced1,null));};exports['update']=function(_0x1663af,_0x19b56f){if(_0x1663af['body']['id']){delete _0x1663af[_0xf81c('0x34')]['id'];}return db['FaxInteraction'][_0xf81c('0x35')]({'where':{'id':_0x1663af[_0xf81c('0x36')]['id']}})[_0xf81c('0x18')](handleEntityNotFound(_0x19b56f,null))[_0xf81c('0x18')](saveUpdates(_0x1663af[_0xf81c('0x34')],null))[_0xf81c('0x18')](respondWithResult(_0x19b56f,null))[_0xf81c('0x32')](handleError(_0x19b56f,null));};exports['destroy']=function(_0x2afdf2,_0x58eebe){return db['FaxInteraction'][_0xf81c('0x35')]({'where':{'id':_0x2afdf2[_0xf81c('0x36')]['id']}})[_0xf81c('0x18')](handleEntityNotFound(_0x58eebe,null))[_0xf81c('0x18')](removeEntity(_0x58eebe,null))[_0xf81c('0x32')](handleError(_0x58eebe,null));};exports[_0xf81c('0x37')]=function(_0x14b1a5,_0x5eb873){return db[_0xf81c('0x1e')][_0xf81c('0x37')]()[_0xf81c('0x18')](respondWithResult(_0x5eb873,null))[_0xf81c('0x32')](handleError(_0x5eb873,null));};exports[_0xf81c('0x38')]=function(_0x5c7111,_0x4e6953){return db[_0xf81c('0x39')]['find']({'where':{'id':_0x5c7111[_0xf81c('0x36')]['id']}})['then'](handleEntityNotFound(_0x4e6953,null))[_0xf81c('0x18')](function(_0x54fff7){if(_0x54fff7){return _0x54fff7[_0xf81c('0x38')](_0x5c7111[_0xf81c('0x34')][_0xf81c('0x3a')],_['omit'](_0x5c7111[_0xf81c('0x34')],['ids','id'])||{});}})[_0xf81c('0x18')](respondWithResult(_0x4e6953,null))[_0xf81c('0x32')](handleError(_0x4e6953,null));};exports['getMessages']=function(_0x395ea3,_0xb7a94c){var _0x12c6c7={'raw':![],'where':{}};var _0x4e58b5={};var _0xb8b846={'count':0x0,'rows':[]};return db[_0xf81c('0x1e')][_0xf81c('0x3b')]({'where':{'id':_0x395ea3['params']['id']}})['then'](handleEntityNotFound(_0xb7a94c,null))['then'](function(_0x3cb87e){if(_0x3cb87e){_0x4e58b5[_0xf81c('0x27')]=_[_0xf81c('0x28')](db[_0xf81c('0x39')][_0xf81c('0x1f')]);_0x4e58b5['query']=_[_0xf81c('0x28')](_0x395ea3[_0xf81c('0x24')]);_0x4e58b5[_0xf81c('0x29')]=_[_0xf81c('0x2a')](_0x4e58b5[_0xf81c('0x27')],_0x4e58b5[_0xf81c('0x24')]);_0x12c6c7[_0xf81c('0x2b')]=_[_0xf81c('0x2a')](_0x4e58b5[_0xf81c('0x27')],qs['fields'](_0x395ea3[_0xf81c('0x24')][_0xf81c('0x2c')]));_0x12c6c7[_0xf81c('0x2b')]=_0x12c6c7[_0xf81c('0x2b')][_0xf81c('0x2d')]?_0x12c6c7[_0xf81c('0x2b')]:_0x4e58b5[_0xf81c('0x27')];if(!_0x395ea3[_0xf81c('0x24')][_0xf81c('0x3c')](_0xf81c('0x3d'))){_0x12c6c7[_0xf81c('0x14')]=qs[_0xf81c('0x14')](_0x395ea3[_0xf81c('0x24')][_0xf81c('0x14')]);_0x12c6c7[_0xf81c('0x12')]=qs[_0xf81c('0x12')](_0x395ea3[_0xf81c('0x24')][_0xf81c('0x12')]);}_0x12c6c7[_0xf81c('0x3e')]=qs[_0xf81c('0x3f')](_0x395ea3[_0xf81c('0x24')][_0xf81c('0x3f')]);_0x12c6c7['where']=qs[_0xf81c('0x29')](_[_0xf81c('0x40')](_0x395ea3[_0xf81c('0x24')],_0x4e58b5[_0xf81c('0x29')]));_0x12c6c7['where']['FaxInteractionId']=_0x3cb87e['id'];if(_0x395ea3['query']['filter']){_0x12c6c7[_0xf81c('0x41')]=_[_0xf81c('0x30')](_0x12c6c7[_0xf81c('0x41')],{'$or':_[_0xf81c('0x42')](_0x12c6c7[_0xf81c('0x2b')],function(_0x45d1dd){var _0x3fac6e={};_0x3fac6e[_0x45d1dd]={'$like':'%'+_0x395ea3[_0xf81c('0x24')]['filter']+'%'};return _0x3fac6e;})});}if(_0x395ea3[_0xf81c('0x24')]['$gte']){var _0x283b00=_0x395ea3[_0xf81c('0x24')][_0xf81c('0x43')][_0xf81c('0x44')](',');var _0x54f0b9={};_0x54f0b9[_0x283b00[0x0]]={'$gte':moment(_0x283b00[0x1])['format']('YYYY-MM-DD\x20HH:mm:ss')};_0x12c6c7[_0xf81c('0x41')]=_[_0xf81c('0x30')](_0x12c6c7['where'],_0x54f0b9);}_0x12c6c7=_[_0xf81c('0x30')]({},_0x12c6c7,_0x395ea3['options']);return db[_0xf81c('0x39')][_0xf81c('0x45')]({'where':_0x12c6c7[_0xf81c('0x41')]})['then'](function(_0x8814af){_0xb8b846['count']=_0x8814af;if(_0x395ea3[_0xf81c('0x24')]['includeAll']){_0x12c6c7[_0xf81c('0x2f')]=[{'all':!![]}];}return db[_0xf81c('0x39')][_0xf81c('0x46')](_0x12c6c7);})[_0xf81c('0x18')](function(_0x1bcf6a){_0xb8b846[_0xf81c('0x47')]=_0x1bcf6a;return _0xb8b846;});}})[_0xf81c('0x18')](respondWithFilteredResult(_0xb7a94c,_0x12c6c7))[_0xf81c('0x32')](handleError(_0xb7a94c,null));};exports[_0xf81c('0x48')]=function(_0x3b7a9f,_0x44dadd){return db[_0xf81c('0x1e')][_0xf81c('0x35')]({'where':{'id':_0x3b7a9f[_0xf81c('0x36')]['id']}})[_0xf81c('0x18')](handleEntityNotFound(_0x44dadd,null))[_0xf81c('0x18')](function(_0x277598){if(_0x277598){return _0x277598[_0xf81c('0x49')](_0x3b7a9f[_0xf81c('0x34')][_0xf81c('0x3a')],_[_0xf81c('0x4a')](_0x3b7a9f[_0xf81c('0x34')],[_0xf81c('0x3a'),'id'])||{})[_0xf81c('0x4b')](function(){return db[_0xf81c('0x4c')]['findAll']({'attributes':['id',_0xf81c('0x1b'),_0xf81c('0x4d')],'where':{'id':_0x3b7a9f['body'][_0xf81c('0x3a')]}});})[_0xf81c('0x18')](function(_0x27abee){socket['emit'](_0xf81c('0x4e'),{'id':Number(_0x3b7a9f[_0xf81c('0x36')]['id']),'tags':_0x27abee||[]});return{'id':Number(_0x3b7a9f[_0xf81c('0x36')]['id']),'tags':_0x27abee||[]};});}})[_0xf81c('0x18')](respondWithResult(_0x44dadd,null))['catch'](handleError(_0x44dadd,null));};exports['removeTags']=function(_0x28d028,_0x211524){return db[_0xf81c('0x1e')][_0xf81c('0x35')]({'where':{'id':_0x28d028[_0xf81c('0x36')]['id']}})[_0xf81c('0x18')](handleEntityNotFound(_0x211524,null))[_0xf81c('0x18')](function(_0x2a39a6){if(_0x2a39a6){return _0x2a39a6['removeTags'](_0x28d028['query'][_0xf81c('0x3a')]);}})[_0xf81c('0x18')](respondWithStatusCode(_0x211524,null))[_0xf81c('0x32')](handleError(_0x211524,null));};exports[_0xf81c('0x4f')]=function(_0x1a2150,_0xf8b5db){var _0x565dd1=moment()[_0xf81c('0x50')]()['toString']();var _0x8272ed=path[_0xf81c('0x51')](config[_0xf81c('0x52')],_0xf81c('0x53'),_0xf81c('0x54'),_0xf81c('0x55'));var _0x20daca=path[_0xf81c('0x51')](config['root'],_0xf81c('0x53'),_0xf81c('0x54'),_0xf81c('0x56'));var _0x132ae8=path[_0xf81c('0x51')](_0x8272ed,_0x565dd1);var _0x52ecd0=util[_0xf81c('0x57')](_0xf81c('0x58'),_0x1a2150[_0xf81c('0x36')]['id'],_0x565dd1);var _0x29701e=path['join'](_0x8272ed,_0x52ecd0);var _0x4d5854=[];_0x4d5854[_0xf81c('0x59')]({'model':db[_0xf81c('0x5a')],'as':_0xf81c('0x5a'),'raw':!![]});var _0x222300=[{'model':db[_0xf81c('0x39')],'as':'Messages','attributes':['id',_0xf81c('0x34'),_0xf81c('0x5b')],'include':_0x4d5854}];return db['FaxInteraction'][_0xf81c('0x35')]({'where':{'id':_0x1a2150[_0xf81c('0x36')]['id']},'include':_0x222300})[_0xf81c('0x18')](handleEntityNotFound(_0xf8b5db,null))[_0xf81c('0x18')](function(_0x2df6fb){if(_0x2df6fb&&_0x2df6fb['Messages']){var _0x114765=_0x2df6fb[_0xf81c('0x5c')]({'plain':!![]});fs[_0xf81c('0x5d')](_0x132ae8);for(var _0x675e9a=0x0;_0x675e9a<_0x114765[_0xf81c('0x5e')][_0xf81c('0x2d')];_0x675e9a++){var _0x44b7ae=_0x114765['Messages'][_0x675e9a][_0xf81c('0x5a')];if(_0x44b7ae){var _0x5a6af8=path[_0xf81c('0x51')](_0x20daca,_0x44b7ae[_0xf81c('0x5f')]);if(fs[_0xf81c('0x60')](_0x5a6af8)){fs[_0xf81c('0x61')](_0x5a6af8)['pipe'](fs[_0xf81c('0x62')](path[_0xf81c('0x51')](_0x132ae8,_0x44b7ae[_0xf81c('0x5f')])));}}}return BPromise[_0xf81c('0x63')]()['then'](function(){return new BPromise(function(_0x575de5,_0x2d6e4d){zipdir(_0x132ae8,{'saveTo':_0x29701e},function(_0x190e23,_0x3146c7){if(_0x190e23)return _0x2d6e4d(_0x190e23);return _0x575de5(_0x3146c7);});})['then'](function(){return new BPromise(function(_0x414a75,_0x3dccdc){rimraf(_0x132ae8,function(_0xbf6e96){if(_0xbf6e96)_0x3dccdc(_0xbf6e96);return _0x414a75();});});})[_0xf81c('0x18')](function(){return _0xf8b5db[_0xf81c('0x4f')](_0x29701e,_0x52ecd0,function(_0x29159b){if(_0x29159b){console[_0xf81c('0x64')](_0xf81c('0x65'),_0x29159b);}else{fs[_0xf81c('0x66')](_0x29701e);}});});});}else{return _0xf8b5db[_0xf81c('0xe')](0xc8);}})[_0xf81c('0x32')](handleError(_0xf8b5db,null));};