Built motion from commit 9d1906d1.|2.5.40
[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 _0xf058=['Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','readFileSync','server/files/templates/context.ejs','join','rimraf','zip-dir','moment','bluebird','mustache','path','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','jayson/promise','client','http','then','info','request\x20sent','debug','stringify','error','code','VoiceContext,\x20%s,\x20%s','message','catch','sendStatus','end','status','offset','undefined','limit','count','set','Content-Range','json','apply','reject','save','update','get','Contexts','UserProfileResource','destroy','name','send','index','map','rawAttributes','fieldName','type','key','model','query','filters','intersection','fields','attributes','length','hasOwnProperty','sort','where','filter','VIRTUAL','merge','options','VoiceContext','include','findAll','rows','params','keys','includeAll','find','create','body','root','utf8','render','writeFileSync','/etc/asterisk/extensions_xcally_contexts.conf','Reload','chan_sip.so','user'];(function(_0x3d9536,_0x1d4bdd){var _0x17b9e9=function(_0x382ef9){while(--_0x382ef9){_0x3d9536['push'](_0x3d9536['shift']());}};_0x17b9e9(++_0x1d4bdd);}(_0xf058,0x72));var _0x8f05=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xf058[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x8f05('0x0'));var zipdir=require(_0x8f05('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x8f05('0x2'));var BPromise=require(_0x8f05('0x3'));var Mustache=require(_0x8f05('0x4'));var util=require('util');var path=require(_0x8f05('0x5'));var sox=require('sox');var csv=require(_0x8f05('0x6'));var ejs=require(_0x8f05('0x7'));var fs=require('fs');var fs_extra=require(_0x8f05('0x8'));var _=require(_0x8f05('0x9'));var squel=require(_0x8f05('0xa'));var crypto=require(_0x8f05('0xb'));var jsforce=require(_0x8f05('0xc'));var deskjs=require(_0x8f05('0xd'));var toCsv=require('to-csv');var querystring=require(_0x8f05('0xe'));var Papa=require(_0x8f05('0xf'));var Redis=require('ioredis');var authService=require(_0x8f05('0x10'));var qs=require(_0x8f05('0x11'));var as=require(_0x8f05('0x12'));var hardwareService=require(_0x8f05('0x13'));var logger=require(_0x8f05('0x14'))(_0x8f05('0x15'));var utils=require(_0x8f05('0x16'));var config=require(_0x8f05('0x17'));var licenseUtil=require(_0x8f05('0x18'));var db=require('../../mysqldb')['db'];var jayson=require(_0x8f05('0x19'));var client=jayson[_0x8f05('0x1a')][_0x8f05('0x1b')]({'port':0x232a});function respondWithRpcPromise(_0x5a5952,_0x22af07,_0x3acbf9,_0x7b5435){return new BPromise(function(_0x323b36,_0x2f7965){var _0xfd6905=_0x7b5435||client;return _0xfd6905['request'](_0x5a5952,_0x3acbf9)[_0x8f05('0x1c')](function(_0x1e08c6){logger[_0x8f05('0x1d')]('VoiceContext,\x20%s,\x20%s',_0x22af07,_0x8f05('0x1e'));logger[_0x8f05('0x1f')]('VoiceContext,\x20%s,\x20%s,\x20%s',_0x22af07,_0x8f05('0x1e'),JSON[_0x8f05('0x20')](_0x1e08c6));if(_0x1e08c6[_0x8f05('0x21')]){if(_0x1e08c6[_0x8f05('0x21')][_0x8f05('0x22')]===0x1f4){logger[_0x8f05('0x21')](_0x8f05('0x23'),_0x22af07,_0x1e08c6[_0x8f05('0x21')][_0x8f05('0x24')]);return _0x2f7965(_0x1e08c6[_0x8f05('0x21')]['message']);}logger[_0x8f05('0x21')](_0x8f05('0x23'),_0x22af07,_0x1e08c6[_0x8f05('0x21')][_0x8f05('0x24')]);return _0x323b36(_0x1e08c6[_0x8f05('0x21')]['message']);}else{logger[_0x8f05('0x1d')]('VoiceContext,\x20%s,\x20%s',_0x22af07,_0x8f05('0x1e'));_0x323b36(_0x1e08c6['result'][_0x8f05('0x24')]);}})[_0x8f05('0x25')](function(_0x50f9bd){logger['error'](_0x8f05('0x23'),_0x22af07,_0x50f9bd);_0x2f7965(_0x50f9bd);});});}function respondWithStatusCode(_0x5d335b,_0x1278f6){_0x1278f6=_0x1278f6||0xcc;return function(_0x5e65cb){if(_0x5e65cb){return _0x5d335b[_0x8f05('0x26')](_0x1278f6);}return _0x5d335b['status'](_0x1278f6)[_0x8f05('0x27')]();};}function respondWithResult(_0x42b4a6,_0x56f19d){_0x56f19d=_0x56f19d||0xc8;return function(_0x3177f1){if(_0x3177f1){return _0x42b4a6[_0x8f05('0x28')](_0x56f19d)['json'](_0x3177f1);}};}function respondWithFilteredResult(_0x350b42,_0x56996c){return function(_0x23efaf){if(_0x23efaf){var _0x38a6d0=typeof _0x56996c[_0x8f05('0x29')]===_0x8f05('0x2a')&&typeof _0x56996c[_0x8f05('0x2b')]==='undefined';var _0x149eae=_0x23efaf[_0x8f05('0x2c')];var _0x1d4a93=_0x38a6d0?0x0:_0x56996c['offset'];var _0x3ddedb=_0x38a6d0?_0x23efaf[_0x8f05('0x2c')]:_0x56996c[_0x8f05('0x29')]+_0x56996c[_0x8f05('0x2b')];var _0x2be01f;if(_0x3ddedb>=_0x149eae){_0x3ddedb=_0x149eae;_0x2be01f=0xc8;}else{_0x2be01f=0xce;}_0x350b42[_0x8f05('0x28')](_0x2be01f);return _0x350b42[_0x8f05('0x2d')](_0x8f05('0x2e'),_0x1d4a93+'-'+_0x3ddedb+'/'+_0x149eae)[_0x8f05('0x2f')](_0x23efaf);}return null;};}function patchUpdates(_0x1ed6ff){return function(_0x28e156){try{jsonpatch[_0x8f05('0x30')](_0x28e156,_0x1ed6ff,!![]);}catch(_0x5691c7){return BPromise[_0x8f05('0x31')](_0x5691c7);}return _0x28e156[_0x8f05('0x32')]();};}function saveUpdates(_0x2f8119,_0x340d45){return function(_0x3d00d7){if(_0x3d00d7){return _0x3d00d7[_0x8f05('0x33')](_0x2f8119)[_0x8f05('0x1c')](function(_0x257cc3){return _0x257cc3;});}return null;};}function removeEntity(_0x57bdc1,_0x12585f){return function(_0x40b7af){if(_0x40b7af){return _0x40b7af['destroy']()[_0x8f05('0x1c')](function(){var _0x5f50b1=_0x40b7af[_0x8f05('0x34')]({'plain':!![]});var _0xf3a903=_0x8f05('0x35');return db[_0x8f05('0x36')][_0x8f05('0x37')]({'where':{'type':_0xf3a903,'resourceId':_0x5f50b1['id']}})[_0x8f05('0x1c')](function(){return _0x40b7af;});})[_0x8f05('0x1c')](function(){_0x57bdc1[_0x8f05('0x28')](0xcc)[_0x8f05('0x27')]();});}};}function handleEntityNotFound(_0x21b5c1,_0x477200){return function(_0xceb57a){if(!_0xceb57a){_0x21b5c1['sendStatus'](0x194);}return _0xceb57a;};}function handleError(_0x1f4fa9,_0x386d3c){_0x386d3c=_0x386d3c||0x1f4;return function(_0x3bd357){logger[_0x8f05('0x21')](_0x3bd357['stack']);if(_0x3bd357['name']){delete _0x3bd357[_0x8f05('0x38')];}_0x1f4fa9[_0x8f05('0x28')](_0x386d3c)[_0x8f05('0x39')](_0x3bd357);};}exports[_0x8f05('0x3a')]=function(_0x41bbd9,_0x4641c5){var _0xc96b8e={},_0x254a64={},_0x180688={'count':0x0,'rows':[]};var _0x568de1=_[_0x8f05('0x3b')](db['VoiceContext'][_0x8f05('0x3c')],function(_0x56dfe1){return{'name':_0x56dfe1[_0x8f05('0x3d')],'type':_0x56dfe1[_0x8f05('0x3e')][_0x8f05('0x3f')]};});_0x254a64[_0x8f05('0x40')]=_[_0x8f05('0x3b')](_0x568de1,'name');_0x254a64[_0x8f05('0x41')]=_['keys'](_0x41bbd9[_0x8f05('0x41')]);_0x254a64[_0x8f05('0x42')]=_[_0x8f05('0x43')](_0x254a64['model'],_0x254a64[_0x8f05('0x41')]);_0xc96b8e['attributes']=_[_0x8f05('0x43')](_0x254a64[_0x8f05('0x40')],qs[_0x8f05('0x44')](_0x41bbd9[_0x8f05('0x41')][_0x8f05('0x44')]));_0xc96b8e[_0x8f05('0x45')]=_0xc96b8e[_0x8f05('0x45')][_0x8f05('0x46')]?_0xc96b8e['attributes']:_0x254a64[_0x8f05('0x40')];if(!_0x41bbd9['query'][_0x8f05('0x47')]('nolimit')){_0xc96b8e[_0x8f05('0x2b')]=qs[_0x8f05('0x2b')](_0x41bbd9[_0x8f05('0x41')][_0x8f05('0x2b')]);_0xc96b8e[_0x8f05('0x29')]=qs['offset'](_0x41bbd9[_0x8f05('0x41')][_0x8f05('0x29')]);}_0xc96b8e['order']=qs[_0x8f05('0x48')](_0x41bbd9['query'][_0x8f05('0x48')]);_0xc96b8e[_0x8f05('0x49')]=qs[_0x8f05('0x42')](_['pick'](_0x41bbd9['query'],_0x254a64[_0x8f05('0x42')]),_0x568de1);if(_0x41bbd9['query'][_0x8f05('0x4a')]){_0xc96b8e[_0x8f05('0x49')]=_['merge'](_0xc96b8e[_0x8f05('0x49')],{'$or':_[_0x8f05('0x3b')](_0x568de1,function(_0x5d5ebd){if(_0x5d5ebd[_0x8f05('0x3e')]!==_0x8f05('0x4b')){var _0x592394={};_0x592394[_0x5d5ebd[_0x8f05('0x38')]]={'$like':'%'+_0x41bbd9['query'][_0x8f05('0x4a')]+'%'};return _0x592394;}})});}_0xc96b8e=_[_0x8f05('0x4c')]({},_0xc96b8e,_0x41bbd9[_0x8f05('0x4d')]);var _0x12de4b={'where':_0xc96b8e[_0x8f05('0x49')]};return db[_0x8f05('0x4e')][_0x8f05('0x2c')](_0x12de4b)[_0x8f05('0x1c')](function(_0x31169e){_0x180688[_0x8f05('0x2c')]=_0x31169e;if(_0x41bbd9[_0x8f05('0x41')]['includeAll']){_0xc96b8e[_0x8f05('0x4f')]=[{'all':!![]}];}return db[_0x8f05('0x4e')][_0x8f05('0x50')](_0xc96b8e);})[_0x8f05('0x1c')](function(_0x113d26){_0x180688[_0x8f05('0x51')]=_0x113d26;return _0x180688;})['then'](respondWithFilteredResult(_0x4641c5,_0xc96b8e))[_0x8f05('0x25')](handleError(_0x4641c5,null));};exports['show']=function(_0x5e42fb,_0x5c1c35){var _0x5ed6e2={'raw':![],'where':{'id':_0x5e42fb[_0x8f05('0x52')]['id']}},_0x88a2e9={};_0x88a2e9[_0x8f05('0x40')]=_[_0x8f05('0x53')](db[_0x8f05('0x4e')]['rawAttributes']);_0x88a2e9[_0x8f05('0x41')]=_[_0x8f05('0x53')](_0x5e42fb['query']);_0x88a2e9[_0x8f05('0x42')]=_[_0x8f05('0x43')](_0x88a2e9['model'],_0x88a2e9['query']);_0x5ed6e2[_0x8f05('0x45')]=_[_0x8f05('0x43')](_0x88a2e9[_0x8f05('0x40')],qs['fields'](_0x5e42fb[_0x8f05('0x41')][_0x8f05('0x44')]));_0x5ed6e2[_0x8f05('0x45')]=_0x5ed6e2[_0x8f05('0x45')][_0x8f05('0x46')]?_0x5ed6e2['attributes']:_0x88a2e9[_0x8f05('0x40')];if(_0x5e42fb[_0x8f05('0x41')][_0x8f05('0x54')]){_0x5ed6e2[_0x8f05('0x4f')]=[{'all':!![]}];}_0x5ed6e2=_[_0x8f05('0x4c')]({},_0x5ed6e2,_0x5e42fb[_0x8f05('0x4d')]);return db['VoiceContext'][_0x8f05('0x55')](_0x5ed6e2)[_0x8f05('0x1c')](handleEntityNotFound(_0x5c1c35,null))['then'](respondWithResult(_0x5c1c35,null))[_0x8f05('0x25')](handleError(_0x5c1c35,null));};exports[_0x8f05('0x56')]=function(_0x21b778,_0x13a3d8,_0x1ccbc9){var _0x346976;return db[_0x8f05('0x4e')][_0x8f05('0x56')](_0x21b778[_0x8f05('0x57')],{'raw':!![]})[_0x8f05('0x1c')](function(_0x233be0){_0x346976=_0x233be0;return db['VoiceContext']['findAll']({'raw':!![]});})[_0x8f05('0x1c')](function(_0xc9d6c1){var _0xe30f28=fs['readFileSync'](path['join'](config[_0x8f05('0x58')],'server/files/templates/context.ejs'),_0x8f05('0x59'));var _0x5c6182=ejs[_0x8f05('0x5a')](_0xe30f28,{'contexts':_0xc9d6c1||[]});fs[_0x8f05('0x5b')](_0x8f05('0x5c'),_0x5c6182);})[_0x8f05('0x1c')](function(){return respondWithRpcPromise(_0x8f05('0x5d'),_0x8f05('0x56'),{'module':_0x8f05('0x5e')});})[_0x8f05('0x1c')](function(){var _0x349401=_0x21b778[_0x8f05('0x5f')][_0x8f05('0x34')]({'plain':!![]});if(!_0x349401)throw new Error(_0x8f05('0x60'));if(_0x349401[_0x8f05('0x61')]===_0x8f05('0x5f')){var _0x26febf=_0x346976[_0x8f05('0x34')]({'plain':!![]});return db[_0x8f05('0x62')][_0x8f05('0x55')]({'where':{'name':_0x8f05('0x35'),'userProfileId':_0x349401[_0x8f05('0x63')]},'raw':!![]})[_0x8f05('0x1c')](function(_0x2742a3){if(_0x2742a3&&_0x2742a3[_0x8f05('0x64')]===0x0){return db[_0x8f05('0x36')]['create']({'name':_0x26febf[_0x8f05('0x38')],'resourceId':_0x26febf['id'],'type':_0x2742a3[_0x8f05('0x38')],'sectionId':_0x2742a3['id']},{})[_0x8f05('0x1c')](function(){return _0x346976;});}else{return _0x346976;}})[_0x8f05('0x25')](function(_0x3adbcf){logger[_0x8f05('0x21')](_0x8f05('0x65'),_0x3adbcf);throw _0x3adbcf;});}return _0x346976;})['then'](respondWithResult(_0x13a3d8,0xc9))[_0x8f05('0x25')](handleError(_0x13a3d8,null));};exports['update']=function(_0x1531d7,_0x171481,_0x1949c3){var _0x16d023;return db[_0x8f05('0x4e')][_0x8f05('0x55')]({'where':{'id':_0x1531d7['params']['id']}})['then'](handleEntityNotFound(_0x171481,null))[_0x8f05('0x1c')](saveUpdates(_0x1531d7[_0x8f05('0x57')],null))[_0x8f05('0x1c')](function(_0x3e9df9){if(_0x3e9df9){_0x16d023=_0x3e9df9;return db[_0x8f05('0x4e')][_0x8f05('0x50')]({'raw':!![]});}})['then'](function(_0x205011){var _0x2dcbba=fs[_0x8f05('0x66')](path['join'](config['root'],_0x8f05('0x67')),'utf8');var _0x3ef3f1=ejs[_0x8f05('0x5a')](_0x2dcbba,{'contexts':_0x205011||[]});fs['writeFileSync'](_0x8f05('0x5c'),_0x3ef3f1);})[_0x8f05('0x1c')](function(){return respondWithRpcPromise(_0x8f05('0x5d'),'update',{'module':_0x8f05('0x5e')});})[_0x8f05('0x1c')](function(){return _0x16d023;})[_0x8f05('0x1c')](respondWithResult(_0x171481,null))['catch'](handleError(_0x171481,null));};exports['destroy']=function(_0x277e02,_0x3d2cae,_0x299bd8){var _0x5b0c45;return db[_0x8f05('0x4e')][_0x8f05('0x55')]({'where':{'id':_0x277e02[_0x8f05('0x52')]['id']}})[_0x8f05('0x1c')](handleEntityNotFound(_0x3d2cae,null))['then'](removeEntity(_0x3d2cae,null))['then'](function(){return db[_0x8f05('0x4e')][_0x8f05('0x50')]({'raw':!![]});})[_0x8f05('0x1c')](function(_0x53b8a9){var _0x43d3b8=fs[_0x8f05('0x66')](path[_0x8f05('0x68')](config['root'],'server/files/templates/context.ejs'),_0x8f05('0x59'));var _0xa98272=ejs[_0x8f05('0x5a')](_0x43d3b8,{'contexts':_0x53b8a9});fs['writeFileSync']('/etc/asterisk/extensions_xcally_contexts.conf',_0xa98272);return respondWithRpcPromise('Reload',_0x8f05('0x37'),{'module':'chan_sip.so'});})[_0x8f05('0x25')](handleError(_0x3d2cae,null));};