Built motion from commit 67e5df37.|2.0.66
[motion2.git] / server / api / voiceMusicOnHold / voiceMusicOnHold.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 _0x0b01=['update','body','find','sequelize','transaction','mode','files','directory','join','server/files/moh','create','0777','readdirSync','unlinkSync','rmdirSync','message','addSound','Sound','findOne','save_name','converted_format','createdAt','root','server/files/sounds/converted','format','extname','%s.%s','symlinkSync','Sequelize','ValidationError','removeSound','id2','getSounds','fast-json-patch','request-promise','moment','util','path','to-csv','ejs','lodash','squel','jsforce','desk.js','querystring','papaparse','../../config/logger','api','../../mysqldb','sendStatus','status','end','offset','limit','set','Content-Range','json','apply','save','destroy','then','error','stack','name','send','index','model','keys','VoiceMusicOnHold','query','dateStart','dateEnd','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','add','day','filter','merge','map','options','count','includeAll','include','findAll','rows','catch','params','rawAttributes'];(function(_0x5b8a99,_0x2e0c12){var _0x31fa01=function(_0x21396c){while(--_0x21396c){_0x5b8a99['push'](_0x5b8a99['shift']());}};_0x31fa01(++_0x2e0c12);}(_0x0b01,0x82));var _0x10b0=function(_0x17d309,_0x2cd46d){_0x17d309=_0x17d309-0x0;var _0x1c649e=_0x0b01[_0x17d309];return _0x1c649e;};'use strict';var jsonpatch=require(_0x10b0('0x0'));var rp=require(_0x10b0('0x1'));var moment=require(_0x10b0('0x2'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x10b0('0x3'));var path=require(_0x10b0('0x4'));var sox=require('sox');var csv=require(_0x10b0('0x5'));var ejs=require(_0x10b0('0x6'));var fs=require('fs');var _=require(_0x10b0('0x7'));var squel=require(_0x10b0('0x8'));var crypto=require('crypto');var jsforce=require(_0x10b0('0x9'));var deskjs=require(_0x10b0('0xa'));var toCsv=require('to-csv');var querystring=require(_0x10b0('0xb'));var Papa=require(_0x10b0('0xc'));var qs=require('../../components/parsers/qs');var logger=require(_0x10b0('0xd'))(_0x10b0('0xe'));var utils=require('../../config/utils');var config=require('../../config/environment');var db=require(_0x10b0('0xf'))['db'];function respondWithStatusCode(_0x4ed8d4,_0x74d623){_0x74d623=_0x74d623||0xcc;return function(_0x2ea9f6){if(_0x2ea9f6){return _0x4ed8d4[_0x10b0('0x10')](_0x74d623);}return _0x4ed8d4[_0x10b0('0x11')](_0x74d623)[_0x10b0('0x12')]();};}function respondWithResult(_0x215652,_0x5a2fc6){_0x5a2fc6=_0x5a2fc6||0xc8;return function(_0x1b4066){if(_0x1b4066){return _0x215652[_0x10b0('0x11')](_0x5a2fc6)['json'](_0x1b4066);}};}function respondWithFilteredResult(_0x2295bb,_0x1c0e38){return function(_0x4bae08){if(_0x4bae08){var _0x5ec096=_0x4bae08['count'],_0x285411=_0x1c0e38[_0x10b0('0x13')],_0x15c7bc=_0x1c0e38[_0x10b0('0x13')]+_0x1c0e38[_0x10b0('0x14')],_0x3346c2;if(_0x15c7bc>=_0x5ec096){_0x15c7bc=_0x5ec096;_0x3346c2=0xc8;}else{_0x3346c2=0xce;}_0x2295bb[_0x10b0('0x11')](_0x3346c2);return _0x2295bb[_0x10b0('0x15')](_0x10b0('0x16'),_0x285411+'-'+_0x15c7bc+'/'+_0x5ec096)[_0x10b0('0x17')](_0x4bae08);}return null;};}function patchUpdates(_0xfa43f3){return function(_0x2b15e0){try{jsonpatch[_0x10b0('0x18')](_0x2b15e0,_0xfa43f3,!![]);}catch(_0x49c7d5){return BPromise['reject'](_0x49c7d5);}return _0x2b15e0[_0x10b0('0x19')]();};}function saveUpdates(_0xbae2b8,_0x2fb9dc){return function(_0x4c5ed7){if(_0x4c5ed7){return _0x4c5ed7['update'](_0xbae2b8)['then'](function(_0x546388){return _0x546388;});}return null;};}function removeEntity(_0x3ead9b,_0x545411){return function(_0x48fca0){if(_0x48fca0){return _0x48fca0[_0x10b0('0x1a')]()[_0x10b0('0x1b')](function(){_0x3ead9b[_0x10b0('0x11')](0xcc)[_0x10b0('0x12')]();});}};}function handleEntityNotFound(_0x5c8005,_0x463b6a){return function(_0x56feb0){if(!_0x56feb0){_0x5c8005['sendStatus'](0x194);}return _0x56feb0;};}function handleError(_0x39b775,_0xa70042){_0xa70042=_0xa70042||0x1f4;return function(_0x218d5a){logger[_0x10b0('0x1c')](_0x218d5a[_0x10b0('0x1d')]);if(_0x218d5a[_0x10b0('0x1e')]){delete _0x218d5a[_0x10b0('0x1e')];}_0x39b775[_0x10b0('0x11')](_0xa70042)[_0x10b0('0x1f')](_0x218d5a);};}exports[_0x10b0('0x20')]=function(_0x4280fd,_0xb0c234){var _0x3e9604={},_0x44e2eb={},_0x94cc28={'count':0x0,'rows':[]};_0x44e2eb[_0x10b0('0x21')]=_[_0x10b0('0x22')](db[_0x10b0('0x23')]['rawAttributes']);var _0x2dc3e5={'dateStart':_0x4280fd[_0x10b0('0x24')][_0x10b0('0x25')],'dateEnd':_0x4280fd['query'][_0x10b0('0x26')]};delete _0x4280fd[_0x10b0('0x24')][_0x10b0('0x25')];delete _0x4280fd[_0x10b0('0x24')][_0x10b0('0x26')];_0x44e2eb[_0x10b0('0x24')]=_[_0x10b0('0x22')](_0x4280fd[_0x10b0('0x24')]);_0x44e2eb[_0x10b0('0x27')]=_[_0x10b0('0x28')](_0x44e2eb[_0x10b0('0x21')],_0x44e2eb[_0x10b0('0x24')]);_0x3e9604[_0x10b0('0x29')]=_[_0x10b0('0x28')](_0x44e2eb[_0x10b0('0x21')],qs[_0x10b0('0x2a')](_0x4280fd['query'][_0x10b0('0x2a')]));_0x3e9604[_0x10b0('0x29')]=_0x3e9604[_0x10b0('0x29')][_0x10b0('0x2b')]?_0x3e9604[_0x10b0('0x29')]:_0x44e2eb['model'];if(!_0x4280fd[_0x10b0('0x24')][_0x10b0('0x2c')](_0x10b0('0x2d'))){_0x3e9604[_0x10b0('0x14')]=qs[_0x10b0('0x14')](_0x4280fd[_0x10b0('0x24')][_0x10b0('0x14')]);_0x3e9604['offset']=qs['offset'](_0x4280fd['query']['offset']);}_0x3e9604[_0x10b0('0x2e')]=qs[_0x10b0('0x2f')](_0x4280fd[_0x10b0('0x24')][_0x10b0('0x2f')]);_0x3e9604[_0x10b0('0x30')]=qs[_0x10b0('0x27')](_[_0x10b0('0x31')](_0x4280fd['query'],_0x44e2eb['filters']));if(_0x2dc3e5[_0x10b0('0x25')]){if(_0x2dc3e5[_0x10b0('0x26')]){_0x2dc3e5[_0x10b0('0x26')]=moment(_0x2dc3e5[_0x10b0('0x26')])[_0x10b0('0x32')](0x1,_0x10b0('0x33'));}else{_0x2dc3e5[_0x10b0('0x26')]=moment(_0x2dc3e5['dateStart'])[_0x10b0('0x32')](0x1,_0x10b0('0x33'));}_['merge'](_0x3e9604[_0x10b0('0x30')],{'createdAt':{'$gte':_0x2dc3e5[_0x10b0('0x25')],'$lte':_0x2dc3e5['dateEnd']}});}if(_0x4280fd[_0x10b0('0x24')][_0x10b0('0x34')]){_0x3e9604[_0x10b0('0x30')]=_[_0x10b0('0x35')](_0x3e9604[_0x10b0('0x30')],{'$or':_[_0x10b0('0x36')](_0x3e9604[_0x10b0('0x29')],function(_0x4693b2){var _0xb8692={};_0xb8692[_0x4693b2]={'$like':'%'+_0x4280fd[_0x10b0('0x24')][_0x10b0('0x34')]+'%'};return _0xb8692;})});}_0x3e9604=_[_0x10b0('0x35')]({},_0x3e9604,_0x4280fd[_0x10b0('0x37')]);var _0x32c524={'where':_0x3e9604[_0x10b0('0x30')]};return db[_0x10b0('0x23')][_0x10b0('0x38')](_0x32c524)[_0x10b0('0x1b')](function(_0x5797d1){_0x94cc28[_0x10b0('0x38')]=_0x5797d1;if(_0x4280fd['query'][_0x10b0('0x39')]){_0x3e9604[_0x10b0('0x3a')]=[{'all':!![]}];}return db[_0x10b0('0x23')][_0x10b0('0x3b')](_0x3e9604);})[_0x10b0('0x1b')](function(_0x173a90){_0x94cc28[_0x10b0('0x3c')]=_0x173a90;return _0x94cc28;})[_0x10b0('0x1b')](respondWithFilteredResult(_0xb0c234,_0x3e9604))[_0x10b0('0x3d')](handleError(_0xb0c234,null));};exports['show']=function(_0x15acff,_0x3226b8){var _0x235cf0={'raw':![],'where':{'id':_0x15acff[_0x10b0('0x3e')]['id']}},_0x4e9cc6={};_0x4e9cc6[_0x10b0('0x21')]=_[_0x10b0('0x22')](db[_0x10b0('0x23')][_0x10b0('0x3f')]);_0x4e9cc6[_0x10b0('0x24')]=_[_0x10b0('0x22')](_0x15acff[_0x10b0('0x24')]);_0x4e9cc6[_0x10b0('0x27')]=_[_0x10b0('0x28')](_0x4e9cc6[_0x10b0('0x21')],_0x4e9cc6[_0x10b0('0x24')]);_0x235cf0['attributes']=_[_0x10b0('0x28')](_0x4e9cc6['model'],qs[_0x10b0('0x2a')](_0x15acff['query'][_0x10b0('0x2a')]));_0x235cf0[_0x10b0('0x29')]=_0x235cf0['attributes'][_0x10b0('0x2b')]?_0x235cf0[_0x10b0('0x29')]:_0x4e9cc6[_0x10b0('0x21')];if(_0x15acff[_0x10b0('0x24')][_0x10b0('0x39')]){_0x235cf0[_0x10b0('0x3a')]=[{'all':!![]}];}_0x235cf0=_[_0x10b0('0x35')]({},_0x235cf0,_0x15acff[_0x10b0('0x37')]);return db[_0x10b0('0x23')]['find'](_0x235cf0)[_0x10b0('0x1b')](handleEntityNotFound(_0x3226b8,null))[_0x10b0('0x1b')](respondWithResult(_0x3226b8,null))[_0x10b0('0x3d')](handleError(_0x3226b8,null));};exports[_0x10b0('0x40')]=function(_0x26047c,_0x3d8d94){if(_0x26047c['body']['id']){delete _0x26047c[_0x10b0('0x41')]['id'];}return db[_0x10b0('0x23')][_0x10b0('0x42')]({'where':{'id':_0x26047c['params']['id']}})[_0x10b0('0x1b')](handleEntityNotFound(_0x3d8d94,null))['then'](saveUpdates(_0x26047c[_0x10b0('0x41')],null))[_0x10b0('0x1b')](respondWithResult(_0x3d8d94,null))['catch'](handleError(_0x3d8d94,null));};exports['create']=function(_0x1fc4ee,_0x432eb6,_0x546401){return db[_0x10b0('0x43')][_0x10b0('0x44')](function(_0x2bebe0){if(_0x1fc4ee[_0x10b0('0x41')][_0x10b0('0x45')]===_0x10b0('0x46')){_0x1fc4ee[_0x10b0('0x41')][_0x10b0('0x47')]=path[_0x10b0('0x48')](config['root'],_0x10b0('0x49'),_0x1fc4ee[_0x10b0('0x41')][_0x10b0('0x1e')]);}return db[_0x10b0('0x23')][_0x10b0('0x4a')](_0x1fc4ee[_0x10b0('0x41')],{'transaction':_0x2bebe0})[_0x10b0('0x1b')](function(_0x23d610){if(_0x1fc4ee[_0x10b0('0x41')][_0x10b0('0x45')]===_0x10b0('0x46')){fs['mkdirSync'](_0x23d610[_0x10b0('0x47')],parseInt(_0x10b0('0x4b'),0x8));}return _0x23d610;});})[_0x10b0('0x1b')](respondWithResult(_0x432eb6,0xc9))[_0x10b0('0x3d')](handleError(_0x432eb6,null));};exports[_0x10b0('0x1a')]=function(_0x68521b,_0x29fdaa,_0x66d2bc){var _0x5ec148;return db[_0x10b0('0x23')][_0x10b0('0x42')]({'where':{'id':_0x68521b['params']['id']}})[_0x10b0('0x1b')](function(_0x3d9504){if(_0x3d9504){return db[_0x10b0('0x43')][_0x10b0('0x44')](function(_0x5cbff8){return _0x3d9504[_0x10b0('0x1a')]({'transaction':_0x5cbff8,'raw':!![]})['then'](function(_0x2cebde){if(_0x2cebde[_0x10b0('0x47')]){fs[_0x10b0('0x4c')](_0x2cebde[_0x10b0('0x47')])['forEach'](function(_0xf27557){fs[_0x10b0('0x4d')](path[_0x10b0('0x48')](_0x2cebde[_0x10b0('0x47')],_0xf27557));});fs[_0x10b0('0x4e')](_0x2cebde['directory']);}return _0x2cebde;});})[_0x10b0('0x3d')](function(_0x348642){throw new db['Sequelize']['ValidationError'](_0x348642[_0x10b0('0x4f')]);});}})[_0x10b0('0x1b')](handleEntityNotFound(_0x29fdaa,null))[_0x10b0('0x1b')](removeEntity(_0x29fdaa,null))[_0x10b0('0x3d')](handleError(_0x29fdaa,null));};exports[_0x10b0('0x50')]=function(_0x431fec,_0x414023,_0x3c2fe7){return db[_0x10b0('0x43')][_0x10b0('0x44')](function(_0x673223){var _0x56e59f;return db[_0x10b0('0x23')][_0x10b0('0x42')]({'where':{'id':_0x431fec['params']['id']}})[_0x10b0('0x1b')](function(_0x3eabf0){if(_0x3eabf0){_0x56e59f=_0x3eabf0;return _0x3eabf0[_0x10b0('0x50')](_0x431fec[_0x10b0('0x41')]['id'],{'transaction':_0x673223});}})[_0x10b0('0x1b')](function(_0x38c06b){if(_0x38c06b){return db[_0x10b0('0x51')][_0x10b0('0x52')]({'where':{'id':_0x431fec[_0x10b0('0x41')]['id']},'attributes':['id','name',_0x10b0('0x53'),_0x10b0('0x54'),_0x10b0('0x55')],'raw':!![]});}})['then'](function(_0xddb1be){if(_0xddb1be){var _0x5fd04a=path[_0x10b0('0x48')](config[_0x10b0('0x56')],_0x10b0('0x57'),util[_0x10b0('0x58')]('%s.%s',path['basename'](_0xddb1be['save_name'],path[_0x10b0('0x59')](_0xddb1be[_0x10b0('0x53')])),_0xddb1be[_0x10b0('0x54')]));var _0x3e8b53=path['join'](config[_0x10b0('0x56')],_0x10b0('0x49'),_0x56e59f['name'],util['format'](_0x10b0('0x5a'),_0xddb1be['name'],_0xddb1be[_0x10b0('0x54')]));fs[_0x10b0('0x5b')](_0x5fd04a,_0x3e8b53);return _0xddb1be;}})[_0x10b0('0x3d')](function(_0x313a84){throw new db[(_0x10b0('0x5c'))][(_0x10b0('0x5d'))](_0x313a84[_0x10b0('0x4f')]);});})['then'](handleEntityNotFound(_0x414023,null))[_0x10b0('0x1b')](respondWithResult(_0x414023,0xc9))[_0x10b0('0x3d')](handleError(_0x414023,null));};exports['removeSound']=function(_0x202e56,_0xdbb7a4,_0x551db8){return db[_0x10b0('0x43')]['transaction'](function(_0x3d7581){var _0x420114;return db[_0x10b0('0x23')][_0x10b0('0x52')]({'where':{'id':_0x202e56[_0x10b0('0x3e')]['id']}})[_0x10b0('0x1b')](handleEntityNotFound(_0xdbb7a4,null))[_0x10b0('0x1b')](function(_0x21d373){if(_0x21d373){_0x420114=_0x21d373;return _0x21d373[_0x10b0('0x5e')](_0x202e56[_0x10b0('0x3e')]['id2']);}})[_0x10b0('0x1b')](function(_0x27878d){if(_0x27878d){return db[_0x10b0('0x51')][_0x10b0('0x52')]({'where':{'id':_0x202e56[_0x10b0('0x3e')][_0x10b0('0x5f')]},'attributes':['id',_0x10b0('0x1e'),_0x10b0('0x53'),'converted_format',_0x10b0('0x55')],'raw':!![]});}})[_0x10b0('0x1b')](function(_0x1f4e99){if(_0x1f4e99){fs['unlinkSync'](path['join'](_0x420114[_0x10b0('0x47')],util[_0x10b0('0x58')]('%s.%s',_0x1f4e99[_0x10b0('0x1e')],_0x1f4e99[_0x10b0('0x54')])));return _0x1f4e99;}})['catch'](function(_0x47c022){throw new db[(_0x10b0('0x5c'))]['ValidationError'](_0x47c022[_0x10b0('0x4f')]);});})[_0x10b0('0x1b')](handleEntityNotFound(_0xdbb7a4,null))[_0x10b0('0x1b')](respondWithStatusCode(_0xdbb7a4,0xcc))['catch'](handleError(_0xdbb7a4,null));};exports['getSounds']=function(_0x4a68f8,_0x55b1b8,_0xaffef6){var _0x27b542={};var _0x230df7={};var _0x15e3d2;var _0x1b0c89;return db['VoiceMusicOnHold'][_0x10b0('0x52')]({'where':{'id':_0x4a68f8['params']['id']}})[_0x10b0('0x1b')](handleEntityNotFound(_0x55b1b8,null))['then'](function(_0x5244d8){if(_0x5244d8){_0x15e3d2=_0x5244d8;_0x230df7[_0x10b0('0x21')]=_[_0x10b0('0x22')](db[_0x10b0('0x51')]['rawAttributes']);_0x230df7[_0x10b0('0x24')]=_['keys'](_0x4a68f8[_0x10b0('0x24')]);_0x230df7[_0x10b0('0x27')]=_[_0x10b0('0x28')](_0x230df7[_0x10b0('0x21')],_0x230df7[_0x10b0('0x24')]);_0x27b542[_0x10b0('0x29')]=_[_0x10b0('0x28')](_0x230df7['model'],qs[_0x10b0('0x2a')](_0x4a68f8['query']['fields']));_0x27b542[_0x10b0('0x29')]=_0x27b542[_0x10b0('0x29')][_0x10b0('0x2b')]?_0x27b542[_0x10b0('0x29')]:_0x230df7['model'];_0x27b542[_0x10b0('0x2e')]=qs[_0x10b0('0x2f')](_0x4a68f8['query'][_0x10b0('0x2f')]);_0x27b542[_0x10b0('0x30')]=qs[_0x10b0('0x27')](_['pick'](_0x4a68f8['query'],_0x230df7[_0x10b0('0x27')]));if(_0x4a68f8[_0x10b0('0x24')][_0x10b0('0x34')]){_0x27b542['where']=_[_0x10b0('0x35')](_0x27b542[_0x10b0('0x30')],{'$or':_[_0x10b0('0x36')](_0x27b542[_0x10b0('0x29')],function(_0x589e09){var _0x372434={};_0x372434[_0x589e09]={'$like':'%'+_0x4a68f8['query']['filter']+'%'};return _0x372434;})});}_0x27b542=_[_0x10b0('0x35')]({},_0x27b542,_0x4a68f8[_0x10b0('0x37')]);return _0x15e3d2['getSounds'](_0x27b542);}})[_0x10b0('0x1b')](function(_0x4762de){if(_0x4762de){_0x1b0c89=_0x4762de[_0x10b0('0x2b')];if(!_0x4a68f8[_0x10b0('0x24')][_0x10b0('0x2c')]('nolimit')){_0x27b542[_0x10b0('0x14')]=qs[_0x10b0('0x14')](_0x4a68f8[_0x10b0('0x24')][_0x10b0('0x14')]);_0x27b542[_0x10b0('0x13')]=qs[_0x10b0('0x13')](_0x4a68f8['query'][_0x10b0('0x13')]);}return _0x15e3d2[_0x10b0('0x60')](_0x27b542);}})[_0x10b0('0x1b')](function(_0x5bd0e3){if(_0x5bd0e3){return _0x5bd0e3?{'count':_0x1b0c89,'rows':_0x5bd0e3}:null;}})[_0x10b0('0x1b')](respondWithResult(_0x55b1b8,null))['catch'](handleError(_0x55b1b8,null));};