08684d74cacacc5f7e490db48c2786726c95edc0
[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 _0xf024=['undefined','limit','count','set','then','destroy','end','stack','name','send','map','FaxInteraction','type','key','getOptions','findAll','fax','user','catch','show','params','model','keys','query','filters','attributes','intersection','fields','includeAll','include','merge','options','create','body','describe','addMessage','FaxMessage','find','ids','getMessages','findOne','rawAttributes','length','hasOwnProperty','nolimit','sort','where','pick','FaxInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','rows','addTags','setTags','omit','spread','color','emit','faxInteractionTags:save','removeTags','download','toString','join','server','files','tmp','root','attachments','transcript-%d-%s.zip','push','Attachment','Messages','createdAt','get','mkdirSync','createReadStream','pipe','createWriteStream','basename','resolve','err','moment','bluebird','util','path','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','defaults','redis','localhost','socket.io-emitter','./faxInteraction.socket','register','sendStatus','status','offset'];(function(_0x1b32e2,_0x202f0e){var _0xdc6735=function(_0x2cc3d5){while(--_0x2cc3d5){_0x1b32e2['push'](_0x1b32e2['shift']());}};_0xdc6735(++_0x202f0e);}(_0xf024,0x124));var _0x4f02=function(_0x13b8f3,_0x442821){_0x13b8f3=_0x13b8f3-0x0;var _0x1a6d06=_0xf024[_0x13b8f3];return _0x1a6d06;};'use strict';var rimraf=require('rimraf');var zipdir=require('zip-dir');var moment=require(_0x4f02('0x0'));var BPromise=require(_0x4f02('0x1'));var util=require(_0x4f02('0x2'));var path=require(_0x4f02('0x3'));var fs=require('fs');var _=require(_0x4f02('0x4'));var Redis=require(_0x4f02('0x5'));var interaction=require(_0x4f02('0x6'));var qs=require(_0x4f02('0x7'));var logger=require(_0x4f02('0x8'))(_0x4f02('0x9'));var config=require(_0x4f02('0xa'));var db=require('../../mysqldb')['db'];config['redis']=_[_0x4f02('0xb')](config[_0x4f02('0xc')],{'host':_0x4f02('0xd'),'port':0x18eb});var socket=require(_0x4f02('0xe'))(new Redis(config[_0x4f02('0xc')]));require(_0x4f02('0xf'))[_0x4f02('0x10')](socket);function respondWithStatusCode(_0x190d5d,_0x3cb1ad){_0x3cb1ad=_0x3cb1ad||0xcc;return function(_0x2f055b){if(_0x2f055b){return _0x190d5d[_0x4f02('0x11')](_0x3cb1ad);}return _0x190d5d[_0x4f02('0x12')](_0x3cb1ad)['end']();};}function respondWithResult(_0x2b17de,_0x391e5a){_0x391e5a=_0x391e5a||0xc8;return function(_0x330fdc){if(_0x330fdc){return _0x2b17de['status'](_0x391e5a)['json'](_0x330fdc);}};}function respondWithFilteredResult(_0x4954ac,_0x5dc890){return function(_0x595eae){if(_0x595eae){var _0x4747e3=typeof _0x5dc890[_0x4f02('0x13')]===_0x4f02('0x14')&&typeof _0x5dc890[_0x4f02('0x15')]==='undefined';var _0x157484=_0x595eae[_0x4f02('0x16')];var _0x215ed8=_0x4747e3?0x0:_0x5dc890[_0x4f02('0x13')];var _0x138278=_0x4747e3?_0x595eae[_0x4f02('0x16')]:_0x5dc890[_0x4f02('0x13')]+_0x5dc890['limit'];var _0x5ca5d7;if(_0x138278>=_0x157484){_0x138278=_0x157484;_0x5ca5d7=0xc8;}else{_0x5ca5d7=0xce;}_0x4954ac[_0x4f02('0x12')](_0x5ca5d7);return _0x4954ac[_0x4f02('0x17')]('Content-Range',_0x215ed8+'-'+_0x138278+'/'+_0x157484)['json'](_0x595eae);}return null;};}function saveUpdates(_0x24dc67){return function(_0x4f6945){if(_0x4f6945){return _0x4f6945['update'](_0x24dc67)[_0x4f02('0x18')](function(_0x697c69){return _0x697c69;});}return null;};}function removeEntity(_0x194d0e){return function(_0x208116){if(_0x208116){return _0x208116[_0x4f02('0x19')]()['then'](function(){_0x194d0e[_0x4f02('0x12')](0xcc)[_0x4f02('0x1a')]();});}};}function handleEntityNotFound(_0x3ef1ea){return function(_0x26c21d){if(!_0x26c21d){_0x3ef1ea[_0x4f02('0x11')](0x194);}return _0x26c21d;};}function handleError(_0x2126b9,_0x3e7337){_0x3e7337=_0x3e7337||0x1f4;return function(_0x2e0fc9){logger['error'](_0x2e0fc9[_0x4f02('0x1b')]);if(_0x2e0fc9[_0x4f02('0x1c')]){delete _0x2e0fc9[_0x4f02('0x1c')];}_0x2126b9[_0x4f02('0x12')](_0x3e7337)[_0x4f02('0x1d')](_0x2e0fc9);};}exports['index']=function(_0x51fd4f,_0x5bda83){var _0x280800={};var _0x5afd98=_[_0x4f02('0x1e')](db[_0x4f02('0x1f')]['rawAttributes'],function(_0x374397){return{'name':_0x374397['fieldName'],'type':_0x374397[_0x4f02('0x20')][_0x4f02('0x21')]};});_0x280800=qs[_0x4f02('0x22')](_0x5afd98,_0x51fd4f);return interaction[_0x4f02('0x23')](_0x4f02('0x24'),_0x51fd4f['query'],_0x51fd4f[_0x4f02('0x25')],_0x280800,_0x5afd98)[_0x4f02('0x18')](respondWithFilteredResult(_0x5bda83,_0x280800))[_0x4f02('0x26')](handleError(_0x5bda83,null));};exports[_0x4f02('0x27')]=function(_0x171c20,_0x49f285){var _0x559f89={'raw':![],'where':{'id':_0x171c20[_0x4f02('0x28')]['id']}},_0x4736bf={};_0x4736bf[_0x4f02('0x29')]=_[_0x4f02('0x2a')](db[_0x4f02('0x1f')]['rawAttributes']);_0x4736bf[_0x4f02('0x2b')]=_[_0x4f02('0x2a')](_0x171c20[_0x4f02('0x2b')]);_0x4736bf[_0x4f02('0x2c')]=_['intersection'](_0x4736bf[_0x4f02('0x29')],_0x4736bf[_0x4f02('0x2b')]);_0x559f89[_0x4f02('0x2d')]=_[_0x4f02('0x2e')](_0x4736bf[_0x4f02('0x29')],qs['fields'](_0x171c20[_0x4f02('0x2b')][_0x4f02('0x2f')]));_0x559f89[_0x4f02('0x2d')]=_0x559f89[_0x4f02('0x2d')]['length']?_0x559f89[_0x4f02('0x2d')]:_0x4736bf[_0x4f02('0x29')];if(_0x171c20[_0x4f02('0x2b')][_0x4f02('0x30')]){_0x559f89[_0x4f02('0x31')]=[{'all':!![]}];}_0x559f89=_[_0x4f02('0x32')]({},_0x559f89,_0x171c20[_0x4f02('0x33')]);return db[_0x4f02('0x1f')]['find'](_0x559f89)[_0x4f02('0x18')](handleEntityNotFound(_0x49f285,null))[_0x4f02('0x18')](respondWithResult(_0x49f285,null))['catch'](handleError(_0x49f285,null));};exports[_0x4f02('0x34')]=function(_0x3a9f03,_0x370f07){return db['FaxInteraction']['create'](_0x3a9f03[_0x4f02('0x35')],{})[_0x4f02('0x18')](respondWithResult(_0x370f07,0xc9))[_0x4f02('0x26')](handleError(_0x370f07,null));};exports['update']=function(_0x2b212a,_0x318ba4){if(_0x2b212a[_0x4f02('0x35')]['id']){delete _0x2b212a[_0x4f02('0x35')]['id'];}return db[_0x4f02('0x1f')]['find']({'where':{'id':_0x2b212a[_0x4f02('0x28')]['id']}})[_0x4f02('0x18')](handleEntityNotFound(_0x318ba4,null))[_0x4f02('0x18')](saveUpdates(_0x2b212a['body'],null))[_0x4f02('0x18')](respondWithResult(_0x318ba4,null))[_0x4f02('0x26')](handleError(_0x318ba4,null));};exports[_0x4f02('0x19')]=function(_0x5b1d13,_0x46a243){return db[_0x4f02('0x1f')]['find']({'where':{'id':_0x5b1d13[_0x4f02('0x28')]['id']}})['then'](handleEntityNotFound(_0x46a243,null))[_0x4f02('0x18')](removeEntity(_0x46a243,null))[_0x4f02('0x26')](handleError(_0x46a243,null));};exports['describe']=function(_0x4509c3,_0x27d421){return db[_0x4f02('0x1f')][_0x4f02('0x36')]()[_0x4f02('0x18')](respondWithResult(_0x27d421,null))[_0x4f02('0x26')](handleError(_0x27d421,null));};exports[_0x4f02('0x37')]=function(_0x489b76,_0x19827c){return db[_0x4f02('0x38')][_0x4f02('0x39')]({'where':{'id':_0x489b76[_0x4f02('0x28')]['id']}})[_0x4f02('0x18')](handleEntityNotFound(_0x19827c,null))[_0x4f02('0x18')](function(_0x1b6de4){if(_0x1b6de4){return _0x1b6de4[_0x4f02('0x37')](_0x489b76[_0x4f02('0x35')][_0x4f02('0x3a')],_['omit'](_0x489b76[_0x4f02('0x35')],[_0x4f02('0x3a'),'id'])||{});}})[_0x4f02('0x18')](respondWithResult(_0x19827c,null))['catch'](handleError(_0x19827c,null));};exports[_0x4f02('0x3b')]=function(_0x59725d,_0x2ca1ce){var _0x2c6182={'raw':![],'where':{}};var _0x40b033={};var _0x1167c1={'count':0x0,'rows':[]};return db[_0x4f02('0x1f')][_0x4f02('0x3c')]({'where':{'id':_0x59725d['params']['id']}})['then'](handleEntityNotFound(_0x2ca1ce,null))[_0x4f02('0x18')](function(_0x33f840){if(_0x33f840){_0x40b033[_0x4f02('0x29')]=_['keys'](db['FaxMessage'][_0x4f02('0x3d')]);_0x40b033[_0x4f02('0x2b')]=_[_0x4f02('0x2a')](_0x59725d[_0x4f02('0x2b')]);_0x40b033[_0x4f02('0x2c')]=_['intersection'](_0x40b033[_0x4f02('0x29')],_0x40b033['query']);_0x2c6182['attributes']=_[_0x4f02('0x2e')](_0x40b033[_0x4f02('0x29')],qs[_0x4f02('0x2f')](_0x59725d['query'][_0x4f02('0x2f')]));_0x2c6182[_0x4f02('0x2d')]=_0x2c6182[_0x4f02('0x2d')][_0x4f02('0x3e')]?_0x2c6182[_0x4f02('0x2d')]:_0x40b033[_0x4f02('0x29')];if(!_0x59725d['query'][_0x4f02('0x3f')](_0x4f02('0x40'))){_0x2c6182[_0x4f02('0x15')]=qs[_0x4f02('0x15')](_0x59725d['query'][_0x4f02('0x15')]);_0x2c6182[_0x4f02('0x13')]=qs[_0x4f02('0x13')](_0x59725d[_0x4f02('0x2b')][_0x4f02('0x13')]);}_0x2c6182['order']=qs[_0x4f02('0x41')](_0x59725d[_0x4f02('0x2b')]['sort']);_0x2c6182[_0x4f02('0x42')]=qs[_0x4f02('0x2c')](_[_0x4f02('0x43')](_0x59725d[_0x4f02('0x2b')],_0x40b033[_0x4f02('0x2c')]));_0x2c6182[_0x4f02('0x42')][_0x4f02('0x44')]=_0x33f840['id'];if(_0x59725d['query'][_0x4f02('0x45')]){_0x2c6182['where']=_[_0x4f02('0x32')](_0x2c6182[_0x4f02('0x42')],{'$or':_[_0x4f02('0x1e')](_0x2c6182[_0x4f02('0x2d')],function(_0x19b1bc){var _0x4f0708={};_0x4f0708[_0x19b1bc]={'$like':'%'+_0x59725d[_0x4f02('0x2b')][_0x4f02('0x45')]+'%'};return _0x4f0708;})});}if(_0x59725d[_0x4f02('0x2b')][_0x4f02('0x46')]){var _0x414010=_0x59725d['query'][_0x4f02('0x46')][_0x4f02('0x47')](',');var _0x6c9583={};_0x6c9583[_0x414010[0x0]]={'$gte':moment(_0x414010[0x1])[_0x4f02('0x48')](_0x4f02('0x49'))};_0x2c6182['where']=_[_0x4f02('0x32')](_0x2c6182[_0x4f02('0x42')],_0x6c9583);}_0x2c6182=_['merge']({},_0x2c6182,_0x59725d['options']);return db['FaxMessage']['count']({'where':_0x2c6182[_0x4f02('0x42')]})[_0x4f02('0x18')](function(_0x409624){_0x1167c1[_0x4f02('0x16')]=_0x409624;if(_0x59725d['query'][_0x4f02('0x30')]){_0x2c6182[_0x4f02('0x31')]=[{'all':!![]}];}return db['FaxMessage'][_0x4f02('0x23')](_0x2c6182);})[_0x4f02('0x18')](function(_0x242ddb){_0x1167c1[_0x4f02('0x4a')]=_0x242ddb;return _0x1167c1;});}})[_0x4f02('0x18')](respondWithFilteredResult(_0x2ca1ce,_0x2c6182))[_0x4f02('0x26')](handleError(_0x2ca1ce,null));};exports[_0x4f02('0x4b')]=function(_0x3b52ee,_0x1dff8f){return db[_0x4f02('0x1f')][_0x4f02('0x39')]({'where':{'id':_0x3b52ee[_0x4f02('0x28')]['id']}})[_0x4f02('0x18')](handleEntityNotFound(_0x1dff8f,null))[_0x4f02('0x18')](function(_0x51d2e4){if(_0x51d2e4){return _0x51d2e4[_0x4f02('0x4c')](_0x3b52ee['body'][_0x4f02('0x3a')],_[_0x4f02('0x4d')](_0x3b52ee[_0x4f02('0x35')],['ids','id'])||{})[_0x4f02('0x4e')](function(){return db['Tag']['findAll']({'attributes':['id',_0x4f02('0x1c'),_0x4f02('0x4f')],'where':{'id':_0x3b52ee['body']['ids']}});})[_0x4f02('0x18')](function(_0x27c617){socket[_0x4f02('0x50')](_0x4f02('0x51'),{'id':Number(_0x3b52ee[_0x4f02('0x28')]['id']),'tags':_0x27c617||[]});return{'id':Number(_0x3b52ee['params']['id']),'tags':_0x27c617||[]};});}})[_0x4f02('0x18')](respondWithResult(_0x1dff8f,null))[_0x4f02('0x26')](handleError(_0x1dff8f,null));};exports[_0x4f02('0x52')]=function(_0x565424,_0x16acc8){return db[_0x4f02('0x1f')][_0x4f02('0x39')]({'where':{'id':_0x565424[_0x4f02('0x28')]['id']}})['then'](handleEntityNotFound(_0x16acc8,null))[_0x4f02('0x18')](function(_0x1593b3){if(_0x1593b3){return _0x1593b3[_0x4f02('0x52')](_0x565424[_0x4f02('0x2b')][_0x4f02('0x3a')]);}})[_0x4f02('0x18')](respondWithStatusCode(_0x16acc8,null))[_0x4f02('0x26')](handleError(_0x16acc8,null));};exports[_0x4f02('0x53')]=function(_0x32e848,_0x18f385){var _0x5f46ea=moment()['unix']()[_0x4f02('0x54')]();var _0x1a29d7=path[_0x4f02('0x55')](config['root'],_0x4f02('0x56'),_0x4f02('0x57'),_0x4f02('0x58'));var _0x4269ac=path[_0x4f02('0x55')](config[_0x4f02('0x59')],_0x4f02('0x56'),_0x4f02('0x57'),_0x4f02('0x5a'));var _0x247618=path[_0x4f02('0x55')](_0x1a29d7,_0x5f46ea);var _0x1d823b=util[_0x4f02('0x48')](_0x4f02('0x5b'),_0x32e848[_0x4f02('0x28')]['id'],_0x5f46ea);var _0x4c4be3=path[_0x4f02('0x55')](_0x1a29d7,_0x1d823b);var _0x119a49=[];_0x119a49[_0x4f02('0x5c')]({'model':db[_0x4f02('0x5d')],'as':_0x4f02('0x5d'),'raw':!![]});var _0x2f5778=[{'model':db[_0x4f02('0x38')],'as':_0x4f02('0x5e'),'attributes':['id',_0x4f02('0x35'),_0x4f02('0x5f')],'include':_0x119a49}];return db[_0x4f02('0x1f')][_0x4f02('0x39')]({'where':{'id':_0x32e848[_0x4f02('0x28')]['id']},'include':_0x2f5778})[_0x4f02('0x18')](handleEntityNotFound(_0x18f385,null))[_0x4f02('0x18')](function(_0x192c8b){if(_0x192c8b&&_0x192c8b[_0x4f02('0x5e')]){var _0x5da2b5=_0x192c8b[_0x4f02('0x60')]({'plain':!![]});fs[_0x4f02('0x61')](_0x247618);for(var _0x522b07=0x0;_0x522b07<_0x5da2b5['Messages'][_0x4f02('0x3e')];_0x522b07++){var _0x4f0d83=_0x5da2b5[_0x4f02('0x5e')][_0x522b07][_0x4f02('0x5d')];if(_0x4f0d83){var _0x53c636=path[_0x4f02('0x55')](_0x4269ac,_0x4f0d83['basename']);if(fs['existsSync'](_0x53c636)){fs[_0x4f02('0x62')](_0x53c636)[_0x4f02('0x63')](fs[_0x4f02('0x64')](path[_0x4f02('0x55')](_0x247618,_0x4f0d83[_0x4f02('0x65')])));}}}return BPromise[_0x4f02('0x66')]()[_0x4f02('0x18')](function(){return new BPromise(function(_0x4c5477,_0x3567ee){zipdir(_0x247618,{'saveTo':_0x4c4be3},function(_0x311b4b,_0xe983c8){if(_0x311b4b)return _0x3567ee(_0x311b4b);return _0x4c5477(_0xe983c8);});})[_0x4f02('0x18')](function(){return new BPromise(function(_0x477dcf,_0x3f9861){rimraf(_0x247618,function(_0x410b5b){if(_0x410b5b)_0x3f9861(_0x410b5b);return _0x477dcf();});});})[_0x4f02('0x18')](function(){return _0x18f385[_0x4f02('0x53')](_0x4c4be3,_0x1d823b,function(_0x5511e5){if(_0x5511e5){console['log'](_0x4f02('0x67'),_0x5511e5);}else{fs['unlinkSync'](_0x4c4be3);}});});});}else{return _0x18f385[_0x4f02('0x11')](0xc8);}})[_0x4f02('0x26')](handleError(_0x18f385,null));};