b14a62b09225bbcff15fd6d82e9bccb1e4757ce7
[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 _0x96f5=['keys','filters','intersection','attributes','fields','length','find','create','body','update','addMessage','FaxMessage','ids','omit','getMessages','findOne','hasOwnProperty','nolimit','order','sort','where','FaxInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','merge','options','include','addTags','spread','Tag','emit','faxInteractionTags:save','removeTags','download','unix','join','root','files','server','attachments','transcript-%d-%s.zip','push','Attachment','Messages','createdAt','get','mkdirSync','basename','existsSync','createWriteStream','log','err','unlinkSync','rimraf','zip-dir','util','path','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','defaults','redis','socket.io-emitter','./faxInteraction.socket','sendStatus','status','end','json','offset','undefined','limit','count','set','destroy','then','error','stack','name','send','index','map','FaxInteraction','rawAttributes','fieldName','findAll','fax','query','user','catch','show','params','model'];(function(_0x1dc6b7,_0x15612a){var _0x386b4c=function(_0x5a5995){while(--_0x5a5995){_0x1dc6b7['push'](_0x1dc6b7['shift']());}};_0x386b4c(++_0x15612a);}(_0x96f5,0x9b));var _0x596f=function(_0x506d2f,_0x5ab76e){_0x506d2f=_0x506d2f-0x0;var _0x101255=_0x96f5[_0x506d2f];return _0x101255;};'use strict';var rimraf=require(_0x596f('0x0'));var zipdir=require(_0x596f('0x1'));var moment=require('moment');var BPromise=require('bluebird');var util=require(_0x596f('0x2'));var path=require(_0x596f('0x3'));var fs=require('fs');var _=require(_0x596f('0x4'));var Redis=require(_0x596f('0x5'));var interaction=require(_0x596f('0x6'));var qs=require(_0x596f('0x7'));var logger=require(_0x596f('0x8'))(_0x596f('0x9'));var config=require(_0x596f('0xa'));var db=require('../../mysqldb')['db'];config['redis']=_[_0x596f('0xb')](config[_0x596f('0xc')],{'host':'localhost','port':0x18eb});var socket=require(_0x596f('0xd'))(new Redis(config[_0x596f('0xc')]));require(_0x596f('0xe'))['register'](socket);function respondWithStatusCode(_0x290f69,_0x45a6e9){_0x45a6e9=_0x45a6e9||0xcc;return function(_0x28b094){if(_0x28b094){return _0x290f69[_0x596f('0xf')](_0x45a6e9);}return _0x290f69[_0x596f('0x10')](_0x45a6e9)[_0x596f('0x11')]();};}function respondWithResult(_0x27beec,_0x215228){_0x215228=_0x215228||0xc8;return function(_0x5ef02d){if(_0x5ef02d){return _0x27beec['status'](_0x215228)[_0x596f('0x12')](_0x5ef02d);}};}function respondWithFilteredResult(_0x5095df,_0x21b6da){return function(_0x311929){if(_0x311929){var _0x3c001e=typeof _0x21b6da[_0x596f('0x13')]===_0x596f('0x14')&&typeof _0x21b6da[_0x596f('0x15')]==='undefined';var _0x1b5524=_0x311929[_0x596f('0x16')];var _0x1aab23=_0x3c001e?0x0:_0x21b6da[_0x596f('0x13')];var _0x545b54=_0x3c001e?_0x311929[_0x596f('0x16')]:_0x21b6da[_0x596f('0x13')]+_0x21b6da[_0x596f('0x15')];var _0x1917ee;if(_0x545b54>=_0x1b5524){_0x545b54=_0x1b5524;_0x1917ee=0xc8;}else{_0x1917ee=0xce;}_0x5095df[_0x596f('0x10')](_0x1917ee);return _0x5095df[_0x596f('0x17')]('Content-Range',_0x1aab23+'-'+_0x545b54+'/'+_0x1b5524)[_0x596f('0x12')](_0x311929);}return null;};}function saveUpdates(_0x50e940){return function(_0x2d8cf6){if(_0x2d8cf6){return _0x2d8cf6['update'](_0x50e940)['then'](function(_0x4f0e85){return _0x4f0e85;});}return null;};}function removeEntity(_0xdd98b4){return function(_0x192f28){if(_0x192f28){return _0x192f28[_0x596f('0x18')]()[_0x596f('0x19')](function(){_0xdd98b4['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x56ab8){return function(_0x5c9f5f){if(!_0x5c9f5f){_0x56ab8[_0x596f('0xf')](0x194);}return _0x5c9f5f;};}function handleError(_0x29f4e7,_0x4b908f){_0x4b908f=_0x4b908f||0x1f4;return function(_0x4601bf){logger[_0x596f('0x1a')](_0x4601bf[_0x596f('0x1b')]);if(_0x4601bf[_0x596f('0x1c')]){delete _0x4601bf[_0x596f('0x1c')];}_0x29f4e7[_0x596f('0x10')](_0x4b908f)[_0x596f('0x1d')](_0x4601bf);};}exports[_0x596f('0x1e')]=function(_0x19eb60,_0x415d92){var _0x419ef2={};var _0x22d33b=_[_0x596f('0x1f')](db[_0x596f('0x20')][_0x596f('0x21')],function(_0x22128f){return{'name':_0x22128f[_0x596f('0x22')],'type':_0x22128f['type']['key']};});_0x419ef2=qs['getOptions'](_0x22d33b,_0x19eb60);return interaction[_0x596f('0x23')](_0x596f('0x24'),_0x19eb60[_0x596f('0x25')],_0x19eb60[_0x596f('0x26')],_0x419ef2,_0x22d33b)[_0x596f('0x19')](respondWithFilteredResult(_0x415d92,_0x419ef2))[_0x596f('0x27')](handleError(_0x415d92,null));};exports[_0x596f('0x28')]=function(_0x5caeb2,_0x251b1a){var _0x297cf1={'raw':![],'where':{'id':_0x5caeb2[_0x596f('0x29')]['id']}},_0x194304={};_0x194304[_0x596f('0x2a')]=_[_0x596f('0x2b')](db[_0x596f('0x20')][_0x596f('0x21')]);_0x194304['query']=_[_0x596f('0x2b')](_0x5caeb2[_0x596f('0x25')]);_0x194304[_0x596f('0x2c')]=_[_0x596f('0x2d')](_0x194304[_0x596f('0x2a')],_0x194304[_0x596f('0x25')]);_0x297cf1[_0x596f('0x2e')]=_[_0x596f('0x2d')](_0x194304[_0x596f('0x2a')],qs[_0x596f('0x2f')](_0x5caeb2[_0x596f('0x25')][_0x596f('0x2f')]));_0x297cf1['attributes']=_0x297cf1[_0x596f('0x2e')][_0x596f('0x30')]?_0x297cf1[_0x596f('0x2e')]:_0x194304[_0x596f('0x2a')];if(_0x5caeb2[_0x596f('0x25')]['includeAll']){_0x297cf1['include']=[{'all':!![]}];}_0x297cf1=_['merge']({},_0x297cf1,_0x5caeb2['options']);return db['FaxInteraction'][_0x596f('0x31')](_0x297cf1)['then'](handleEntityNotFound(_0x251b1a,null))['then'](respondWithResult(_0x251b1a,null))[_0x596f('0x27')](handleError(_0x251b1a,null));};exports[_0x596f('0x32')]=function(_0x46bd69,_0x580df9){return db[_0x596f('0x20')][_0x596f('0x32')](_0x46bd69[_0x596f('0x33')],{})[_0x596f('0x19')](respondWithResult(_0x580df9,0xc9))['catch'](handleError(_0x580df9,null));};exports[_0x596f('0x34')]=function(_0x2dcb6a,_0x554467){if(_0x2dcb6a[_0x596f('0x33')]['id']){delete _0x2dcb6a[_0x596f('0x33')]['id'];}return db[_0x596f('0x20')]['find']({'where':{'id':_0x2dcb6a['params']['id']}})[_0x596f('0x19')](handleEntityNotFound(_0x554467,null))[_0x596f('0x19')](saveUpdates(_0x2dcb6a[_0x596f('0x33')],null))['then'](respondWithResult(_0x554467,null))[_0x596f('0x27')](handleError(_0x554467,null));};exports[_0x596f('0x18')]=function(_0x3e03bd,_0x229fce){return db[_0x596f('0x20')]['find']({'where':{'id':_0x3e03bd[_0x596f('0x29')]['id']}})[_0x596f('0x19')](handleEntityNotFound(_0x229fce,null))[_0x596f('0x19')](removeEntity(_0x229fce,null))[_0x596f('0x27')](handleError(_0x229fce,null));};exports['describe']=function(_0x44593b,_0x3db746){return db[_0x596f('0x20')]['describe']()[_0x596f('0x19')](respondWithResult(_0x3db746,null))[_0x596f('0x27')](handleError(_0x3db746,null));};exports[_0x596f('0x35')]=function(_0x3da3e6,_0x4024f6){return db[_0x596f('0x36')][_0x596f('0x31')]({'where':{'id':_0x3da3e6['params']['id']}})[_0x596f('0x19')](handleEntityNotFound(_0x4024f6,null))['then'](function(_0x4b0f86){if(_0x4b0f86){return _0x4b0f86[_0x596f('0x35')](_0x3da3e6[_0x596f('0x33')][_0x596f('0x37')],_[_0x596f('0x38')](_0x3da3e6['body'],[_0x596f('0x37'),'id'])||{});}})[_0x596f('0x19')](respondWithResult(_0x4024f6,null))['catch'](handleError(_0x4024f6,null));};exports[_0x596f('0x39')]=function(_0x431f6b,_0x5c1432){var _0x26c962={'raw':![],'where':{}};var _0x35677b={};var _0x1cd665={'count':0x0,'rows':[]};return db[_0x596f('0x20')][_0x596f('0x3a')]({'where':{'id':_0x431f6b['params']['id']}})[_0x596f('0x19')](handleEntityNotFound(_0x5c1432,null))[_0x596f('0x19')](function(_0x46f44d){if(_0x46f44d){_0x35677b[_0x596f('0x2a')]=_[_0x596f('0x2b')](db[_0x596f('0x36')][_0x596f('0x21')]);_0x35677b[_0x596f('0x25')]=_['keys'](_0x431f6b[_0x596f('0x25')]);_0x35677b[_0x596f('0x2c')]=_[_0x596f('0x2d')](_0x35677b['model'],_0x35677b[_0x596f('0x25')]);_0x26c962[_0x596f('0x2e')]=_[_0x596f('0x2d')](_0x35677b['model'],qs['fields'](_0x431f6b['query'][_0x596f('0x2f')]));_0x26c962[_0x596f('0x2e')]=_0x26c962[_0x596f('0x2e')][_0x596f('0x30')]?_0x26c962['attributes']:_0x35677b['model'];if(!_0x431f6b[_0x596f('0x25')][_0x596f('0x3b')](_0x596f('0x3c'))){_0x26c962['limit']=qs[_0x596f('0x15')](_0x431f6b['query'][_0x596f('0x15')]);_0x26c962[_0x596f('0x13')]=qs['offset'](_0x431f6b[_0x596f('0x25')][_0x596f('0x13')]);}_0x26c962[_0x596f('0x3d')]=qs[_0x596f('0x3e')](_0x431f6b[_0x596f('0x25')][_0x596f('0x3e')]);_0x26c962[_0x596f('0x3f')]=qs['filters'](_['pick'](_0x431f6b[_0x596f('0x25')],_0x35677b[_0x596f('0x2c')]));_0x26c962[_0x596f('0x3f')][_0x596f('0x40')]=_0x46f44d['id'];if(_0x431f6b[_0x596f('0x25')]['filter']){_0x26c962[_0x596f('0x3f')]=_['merge'](_0x26c962[_0x596f('0x3f')],{'$or':_['map'](_0x26c962['attributes'],function(_0x93feb0){var _0x3ea6a9={};_0x3ea6a9[_0x93feb0]={'$like':'%'+_0x431f6b[_0x596f('0x25')][_0x596f('0x41')]+'%'};return _0x3ea6a9;})});}if(_0x431f6b['query'][_0x596f('0x42')]){var _0x2a4c7a=_0x431f6b['query'][_0x596f('0x42')][_0x596f('0x43')](',');var _0x52c37c={};_0x52c37c[_0x2a4c7a[0x0]]={'$gte':moment(_0x2a4c7a[0x1])[_0x596f('0x44')](_0x596f('0x45'))};_0x26c962[_0x596f('0x3f')]=_['merge'](_0x26c962[_0x596f('0x3f')],_0x52c37c);}_0x26c962=_[_0x596f('0x46')]({},_0x26c962,_0x431f6b[_0x596f('0x47')]);return db[_0x596f('0x36')][_0x596f('0x16')]({'where':_0x26c962[_0x596f('0x3f')]})['then'](function(_0x27d630){_0x1cd665['count']=_0x27d630;if(_0x431f6b['query']['includeAll']){_0x26c962[_0x596f('0x48')]=[{'all':!![]}];}return db[_0x596f('0x36')]['findAll'](_0x26c962);})['then'](function(_0x8de5a9){_0x1cd665['rows']=_0x8de5a9;return _0x1cd665;});}})[_0x596f('0x19')](respondWithFilteredResult(_0x5c1432,_0x26c962))['catch'](handleError(_0x5c1432,null));};exports[_0x596f('0x49')]=function(_0x1ecab6,_0x2e81d3){return db[_0x596f('0x20')][_0x596f('0x31')]({'where':{'id':_0x1ecab6[_0x596f('0x29')]['id']}})[_0x596f('0x19')](handleEntityNotFound(_0x2e81d3,null))[_0x596f('0x19')](function(_0x242342){if(_0x242342){return _0x242342['setTags'](_0x1ecab6[_0x596f('0x33')][_0x596f('0x37')],_['omit'](_0x1ecab6[_0x596f('0x33')],['ids','id'])||{})[_0x596f('0x4a')](function(){return db[_0x596f('0x4b')][_0x596f('0x23')]({'attributes':['id',_0x596f('0x1c'),'color'],'where':{'id':_0x1ecab6[_0x596f('0x33')][_0x596f('0x37')]}});})[_0x596f('0x19')](function(_0x5e9ddb){socket[_0x596f('0x4c')](_0x596f('0x4d'),{'id':Number(_0x1ecab6[_0x596f('0x29')]['id']),'tags':_0x5e9ddb||[]});return{'id':Number(_0x1ecab6['params']['id']),'tags':_0x5e9ddb||[]};});}})[_0x596f('0x19')](respondWithResult(_0x2e81d3,null))[_0x596f('0x27')](handleError(_0x2e81d3,null));};exports[_0x596f('0x4e')]=function(_0x196ca8,_0x57799b){return db[_0x596f('0x20')][_0x596f('0x31')]({'where':{'id':_0x196ca8[_0x596f('0x29')]['id']}})['then'](handleEntityNotFound(_0x57799b,null))[_0x596f('0x19')](function(_0x304f44){if(_0x304f44){return _0x304f44[_0x596f('0x4e')](_0x196ca8[_0x596f('0x25')][_0x596f('0x37')]);}})[_0x596f('0x19')](respondWithStatusCode(_0x57799b,null))[_0x596f('0x27')](handleError(_0x57799b,null));};exports[_0x596f('0x4f')]=function(_0x1eec81,_0x441d83){var _0x15f852=moment()[_0x596f('0x50')]()['toString']();var _0x1ec9ec=path[_0x596f('0x51')](config[_0x596f('0x52')],'server',_0x596f('0x53'),'tmp');var _0x375352=path['join'](config[_0x596f('0x52')],_0x596f('0x54'),'files',_0x596f('0x55'));var _0x14394b=path[_0x596f('0x51')](_0x1ec9ec,_0x15f852);var _0x4dc7f3=util[_0x596f('0x44')](_0x596f('0x56'),_0x1eec81['params']['id'],_0x15f852);var _0x1495d0=path[_0x596f('0x51')](_0x1ec9ec,_0x4dc7f3);var _0x339d85=[];_0x339d85[_0x596f('0x57')]({'model':db[_0x596f('0x58')],'as':'Attachment','raw':!![]});var _0x10e96d=[{'model':db[_0x596f('0x36')],'as':_0x596f('0x59'),'attributes':['id',_0x596f('0x33'),_0x596f('0x5a')],'include':_0x339d85}];return db[_0x596f('0x20')]['find']({'where':{'id':_0x1eec81[_0x596f('0x29')]['id']},'include':_0x10e96d})[_0x596f('0x19')](handleEntityNotFound(_0x441d83,null))[_0x596f('0x19')](function(_0x30a1d){if(_0x30a1d&&_0x30a1d['Messages']){var _0x229ef5=_0x30a1d[_0x596f('0x5b')]({'plain':!![]});fs[_0x596f('0x5c')](_0x14394b);for(var _0x7a5a02=0x0;_0x7a5a02<_0x229ef5[_0x596f('0x59')][_0x596f('0x30')];_0x7a5a02++){var _0x5cb905=_0x229ef5[_0x596f('0x59')][_0x7a5a02]['Attachment'];if(_0x5cb905){var _0x2c5587=path[_0x596f('0x51')](_0x375352,_0x5cb905[_0x596f('0x5d')]);if(fs[_0x596f('0x5e')](_0x2c5587)){fs['createReadStream'](_0x2c5587)['pipe'](fs[_0x596f('0x5f')](path[_0x596f('0x51')](_0x14394b,_0x5cb905['basename'])));}}}return BPromise['resolve']()[_0x596f('0x19')](function(){return new BPromise(function(_0x2ac51c,_0x5afcf3){zipdir(_0x14394b,{'saveTo':_0x1495d0},function(_0x6fc697,_0x52bdef){if(_0x6fc697)return _0x5afcf3(_0x6fc697);return _0x2ac51c(_0x52bdef);});})[_0x596f('0x19')](function(){return new BPromise(function(_0x2abca7,_0x339eba){rimraf(_0x14394b,function(_0x343bb0){if(_0x343bb0)_0x339eba(_0x343bb0);return _0x2abca7();});});})[_0x596f('0x19')](function(){return _0x441d83[_0x596f('0x4f')](_0x1495d0,_0x4dc7f3,function(_0x3a78cb){if(_0x3a78cb){console[_0x596f('0x60')](_0x596f('0x61'),_0x3a78cb);}else{fs[_0x596f('0x62')](_0x1495d0);}});});});}else{return _0x441d83[_0x596f('0xf')](0xc8);}})['catch'](handleError(_0x441d83,null));};