3f42f2dfb605487f81afffe121948cf75060e347
[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 _0x795f=['papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','limit','undefined','count','Content-Range','then','get','MusicOnHolds','UserProfileResource','end','error','stack','name','send','index','map','VoiceMusicOnHold','rawAttributes','fieldName','type','model','query','keys','intersection','fields','attributes','sort','where','filters','filter','merge','options','includeAll','include','rows','catch','show','length','find','body','params','sequelize','transaction','files','directory','join','root','server/files/moh','create','mkdirSync','0777','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','destroy','readdirSync','forEach','unlinkSync','rmdirSync','Sequelize','ValidationError','addSound','save_name','converted_format','server/files/sounds/converted','%s.%s','basename','extname','format','symlinkSync','removeSound','findOne','id2','Sound','createdAt','message','pick','getSounds','hasOwnProperty','nolimit','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js'];(function(_0x150eaf,_0xcc6e33){var _0x41c3dd=function(_0x56624b){while(--_0x56624b){_0x150eaf['push'](_0x150eaf['shift']());}};_0x41c3dd(++_0xcc6e33);}(_0x795f,0xcf));var _0xf795=function(_0x46b20b,_0x7c3ca7){_0x46b20b=_0x46b20b-0x0;var _0x3cd8bd=_0x795f[_0x46b20b];return _0x3cd8bd;};'use strict';var emlformat=require(_0xf795('0x0'));var rimraf=require(_0xf795('0x1'));var zipdir=require(_0xf795('0x2'));var jsonpatch=require(_0xf795('0x3'));var rp=require(_0xf795('0x4'));var moment=require(_0xf795('0x5'));var BPromise=require(_0xf795('0x6'));var Mustache=require('mustache');var util=require(_0xf795('0x7'));var path=require(_0xf795('0x8'));var sox=require(_0xf795('0x9'));var csv=require(_0xf795('0xa'));var ejs=require(_0xf795('0xb'));var fs=require('fs');var fs_extra=require(_0xf795('0xc'));var _=require('lodash');var squel=require(_0xf795('0xd'));var crypto=require(_0xf795('0xe'));var jsforce=require(_0xf795('0xf'));var deskjs=require(_0xf795('0x10'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0xf795('0x11'));var Redis=require('ioredis');var authService=require(_0xf795('0x12'));var qs=require(_0xf795('0x13'));var as=require(_0xf795('0x14'));var hardwareService=require(_0xf795('0x15'));var logger=require(_0xf795('0x16'))(_0xf795('0x17'));var utils=require(_0xf795('0x18'));var config=require(_0xf795('0x19'));var licenseUtil=require(_0xf795('0x1a'));var db=require(_0xf795('0x1b'))['db'];function respondWithStatusCode(_0x507bd1,_0x13d9f1){_0x13d9f1=_0x13d9f1||0xcc;return function(_0x2f39a8){if(_0x2f39a8){return _0x507bd1[_0xf795('0x1c')](_0x13d9f1);}return _0x507bd1['status'](_0x13d9f1)['end']();};}function respondWithResult(_0x379be9,_0x54fa25){_0x54fa25=_0x54fa25||0xc8;return function(_0x5748dc){if(_0x5748dc){return _0x379be9[_0xf795('0x1d')](_0x54fa25)[_0xf795('0x1e')](_0x5748dc);}};}function respondWithFilteredResult(_0x4f4476,_0x37a463){return function(_0x1ed363){if(_0x1ed363){var _0x2d42f1=typeof _0x37a463[_0xf795('0x1f')]==='undefined'&&typeof _0x37a463[_0xf795('0x20')]===_0xf795('0x21');var _0x4ba99b=_0x1ed363[_0xf795('0x22')];var _0x2605a=_0x2d42f1?0x0:_0x37a463[_0xf795('0x1f')];var _0x3f7136=_0x2d42f1?_0x1ed363[_0xf795('0x22')]:_0x37a463[_0xf795('0x1f')]+_0x37a463[_0xf795('0x20')];var _0x1bef29;if(_0x3f7136>=_0x4ba99b){_0x3f7136=_0x4ba99b;_0x1bef29=0xc8;}else{_0x1bef29=0xce;}_0x4f4476['status'](_0x1bef29);return _0x4f4476['set'](_0xf795('0x23'),_0x2605a+'-'+_0x3f7136+'/'+_0x4ba99b)['json'](_0x1ed363);}return null;};}function patchUpdates(_0x4a2869){return function(_0x24aecb){try{jsonpatch['apply'](_0x24aecb,_0x4a2869,!![]);}catch(_0x435bc3){return BPromise['reject'](_0x435bc3);}return _0x24aecb['save']();};}function saveUpdates(_0x4c97bd,_0x42d07b){return function(_0x196c45){if(_0x196c45){return _0x196c45['update'](_0x4c97bd)['then'](function(_0xcad389){return _0xcad389;});}return null;};}function removeEntity(_0x3c77c7,_0x17595a){return function(_0x3ea206){if(_0x3ea206){return _0x3ea206['destroy']()[_0xf795('0x24')](function(){var _0x3b4e3c=_0x3ea206[_0xf795('0x25')]({'plain':!![]});var _0x166d67=_0xf795('0x26');return db[_0xf795('0x27')]['destroy']({'where':{'type':_0x166d67,'resourceId':_0x3b4e3c['id']}})[_0xf795('0x24')](function(){return _0x3ea206;});})['then'](function(){_0x3c77c7[_0xf795('0x1d')](0xcc)[_0xf795('0x28')]();});}};}function handleEntityNotFound(_0x20eb97,_0x3b695c){return function(_0x197ed2){if(!_0x197ed2){_0x20eb97['sendStatus'](0x194);}return _0x197ed2;};}function handleError(_0x4123f1,_0x3a1a62){_0x3a1a62=_0x3a1a62||0x1f4;return function(_0x5795bc){logger[_0xf795('0x29')](_0x5795bc[_0xf795('0x2a')]);if(_0x5795bc['name']){delete _0x5795bc[_0xf795('0x2b')];}_0x4123f1[_0xf795('0x1d')](_0x3a1a62)[_0xf795('0x2c')](_0x5795bc);};}exports[_0xf795('0x2d')]=function(_0x4277dc,_0x50839d){var _0x5094e4={},_0x42e099={},_0x2cc931={'count':0x0,'rows':[]};var _0x47a3ee=_[_0xf795('0x2e')](db[_0xf795('0x2f')][_0xf795('0x30')],function(_0x332eef){return{'name':_0x332eef[_0xf795('0x31')],'type':_0x332eef[_0xf795('0x32')]['key']};});_0x42e099[_0xf795('0x33')]=_['map'](_0x47a3ee,'name');_0x42e099[_0xf795('0x34')]=_[_0xf795('0x35')](_0x4277dc[_0xf795('0x34')]);_0x42e099['filters']=_['intersection'](_0x42e099[_0xf795('0x33')],_0x42e099[_0xf795('0x34')]);_0x5094e4['attributes']=_[_0xf795('0x36')](_0x42e099[_0xf795('0x33')],qs[_0xf795('0x37')](_0x4277dc[_0xf795('0x34')][_0xf795('0x37')]));_0x5094e4[_0xf795('0x38')]=_0x5094e4[_0xf795('0x38')]['length']?_0x5094e4['attributes']:_0x42e099[_0xf795('0x33')];if(!_0x4277dc[_0xf795('0x34')]['hasOwnProperty']('nolimit')){_0x5094e4[_0xf795('0x20')]=qs[_0xf795('0x20')](_0x4277dc['query'][_0xf795('0x20')]);_0x5094e4[_0xf795('0x1f')]=qs['offset'](_0x4277dc[_0xf795('0x34')][_0xf795('0x1f')]);}_0x5094e4['order']=qs[_0xf795('0x39')](_0x4277dc[_0xf795('0x34')][_0xf795('0x39')]);delete _0x4277dc[_0xf795('0x34')][_0xf795('0x39')];_0x5094e4[_0xf795('0x3a')]=qs[_0xf795('0x3b')](_['pick'](_0x4277dc[_0xf795('0x34')],_0x42e099[_0xf795('0x3b')]),_0x47a3ee);if(_0x4277dc[_0xf795('0x34')][_0xf795('0x3c')]){_0x5094e4[_0xf795('0x3a')]=_[_0xf795('0x3d')](_0x5094e4[_0xf795('0x3a')],{'$or':_['map'](_0x47a3ee,function(_0x3ef237){if(_0x3ef237['type']!=='VIRTUAL'){var _0x3abee9={};_0x3abee9[_0x3ef237['name']]={'$like':'%'+_0x4277dc[_0xf795('0x34')][_0xf795('0x3c')]+'%'};return _0x3abee9;}})});}_0x5094e4=_[_0xf795('0x3d')]({},_0x5094e4,_0x4277dc[_0xf795('0x3e')]);var _0x5380e2={'where':_0x5094e4[_0xf795('0x3a')]};return db[_0xf795('0x2f')][_0xf795('0x22')](_0x5380e2)[_0xf795('0x24')](function(_0x1d10ac){_0x2cc931[_0xf795('0x22')]=_0x1d10ac;if(_0x4277dc[_0xf795('0x34')][_0xf795('0x3f')]){_0x5094e4[_0xf795('0x40')]=[{'all':!![]}];}return db['VoiceMusicOnHold']['findAll'](_0x5094e4);})[_0xf795('0x24')](function(_0x1b9959){_0x2cc931[_0xf795('0x41')]=_0x1b9959;return _0x2cc931;})['then'](respondWithFilteredResult(_0x50839d,_0x5094e4))[_0xf795('0x42')](handleError(_0x50839d,null));};exports[_0xf795('0x43')]=function(_0x301d7f,_0x3ffa12){var _0x198cbc={'raw':![],'where':{'id':_0x301d7f['params']['id']}},_0x511701={};_0x511701[_0xf795('0x33')]=_[_0xf795('0x35')](db[_0xf795('0x2f')][_0xf795('0x30')]);_0x511701[_0xf795('0x34')]=_['keys'](_0x301d7f[_0xf795('0x34')]);_0x511701[_0xf795('0x3b')]=_[_0xf795('0x36')](_0x511701[_0xf795('0x33')],_0x511701[_0xf795('0x34')]);_0x198cbc['attributes']=_['intersection'](_0x511701[_0xf795('0x33')],qs[_0xf795('0x37')](_0x301d7f[_0xf795('0x34')]['fields']));_0x198cbc[_0xf795('0x38')]=_0x198cbc['attributes'][_0xf795('0x44')]?_0x198cbc[_0xf795('0x38')]:_0x511701[_0xf795('0x33')];if(_0x301d7f[_0xf795('0x34')][_0xf795('0x3f')]){_0x198cbc['include']=[{'all':!![]}];}_0x198cbc=_[_0xf795('0x3d')]({},_0x198cbc,_0x301d7f[_0xf795('0x3e')]);return db['VoiceMusicOnHold'][_0xf795('0x45')](_0x198cbc)['then'](handleEntityNotFound(_0x3ffa12,null))[_0xf795('0x24')](respondWithResult(_0x3ffa12,null))[_0xf795('0x42')](handleError(_0x3ffa12,null));};exports['update']=function(_0x7964e,_0x1ebfcb){if(_0x7964e['body']['id']){delete _0x7964e[_0xf795('0x46')]['id'];}return db[_0xf795('0x2f')][_0xf795('0x45')]({'where':{'id':_0x7964e[_0xf795('0x47')]['id']}})['then'](handleEntityNotFound(_0x1ebfcb,null))['then'](saveUpdates(_0x7964e[_0xf795('0x46')],null))[_0xf795('0x24')](respondWithResult(_0x1ebfcb,null))[_0xf795('0x42')](handleError(_0x1ebfcb,null));};exports['create']=function(_0x5f201a,_0x1b7797,_0x36edb5){return db[_0xf795('0x48')][_0xf795('0x49')](function(_0x94a1c7){if(_0x5f201a['body']['mode']===_0xf795('0x4a')){_0x5f201a[_0xf795('0x46')][_0xf795('0x4b')]=path[_0xf795('0x4c')](config[_0xf795('0x4d')],_0xf795('0x4e'),_0x5f201a[_0xf795('0x46')][_0xf795('0x2b')]);}return db['VoiceMusicOnHold'][_0xf795('0x4f')](_0x5f201a[_0xf795('0x46')],{'transaction':_0x94a1c7})[_0xf795('0x24')](function(_0x51d476){if(_0x5f201a['body']['mode']===_0xf795('0x4a')){fs[_0xf795('0x50')](_0x51d476[_0xf795('0x4b')],parseInt(_0xf795('0x51'),0x8));}var _0x4499fa=_0x5f201a[_0xf795('0x52')][_0xf795('0x25')]({'plain':!![]});if(!_0x4499fa)throw new Error(_0xf795('0x53'));if(_0x4499fa['role']===_0xf795('0x52')){var _0x4f72c2=_0x51d476[_0xf795('0x25')]({'plain':!![]});return db[_0xf795('0x54')][_0xf795('0x45')]({'where':{'name':_0xf795('0x26'),'userProfileId':_0x4499fa['userProfileId']},'raw':!![]})[_0xf795('0x24')](function(_0x2a1db6){if(_0x2a1db6&&_0x2a1db6['autoAssociation']===0x0){return db[_0xf795('0x27')][_0xf795('0x4f')]({'name':_0x4f72c2['name'],'resourceId':_0x4f72c2['id'],'type':_0x2a1db6['name'],'sectionId':_0x2a1db6['id']},{})[_0xf795('0x24')](function(){return _0x51d476;});}else{return _0x51d476;}})[_0xf795('0x42')](function(_0x167416){logger[_0xf795('0x29')](_0xf795('0x55'),_0x167416);});}return _0x51d476;});})['then'](respondWithResult(_0x1b7797,0xc9))[_0xf795('0x42')](handleError(_0x1b7797,null));};exports['destroy']=function(_0x47dfe3,_0x154a9e,_0x242c14){var _0x1771e1;return db[_0xf795('0x2f')][_0xf795('0x45')]({'where':{'id':_0x47dfe3[_0xf795('0x47')]['id']}})[_0xf795('0x24')](function(_0x558d59){if(_0x558d59){return db['sequelize'][_0xf795('0x49')](function(_0x44baa1){return _0x558d59[_0xf795('0x56')]({'transaction':_0x44baa1,'raw':!![]})[_0xf795('0x24')](function(_0x412f81){if(_0x412f81['directory']){fs[_0xf795('0x57')](_0x412f81[_0xf795('0x4b')])[_0xf795('0x58')](function(_0x1f10f0){fs[_0xf795('0x59')](path[_0xf795('0x4c')](_0x412f81['directory'],_0x1f10f0));});fs[_0xf795('0x5a')](_0x412f81['directory']);}return _0x412f81;});})[_0xf795('0x42')](function(_0x4d1b33){throw new db[(_0xf795('0x5b'))][(_0xf795('0x5c'))](_0x4d1b33['message']);});}})[_0xf795('0x24')](handleEntityNotFound(_0x154a9e,null))[_0xf795('0x24')](removeEntity(_0x154a9e,null))[_0xf795('0x42')](handleError(_0x154a9e,null));};exports['addSound']=function(_0x14b312,_0x3e296b,_0x5af172){return db[_0xf795('0x48')][_0xf795('0x49')](function(_0x565e95){var _0x166861;return db[_0xf795('0x2f')][_0xf795('0x45')]({'where':{'id':_0x14b312[_0xf795('0x47')]['id']}})['then'](function(_0x55f977){if(_0x55f977){_0x166861=_0x55f977;return _0x55f977[_0xf795('0x5d')](_0x14b312[_0xf795('0x46')]['id'],{'transaction':_0x565e95});}})['then'](function(_0x414b6d){if(_0x414b6d){return db['Sound']['findOne']({'where':{'id':_0x14b312[_0xf795('0x46')]['id']},'attributes':['id','name',_0xf795('0x5e'),_0xf795('0x5f'),'createdAt'],'raw':!![]});}})[_0xf795('0x24')](function(_0x1cdd72){if(_0x1cdd72){var _0x4f2a30=path[_0xf795('0x4c')](config[_0xf795('0x4d')],_0xf795('0x60'),util['format'](_0xf795('0x61'),path[_0xf795('0x62')](_0x1cdd72['save_name'],path[_0xf795('0x63')](_0x1cdd72['save_name'])),_0x1cdd72['converted_format']));var _0x48a2d6=path[_0xf795('0x4c')](config[_0xf795('0x4d')],_0xf795('0x4e'),_0x166861[_0xf795('0x2b')],util[_0xf795('0x64')](_0xf795('0x61'),_0x1cdd72[_0xf795('0x2b')],_0x1cdd72[_0xf795('0x5f')]));fs[_0xf795('0x65')](_0x4f2a30,_0x48a2d6);return _0x1cdd72;}})[_0xf795('0x42')](function(_0x43c365){throw new db[(_0xf795('0x5b'))][(_0xf795('0x5c'))](_0x43c365['message']);});})['then'](handleEntityNotFound(_0x3e296b,null))[_0xf795('0x24')](respondWithResult(_0x3e296b,0xc9))[_0xf795('0x42')](handleError(_0x3e296b,null));};exports[_0xf795('0x66')]=function(_0x2c5d13,_0x2751cd,_0x84856c){return db[_0xf795('0x48')][_0xf795('0x49')](function(_0x51a1f0){var _0x1fab53;return db[_0xf795('0x2f')][_0xf795('0x67')]({'where':{'id':_0x2c5d13['params']['id']}})[_0xf795('0x24')](handleEntityNotFound(_0x2751cd,null))[_0xf795('0x24')](function(_0x3c7a71){if(_0x3c7a71){_0x1fab53=_0x3c7a71;return _0x3c7a71[_0xf795('0x66')](_0x2c5d13[_0xf795('0x47')][_0xf795('0x68')]);}})[_0xf795('0x24')](function(_0x2d8bef){if(_0x2d8bef){return db[_0xf795('0x69')][_0xf795('0x67')]({'where':{'id':_0x2c5d13[_0xf795('0x47')][_0xf795('0x68')]},'attributes':['id',_0xf795('0x2b'),'save_name','converted_format',_0xf795('0x6a')],'raw':!![]});}})[_0xf795('0x24')](function(_0x92d19c){if(_0x92d19c){fs[_0xf795('0x59')](path[_0xf795('0x4c')](_0x1fab53[_0xf795('0x4b')],util[_0xf795('0x64')]('%s.%s',_0x92d19c['name'],_0x92d19c[_0xf795('0x5f')])));return _0x92d19c;}})['catch'](function(_0x112661){throw new db[(_0xf795('0x5b'))][(_0xf795('0x5c'))](_0x112661[_0xf795('0x6b')]);});})[_0xf795('0x24')](handleEntityNotFound(_0x2751cd,null))[_0xf795('0x24')](respondWithStatusCode(_0x2751cd,0xcc))[_0xf795('0x42')](handleError(_0x2751cd,null));};exports['getSounds']=function(_0xc80038,_0xc41a7a,_0x4dae11){var _0xb1a628={};var _0x266789={};var _0xcadd74;var _0x4739d8;return db[_0xf795('0x2f')][_0xf795('0x67')]({'where':{'id':_0xc80038[_0xf795('0x47')]['id']}})[_0xf795('0x24')](handleEntityNotFound(_0xc41a7a,null))[_0xf795('0x24')](function(_0x50a2da){if(_0x50a2da){_0xcadd74=_0x50a2da;_0x266789[_0xf795('0x33')]=_['keys'](db[_0xf795('0x69')][_0xf795('0x30')]);_0x266789['query']=_[_0xf795('0x35')](_0xc80038[_0xf795('0x34')]);_0x266789[_0xf795('0x3b')]=_['intersection'](_0x266789[_0xf795('0x33')],_0x266789[_0xf795('0x34')]);_0xb1a628[_0xf795('0x38')]=_[_0xf795('0x36')](_0x266789[_0xf795('0x33')],qs[_0xf795('0x37')](_0xc80038[_0xf795('0x34')]['fields']));_0xb1a628[_0xf795('0x38')]=_0xb1a628['attributes'][_0xf795('0x44')]?_0xb1a628[_0xf795('0x38')]:_0x266789['model'];_0xb1a628['order']=qs[_0xf795('0x39')](_0xc80038[_0xf795('0x34')][_0xf795('0x39')]);_0xb1a628[_0xf795('0x3a')]=qs[_0xf795('0x3b')](_[_0xf795('0x6c')](_0xc80038['query'],_0x266789[_0xf795('0x3b')]));if(_0xc80038[_0xf795('0x34')]['filter']){_0xb1a628[_0xf795('0x3a')]=_['merge'](_0xb1a628[_0xf795('0x3a')],{'$or':_[_0xf795('0x2e')](_0xb1a628[_0xf795('0x38')],function(_0xef4825){var _0x768786={};_0x768786[_0xef4825]={'$like':'%'+_0xc80038[_0xf795('0x34')][_0xf795('0x3c')]+'%'};return _0x768786;})});}_0xb1a628=_[_0xf795('0x3d')]({},_0xb1a628,_0xc80038[_0xf795('0x3e')]);return _0xcadd74[_0xf795('0x6d')](_0xb1a628);}})['then'](function(_0x119bbe){if(_0x119bbe){_0x4739d8=_0x119bbe[_0xf795('0x44')];if(!_0xc80038[_0xf795('0x34')][_0xf795('0x6e')](_0xf795('0x6f'))){_0xb1a628[_0xf795('0x20')]=qs['limit'](_0xc80038[_0xf795('0x34')]['limit']);_0xb1a628[_0xf795('0x1f')]=qs[_0xf795('0x1f')](_0xc80038[_0xf795('0x34')]['offset']);}return _0xcadd74[_0xf795('0x6d')](_0xb1a628);}})[_0xf795('0x24')](function(_0x478160){if(_0x478160){return _0x478160?{'count':_0x4739d8,'rows':_0x478160}:null;}})['then'](respondWithResult(_0xc41a7a,null))[_0xf795('0x42')](handleError(_0xc41a7a,null));};