Built motion from commit d5e4af8c.|2.6.23
[motion2.git] / server / api / voiceContext / voiceContext.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 _0x0bf9=['root','server/files/templates/context.ejs','utf8','writeFileSync','/etc/asterisk/extensions_xcally_contexts.conf','Reload','chan_sip.so','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','render','params','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','client','http','request','then','info','VoiceContext,\x20%s,\x20%s','request\x20sent','debug','VoiceContext,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','destroy','get','name','send','index','VoiceContext','rawAttributes','type','key','model','map','query','intersection','fields','attributes','hasOwnProperty','order','sort','filters','pick','where','merge','VIRTUAL','include','findAll','rows','show','keys','length','includeAll','find','create','body','readFileSync','join'];(function(_0xafde4a,_0x14e260){var _0x4199fb=function(_0xc5db4d){while(--_0xc5db4d){_0xafde4a['push'](_0xafde4a['shift']());}};_0x4199fb(++_0x14e260);}(_0x0bf9,0xe3));var _0x90bf=function(_0x7abf43,_0x2efe0c){_0x7abf43=_0x7abf43-0x0;var _0x365cb5=_0x0bf9[_0x7abf43];return _0x365cb5;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x90bf('0x0'));var rp=require(_0x90bf('0x1'));var moment=require(_0x90bf('0x2'));var BPromise=require(_0x90bf('0x3'));var Mustache=require(_0x90bf('0x4'));var util=require(_0x90bf('0x5'));var path=require(_0x90bf('0x6'));var sox=require(_0x90bf('0x7'));var csv=require(_0x90bf('0x8'));var ejs=require(_0x90bf('0x9'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x90bf('0xa'));var squel=require(_0x90bf('0xb'));var crypto=require(_0x90bf('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x90bf('0xd'));var toCsv=require(_0x90bf('0x8'));var querystring=require(_0x90bf('0xe'));var Papa=require('papaparse');var Redis=require(_0x90bf('0xf'));var authService=require(_0x90bf('0x10'));var qs=require(_0x90bf('0x11'));var as=require(_0x90bf('0x12'));var hardwareService=require(_0x90bf('0x13'));var logger=require(_0x90bf('0x14'))(_0x90bf('0x15'));var utils=require(_0x90bf('0x16'));var config=require('../../config/environment');var licenseUtil=require(_0x90bf('0x17'));var db=require(_0x90bf('0x18'))['db'];var jayson=require('jayson/promise');var client=jayson[_0x90bf('0x19')][_0x90bf('0x1a')]({'port':0x232a});function respondWithRpcPromise(_0x36ee85,_0x2c077d,_0x2004b0,_0x56c671){return new BPromise(function(_0x1fd823,_0x23d1c0){var _0x31d6c9=_0x56c671||client;return _0x31d6c9[_0x90bf('0x1b')](_0x36ee85,_0x2004b0)[_0x90bf('0x1c')](function(_0x4c1f0e){logger[_0x90bf('0x1d')](_0x90bf('0x1e'),_0x2c077d,_0x90bf('0x1f'));logger[_0x90bf('0x20')](_0x90bf('0x21'),_0x2c077d,_0x90bf('0x1f'),JSON[_0x90bf('0x22')](_0x4c1f0e));if(_0x4c1f0e[_0x90bf('0x23')]){if(_0x4c1f0e['error'][_0x90bf('0x24')]===0x1f4){logger[_0x90bf('0x23')](_0x90bf('0x1e'),_0x2c077d,_0x4c1f0e[_0x90bf('0x23')][_0x90bf('0x25')]);return _0x23d1c0(_0x4c1f0e[_0x90bf('0x23')]['message']);}logger[_0x90bf('0x23')](_0x90bf('0x1e'),_0x2c077d,_0x4c1f0e[_0x90bf('0x23')][_0x90bf('0x25')]);return _0x1fd823(_0x4c1f0e[_0x90bf('0x23')][_0x90bf('0x25')]);}else{logger[_0x90bf('0x1d')](_0x90bf('0x1e'),_0x2c077d,'request\x20sent');_0x1fd823(_0x4c1f0e[_0x90bf('0x26')][_0x90bf('0x25')]);}})[_0x90bf('0x27')](function(_0x28a02a){logger[_0x90bf('0x23')](_0x90bf('0x1e'),_0x2c077d,_0x28a02a);_0x23d1c0(_0x28a02a);});});}function respondWithStatusCode(_0x158d27,_0x5a4d2f){_0x5a4d2f=_0x5a4d2f||0xcc;return function(_0x52b51f){if(_0x52b51f){return _0x158d27[_0x90bf('0x28')](_0x5a4d2f);}return _0x158d27[_0x90bf('0x29')](_0x5a4d2f)[_0x90bf('0x2a')]();};}function respondWithResult(_0x58e40d,_0x22fe18){_0x22fe18=_0x22fe18||0xc8;return function(_0x477c77){if(_0x477c77){return _0x58e40d[_0x90bf('0x29')](_0x22fe18)[_0x90bf('0x2b')](_0x477c77);}};}function respondWithFilteredResult(_0x2f77ef,_0x4060c9){return function(_0x2aa241){if(_0x2aa241){var _0x580592=typeof _0x4060c9[_0x90bf('0x2c')]===_0x90bf('0x2d')&&typeof _0x4060c9[_0x90bf('0x2e')]===_0x90bf('0x2d');var _0x51b52b=_0x2aa241[_0x90bf('0x2f')];var _0x37fd8a=_0x580592?0x0:_0x4060c9[_0x90bf('0x2c')];var _0xce4e28=_0x580592?_0x2aa241[_0x90bf('0x2f')]:_0x4060c9['offset']+_0x4060c9[_0x90bf('0x2e')];var _0x10b1e3;if(_0xce4e28>=_0x51b52b){_0xce4e28=_0x51b52b;_0x10b1e3=0xc8;}else{_0x10b1e3=0xce;}_0x2f77ef[_0x90bf('0x29')](_0x10b1e3);return _0x2f77ef[_0x90bf('0x30')](_0x90bf('0x31'),_0x37fd8a+'-'+_0xce4e28+'/'+_0x51b52b)[_0x90bf('0x2b')](_0x2aa241);}return null;};}function patchUpdates(_0x196bd2){return function(_0x23517c){try{jsonpatch['apply'](_0x23517c,_0x196bd2,!![]);}catch(_0x5316d6){return BPromise[_0x90bf('0x32')](_0x5316d6);}return _0x23517c[_0x90bf('0x33')]();};}function saveUpdates(_0x3d39c8,_0x210b09){return function(_0x1b4748){if(_0x1b4748){return _0x1b4748[_0x90bf('0x34')](_0x3d39c8)[_0x90bf('0x1c')](function(_0xa72b7a){return _0xa72b7a;});}return null;};}function removeEntity(_0x490b58,_0x3ee594){return function(_0xc43c62){if(_0xc43c62){return _0xc43c62[_0x90bf('0x35')]()[_0x90bf('0x1c')](function(){var _0x441c86=_0xc43c62[_0x90bf('0x36')]({'plain':!![]});var _0x24b6da='Contexts';return db['UserProfileResource'][_0x90bf('0x35')]({'where':{'type':_0x24b6da,'resourceId':_0x441c86['id']}})[_0x90bf('0x1c')](function(){return _0xc43c62;});})[_0x90bf('0x1c')](function(){_0x490b58[_0x90bf('0x29')](0xcc)['end']();});}};}function handleEntityNotFound(_0x53b089,_0x30b801){return function(_0x3cdf9d){if(!_0x3cdf9d){_0x53b089[_0x90bf('0x28')](0x194);}return _0x3cdf9d;};}function handleError(_0x3a3a5b,_0x2d513b){_0x2d513b=_0x2d513b||0x1f4;return function(_0x4c2358){logger[_0x90bf('0x23')](_0x4c2358['stack']);if(_0x4c2358[_0x90bf('0x37')]){delete _0x4c2358[_0x90bf('0x37')];}_0x3a3a5b[_0x90bf('0x29')](_0x2d513b)[_0x90bf('0x38')](_0x4c2358);};}exports[_0x90bf('0x39')]=function(_0x21e15f,_0x5d2a91){var _0xb1eeb9={},_0x2f678a={},_0x4c5899={'count':0x0,'rows':[]};var _0x50be25=_['map'](db[_0x90bf('0x3a')][_0x90bf('0x3b')],function(_0x15b14c){return{'name':_0x15b14c['fieldName'],'type':_0x15b14c[_0x90bf('0x3c')][_0x90bf('0x3d')]};});_0x2f678a[_0x90bf('0x3e')]=_[_0x90bf('0x3f')](_0x50be25,_0x90bf('0x37'));_0x2f678a[_0x90bf('0x40')]=_['keys'](_0x21e15f[_0x90bf('0x40')]);_0x2f678a['filters']=_[_0x90bf('0x41')](_0x2f678a[_0x90bf('0x3e')],_0x2f678a['query']);_0xb1eeb9['attributes']=_[_0x90bf('0x41')](_0x2f678a[_0x90bf('0x3e')],qs['fields'](_0x21e15f[_0x90bf('0x40')][_0x90bf('0x42')]));_0xb1eeb9[_0x90bf('0x43')]=_0xb1eeb9['attributes']['length']?_0xb1eeb9[_0x90bf('0x43')]:_0x2f678a['model'];if(!_0x21e15f[_0x90bf('0x40')][_0x90bf('0x44')]('nolimit')){_0xb1eeb9[_0x90bf('0x2e')]=qs[_0x90bf('0x2e')](_0x21e15f[_0x90bf('0x40')]['limit']);_0xb1eeb9[_0x90bf('0x2c')]=qs[_0x90bf('0x2c')](_0x21e15f[_0x90bf('0x40')][_0x90bf('0x2c')]);}_0xb1eeb9[_0x90bf('0x45')]=qs[_0x90bf('0x46')](_0x21e15f[_0x90bf('0x40')][_0x90bf('0x46')]);_0xb1eeb9['where']=qs[_0x90bf('0x47')](_[_0x90bf('0x48')](_0x21e15f[_0x90bf('0x40')],_0x2f678a[_0x90bf('0x47')]),_0x50be25);if(_0x21e15f['query']['filter']){_0xb1eeb9[_0x90bf('0x49')]=_[_0x90bf('0x4a')](_0xb1eeb9[_0x90bf('0x49')],{'$or':_[_0x90bf('0x3f')](_0x50be25,function(_0x39d34f){if(_0x39d34f[_0x90bf('0x3c')]!==_0x90bf('0x4b')){var _0x135016={};_0x135016[_0x39d34f['name']]={'$like':'%'+_0x21e15f[_0x90bf('0x40')]['filter']+'%'};return _0x135016;}})});}_0xb1eeb9=_['merge']({},_0xb1eeb9,_0x21e15f['options']);var _0x43d538={'where':_0xb1eeb9[_0x90bf('0x49')]};return db[_0x90bf('0x3a')][_0x90bf('0x2f')](_0x43d538)[_0x90bf('0x1c')](function(_0x2a50f2){_0x4c5899[_0x90bf('0x2f')]=_0x2a50f2;if(_0x21e15f['query']['includeAll']){_0xb1eeb9[_0x90bf('0x4c')]=[{'all':!![]}];}return db[_0x90bf('0x3a')][_0x90bf('0x4d')](_0xb1eeb9);})[_0x90bf('0x1c')](function(_0x4cc6d3){_0x4c5899[_0x90bf('0x4e')]=_0x4cc6d3;return _0x4c5899;})[_0x90bf('0x1c')](respondWithFilteredResult(_0x5d2a91,_0xb1eeb9))[_0x90bf('0x27')](handleError(_0x5d2a91,null));};exports[_0x90bf('0x4f')]=function(_0x453a07,_0x162ca0){var _0x35c43b={'raw':![],'where':{'id':_0x453a07['params']['id']}},_0x2536c2={};_0x2536c2['model']=_[_0x90bf('0x50')](db['VoiceContext'][_0x90bf('0x3b')]);_0x2536c2[_0x90bf('0x40')]=_[_0x90bf('0x50')](_0x453a07['query']);_0x2536c2['filters']=_[_0x90bf('0x41')](_0x2536c2[_0x90bf('0x3e')],_0x2536c2[_0x90bf('0x40')]);_0x35c43b['attributes']=_['intersection'](_0x2536c2['model'],qs[_0x90bf('0x42')](_0x453a07['query'][_0x90bf('0x42')]));_0x35c43b[_0x90bf('0x43')]=_0x35c43b['attributes'][_0x90bf('0x51')]?_0x35c43b['attributes']:_0x2536c2[_0x90bf('0x3e')];if(_0x453a07[_0x90bf('0x40')][_0x90bf('0x52')]){_0x35c43b[_0x90bf('0x4c')]=[{'all':!![]}];}_0x35c43b=_[_0x90bf('0x4a')]({},_0x35c43b,_0x453a07['options']);return db[_0x90bf('0x3a')][_0x90bf('0x53')](_0x35c43b)['then'](handleEntityNotFound(_0x162ca0,null))[_0x90bf('0x1c')](respondWithResult(_0x162ca0,null))[_0x90bf('0x27')](handleError(_0x162ca0,null));};exports[_0x90bf('0x54')]=function(_0x256475,_0x4107f6,_0x4367d1){var _0x388546;return db[_0x90bf('0x3a')][_0x90bf('0x54')](_0x256475[_0x90bf('0x55')],{'raw':!![]})[_0x90bf('0x1c')](function(_0x3e89ab){_0x388546=_0x3e89ab;return db['VoiceContext'][_0x90bf('0x4d')]({'raw':!![]});})[_0x90bf('0x1c')](function(_0x3309c5){var _0x4a205b=fs[_0x90bf('0x56')](path[_0x90bf('0x57')](config[_0x90bf('0x58')],_0x90bf('0x59')),_0x90bf('0x5a'));var _0x7b605d=ejs['render'](_0x4a205b,{'contexts':_0x3309c5||[]});fs[_0x90bf('0x5b')](_0x90bf('0x5c'),_0x7b605d);})['then'](function(){return respondWithRpcPromise(_0x90bf('0x5d'),_0x90bf('0x54'),{'module':_0x90bf('0x5e')});})[_0x90bf('0x1c')](function(){var _0x55d3a0=_0x256475[_0x90bf('0x5f')][_0x90bf('0x36')]({'plain':!![]});if(!_0x55d3a0)throw new Error(_0x90bf('0x60'));if(_0x55d3a0[_0x90bf('0x61')]===_0x90bf('0x5f')){var _0x55fd5d=_0x388546[_0x90bf('0x36')]({'plain':!![]});return db[_0x90bf('0x62')]['find']({'where':{'name':'Contexts','userProfileId':_0x55d3a0[_0x90bf('0x63')]},'raw':!![]})[_0x90bf('0x1c')](function(_0x3aa314){if(_0x3aa314&&_0x3aa314[_0x90bf('0x64')]===0x0){return db[_0x90bf('0x65')][_0x90bf('0x54')]({'name':_0x55fd5d['name'],'resourceId':_0x55fd5d['id'],'type':_0x3aa314[_0x90bf('0x37')],'sectionId':_0x3aa314['id']},{})[_0x90bf('0x1c')](function(){return _0x388546;});}else{return _0x388546;}})[_0x90bf('0x27')](function(_0x1707d1){logger[_0x90bf('0x23')](_0x90bf('0x66'),_0x1707d1);throw _0x1707d1;});}return _0x388546;})[_0x90bf('0x1c')](respondWithResult(_0x4107f6,0xc9))['catch'](handleError(_0x4107f6,null));};exports[_0x90bf('0x34')]=function(_0x81b4ab,_0x2f9329,_0x257c50){var _0x588a82;return db[_0x90bf('0x3a')]['find']({'where':{'id':_0x81b4ab['params']['id']}})['then'](handleEntityNotFound(_0x2f9329,null))[_0x90bf('0x1c')](saveUpdates(_0x81b4ab[_0x90bf('0x55')],null))[_0x90bf('0x1c')](function(_0x41815a){if(_0x41815a){_0x588a82=_0x41815a;return db[_0x90bf('0x3a')][_0x90bf('0x4d')]({'raw':!![]});}})[_0x90bf('0x1c')](function(_0x267b75){var _0x5c02f1=fs[_0x90bf('0x56')](path[_0x90bf('0x57')](config[_0x90bf('0x58')],_0x90bf('0x59')),_0x90bf('0x5a'));var _0x230a68=ejs[_0x90bf('0x67')](_0x5c02f1,{'contexts':_0x267b75||[]});fs[_0x90bf('0x5b')](_0x90bf('0x5c'),_0x230a68);})[_0x90bf('0x1c')](function(){return respondWithRpcPromise(_0x90bf('0x5d'),'update',{'module':'chan_sip.so'});})['then'](function(){return _0x588a82;})[_0x90bf('0x1c')](respondWithResult(_0x2f9329,null))['catch'](handleError(_0x2f9329,null));};exports['destroy']=function(_0x2fe228,_0x187d4b,_0x5ca4e4){var _0x3d66ab;return db[_0x90bf('0x3a')][_0x90bf('0x53')]({'where':{'id':_0x2fe228[_0x90bf('0x68')]['id']}})[_0x90bf('0x1c')](handleEntityNotFound(_0x187d4b,null))[_0x90bf('0x1c')](removeEntity(_0x187d4b,null))[_0x90bf('0x1c')](function(){return db[_0x90bf('0x3a')]['findAll']({'raw':!![]});})[_0x90bf('0x1c')](function(_0x48f7a3){var _0xb92580=fs[_0x90bf('0x56')](path[_0x90bf('0x57')](config[_0x90bf('0x58')],_0x90bf('0x59')),'utf8');var _0x31088c=ejs[_0x90bf('0x67')](_0xb92580,{'contexts':_0x48f7a3});fs[_0x90bf('0x5b')]('/etc/asterisk/extensions_xcally_contexts.conf',_0x31088c);return respondWithRpcPromise(_0x90bf('0x5d'),_0x90bf('0x35'),{'module':_0x90bf('0x5e')});})[_0x90bf('0x27')](handleError(_0x187d4b,null));};