fe2445de0910e838d643e8a8f0dbd1163f9abf06
[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 _0x17b0=['nolimit','sort','pick','filter','where','merge','VIRTUAL','options','VoiceMusicOnHold','count','includeAll','include','findAll','rows','catch','params','find','body','create','sequelize','transaction','mode','files','directory','root','server/files/moh','mkdirSync','0777','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','readdirSync','forEach','unlinkSync','join','Sequelize','ValidationError','message','addSound','Sound','findOne','save_name','converted_format','createdAt','server/files/sounds/converted','format','%s.%s','basename','extname','symlinkSync','removeSound','id2','order','getSounds','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','sox','to-csv','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','set','Content-Range','apply','reject','save','update','destroy','get','MusicOnHolds','UserProfileResource','then','sendStatus','error','name','send','index','map','rawAttributes','fieldName','type','key','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty'];(function(_0x587fb5,_0x59cb3b){var _0x551105=function(_0x5bbef4){while(--_0x5bbef4){_0x587fb5['push'](_0x587fb5['shift']());}};_0x551105(++_0x59cb3b);}(_0x17b0,0x19b));var _0x017b=function(_0x5ddb1c,_0x5ea4df){_0x5ddb1c=_0x5ddb1c-0x0;var _0x354adc=_0x17b0[_0x5ddb1c];return _0x354adc;};'use strict';var emlformat=require(_0x017b('0x0'));var rimraf=require(_0x017b('0x1'));var zipdir=require(_0x017b('0x2'));var jsonpatch=require(_0x017b('0x3'));var rp=require(_0x017b('0x4'));var moment=require(_0x017b('0x5'));var BPromise=require(_0x017b('0x6'));var Mustache=require('mustache');var util=require(_0x017b('0x7'));var path=require('path');var sox=require(_0x017b('0x8'));var csv=require(_0x017b('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x017b('0xa'));var _=require(_0x017b('0xb'));var squel=require(_0x017b('0xc'));var crypto=require('crypto');var jsforce=require(_0x017b('0xd'));var deskjs=require(_0x017b('0xe'));var toCsv=require(_0x017b('0x9'));var querystring=require(_0x017b('0xf'));var Papa=require(_0x017b('0x10'));var Redis=require(_0x017b('0x11'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x017b('0x12'));var hardwareService=require(_0x017b('0x13'));var logger=require(_0x017b('0x14'))(_0x017b('0x15'));var utils=require(_0x017b('0x16'));var config=require('../../config/environment');var licenseUtil=require(_0x017b('0x17'));var db=require(_0x017b('0x18'))['db'];function respondWithStatusCode(_0x3a3852,_0x2bb98b){_0x2bb98b=_0x2bb98b||0xcc;return function(_0x5a49c8){if(_0x5a49c8){return _0x3a3852['sendStatus'](_0x2bb98b);}return _0x3a3852[_0x017b('0x19')](_0x2bb98b)[_0x017b('0x1a')]();};}function respondWithResult(_0x2c5c8e,_0x2ade62){_0x2ade62=_0x2ade62||0xc8;return function(_0x5209ad){if(_0x5209ad){return _0x2c5c8e[_0x017b('0x19')](_0x2ade62)[_0x017b('0x1b')](_0x5209ad);}};}function respondWithFilteredResult(_0x1853cb,_0x19de23){return function(_0xf9d054){if(_0xf9d054){var _0x26da65=typeof _0x19de23[_0x017b('0x1c')]===_0x017b('0x1d')&&typeof _0x19de23[_0x017b('0x1e')]===_0x017b('0x1d');var _0x260078=_0xf9d054['count'];var _0x548d0e=_0x26da65?0x0:_0x19de23[_0x017b('0x1c')];var _0x174108=_0x26da65?_0xf9d054['count']:_0x19de23[_0x017b('0x1c')]+_0x19de23[_0x017b('0x1e')];var _0x165b1c;if(_0x174108>=_0x260078){_0x174108=_0x260078;_0x165b1c=0xc8;}else{_0x165b1c=0xce;}_0x1853cb[_0x017b('0x19')](_0x165b1c);return _0x1853cb[_0x017b('0x1f')](_0x017b('0x20'),_0x548d0e+'-'+_0x174108+'/'+_0x260078)[_0x017b('0x1b')](_0xf9d054);}return null;};}function patchUpdates(_0x3b7c9a){return function(_0x37f363){try{jsonpatch[_0x017b('0x21')](_0x37f363,_0x3b7c9a,!![]);}catch(_0x52e381){return BPromise[_0x017b('0x22')](_0x52e381);}return _0x37f363[_0x017b('0x23')]();};}function saveUpdates(_0x3c04c7,_0x1172de){return function(_0x462a5d){if(_0x462a5d){return _0x462a5d[_0x017b('0x24')](_0x3c04c7)['then'](function(_0x5816f4){return _0x5816f4;});}return null;};}function removeEntity(_0xaf2051,_0x370c25){return function(_0x36c02c){if(_0x36c02c){return _0x36c02c[_0x017b('0x25')]()['then'](function(){var _0x5cd470=_0x36c02c[_0x017b('0x26')]({'plain':!![]});var _0x1843fd=_0x017b('0x27');return db[_0x017b('0x28')]['destroy']({'where':{'type':_0x1843fd,'resourceId':_0x5cd470['id']}})['then'](function(){return _0x36c02c;});})[_0x017b('0x29')](function(){_0xaf2051[_0x017b('0x19')](0xcc)[_0x017b('0x1a')]();});}};}function handleEntityNotFound(_0x4bf49c,_0x90df3c){return function(_0x337e93){if(!_0x337e93){_0x4bf49c[_0x017b('0x2a')](0x194);}return _0x337e93;};}function handleError(_0x1a25a6,_0x200885){_0x200885=_0x200885||0x1f4;return function(_0x281f38){logger[_0x017b('0x2b')](_0x281f38['stack']);if(_0x281f38[_0x017b('0x2c')]){delete _0x281f38['name'];}_0x1a25a6[_0x017b('0x19')](_0x200885)[_0x017b('0x2d')](_0x281f38);};}exports[_0x017b('0x2e')]=function(_0x2b25c7,_0x1eed4c){var _0x2b7455={},_0x23cc34={},_0x6f2c37={'count':0x0,'rows':[]};var _0x3d60ad=_[_0x017b('0x2f')](db['VoiceMusicOnHold'][_0x017b('0x30')],function(_0x3d6a30){return{'name':_0x3d6a30[_0x017b('0x31')],'type':_0x3d6a30[_0x017b('0x32')][_0x017b('0x33')]};});_0x23cc34['model']=_[_0x017b('0x2f')](_0x3d60ad,_0x017b('0x2c'));_0x23cc34[_0x017b('0x34')]=_[_0x017b('0x35')](_0x2b25c7[_0x017b('0x34')]);_0x23cc34[_0x017b('0x36')]=_[_0x017b('0x37')](_0x23cc34[_0x017b('0x38')],_0x23cc34[_0x017b('0x34')]);_0x2b7455[_0x017b('0x39')]=_[_0x017b('0x37')](_0x23cc34['model'],qs[_0x017b('0x3a')](_0x2b25c7[_0x017b('0x34')][_0x017b('0x3a')]));_0x2b7455[_0x017b('0x39')]=_0x2b7455[_0x017b('0x39')][_0x017b('0x3b')]?_0x2b7455['attributes']:_0x23cc34[_0x017b('0x38')];if(!_0x2b25c7[_0x017b('0x34')][_0x017b('0x3c')](_0x017b('0x3d'))){_0x2b7455[_0x017b('0x1e')]=qs[_0x017b('0x1e')](_0x2b25c7[_0x017b('0x34')]['limit']);_0x2b7455[_0x017b('0x1c')]=qs[_0x017b('0x1c')](_0x2b25c7[_0x017b('0x34')][_0x017b('0x1c')]);}_0x2b7455['order']=qs[_0x017b('0x3e')](_0x2b25c7[_0x017b('0x34')][_0x017b('0x3e')]);delete _0x2b25c7[_0x017b('0x34')][_0x017b('0x3e')];_0x2b7455['where']=qs[_0x017b('0x36')](_[_0x017b('0x3f')](_0x2b25c7[_0x017b('0x34')],_0x23cc34['filters']),_0x3d60ad);if(_0x2b25c7[_0x017b('0x34')][_0x017b('0x40')]){_0x2b7455[_0x017b('0x41')]=_[_0x017b('0x42')](_0x2b7455[_0x017b('0x41')],{'$or':_['map'](_0x3d60ad,function(_0x348911){if(_0x348911['type']!==_0x017b('0x43')){var _0xb7a9df={};_0xb7a9df[_0x348911[_0x017b('0x2c')]]={'$like':'%'+_0x2b25c7[_0x017b('0x34')][_0x017b('0x40')]+'%'};return _0xb7a9df;}})});}_0x2b7455=_[_0x017b('0x42')]({},_0x2b7455,_0x2b25c7[_0x017b('0x44')]);var _0x275929={'where':_0x2b7455[_0x017b('0x41')]};return db[_0x017b('0x45')][_0x017b('0x46')](_0x275929)[_0x017b('0x29')](function(_0x17292d){_0x6f2c37[_0x017b('0x46')]=_0x17292d;if(_0x2b25c7[_0x017b('0x34')][_0x017b('0x47')]){_0x2b7455[_0x017b('0x48')]=[{'all':!![]}];}return db[_0x017b('0x45')][_0x017b('0x49')](_0x2b7455);})[_0x017b('0x29')](function(_0x244d27){_0x6f2c37[_0x017b('0x4a')]=_0x244d27;return _0x6f2c37;})['then'](respondWithFilteredResult(_0x1eed4c,_0x2b7455))[_0x017b('0x4b')](handleError(_0x1eed4c,null));};exports['show']=function(_0x39ec0e,_0x1ffb43){var _0x33fc26={'raw':![],'where':{'id':_0x39ec0e[_0x017b('0x4c')]['id']}},_0x45c459={};_0x45c459['model']=_['keys'](db['VoiceMusicOnHold']['rawAttributes']);_0x45c459['query']=_[_0x017b('0x35')](_0x39ec0e[_0x017b('0x34')]);_0x45c459[_0x017b('0x36')]=_[_0x017b('0x37')](_0x45c459[_0x017b('0x38')],_0x45c459[_0x017b('0x34')]);_0x33fc26['attributes']=_[_0x017b('0x37')](_0x45c459[_0x017b('0x38')],qs['fields'](_0x39ec0e[_0x017b('0x34')][_0x017b('0x3a')]));_0x33fc26[_0x017b('0x39')]=_0x33fc26[_0x017b('0x39')][_0x017b('0x3b')]?_0x33fc26['attributes']:_0x45c459[_0x017b('0x38')];if(_0x39ec0e[_0x017b('0x34')][_0x017b('0x47')]){_0x33fc26[_0x017b('0x48')]=[{'all':!![]}];}_0x33fc26=_['merge']({},_0x33fc26,_0x39ec0e['options']);return db[_0x017b('0x45')][_0x017b('0x4d')](_0x33fc26)[_0x017b('0x29')](handleEntityNotFound(_0x1ffb43,null))['then'](respondWithResult(_0x1ffb43,null))[_0x017b('0x4b')](handleError(_0x1ffb43,null));};exports[_0x017b('0x24')]=function(_0xa2f2cd,_0x1bbd9f){if(_0xa2f2cd[_0x017b('0x4e')]['id']){delete _0xa2f2cd[_0x017b('0x4e')]['id'];}return db[_0x017b('0x45')]['find']({'where':{'id':_0xa2f2cd['params']['id']}})[_0x017b('0x29')](handleEntityNotFound(_0x1bbd9f,null))['then'](saveUpdates(_0xa2f2cd[_0x017b('0x4e')],null))['then'](respondWithResult(_0x1bbd9f,null))[_0x017b('0x4b')](handleError(_0x1bbd9f,null));};exports[_0x017b('0x4f')]=function(_0x5ea15f,_0x5b33ea,_0x213573){return db[_0x017b('0x50')][_0x017b('0x51')](function(_0x5d2772){if(_0x5ea15f[_0x017b('0x4e')][_0x017b('0x52')]===_0x017b('0x53')){_0x5ea15f[_0x017b('0x4e')][_0x017b('0x54')]=path['join'](config[_0x017b('0x55')],_0x017b('0x56'),_0x5ea15f[_0x017b('0x4e')][_0x017b('0x2c')]);}return db['VoiceMusicOnHold'][_0x017b('0x4f')](_0x5ea15f[_0x017b('0x4e')],{'transaction':_0x5d2772})['then'](function(_0x5d1283){if(_0x5ea15f[_0x017b('0x4e')]['mode']==='files'){fs[_0x017b('0x57')](_0x5d1283[_0x017b('0x54')],parseInt(_0x017b('0x58'),0x8));}var _0x115f5b=_0x5ea15f[_0x017b('0x59')][_0x017b('0x26')]({'plain':!![]});if(!_0x115f5b)throw new Error(_0x017b('0x5a'));if(_0x115f5b[_0x017b('0x5b')]==='user'){var _0xbbff75=_0x5d1283[_0x017b('0x26')]({'plain':!![]});return db[_0x017b('0x5c')][_0x017b('0x4d')]({'where':{'name':_0x017b('0x27'),'userProfileId':_0x115f5b[_0x017b('0x5d')]},'raw':!![]})[_0x017b('0x29')](function(_0x4b7b81){if(_0x4b7b81&&_0x4b7b81[_0x017b('0x5e')]===0x0){return db[_0x017b('0x28')][_0x017b('0x4f')]({'name':_0xbbff75[_0x017b('0x2c')],'resourceId':_0xbbff75['id'],'type':_0x4b7b81[_0x017b('0x2c')],'sectionId':_0x4b7b81['id']},{})[_0x017b('0x29')](function(){return _0x5d1283;});}else{return _0x5d1283;}})[_0x017b('0x4b')](function(_0x28b3be){logger[_0x017b('0x2b')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x28b3be);});}return _0x5d1283;});})['then'](respondWithResult(_0x5b33ea,0xc9))['catch'](handleError(_0x5b33ea,null));};exports[_0x017b('0x25')]=function(_0x574804,_0x536722,_0x5b9110){var _0x18bcf5;return db[_0x017b('0x45')]['find']({'where':{'id':_0x574804['params']['id']}})[_0x017b('0x29')](function(_0x3abee6){if(_0x3abee6){return db['sequelize'][_0x017b('0x51')](function(_0x4d9611){return _0x3abee6[_0x017b('0x25')]({'transaction':_0x4d9611,'raw':!![]})[_0x017b('0x29')](function(_0x5b5ab0){if(_0x5b5ab0[_0x017b('0x54')]){fs[_0x017b('0x5f')](_0x5b5ab0['directory'])[_0x017b('0x60')](function(_0x3ff81c){fs[_0x017b('0x61')](path[_0x017b('0x62')](_0x5b5ab0[_0x017b('0x54')],_0x3ff81c));});fs['rmdirSync'](_0x5b5ab0[_0x017b('0x54')]);}return _0x5b5ab0;});})['catch'](function(_0x512540){throw new db[(_0x017b('0x63'))][(_0x017b('0x64'))](_0x512540[_0x017b('0x65')]);});}})[_0x017b('0x29')](handleEntityNotFound(_0x536722,null))['then'](removeEntity(_0x536722,null))[_0x017b('0x4b')](handleError(_0x536722,null));};exports['addSound']=function(_0x20c59a,_0x45d497,_0x3c6663){return db[_0x017b('0x50')][_0x017b('0x51')](function(_0x23e127){var _0x352334;return db[_0x017b('0x45')]['find']({'where':{'id':_0x20c59a[_0x017b('0x4c')]['id']}})[_0x017b('0x29')](function(_0x449a1c){if(_0x449a1c){_0x352334=_0x449a1c;return _0x449a1c[_0x017b('0x66')](_0x20c59a[_0x017b('0x4e')]['id'],{'transaction':_0x23e127});}})[_0x017b('0x29')](function(_0x3bf7e6){if(_0x3bf7e6){return db[_0x017b('0x67')][_0x017b('0x68')]({'where':{'id':_0x20c59a[_0x017b('0x4e')]['id']},'attributes':['id',_0x017b('0x2c'),_0x017b('0x69'),_0x017b('0x6a'),_0x017b('0x6b')],'raw':!![]});}})['then'](function(_0x432f40){if(_0x432f40){var _0x3d7460=path[_0x017b('0x62')](config[_0x017b('0x55')],_0x017b('0x6c'),util[_0x017b('0x6d')](_0x017b('0x6e'),path[_0x017b('0x6f')](_0x432f40[_0x017b('0x69')],path[_0x017b('0x70')](_0x432f40[_0x017b('0x69')])),_0x432f40['converted_format']));var _0x411731=path[_0x017b('0x62')](config[_0x017b('0x55')],_0x017b('0x56'),_0x352334[_0x017b('0x2c')],util[_0x017b('0x6d')](_0x017b('0x6e'),_0x432f40['name'],_0x432f40['converted_format']));fs[_0x017b('0x71')](_0x3d7460,_0x411731);return _0x432f40;}})[_0x017b('0x4b')](function(_0x59f966){throw new db['Sequelize']['ValidationError'](_0x59f966['message']);});})[_0x017b('0x29')](handleEntityNotFound(_0x45d497,null))['then'](respondWithResult(_0x45d497,0xc9))[_0x017b('0x4b')](handleError(_0x45d497,null));};exports[_0x017b('0x72')]=function(_0x2a03e6,_0x648ece,_0x5d20a5){return db[_0x017b('0x50')][_0x017b('0x51')](function(_0x525ea2){var _0x254af4;return db['VoiceMusicOnHold']['findOne']({'where':{'id':_0x2a03e6['params']['id']}})[_0x017b('0x29')](handleEntityNotFound(_0x648ece,null))[_0x017b('0x29')](function(_0x2397ca){if(_0x2397ca){_0x254af4=_0x2397ca;return _0x2397ca[_0x017b('0x72')](_0x2a03e6['params'][_0x017b('0x73')]);}})[_0x017b('0x29')](function(_0x433860){if(_0x433860){return db[_0x017b('0x67')][_0x017b('0x68')]({'where':{'id':_0x2a03e6[_0x017b('0x4c')][_0x017b('0x73')]},'attributes':['id','name',_0x017b('0x69'),_0x017b('0x6a'),_0x017b('0x6b')],'raw':!![]});}})[_0x017b('0x29')](function(_0x587caf){if(_0x587caf){fs[_0x017b('0x61')](path[_0x017b('0x62')](_0x254af4[_0x017b('0x54')],util[_0x017b('0x6d')](_0x017b('0x6e'),_0x587caf['name'],_0x587caf[_0x017b('0x6a')])));return _0x587caf;}})[_0x017b('0x4b')](function(_0x5e6a39){throw new db[(_0x017b('0x63'))][(_0x017b('0x64'))](_0x5e6a39[_0x017b('0x65')]);});})[_0x017b('0x29')](handleEntityNotFound(_0x648ece,null))[_0x017b('0x29')](respondWithStatusCode(_0x648ece,0xcc))[_0x017b('0x4b')](handleError(_0x648ece,null));};exports['getSounds']=function(_0x3421d4,_0x50db34,_0x416844){var _0x24c55d={};var _0x6f25cb={};var _0x3e7518;var _0x4c5c0f;return db['VoiceMusicOnHold'][_0x017b('0x68')]({'where':{'id':_0x3421d4[_0x017b('0x4c')]['id']}})[_0x017b('0x29')](handleEntityNotFound(_0x50db34,null))['then'](function(_0x759be1){if(_0x759be1){_0x3e7518=_0x759be1;_0x6f25cb[_0x017b('0x38')]=_[_0x017b('0x35')](db[_0x017b('0x67')][_0x017b('0x30')]);_0x6f25cb[_0x017b('0x34')]=_[_0x017b('0x35')](_0x3421d4[_0x017b('0x34')]);_0x6f25cb[_0x017b('0x36')]=_['intersection'](_0x6f25cb['model'],_0x6f25cb[_0x017b('0x34')]);_0x24c55d['attributes']=_[_0x017b('0x37')](_0x6f25cb[_0x017b('0x38')],qs[_0x017b('0x3a')](_0x3421d4[_0x017b('0x34')][_0x017b('0x3a')]));_0x24c55d[_0x017b('0x39')]=_0x24c55d[_0x017b('0x39')][_0x017b('0x3b')]?_0x24c55d[_0x017b('0x39')]:_0x6f25cb[_0x017b('0x38')];_0x24c55d[_0x017b('0x74')]=qs[_0x017b('0x3e')](_0x3421d4[_0x017b('0x34')][_0x017b('0x3e')]);_0x24c55d[_0x017b('0x41')]=qs[_0x017b('0x36')](_['pick'](_0x3421d4[_0x017b('0x34')],_0x6f25cb[_0x017b('0x36')]));if(_0x3421d4[_0x017b('0x34')][_0x017b('0x40')]){_0x24c55d[_0x017b('0x41')]=_[_0x017b('0x42')](_0x24c55d[_0x017b('0x41')],{'$or':_[_0x017b('0x2f')](_0x24c55d[_0x017b('0x39')],function(_0x2ce6e7){var _0x3179b2={};_0x3179b2[_0x2ce6e7]={'$like':'%'+_0x3421d4[_0x017b('0x34')][_0x017b('0x40')]+'%'};return _0x3179b2;})});}_0x24c55d=_[_0x017b('0x42')]({},_0x24c55d,_0x3421d4['options']);return _0x3e7518[_0x017b('0x75')](_0x24c55d);}})['then'](function(_0x30a5d9){if(_0x30a5d9){_0x4c5c0f=_0x30a5d9[_0x017b('0x3b')];if(!_0x3421d4['query'][_0x017b('0x3c')](_0x017b('0x3d'))){_0x24c55d['limit']=qs[_0x017b('0x1e')](_0x3421d4[_0x017b('0x34')]['limit']);_0x24c55d[_0x017b('0x1c')]=qs['offset'](_0x3421d4[_0x017b('0x34')]['offset']);}return _0x3e7518[_0x017b('0x75')](_0x24c55d);}})[_0x017b('0x29')](function(_0x3c2b9d){if(_0x3c2b9d){return _0x3c2b9d?{'count':_0x4c5c0f,'rows':_0x3c2b9d}:null;}})[_0x017b('0x29')](respondWithResult(_0x50db34,null))[_0x017b('0x4b')](handleError(_0x50db34,null));};