Built motion from commit c116dc8b.|2.6.30
[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 _0x43b1=['attributes','model','fields','length','includeAll','include','merge','options','find','create','body','params','describe','addMessage','ids','omit','findOne','FaxMessage','hasOwnProperty','nolimit','sort','where','filters','filter','$gte','split','rows','addTags','setTags','spread','Tag','color','faxInteractionTags:save','removeTags','download','join','root','tmp','files','attachments','format','transcript-%d-%s.zip','push','Attachment','Messages','get','mkdirSync','basename','existsSync','createReadStream','pipe','createWriteStream','resolve','rimraf','zip-dir','moment','bluebird','util','path','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','socket.io-emitter','./faxInteraction.socket','register','sendStatus','status','undefined','limit','offset','count','Content-Range','json','update','then','destroy','end','stack','name','send','index','map','FaxInteraction','rawAttributes','type','key','getOptions','findAll','fax','query','user','catch','show','keys','intersection'];(function(_0x491faa,_0x4f2907){var _0x414440=function(_0x310864){while(--_0x310864){_0x491faa['push'](_0x491faa['shift']());}};_0x414440(++_0x4f2907);}(_0x43b1,0x9b));var _0x143b=function(_0x141c28,_0x7e7f91){_0x141c28=_0x141c28-0x0;var _0x88c1dc=_0x43b1[_0x141c28];return _0x88c1dc;};'use strict';var rimraf=require(_0x143b('0x0'));var zipdir=require(_0x143b('0x1'));var moment=require(_0x143b('0x2'));var BPromise=require(_0x143b('0x3'));var util=require(_0x143b('0x4'));var path=require(_0x143b('0x5'));var fs=require('fs');var _=require(_0x143b('0x6'));var Redis=require(_0x143b('0x7'));var interaction=require(_0x143b('0x8'));var qs=require(_0x143b('0x9'));var logger=require(_0x143b('0xa'))(_0x143b('0xb'));var config=require(_0x143b('0xc'));var db=require(_0x143b('0xd'))['db'];config[_0x143b('0xe')]=_[_0x143b('0xf')](config[_0x143b('0xe')],{'host':'localhost','port':0x18eb});var socket=require(_0x143b('0x10'))(new Redis(config[_0x143b('0xe')]));require(_0x143b('0x11'))[_0x143b('0x12')](socket);function respondWithStatusCode(_0x1c1093,_0x12e878){_0x12e878=_0x12e878||0xcc;return function(_0xec357a){if(_0xec357a){return _0x1c1093[_0x143b('0x13')](_0x12e878);}return _0x1c1093[_0x143b('0x14')](_0x12e878)['end']();};}function respondWithResult(_0xf8672e,_0x14dcf1){_0x14dcf1=_0x14dcf1||0xc8;return function(_0x2315a2){if(_0x2315a2){return _0xf8672e['status'](_0x14dcf1)['json'](_0x2315a2);}};}function respondWithFilteredResult(_0x19898c,_0x457ea4){return function(_0x27b27c){if(_0x27b27c){var _0x539cb5=typeof _0x457ea4['offset']===_0x143b('0x15')&&typeof _0x457ea4[_0x143b('0x16')]===_0x143b('0x15');var _0x1a4e6b=_0x27b27c['count'];var _0x50f9ca=_0x539cb5?0x0:_0x457ea4[_0x143b('0x17')];var _0x505471=_0x539cb5?_0x27b27c[_0x143b('0x18')]:_0x457ea4[_0x143b('0x17')]+_0x457ea4[_0x143b('0x16')];var _0x3f4ca7;if(_0x505471>=_0x1a4e6b){_0x505471=_0x1a4e6b;_0x3f4ca7=0xc8;}else{_0x3f4ca7=0xce;}_0x19898c[_0x143b('0x14')](_0x3f4ca7);return _0x19898c['set'](_0x143b('0x19'),_0x50f9ca+'-'+_0x505471+'/'+_0x1a4e6b)[_0x143b('0x1a')](_0x27b27c);}return null;};}function saveUpdates(_0x5ce35d){return function(_0x141ae6){if(_0x141ae6){return _0x141ae6[_0x143b('0x1b')](_0x5ce35d)[_0x143b('0x1c')](function(_0xe5aa9f){return _0xe5aa9f;});}return null;};}function removeEntity(_0x45133d){return function(_0x5e43a9){if(_0x5e43a9){return _0x5e43a9[_0x143b('0x1d')]()['then'](function(){_0x45133d['status'](0xcc)[_0x143b('0x1e')]();});}};}function handleEntityNotFound(_0x453e70){return function(_0x1738bf){if(!_0x1738bf){_0x453e70[_0x143b('0x13')](0x194);}return _0x1738bf;};}function handleError(_0x543154,_0x23797e){_0x23797e=_0x23797e||0x1f4;return function(_0x797901){logger['error'](_0x797901[_0x143b('0x1f')]);if(_0x797901[_0x143b('0x20')]){delete _0x797901['name'];}_0x543154[_0x143b('0x14')](_0x23797e)[_0x143b('0x21')](_0x797901);};}exports[_0x143b('0x22')]=function(_0x2e05b8,_0x3d9dbe){var _0x48b6e2={};var _0x4bf2c9=_[_0x143b('0x23')](db[_0x143b('0x24')][_0x143b('0x25')],function(_0x1d9d25){return{'name':_0x1d9d25['fieldName'],'type':_0x1d9d25[_0x143b('0x26')][_0x143b('0x27')]};});_0x48b6e2=qs[_0x143b('0x28')](_0x4bf2c9,_0x2e05b8);return interaction[_0x143b('0x29')](_0x143b('0x2a'),_0x2e05b8[_0x143b('0x2b')],_0x2e05b8[_0x143b('0x2c')],_0x48b6e2,_0x4bf2c9)[_0x143b('0x1c')](respondWithFilteredResult(_0x3d9dbe,_0x48b6e2))[_0x143b('0x2d')](handleError(_0x3d9dbe,null));};exports[_0x143b('0x2e')]=function(_0x2463d5,_0x371737){var _0x2cc736={'raw':![],'where':{'id':_0x2463d5['params']['id']}},_0x31e578={};_0x31e578['model']=_[_0x143b('0x2f')](db[_0x143b('0x24')]['rawAttributes']);_0x31e578[_0x143b('0x2b')]=_[_0x143b('0x2f')](_0x2463d5[_0x143b('0x2b')]);_0x31e578['filters']=_[_0x143b('0x30')](_0x31e578['model'],_0x31e578['query']);_0x2cc736[_0x143b('0x31')]=_[_0x143b('0x30')](_0x31e578[_0x143b('0x32')],qs[_0x143b('0x33')](_0x2463d5[_0x143b('0x2b')][_0x143b('0x33')]));_0x2cc736[_0x143b('0x31')]=_0x2cc736['attributes'][_0x143b('0x34')]?_0x2cc736[_0x143b('0x31')]:_0x31e578[_0x143b('0x32')];if(_0x2463d5[_0x143b('0x2b')][_0x143b('0x35')]){_0x2cc736[_0x143b('0x36')]=[{'all':!![]}];}_0x2cc736=_[_0x143b('0x37')]({},_0x2cc736,_0x2463d5[_0x143b('0x38')]);return db[_0x143b('0x24')][_0x143b('0x39')](_0x2cc736)['then'](handleEntityNotFound(_0x371737,null))[_0x143b('0x1c')](respondWithResult(_0x371737,null))[_0x143b('0x2d')](handleError(_0x371737,null));};exports[_0x143b('0x3a')]=function(_0xc7104b,_0x1aa9b7){return db[_0x143b('0x24')][_0x143b('0x3a')](_0xc7104b[_0x143b('0x3b')],{})[_0x143b('0x1c')](respondWithResult(_0x1aa9b7,0xc9))[_0x143b('0x2d')](handleError(_0x1aa9b7,null));};exports['update']=function(_0x45af54,_0x37cdbb){if(_0x45af54[_0x143b('0x3b')]['id']){delete _0x45af54['body']['id'];}return db[_0x143b('0x24')][_0x143b('0x39')]({'where':{'id':_0x45af54[_0x143b('0x3c')]['id']}})[_0x143b('0x1c')](handleEntityNotFound(_0x37cdbb,null))['then'](saveUpdates(_0x45af54[_0x143b('0x3b')],null))[_0x143b('0x1c')](respondWithResult(_0x37cdbb,null))[_0x143b('0x2d')](handleError(_0x37cdbb,null));};exports[_0x143b('0x1d')]=function(_0x2f7644,_0x57ece9){return db[_0x143b('0x24')]['find']({'where':{'id':_0x2f7644[_0x143b('0x3c')]['id']}})[_0x143b('0x1c')](handleEntityNotFound(_0x57ece9,null))['then'](removeEntity(_0x57ece9,null))[_0x143b('0x2d')](handleError(_0x57ece9,null));};exports[_0x143b('0x3d')]=function(_0x112ae1,_0x46620a){return db[_0x143b('0x24')][_0x143b('0x3d')]()[_0x143b('0x1c')](respondWithResult(_0x46620a,null))[_0x143b('0x2d')](handleError(_0x46620a,null));};exports[_0x143b('0x3e')]=function(_0x47ada5,_0x103854){return db['FaxMessage'][_0x143b('0x39')]({'where':{'id':_0x47ada5['params']['id']}})[_0x143b('0x1c')](handleEntityNotFound(_0x103854,null))[_0x143b('0x1c')](function(_0x51e28a){if(_0x51e28a){return _0x51e28a[_0x143b('0x3e')](_0x47ada5[_0x143b('0x3b')][_0x143b('0x3f')],_[_0x143b('0x40')](_0x47ada5['body'],['ids','id'])||{});}})[_0x143b('0x1c')](respondWithResult(_0x103854,null))[_0x143b('0x2d')](handleError(_0x103854,null));};exports['getMessages']=function(_0xef27ec,_0x268ba5){var _0x1b99e4={'raw':![],'where':{}};var _0x24a1ec={};var _0x5c52cf={'count':0x0,'rows':[]};return db['FaxInteraction'][_0x143b('0x41')]({'where':{'id':_0xef27ec[_0x143b('0x3c')]['id']}})[_0x143b('0x1c')](handleEntityNotFound(_0x268ba5,null))[_0x143b('0x1c')](function(_0x38f011){if(_0x38f011){_0x24a1ec['model']=_[_0x143b('0x2f')](db[_0x143b('0x42')]['rawAttributes']);_0x24a1ec['query']=_[_0x143b('0x2f')](_0xef27ec[_0x143b('0x2b')]);_0x24a1ec['filters']=_[_0x143b('0x30')](_0x24a1ec[_0x143b('0x32')],_0x24a1ec[_0x143b('0x2b')]);_0x1b99e4[_0x143b('0x31')]=_[_0x143b('0x30')](_0x24a1ec['model'],qs[_0x143b('0x33')](_0xef27ec[_0x143b('0x2b')][_0x143b('0x33')]));_0x1b99e4['attributes']=_0x1b99e4['attributes'][_0x143b('0x34')]?_0x1b99e4[_0x143b('0x31')]:_0x24a1ec[_0x143b('0x32')];if(!_0xef27ec[_0x143b('0x2b')][_0x143b('0x43')](_0x143b('0x44'))){_0x1b99e4[_0x143b('0x16')]=qs[_0x143b('0x16')](_0xef27ec[_0x143b('0x2b')][_0x143b('0x16')]);_0x1b99e4['offset']=qs[_0x143b('0x17')](_0xef27ec['query']['offset']);}_0x1b99e4['order']=qs['sort'](_0xef27ec['query'][_0x143b('0x45')]);_0x1b99e4[_0x143b('0x46')]=qs['filters'](_['pick'](_0xef27ec[_0x143b('0x2b')],_0x24a1ec[_0x143b('0x47')]));_0x1b99e4[_0x143b('0x46')]['FaxInteractionId']=_0x38f011['id'];if(_0xef27ec['query'][_0x143b('0x48')]){_0x1b99e4[_0x143b('0x46')]=_['merge'](_0x1b99e4[_0x143b('0x46')],{'$or':_['map'](_0x1b99e4[_0x143b('0x31')],function(_0xa5d6ad){var _0x1c9a31={};_0x1c9a31[_0xa5d6ad]={'$like':'%'+_0xef27ec[_0x143b('0x2b')][_0x143b('0x48')]+'%'};return _0x1c9a31;})});}if(_0xef27ec[_0x143b('0x2b')]['$gte']){var _0x7c45ac=_0xef27ec[_0x143b('0x2b')][_0x143b('0x49')][_0x143b('0x4a')](',');var _0x2c24ba={};_0x2c24ba[_0x7c45ac[0x0]]={'$gte':moment(_0x7c45ac[0x1])['format']('YYYY-MM-DD\x20HH:mm:ss')};_0x1b99e4['where']=_[_0x143b('0x37')](_0x1b99e4[_0x143b('0x46')],_0x2c24ba);}_0x1b99e4=_[_0x143b('0x37')]({},_0x1b99e4,_0xef27ec['options']);return db['FaxMessage'][_0x143b('0x18')]({'where':_0x1b99e4[_0x143b('0x46')]})[_0x143b('0x1c')](function(_0x316f4){_0x5c52cf[_0x143b('0x18')]=_0x316f4;if(_0xef27ec[_0x143b('0x2b')][_0x143b('0x35')]){_0x1b99e4[_0x143b('0x36')]=[{'all':!![]}];}return db[_0x143b('0x42')]['findAll'](_0x1b99e4);})[_0x143b('0x1c')](function(_0x30e484){_0x5c52cf[_0x143b('0x4b')]=_0x30e484;return _0x5c52cf;});}})[_0x143b('0x1c')](respondWithFilteredResult(_0x268ba5,_0x1b99e4))[_0x143b('0x2d')](handleError(_0x268ba5,null));};exports[_0x143b('0x4c')]=function(_0x5f54ea,_0x42b1af){return db[_0x143b('0x24')][_0x143b('0x39')]({'where':{'id':_0x5f54ea[_0x143b('0x3c')]['id']}})[_0x143b('0x1c')](handleEntityNotFound(_0x42b1af,null))[_0x143b('0x1c')](function(_0xbc7d3b){if(_0xbc7d3b){return _0xbc7d3b[_0x143b('0x4d')](_0x5f54ea[_0x143b('0x3b')][_0x143b('0x3f')],_[_0x143b('0x40')](_0x5f54ea[_0x143b('0x3b')],[_0x143b('0x3f'),'id'])||{})[_0x143b('0x4e')](function(){return db[_0x143b('0x4f')][_0x143b('0x29')]({'attributes':['id','name',_0x143b('0x50')],'where':{'id':_0x5f54ea[_0x143b('0x3b')][_0x143b('0x3f')]}});})['then'](function(_0x497c19){socket['emit'](_0x143b('0x51'),{'id':Number(_0x5f54ea[_0x143b('0x3c')]['id']),'tags':_0x497c19||[]});return{'id':Number(_0x5f54ea['params']['id']),'tags':_0x497c19||[]};});}})[_0x143b('0x1c')](respondWithResult(_0x42b1af,null))['catch'](handleError(_0x42b1af,null));};exports[_0x143b('0x52')]=function(_0x271f84,_0x99fd70){return db[_0x143b('0x24')][_0x143b('0x39')]({'where':{'id':_0x271f84[_0x143b('0x3c')]['id']}})[_0x143b('0x1c')](handleEntityNotFound(_0x99fd70,null))[_0x143b('0x1c')](function(_0x146c20){if(_0x146c20){return _0x146c20['removeTags'](_0x271f84[_0x143b('0x2b')][_0x143b('0x3f')]);}})['then'](respondWithStatusCode(_0x99fd70,null))[_0x143b('0x2d')](handleError(_0x99fd70,null));};exports[_0x143b('0x53')]=function(_0x3888eb,_0x457a5b){var _0x2d0b04=moment()['unix']()['toString']();var _0xc1b2d7=path[_0x143b('0x54')](config[_0x143b('0x55')],'server','files',_0x143b('0x56'));var _0x148996=path['join'](config[_0x143b('0x55')],'server',_0x143b('0x57'),_0x143b('0x58'));var _0xd052c7=path[_0x143b('0x54')](_0xc1b2d7,_0x2d0b04);var _0xf8c8b6=util[_0x143b('0x59')](_0x143b('0x5a'),_0x3888eb[_0x143b('0x3c')]['id'],_0x2d0b04);var _0xb6da16=path[_0x143b('0x54')](_0xc1b2d7,_0xf8c8b6);var _0x451552=[];_0x451552[_0x143b('0x5b')]({'model':db[_0x143b('0x5c')],'as':_0x143b('0x5c'),'raw':!![]});var _0x548dc4=[{'model':db[_0x143b('0x42')],'as':_0x143b('0x5d'),'attributes':['id',_0x143b('0x3b'),'createdAt'],'include':_0x451552}];return db['FaxInteraction'][_0x143b('0x39')]({'where':{'id':_0x3888eb['params']['id']},'include':_0x548dc4})['then'](handleEntityNotFound(_0x457a5b,null))['then'](function(_0x176743){if(_0x176743&&_0x176743[_0x143b('0x5d')]){var _0x2a361d=_0x176743[_0x143b('0x5e')]({'plain':!![]});fs[_0x143b('0x5f')](_0xd052c7);for(var _0x556469=0x0;_0x556469<_0x2a361d[_0x143b('0x5d')][_0x143b('0x34')];_0x556469++){var _0x5902df=_0x2a361d['Messages'][_0x556469][_0x143b('0x5c')];if(_0x5902df){var _0xf40bae=path['join'](_0x148996,_0x5902df[_0x143b('0x60')]);if(fs[_0x143b('0x61')](_0xf40bae)){fs[_0x143b('0x62')](_0xf40bae)[_0x143b('0x63')](fs[_0x143b('0x64')](path['join'](_0xd052c7,_0x5902df[_0x143b('0x60')])));}}}return BPromise[_0x143b('0x65')]()[_0x143b('0x1c')](function(){return new BPromise(function(_0x2e8e78,_0xc2b1ff){zipdir(_0xd052c7,{'saveTo':_0xb6da16},function(_0x5a8449,_0x29efec){if(_0x5a8449)return _0xc2b1ff(_0x5a8449);return _0x2e8e78(_0x29efec);});})[_0x143b('0x1c')](function(){return new BPromise(function(_0x514531,_0x555588){rimraf(_0xd052c7,function(_0x2f2b11){if(_0x2f2b11)_0x555588(_0x2f2b11);return _0x514531();});});})[_0x143b('0x1c')](function(){return _0x457a5b[_0x143b('0x53')](_0xb6da16,_0xf8c8b6,function(_0x306ad5){if(_0x306ad5){console['log']('err',_0x306ad5);}else{fs['unlinkSync'](_0xb6da16);}});});});}else{return _0x457a5b[_0x143b('0x13')](0xc8);}})['catch'](handleError(_0x457a5b,null));};