Built motion from commit (unavailable).|2.2.3
[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 _0x16f9=['dateEnd','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','limit','sort','where','add','day','merge','filter','type','key','VIRTUAL','options','VoiceMusicOnHold','count','includeAll','include','findAll','rows','show','params','find','catch','body','create','sequelize','transaction','mode','files','join','root','server/files/moh','0777','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','directory','readdirSync','unlinkSync','rmdirSync','Sequelize','ValidationError','Sound','findOne','createdAt','format','%s.%s','basename','save_name','converted_format','symlinkSync','message','removeSound','id2','getSounds','order','pick','map','html-pdf','rimraf','fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','json','offset','set','Content-Range','apply','reject','save','update','destroy','MusicOnHolds','UserProfileResource','then','status','error','name','send','index','rawAttributes','model','keys','dateStart'];(function(_0x43b702,_0x5b6a66){var _0x4ba7c7=function(_0x563c7b){while(--_0x563c7b){_0x43b702['push'](_0x43b702['shift']());}};_0x4ba7c7(++_0x5b6a66);}(_0x16f9,0x130));var _0x916f=function(_0x1c15bd,_0x15e665){_0x1c15bd=_0x1c15bd-0x0;var _0xc0abd2=_0x16f9[_0x1c15bd];return _0xc0abd2;};'use strict';var pdf=require(_0x916f('0x0'));var emlformat=require('eml-format');var rimraf=require(_0x916f('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x916f('0x2'));var rp=require(_0x916f('0x3'));var moment=require(_0x916f('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x916f('0x5'));var util=require(_0x916f('0x6'));var path=require(_0x916f('0x7'));var sox=require(_0x916f('0x8'));var csv=require(_0x916f('0x9'));var ejs=require(_0x916f('0xa'));var fs=require('fs');var _=require(_0x916f('0xb'));var squel=require(_0x916f('0xc'));var crypto=require(_0x916f('0xd'));var jsforce=require(_0x916f('0xe'));var deskjs=require(_0x916f('0xf'));var toCsv=require(_0x916f('0x9'));var querystring=require(_0x916f('0x10'));var Papa=require('papaparse');var Redis=require(_0x916f('0x11'));var qs=require(_0x916f('0x12'));var logger=require(_0x916f('0x13'))(_0x916f('0x14'));var utils=require(_0x916f('0x15'));var config=require(_0x916f('0x16'));var db=require(_0x916f('0x17'))['db'];function respondWithStatusCode(_0x56231a,_0x4f0958){_0x4f0958=_0x4f0958||0xcc;return function(_0x11e97f){if(_0x11e97f){return _0x56231a[_0x916f('0x18')](_0x4f0958);}return _0x56231a['status'](_0x4f0958)[_0x916f('0x19')]();};}function respondWithResult(_0x16bdd2,_0xe323d4){_0xe323d4=_0xe323d4||0xc8;return function(_0x1cf6df){if(_0x1cf6df){return _0x16bdd2['status'](_0xe323d4)[_0x916f('0x1a')](_0x1cf6df);}};}function respondWithFilteredResult(_0x6ddd10,_0x5eb399){return function(_0x55a00e){if(_0x55a00e){var _0x30a626=_0x55a00e['count'],_0x3b2439=_0x5eb399[_0x916f('0x1b')],_0x272fdf=_0x5eb399[_0x916f('0x1b')]+_0x5eb399['limit'],_0x3c24df;if(_0x272fdf>=_0x30a626){_0x272fdf=_0x30a626;_0x3c24df=0xc8;}else{_0x3c24df=0xce;}_0x6ddd10['status'](_0x3c24df);return _0x6ddd10[_0x916f('0x1c')](_0x916f('0x1d'),_0x3b2439+'-'+_0x272fdf+'/'+_0x30a626)['json'](_0x55a00e);}return null;};}function patchUpdates(_0x303679){return function(_0x168f25){try{jsonpatch[_0x916f('0x1e')](_0x168f25,_0x303679,!![]);}catch(_0x3e098c){return BPromise[_0x916f('0x1f')](_0x3e098c);}return _0x168f25[_0x916f('0x20')]();};}function saveUpdates(_0x57a279,_0x3b3a41){return function(_0x33f464){if(_0x33f464){return _0x33f464[_0x916f('0x21')](_0x57a279)['then'](function(_0x45e781){return _0x45e781;});}return null;};}function removeEntity(_0x31a99b,_0x3b7db1){return function(_0x47daf3){if(_0x47daf3){return _0x47daf3[_0x916f('0x22')]()['then'](function(){var _0x115233=_0x47daf3['get']({'plain':!![]});var _0x2b889d=_0x916f('0x23');return db[_0x916f('0x24')][_0x916f('0x22')]({'where':{'type':_0x2b889d,'resourceId':_0x115233['id']}})[_0x916f('0x25')](function(){return _0x47daf3;});})['then'](function(){_0x31a99b[_0x916f('0x26')](0xcc)[_0x916f('0x19')]();});}};}function handleEntityNotFound(_0x17aea1,_0x2ce0e4){return function(_0xc96b3b){if(!_0xc96b3b){_0x17aea1['sendStatus'](0x194);}return _0xc96b3b;};}function handleError(_0x11a413,_0x1db1e4){_0x1db1e4=_0x1db1e4||0x1f4;return function(_0x370510){logger[_0x916f('0x27')](_0x370510['stack']);if(_0x370510[_0x916f('0x28')]){delete _0x370510[_0x916f('0x28')];}_0x11a413[_0x916f('0x26')](_0x1db1e4)[_0x916f('0x29')](_0x370510);};}exports[_0x916f('0x2a')]=function(_0x25a9a9,_0x38bd11){var _0x4bbb19={},_0xa8835={},_0x1bddfd={'count':0x0,'rows':[]};var _0x1001ad=db['VoiceMusicOnHold'][_0x916f('0x2b')];_0xa8835[_0x916f('0x2c')]=_[_0x916f('0x2d')](_0x1001ad);var _0x58add5={'dateStart':_0x25a9a9['query'][_0x916f('0x2e')],'dateEnd':_0x25a9a9['query'][_0x916f('0x2f')]};delete _0x25a9a9[_0x916f('0x30')]['dateStart'];delete _0x25a9a9[_0x916f('0x30')]['dateEnd'];_0xa8835[_0x916f('0x30')]=_[_0x916f('0x2d')](_0x25a9a9[_0x916f('0x30')]);_0xa8835[_0x916f('0x31')]=_[_0x916f('0x32')](_0xa8835['model'],_0xa8835[_0x916f('0x30')]);_0x4bbb19[_0x916f('0x33')]=_[_0x916f('0x32')](_0xa8835[_0x916f('0x2c')],qs[_0x916f('0x34')](_0x25a9a9[_0x916f('0x30')][_0x916f('0x34')]));_0x4bbb19[_0x916f('0x33')]=_0x4bbb19[_0x916f('0x33')][_0x916f('0x35')]?_0x4bbb19['attributes']:_0xa8835['model'];if(!_0x25a9a9[_0x916f('0x30')][_0x916f('0x36')](_0x916f('0x37'))){_0x4bbb19[_0x916f('0x38')]=qs['limit'](_0x25a9a9[_0x916f('0x30')]['limit']);_0x4bbb19[_0x916f('0x1b')]=qs[_0x916f('0x1b')](_0x25a9a9[_0x916f('0x30')][_0x916f('0x1b')]);}_0x4bbb19['order']=qs[_0x916f('0x39')](_0x25a9a9[_0x916f('0x30')][_0x916f('0x39')]);_0x4bbb19[_0x916f('0x3a')]=qs[_0x916f('0x31')](_['pick'](_0x25a9a9['query'],_0xa8835[_0x916f('0x31')]));if(_0x58add5[_0x916f('0x2e')]){if(_0x58add5[_0x916f('0x2f')]){_0x58add5['dateEnd']=moment(_0x58add5[_0x916f('0x2f')])[_0x916f('0x3b')](0x1,_0x916f('0x3c'));}else{_0x58add5[_0x916f('0x2f')]=moment(_0x58add5[_0x916f('0x2e')])[_0x916f('0x3b')](0x1,_0x916f('0x3c'));}_[_0x916f('0x3d')](_0x4bbb19[_0x916f('0x3a')],{'createdAt':{'$gte':_0x58add5[_0x916f('0x2e')],'$lte':_0x58add5[_0x916f('0x2f')]}});}if(_0x25a9a9[_0x916f('0x30')][_0x916f('0x3e')]){_0x4bbb19[_0x916f('0x3a')]=_['merge'](_0x4bbb19['where'],{'$or':_['map'](_0x1001ad,function(_0x50e190){if(_0x50e190[_0x916f('0x3f')][_0x916f('0x40')]!==_0x916f('0x41')){var _0x5ea82a={};_0x5ea82a[_0x50e190['field']]={'$like':'%'+_0x25a9a9[_0x916f('0x30')]['filter']+'%'};return _0x5ea82a;}})});}_0x4bbb19=_['merge']({},_0x4bbb19,_0x25a9a9[_0x916f('0x42')]);var _0x413a93={'where':_0x4bbb19['where']};return db[_0x916f('0x43')][_0x916f('0x44')](_0x413a93)[_0x916f('0x25')](function(_0x5bcdfc){_0x1bddfd['count']=_0x5bcdfc;if(_0x25a9a9[_0x916f('0x30')][_0x916f('0x45')]){_0x4bbb19[_0x916f('0x46')]=[{'all':!![]}];}return db['VoiceMusicOnHold'][_0x916f('0x47')](_0x4bbb19);})[_0x916f('0x25')](function(_0x15089a){_0x1bddfd[_0x916f('0x48')]=_0x15089a;return _0x1bddfd;})['then'](respondWithFilteredResult(_0x38bd11,_0x4bbb19))['catch'](handleError(_0x38bd11,null));};exports[_0x916f('0x49')]=function(_0x21a046,_0x448484){var _0x4946ef={'raw':![],'where':{'id':_0x21a046[_0x916f('0x4a')]['id']}},_0x2dbd56={};_0x2dbd56[_0x916f('0x2c')]=_['keys'](db[_0x916f('0x43')][_0x916f('0x2b')]);_0x2dbd56[_0x916f('0x30')]=_[_0x916f('0x2d')](_0x21a046[_0x916f('0x30')]);_0x2dbd56[_0x916f('0x31')]=_[_0x916f('0x32')](_0x2dbd56[_0x916f('0x2c')],_0x2dbd56[_0x916f('0x30')]);_0x4946ef[_0x916f('0x33')]=_[_0x916f('0x32')](_0x2dbd56[_0x916f('0x2c')],qs[_0x916f('0x34')](_0x21a046[_0x916f('0x30')][_0x916f('0x34')]));_0x4946ef[_0x916f('0x33')]=_0x4946ef['attributes'][_0x916f('0x35')]?_0x4946ef[_0x916f('0x33')]:_0x2dbd56['model'];if(_0x21a046[_0x916f('0x30')][_0x916f('0x45')]){_0x4946ef[_0x916f('0x46')]=[{'all':!![]}];}_0x4946ef=_[_0x916f('0x3d')]({},_0x4946ef,_0x21a046[_0x916f('0x42')]);return db[_0x916f('0x43')][_0x916f('0x4b')](_0x4946ef)['then'](handleEntityNotFound(_0x448484,null))['then'](respondWithResult(_0x448484,null))[_0x916f('0x4c')](handleError(_0x448484,null));};exports['update']=function(_0x4739ec,_0x13432e){if(_0x4739ec[_0x916f('0x4d')]['id']){delete _0x4739ec[_0x916f('0x4d')]['id'];}return db[_0x916f('0x43')][_0x916f('0x4b')]({'where':{'id':_0x4739ec[_0x916f('0x4a')]['id']}})[_0x916f('0x25')](handleEntityNotFound(_0x13432e,null))[_0x916f('0x25')](saveUpdates(_0x4739ec[_0x916f('0x4d')],null))['then'](respondWithResult(_0x13432e,null))[_0x916f('0x4c')](handleError(_0x13432e,null));};exports[_0x916f('0x4e')]=function(_0x6091e3,_0x47e11f,_0x4e3907){return db[_0x916f('0x4f')][_0x916f('0x50')](function(_0x570742){if(_0x6091e3[_0x916f('0x4d')][_0x916f('0x51')]===_0x916f('0x52')){_0x6091e3[_0x916f('0x4d')]['directory']=path[_0x916f('0x53')](config[_0x916f('0x54')],_0x916f('0x55'),_0x6091e3[_0x916f('0x4d')][_0x916f('0x28')]);}return db[_0x916f('0x43')][_0x916f('0x4e')](_0x6091e3['body'],{'transaction':_0x570742})['then'](function(_0x1dedca){if(_0x6091e3[_0x916f('0x4d')][_0x916f('0x51')]==='files'){fs['mkdirSync'](_0x1dedca['directory'],parseInt(_0x916f('0x56'),0x8));}var _0x438eac=_0x6091e3[_0x916f('0x57')][_0x916f('0x58')]({'plain':!![]});if(!_0x438eac)throw new Error(_0x916f('0x59'));if(_0x438eac[_0x916f('0x5a')]===_0x916f('0x57')){var _0x493759=_0x1dedca['get']({'plain':!![]});return db[_0x916f('0x5b')][_0x916f('0x4b')]({'where':{'name':'MusicOnHolds','userProfileId':_0x438eac[_0x916f('0x5c')]},'raw':!![]})[_0x916f('0x25')](function(_0x5e4fc9){if(_0x5e4fc9&&_0x5e4fc9[_0x916f('0x5d')]===0x0){return db[_0x916f('0x24')]['create']({'name':_0x493759[_0x916f('0x28')],'resourceId':_0x493759['id'],'type':_0x5e4fc9[_0x916f('0x28')],'sectionId':_0x5e4fc9['id']},{})[_0x916f('0x25')](function(){return _0x1dedca;});}else{return _0x1dedca;}})[_0x916f('0x4c')](function(_0x3d20c2){logger['error'](_0x916f('0x5e'),_0x3d20c2);});}return _0x1dedca;});})['then'](respondWithResult(_0x47e11f,0xc9))['catch'](handleError(_0x47e11f,null));};exports[_0x916f('0x22')]=function(_0x5f1975,_0x4a4621,_0x5d9285){var _0x1d18a8;return db['VoiceMusicOnHold'][_0x916f('0x4b')]({'where':{'id':_0x5f1975['params']['id']}})[_0x916f('0x25')](function(_0x59da93){if(_0x59da93){return db['sequelize'][_0x916f('0x50')](function(_0x4d0bf9){return _0x59da93['destroy']({'transaction':_0x4d0bf9,'raw':!![]})[_0x916f('0x25')](function(_0x3b9051){if(_0x3b9051[_0x916f('0x5f')]){fs[_0x916f('0x60')](_0x3b9051[_0x916f('0x5f')])['forEach'](function(_0x1a416b){fs[_0x916f('0x61')](path[_0x916f('0x53')](_0x3b9051[_0x916f('0x5f')],_0x1a416b));});fs[_0x916f('0x62')](_0x3b9051['directory']);}return _0x3b9051;});})['catch'](function(_0x5cdb55){throw new db[(_0x916f('0x63'))][(_0x916f('0x64'))](_0x5cdb55['message']);});}})['then'](handleEntityNotFound(_0x4a4621,null))[_0x916f('0x25')](removeEntity(_0x4a4621,null))[_0x916f('0x4c')](handleError(_0x4a4621,null));};exports['addSound']=function(_0x1ca490,_0x4aeb3c,_0x48dc28){return db['sequelize']['transaction'](function(_0x35f644){var _0x1d8fb0;return db['VoiceMusicOnHold'][_0x916f('0x4b')]({'where':{'id':_0x1ca490['params']['id']}})[_0x916f('0x25')](function(_0x518f1a){if(_0x518f1a){_0x1d8fb0=_0x518f1a;return _0x518f1a['addSound'](_0x1ca490[_0x916f('0x4d')]['id'],{'transaction':_0x35f644});}})['then'](function(_0x5634d6){if(_0x5634d6){return db[_0x916f('0x65')][_0x916f('0x66')]({'where':{'id':_0x1ca490[_0x916f('0x4d')]['id']},'attributes':['id',_0x916f('0x28'),'save_name','converted_format',_0x916f('0x67')],'raw':!![]});}})[_0x916f('0x25')](function(_0x193e5d){if(_0x193e5d){var _0x4cac37=path[_0x916f('0x53')](config['root'],'server/files/sounds/converted',util[_0x916f('0x68')](_0x916f('0x69'),path[_0x916f('0x6a')](_0x193e5d[_0x916f('0x6b')],path['extname'](_0x193e5d[_0x916f('0x6b')])),_0x193e5d[_0x916f('0x6c')]));var _0x33bddb=path[_0x916f('0x53')](config[_0x916f('0x54')],_0x916f('0x55'),_0x1d8fb0[_0x916f('0x28')],util['format'](_0x916f('0x69'),_0x193e5d[_0x916f('0x28')],_0x193e5d[_0x916f('0x6c')]));fs[_0x916f('0x6d')](_0x4cac37,_0x33bddb);return _0x193e5d;}})[_0x916f('0x4c')](function(_0x4b80ca){throw new db[(_0x916f('0x63'))][(_0x916f('0x64'))](_0x4b80ca[_0x916f('0x6e')]);});})[_0x916f('0x25')](handleEntityNotFound(_0x4aeb3c,null))[_0x916f('0x25')](respondWithResult(_0x4aeb3c,0xc9))[_0x916f('0x4c')](handleError(_0x4aeb3c,null));};exports[_0x916f('0x6f')]=function(_0x5aa1bb,_0x457832,_0x5d8231){return db['sequelize'][_0x916f('0x50')](function(_0x22c84e){var _0x1addf7;return db[_0x916f('0x43')][_0x916f('0x66')]({'where':{'id':_0x5aa1bb[_0x916f('0x4a')]['id']}})[_0x916f('0x25')](handleEntityNotFound(_0x457832,null))[_0x916f('0x25')](function(_0x7c4dd6){if(_0x7c4dd6){_0x1addf7=_0x7c4dd6;return _0x7c4dd6['removeSound'](_0x5aa1bb['params'][_0x916f('0x70')]);}})[_0x916f('0x25')](function(_0x9f936e){if(_0x9f936e){return db[_0x916f('0x65')]['findOne']({'where':{'id':_0x5aa1bb['params']['id2']},'attributes':['id',_0x916f('0x28'),_0x916f('0x6b'),_0x916f('0x6c'),_0x916f('0x67')],'raw':!![]});}})['then'](function(_0x49cf8e){if(_0x49cf8e){fs[_0x916f('0x61')](path[_0x916f('0x53')](_0x1addf7[_0x916f('0x5f')],util[_0x916f('0x68')](_0x916f('0x69'),_0x49cf8e[_0x916f('0x28')],_0x49cf8e[_0x916f('0x6c')])));return _0x49cf8e;}})[_0x916f('0x4c')](function(_0x4ea904){throw new db[(_0x916f('0x63'))]['ValidationError'](_0x4ea904['message']);});})[_0x916f('0x25')](handleEntityNotFound(_0x457832,null))[_0x916f('0x25')](respondWithStatusCode(_0x457832,0xcc))[_0x916f('0x4c')](handleError(_0x457832,null));};exports[_0x916f('0x71')]=function(_0x35ab4a,_0x204a6b,_0x1d7ce9){var _0x7eb8fe={};var _0x35d7c2={};var _0x48dfbd;var _0x56306e;return db[_0x916f('0x43')]['findOne']({'where':{'id':_0x35ab4a['params']['id']}})['then'](handleEntityNotFound(_0x204a6b,null))[_0x916f('0x25')](function(_0x25f95a){if(_0x25f95a){_0x48dfbd=_0x25f95a;_0x35d7c2['model']=_[_0x916f('0x2d')](db[_0x916f('0x65')][_0x916f('0x2b')]);_0x35d7c2[_0x916f('0x30')]=_['keys'](_0x35ab4a[_0x916f('0x30')]);_0x35d7c2['filters']=_['intersection'](_0x35d7c2[_0x916f('0x2c')],_0x35d7c2[_0x916f('0x30')]);_0x7eb8fe['attributes']=_['intersection'](_0x35d7c2[_0x916f('0x2c')],qs['fields'](_0x35ab4a[_0x916f('0x30')][_0x916f('0x34')]));_0x7eb8fe['attributes']=_0x7eb8fe[_0x916f('0x33')][_0x916f('0x35')]?_0x7eb8fe[_0x916f('0x33')]:_0x35d7c2[_0x916f('0x2c')];_0x7eb8fe[_0x916f('0x72')]=qs['sort'](_0x35ab4a[_0x916f('0x30')][_0x916f('0x39')]);_0x7eb8fe[_0x916f('0x3a')]=qs[_0x916f('0x31')](_[_0x916f('0x73')](_0x35ab4a[_0x916f('0x30')],_0x35d7c2['filters']));if(_0x35ab4a[_0x916f('0x30')][_0x916f('0x3e')]){_0x7eb8fe['where']=_['merge'](_0x7eb8fe[_0x916f('0x3a')],{'$or':_[_0x916f('0x74')](_0x7eb8fe[_0x916f('0x33')],function(_0x348e62){var _0xa1b958={};_0xa1b958[_0x348e62]={'$like':'%'+_0x35ab4a[_0x916f('0x30')]['filter']+'%'};return _0xa1b958;})});}_0x7eb8fe=_[_0x916f('0x3d')]({},_0x7eb8fe,_0x35ab4a[_0x916f('0x42')]);return _0x48dfbd[_0x916f('0x71')](_0x7eb8fe);}})[_0x916f('0x25')](function(_0x28e609){if(_0x28e609){_0x56306e=_0x28e609[_0x916f('0x35')];if(!_0x35ab4a[_0x916f('0x30')][_0x916f('0x36')](_0x916f('0x37'))){_0x7eb8fe['limit']=qs[_0x916f('0x38')](_0x35ab4a[_0x916f('0x30')][_0x916f('0x38')]);_0x7eb8fe['offset']=qs[_0x916f('0x1b')](_0x35ab4a['query'][_0x916f('0x1b')]);}return _0x48dfbd['getSounds'](_0x7eb8fe);}})['then'](function(_0xf714ef){if(_0xf714ef){return _0xf714ef?{'count':_0x56306e,'rows':_0xf714ef}:null;}})[_0x916f('0x25')](respondWithResult(_0x204a6b,null))[_0x916f('0x4c')](handleError(_0x204a6b,null));};