158d1ca270acde9df067b7ad59387ed3ef96ee28
[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 _0x626f=['attributes','fields','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','VIRTUAL','merge','options','includeAll','include','findAll','rows','catch','show','params','find','body','sequelize','mode','files','directory','join','root','server/files/moh','create','mkdirSync','0777','user','get','role','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','transaction','readdirSync','rmdirSync','Sequelize','message','addSound','findOne','format','save_name','extname','converted_format','%s.%s','symlinkSync','removeSound','Sound','createdAt','unlinkSync','ValidationError','getSounds','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','util','sox','to-csv','fs-extra','lodash','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','limit','undefined','count','set','Content-Range','apply','reject','save','update','then','destroy','MusicOnHolds','UserProfileResource','error','stack','name','send','index','map','VoiceMusicOnHold','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection'];(function(_0x451735,_0x391a76){var _0x41d1e2=function(_0x3566d7){while(--_0x3566d7){_0x451735['push'](_0x451735['shift']());}};_0x41d1e2(++_0x391a76);}(_0x626f,0x116));var _0xf626=function(_0x20545f,_0x2dadb2){_0x20545f=_0x20545f-0x0;var _0x54f142=_0x626f[_0x20545f];return _0x54f142;};'use strict';var emlformat=require(_0xf626('0x0'));var rimraf=require(_0xf626('0x1'));var zipdir=require(_0xf626('0x2'));var jsonpatch=require(_0xf626('0x3'));var rp=require(_0xf626('0x4'));var moment=require(_0xf626('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xf626('0x6'));var path=require('path');var sox=require(_0xf626('0x7'));var csv=require(_0xf626('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xf626('0x9'));var _=require(_0xf626('0xa'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0xf626('0xb'));var deskjs=require(_0xf626('0xc'));var toCsv=require('to-csv');var querystring=require(_0xf626('0xd'));var Papa=require(_0xf626('0xe'));var Redis=require('ioredis');var authService=require(_0xf626('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0xf626('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xf626('0x11'))('api');var utils=require(_0xf626('0x12'));var config=require(_0xf626('0x13'));var licenseUtil=require(_0xf626('0x14'));var db=require(_0xf626('0x15'))['db'];function respondWithStatusCode(_0x1b339a,_0x24d07f){_0x24d07f=_0x24d07f||0xcc;return function(_0x24dd29){if(_0x24dd29){return _0x1b339a[_0xf626('0x16')](_0x24d07f);}return _0x1b339a[_0xf626('0x17')](_0x24d07f)['end']();};}function respondWithResult(_0x135c47,_0x3b46bb){_0x3b46bb=_0x3b46bb||0xc8;return function(_0xb3f618){if(_0xb3f618){return _0x135c47[_0xf626('0x17')](_0x3b46bb)[_0xf626('0x18')](_0xb3f618);}};}function respondWithFilteredResult(_0x4347c5,_0x1d75d6){return function(_0x1fe3d3){if(_0x1fe3d3){var _0x4a1dd1=typeof _0x1d75d6[_0xf626('0x19')]==='undefined'&&typeof _0x1d75d6[_0xf626('0x1a')]===_0xf626('0x1b');var _0x5c3efd=_0x1fe3d3[_0xf626('0x1c')];var _0x527f88=_0x4a1dd1?0x0:_0x1d75d6[_0xf626('0x19')];var _0x29f5c3=_0x4a1dd1?_0x1fe3d3['count']:_0x1d75d6[_0xf626('0x19')]+_0x1d75d6[_0xf626('0x1a')];var _0x4655cc;if(_0x29f5c3>=_0x5c3efd){_0x29f5c3=_0x5c3efd;_0x4655cc=0xc8;}else{_0x4655cc=0xce;}_0x4347c5['status'](_0x4655cc);return _0x4347c5[_0xf626('0x1d')](_0xf626('0x1e'),_0x527f88+'-'+_0x29f5c3+'/'+_0x5c3efd)[_0xf626('0x18')](_0x1fe3d3);}return null;};}function patchUpdates(_0x350b1c){return function(_0x2003df){try{jsonpatch[_0xf626('0x1f')](_0x2003df,_0x350b1c,!![]);}catch(_0x65db8f){return BPromise[_0xf626('0x20')](_0x65db8f);}return _0x2003df[_0xf626('0x21')]();};}function saveUpdates(_0x2c89a1,_0x10392b){return function(_0x5eb41e){if(_0x5eb41e){return _0x5eb41e[_0xf626('0x22')](_0x2c89a1)[_0xf626('0x23')](function(_0x561074){return _0x561074;});}return null;};}function removeEntity(_0x343f57,_0x31f269){return function(_0x4d38c7){if(_0x4d38c7){return _0x4d38c7[_0xf626('0x24')]()['then'](function(){var _0x11d14d=_0x4d38c7['get']({'plain':!![]});var _0x1a2b39=_0xf626('0x25');return db[_0xf626('0x26')][_0xf626('0x24')]({'where':{'type':_0x1a2b39,'resourceId':_0x11d14d['id']}})[_0xf626('0x23')](function(){return _0x4d38c7;});})[_0xf626('0x23')](function(){_0x343f57[_0xf626('0x17')](0xcc)['end']();});}};}function handleEntityNotFound(_0x5d1000,_0x4f41a0){return function(_0x2f483c){if(!_0x2f483c){_0x5d1000[_0xf626('0x16')](0x194);}return _0x2f483c;};}function handleError(_0x47b41c,_0x27ffb5){_0x27ffb5=_0x27ffb5||0x1f4;return function(_0x59a956){logger[_0xf626('0x27')](_0x59a956[_0xf626('0x28')]);if(_0x59a956[_0xf626('0x29')]){delete _0x59a956[_0xf626('0x29')];}_0x47b41c[_0xf626('0x17')](_0x27ffb5)[_0xf626('0x2a')](_0x59a956);};}exports[_0xf626('0x2b')]=function(_0x218728,_0x1dbd50){var _0x3b47cb={},_0x418a05={},_0x30f87c={'count':0x0,'rows':[]};var _0x3be1ea=_[_0xf626('0x2c')](db[_0xf626('0x2d')][_0xf626('0x2e')],function(_0x5df7da){return{'name':_0x5df7da[_0xf626('0x2f')],'type':_0x5df7da[_0xf626('0x30')][_0xf626('0x31')]};});_0x418a05[_0xf626('0x32')]=_['map'](_0x3be1ea,_0xf626('0x29'));_0x418a05[_0xf626('0x33')]=_[_0xf626('0x34')](_0x218728[_0xf626('0x33')]);_0x418a05[_0xf626('0x35')]=_[_0xf626('0x36')](_0x418a05[_0xf626('0x32')],_0x418a05[_0xf626('0x33')]);_0x3b47cb[_0xf626('0x37')]=_['intersection'](_0x418a05['model'],qs[_0xf626('0x38')](_0x218728[_0xf626('0x33')][_0xf626('0x38')]));_0x3b47cb[_0xf626('0x37')]=_0x3b47cb[_0xf626('0x37')][_0xf626('0x39')]?_0x3b47cb[_0xf626('0x37')]:_0x418a05[_0xf626('0x32')];if(!_0x218728['query'][_0xf626('0x3a')](_0xf626('0x3b'))){_0x3b47cb[_0xf626('0x1a')]=qs[_0xf626('0x1a')](_0x218728[_0xf626('0x33')][_0xf626('0x1a')]);_0x3b47cb[_0xf626('0x19')]=qs[_0xf626('0x19')](_0x218728[_0xf626('0x33')][_0xf626('0x19')]);}_0x3b47cb[_0xf626('0x3c')]=qs[_0xf626('0x3d')](_0x218728[_0xf626('0x33')]['sort']);delete _0x218728['query']['sort'];_0x3b47cb['where']=qs['filters'](_[_0xf626('0x3e')](_0x218728['query'],_0x418a05['filters']),_0x3be1ea);if(_0x218728[_0xf626('0x33')][_0xf626('0x3f')]){_0x3b47cb[_0xf626('0x40')]=_['merge'](_0x3b47cb[_0xf626('0x40')],{'$or':_[_0xf626('0x2c')](_0x3be1ea,function(_0x3929aa){if(_0x3929aa[_0xf626('0x30')]!==_0xf626('0x41')){var _0x5b49b7={};_0x5b49b7[_0x3929aa[_0xf626('0x29')]]={'$like':'%'+_0x218728[_0xf626('0x33')][_0xf626('0x3f')]+'%'};return _0x5b49b7;}})});}_0x3b47cb=_[_0xf626('0x42')]({},_0x3b47cb,_0x218728[_0xf626('0x43')]);var _0x44323c={'where':_0x3b47cb[_0xf626('0x40')]};return db[_0xf626('0x2d')][_0xf626('0x1c')](_0x44323c)[_0xf626('0x23')](function(_0x279887){_0x30f87c[_0xf626('0x1c')]=_0x279887;if(_0x218728[_0xf626('0x33')][_0xf626('0x44')]){_0x3b47cb[_0xf626('0x45')]=[{'all':!![]}];}return db[_0xf626('0x2d')][_0xf626('0x46')](_0x3b47cb);})[_0xf626('0x23')](function(_0x255a82){_0x30f87c[_0xf626('0x47')]=_0x255a82;return _0x30f87c;})[_0xf626('0x23')](respondWithFilteredResult(_0x1dbd50,_0x3b47cb))[_0xf626('0x48')](handleError(_0x1dbd50,null));};exports[_0xf626('0x49')]=function(_0x2d413e,_0x4abe05){var _0x2f5b11={'raw':![],'where':{'id':_0x2d413e[_0xf626('0x4a')]['id']}},_0x34fd7f={};_0x34fd7f[_0xf626('0x32')]=_[_0xf626('0x34')](db[_0xf626('0x2d')]['rawAttributes']);_0x34fd7f[_0xf626('0x33')]=_[_0xf626('0x34')](_0x2d413e[_0xf626('0x33')]);_0x34fd7f[_0xf626('0x35')]=_['intersection'](_0x34fd7f[_0xf626('0x32')],_0x34fd7f['query']);_0x2f5b11[_0xf626('0x37')]=_[_0xf626('0x36')](_0x34fd7f[_0xf626('0x32')],qs[_0xf626('0x38')](_0x2d413e[_0xf626('0x33')][_0xf626('0x38')]));_0x2f5b11[_0xf626('0x37')]=_0x2f5b11[_0xf626('0x37')]['length']?_0x2f5b11[_0xf626('0x37')]:_0x34fd7f[_0xf626('0x32')];if(_0x2d413e['query'][_0xf626('0x44')]){_0x2f5b11[_0xf626('0x45')]=[{'all':!![]}];}_0x2f5b11=_[_0xf626('0x42')]({},_0x2f5b11,_0x2d413e[_0xf626('0x43')]);return db[_0xf626('0x2d')][_0xf626('0x4b')](_0x2f5b11)[_0xf626('0x23')](handleEntityNotFound(_0x4abe05,null))['then'](respondWithResult(_0x4abe05,null))['catch'](handleError(_0x4abe05,null));};exports[_0xf626('0x22')]=function(_0x79fc35,_0x5201c0){if(_0x79fc35[_0xf626('0x4c')]['id']){delete _0x79fc35[_0xf626('0x4c')]['id'];}return db['VoiceMusicOnHold'][_0xf626('0x4b')]({'where':{'id':_0x79fc35['params']['id']}})[_0xf626('0x23')](handleEntityNotFound(_0x5201c0,null))[_0xf626('0x23')](saveUpdates(_0x79fc35[_0xf626('0x4c')],null))['then'](respondWithResult(_0x5201c0,null))['catch'](handleError(_0x5201c0,null));};exports['create']=function(_0x24b820,_0x24fef2,_0x261301){return db[_0xf626('0x4d')]['transaction'](function(_0xeb9cb3){if(_0x24b820[_0xf626('0x4c')][_0xf626('0x4e')]===_0xf626('0x4f')){_0x24b820['body'][_0xf626('0x50')]=path[_0xf626('0x51')](config[_0xf626('0x52')],_0xf626('0x53'),_0x24b820['body']['name']);}return db[_0xf626('0x2d')][_0xf626('0x54')](_0x24b820[_0xf626('0x4c')],{'transaction':_0xeb9cb3})[_0xf626('0x23')](function(_0x15012c){if(_0x24b820['body'][_0xf626('0x4e')]===_0xf626('0x4f')){fs[_0xf626('0x55')](_0x15012c[_0xf626('0x50')],parseInt(_0xf626('0x56'),0x8));}var _0x2bc35f=_0x24b820[_0xf626('0x57')][_0xf626('0x58')]({'plain':!![]});if(!_0x2bc35f)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x2bc35f[_0xf626('0x59')]===_0xf626('0x57')){var _0x1f9361=_0x15012c[_0xf626('0x58')]({'plain':!![]});return db['UserProfileSection']['find']({'where':{'name':_0xf626('0x25'),'userProfileId':_0x2bc35f['userProfileId']},'raw':!![]})['then'](function(_0x4c7490){if(_0x4c7490&&_0x4c7490[_0xf626('0x5a')]===0x0){return db[_0xf626('0x26')][_0xf626('0x54')]({'name':_0x1f9361[_0xf626('0x29')],'resourceId':_0x1f9361['id'],'type':_0x4c7490[_0xf626('0x29')],'sectionId':_0x4c7490['id']},{})['then'](function(){return _0x15012c;});}else{return _0x15012c;}})[_0xf626('0x48')](function(_0x4b9630){logger[_0xf626('0x27')](_0xf626('0x5b'),_0x4b9630);});}return _0x15012c;});})[_0xf626('0x23')](respondWithResult(_0x24fef2,0xc9))[_0xf626('0x48')](handleError(_0x24fef2,null));};exports[_0xf626('0x24')]=function(_0x183e3f,_0x1098bd,_0x1c4876){var _0x5f164d;return db[_0xf626('0x2d')][_0xf626('0x4b')]({'where':{'id':_0x183e3f[_0xf626('0x4a')]['id']}})[_0xf626('0x23')](function(_0x106202){if(_0x106202){return db[_0xf626('0x4d')][_0xf626('0x5c')](function(_0x374f21){return _0x106202[_0xf626('0x24')]({'transaction':_0x374f21,'raw':!![]})[_0xf626('0x23')](function(_0x3f125e){if(_0x3f125e[_0xf626('0x50')]){fs[_0xf626('0x5d')](_0x3f125e[_0xf626('0x50')])['forEach'](function(_0x12e83f){fs['unlinkSync'](path[_0xf626('0x51')](_0x3f125e['directory'],_0x12e83f));});fs[_0xf626('0x5e')](_0x3f125e[_0xf626('0x50')]);}return _0x3f125e;});})[_0xf626('0x48')](function(_0x3c47bc){throw new db[(_0xf626('0x5f'))]['ValidationError'](_0x3c47bc[_0xf626('0x60')]);});}})[_0xf626('0x23')](handleEntityNotFound(_0x1098bd,null))['then'](removeEntity(_0x1098bd,null))[_0xf626('0x48')](handleError(_0x1098bd,null));};exports[_0xf626('0x61')]=function(_0x52a52b,_0x1a7901,_0x621c59){return db[_0xf626('0x4d')][_0xf626('0x5c')](function(_0x476cfc){var _0x14eab5;return db['VoiceMusicOnHold'][_0xf626('0x4b')]({'where':{'id':_0x52a52b[_0xf626('0x4a')]['id']}})[_0xf626('0x23')](function(_0x82ed16){if(_0x82ed16){_0x14eab5=_0x82ed16;return _0x82ed16[_0xf626('0x61')](_0x52a52b[_0xf626('0x4c')]['id'],{'transaction':_0x476cfc});}})[_0xf626('0x23')](function(_0x58a5a8){if(_0x58a5a8){return db['Sound'][_0xf626('0x62')]({'where':{'id':_0x52a52b[_0xf626('0x4c')]['id']},'attributes':['id',_0xf626('0x29'),'save_name','converted_format','createdAt'],'raw':!![]});}})['then'](function(_0x25ce54){if(_0x25ce54){var _0x405432=path[_0xf626('0x51')](config[_0xf626('0x52')],'server/files/sounds/converted',util[_0xf626('0x63')]('%s.%s',path['basename'](_0x25ce54[_0xf626('0x64')],path[_0xf626('0x65')](_0x25ce54[_0xf626('0x64')])),_0x25ce54[_0xf626('0x66')]));var _0x144d69=path[_0xf626('0x51')](config[_0xf626('0x52')],_0xf626('0x53'),_0x14eab5[_0xf626('0x29')],util[_0xf626('0x63')](_0xf626('0x67'),_0x25ce54[_0xf626('0x29')],_0x25ce54['converted_format']));fs[_0xf626('0x68')](_0x405432,_0x144d69);return _0x25ce54;}})[_0xf626('0x48')](function(_0x1f6db6){throw new db[(_0xf626('0x5f'))]['ValidationError'](_0x1f6db6[_0xf626('0x60')]);});})[_0xf626('0x23')](handleEntityNotFound(_0x1a7901,null))['then'](respondWithResult(_0x1a7901,0xc9))[_0xf626('0x48')](handleError(_0x1a7901,null));};exports[_0xf626('0x69')]=function(_0x50d20b,_0x511355,_0x459f36){return db['sequelize'][_0xf626('0x5c')](function(_0x5a10ca){var _0x412609;return db[_0xf626('0x2d')][_0xf626('0x62')]({'where':{'id':_0x50d20b['params']['id']}})[_0xf626('0x23')](handleEntityNotFound(_0x511355,null))[_0xf626('0x23')](function(_0x173a80){if(_0x173a80){_0x412609=_0x173a80;return _0x173a80[_0xf626('0x69')](_0x50d20b[_0xf626('0x4a')]['id2']);}})[_0xf626('0x23')](function(_0x4a8748){if(_0x4a8748){return db[_0xf626('0x6a')][_0xf626('0x62')]({'where':{'id':_0x50d20b['params']['id2']},'attributes':['id','name',_0xf626('0x64'),'converted_format',_0xf626('0x6b')],'raw':!![]});}})[_0xf626('0x23')](function(_0x43fdd1){if(_0x43fdd1){fs[_0xf626('0x6c')](path[_0xf626('0x51')](_0x412609[_0xf626('0x50')],util[_0xf626('0x63')](_0xf626('0x67'),_0x43fdd1['name'],_0x43fdd1['converted_format'])));return _0x43fdd1;}})[_0xf626('0x48')](function(_0xab581d){throw new db[(_0xf626('0x5f'))][(_0xf626('0x6d'))](_0xab581d[_0xf626('0x60')]);});})[_0xf626('0x23')](handleEntityNotFound(_0x511355,null))[_0xf626('0x23')](respondWithStatusCode(_0x511355,0xcc))[_0xf626('0x48')](handleError(_0x511355,null));};exports['getSounds']=function(_0x57c797,_0x481260,_0x248612){var _0x345310={};var _0x2cc8bd={};var _0x4d6fe8;var _0x83b746;return db[_0xf626('0x2d')][_0xf626('0x62')]({'where':{'id':_0x57c797[_0xf626('0x4a')]['id']}})[_0xf626('0x23')](handleEntityNotFound(_0x481260,null))[_0xf626('0x23')](function(_0x451d12){if(_0x451d12){_0x4d6fe8=_0x451d12;_0x2cc8bd[_0xf626('0x32')]=_[_0xf626('0x34')](db[_0xf626('0x6a')][_0xf626('0x2e')]);_0x2cc8bd[_0xf626('0x33')]=_[_0xf626('0x34')](_0x57c797[_0xf626('0x33')]);_0x2cc8bd[_0xf626('0x35')]=_[_0xf626('0x36')](_0x2cc8bd[_0xf626('0x32')],_0x2cc8bd[_0xf626('0x33')]);_0x345310[_0xf626('0x37')]=_[_0xf626('0x36')](_0x2cc8bd[_0xf626('0x32')],qs['fields'](_0x57c797['query'][_0xf626('0x38')]));_0x345310[_0xf626('0x37')]=_0x345310[_0xf626('0x37')][_0xf626('0x39')]?_0x345310[_0xf626('0x37')]:_0x2cc8bd[_0xf626('0x32')];_0x345310[_0xf626('0x3c')]=qs[_0xf626('0x3d')](_0x57c797[_0xf626('0x33')][_0xf626('0x3d')]);_0x345310[_0xf626('0x40')]=qs[_0xf626('0x35')](_[_0xf626('0x3e')](_0x57c797[_0xf626('0x33')],_0x2cc8bd[_0xf626('0x35')]));if(_0x57c797[_0xf626('0x33')][_0xf626('0x3f')]){_0x345310['where']=_['merge'](_0x345310['where'],{'$or':_[_0xf626('0x2c')](_0x345310['attributes'],function(_0x4f19dd){var _0x526305={};_0x526305[_0x4f19dd]={'$like':'%'+_0x57c797[_0xf626('0x33')]['filter']+'%'};return _0x526305;})});}_0x345310=_[_0xf626('0x42')]({},_0x345310,_0x57c797['options']);return _0x4d6fe8[_0xf626('0x6e')](_0x345310);}})[_0xf626('0x23')](function(_0x139e9a){if(_0x139e9a){_0x83b746=_0x139e9a['length'];if(!_0x57c797['query'][_0xf626('0x3a')](_0xf626('0x3b'))){_0x345310[_0xf626('0x1a')]=qs[_0xf626('0x1a')](_0x57c797[_0xf626('0x33')]['limit']);_0x345310[_0xf626('0x19')]=qs[_0xf626('0x19')](_0x57c797[_0xf626('0x33')][_0xf626('0x19')]);}return _0x4d6fe8[_0xf626('0x6e')](_0x345310);}})['then'](function(_0x59eda3){if(_0x59eda3){return _0x59eda3?{'count':_0x83b746,'rows':_0x59eda3}:null;}})['then'](respondWithResult(_0x481260,null))['catch'](handleError(_0x481260,null));};