Built motion from commit (unavailable).|2.5.29
[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 _0xbb48=['hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','VIRTUAL','includeAll','show','params','include','options','find','create','body','findAll','readFileSync','server/files/templates/context.ejs','utf8','writeFileSync','/etc/asterisk/extensions_xcally_contexts.conf','user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','join','render','Reload','eml-format','rimraf','request-promise','bluebird','mustache','util','sox','to-csv','ejs','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../mysqldb','jayson/promise','client','http','request','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','apply','reject','then','destroy','get','Contexts','UserProfileResource','stack','name','send','index','VoiceContext','rawAttributes','fieldName','type','model','map','keys','query','filters','attributes','intersection','fields'];(function(_0x4e67df,_0x59308c){var _0x1a49a9=function(_0x396881){while(--_0x396881){_0x4e67df['push'](_0x4e67df['shift']());}};_0x1a49a9(++_0x59308c);}(_0xbb48,0x1bc));var _0x8bb4=function(_0x2c91e9,_0x2fb965){_0x2c91e9=_0x2c91e9-0x0;var _0x13f0ff=_0xbb48[_0x2c91e9];return _0x13f0ff;};'use strict';var emlformat=require(_0x8bb4('0x0'));var rimraf=require(_0x8bb4('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x8bb4('0x2'));var moment=require('moment');var BPromise=require(_0x8bb4('0x3'));var Mustache=require(_0x8bb4('0x4'));var util=require(_0x8bb4('0x5'));var path=require('path');var sox=require(_0x8bb4('0x6'));var csv=require(_0x8bb4('0x7'));var ejs=require(_0x8bb4('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x8bb4('0x9'));var squel=require(_0x8bb4('0xa'));var crypto=require(_0x8bb4('0xb'));var jsforce=require(_0x8bb4('0xc'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x8bb4('0xd'));var Papa=require(_0x8bb4('0xe'));var Redis=require(_0x8bb4('0xf'));var authService=require(_0x8bb4('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0x8bb4('0x11'));var hardwareService=require(_0x8bb4('0x12'));var logger=require('../../config/logger')(_0x8bb4('0x13'));var utils=require(_0x8bb4('0x14'));var config=require(_0x8bb4('0x15'));var licenseUtil=require('../../config/license/util');var db=require(_0x8bb4('0x16'))['db'];var jayson=require(_0x8bb4('0x17'));var client=jayson[_0x8bb4('0x18')][_0x8bb4('0x19')]({'port':0x232a});function respondWithRpcPromise(_0x515692,_0x4cff8e,_0x2e13d3,_0x4238ae){return new BPromise(function(_0x35bdd4,_0x2c777c){var _0x1b9f96=_0x4238ae||client;return _0x1b9f96[_0x8bb4('0x1a')](_0x515692,_0x2e13d3)['then'](function(_0x42f849){logger[_0x8bb4('0x1b')](_0x8bb4('0x1c'),_0x4cff8e,_0x8bb4('0x1d'));logger[_0x8bb4('0x1e')](_0x8bb4('0x1f'),_0x4cff8e,_0x8bb4('0x1d'),JSON[_0x8bb4('0x20')](_0x42f849));if(_0x42f849[_0x8bb4('0x21')]){if(_0x42f849[_0x8bb4('0x21')][_0x8bb4('0x22')]===0x1f4){logger['error'](_0x8bb4('0x1c'),_0x4cff8e,_0x42f849[_0x8bb4('0x21')][_0x8bb4('0x23')]);return _0x2c777c(_0x42f849[_0x8bb4('0x21')][_0x8bb4('0x23')]);}logger['error'](_0x8bb4('0x1c'),_0x4cff8e,_0x42f849['error'][_0x8bb4('0x23')]);return _0x35bdd4(_0x42f849[_0x8bb4('0x21')][_0x8bb4('0x23')]);}else{logger['info'](_0x8bb4('0x1c'),_0x4cff8e,_0x8bb4('0x1d'));_0x35bdd4(_0x42f849[_0x8bb4('0x24')][_0x8bb4('0x23')]);}})[_0x8bb4('0x25')](function(_0x22a0af){logger['error'](_0x8bb4('0x1c'),_0x4cff8e,_0x22a0af);_0x2c777c(_0x22a0af);});});}function respondWithStatusCode(_0x2ff997,_0xba3bbc){_0xba3bbc=_0xba3bbc||0xcc;return function(_0x2f487c){if(_0x2f487c){return _0x2ff997[_0x8bb4('0x26')](_0xba3bbc);}return _0x2ff997[_0x8bb4('0x27')](_0xba3bbc)[_0x8bb4('0x28')]();};}function respondWithResult(_0x402556,_0x5ef6cc){_0x5ef6cc=_0x5ef6cc||0xc8;return function(_0x2c0e4b){if(_0x2c0e4b){return _0x402556[_0x8bb4('0x27')](_0x5ef6cc)[_0x8bb4('0x29')](_0x2c0e4b);}};}function respondWithFilteredResult(_0x376c57,_0x31f0ee){return function(_0x4847fd){if(_0x4847fd){var _0x2a4782=typeof _0x31f0ee[_0x8bb4('0x2a')]===_0x8bb4('0x2b')&&typeof _0x31f0ee[_0x8bb4('0x2c')]===_0x8bb4('0x2b');var _0xb131af=_0x4847fd[_0x8bb4('0x2d')];var _0x56aa85=_0x2a4782?0x0:_0x31f0ee[_0x8bb4('0x2a')];var _0x587ef1=_0x2a4782?_0x4847fd['count']:_0x31f0ee[_0x8bb4('0x2a')]+_0x31f0ee[_0x8bb4('0x2c')];var _0x73a0ef;if(_0x587ef1>=_0xb131af){_0x587ef1=_0xb131af;_0x73a0ef=0xc8;}else{_0x73a0ef=0xce;}_0x376c57[_0x8bb4('0x27')](_0x73a0ef);return _0x376c57[_0x8bb4('0x2e')](_0x8bb4('0x2f'),_0x56aa85+'-'+_0x587ef1+'/'+_0xb131af)[_0x8bb4('0x29')](_0x4847fd);}return null;};}function patchUpdates(_0xe6a89f){return function(_0x510293){try{jsonpatch[_0x8bb4('0x30')](_0x510293,_0xe6a89f,!![]);}catch(_0xa83fc5){return BPromise[_0x8bb4('0x31')](_0xa83fc5);}return _0x510293['save']();};}function saveUpdates(_0x31ca9f,_0xd92a00){return function(_0x2ce606){if(_0x2ce606){return _0x2ce606['update'](_0x31ca9f)[_0x8bb4('0x32')](function(_0x2c0e3f){return _0x2c0e3f;});}return null;};}function removeEntity(_0x3fa8c5,_0xe0c685){return function(_0x340101){if(_0x340101){return _0x340101[_0x8bb4('0x33')]()[_0x8bb4('0x32')](function(){var _0x4c8551=_0x340101[_0x8bb4('0x34')]({'plain':!![]});var _0x276110=_0x8bb4('0x35');return db[_0x8bb4('0x36')]['destroy']({'where':{'type':_0x276110,'resourceId':_0x4c8551['id']}})[_0x8bb4('0x32')](function(){return _0x340101;});})[_0x8bb4('0x32')](function(){_0x3fa8c5['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x33e1e2,_0x3ab05d){return function(_0x1d3658){if(!_0x1d3658){_0x33e1e2[_0x8bb4('0x26')](0x194);}return _0x1d3658;};}function handleError(_0xdcee52,_0x54be3b){_0x54be3b=_0x54be3b||0x1f4;return function(_0x14db3c){logger[_0x8bb4('0x21')](_0x14db3c[_0x8bb4('0x37')]);if(_0x14db3c[_0x8bb4('0x38')]){delete _0x14db3c[_0x8bb4('0x38')];}_0xdcee52[_0x8bb4('0x27')](_0x54be3b)[_0x8bb4('0x39')](_0x14db3c);};}exports[_0x8bb4('0x3a')]=function(_0x1b51b1,_0x4208cb){var _0x42693a={},_0x42746a={},_0x29e2d7={'count':0x0,'rows':[]};var _0x165b7d=_['map'](db[_0x8bb4('0x3b')][_0x8bb4('0x3c')],function(_0x5755a6){return{'name':_0x5755a6[_0x8bb4('0x3d')],'type':_0x5755a6[_0x8bb4('0x3e')]['key']};});_0x42746a[_0x8bb4('0x3f')]=_[_0x8bb4('0x40')](_0x165b7d,_0x8bb4('0x38'));_0x42746a['query']=_[_0x8bb4('0x41')](_0x1b51b1[_0x8bb4('0x42')]);_0x42746a[_0x8bb4('0x43')]=_['intersection'](_0x42746a[_0x8bb4('0x3f')],_0x42746a[_0x8bb4('0x42')]);_0x42693a[_0x8bb4('0x44')]=_[_0x8bb4('0x45')](_0x42746a[_0x8bb4('0x3f')],qs['fields'](_0x1b51b1[_0x8bb4('0x42')][_0x8bb4('0x46')]));_0x42693a['attributes']=_0x42693a[_0x8bb4('0x44')]['length']?_0x42693a[_0x8bb4('0x44')]:_0x42746a[_0x8bb4('0x3f')];if(!_0x1b51b1[_0x8bb4('0x42')][_0x8bb4('0x47')](_0x8bb4('0x48'))){_0x42693a[_0x8bb4('0x2c')]=qs[_0x8bb4('0x2c')](_0x1b51b1[_0x8bb4('0x42')][_0x8bb4('0x2c')]);_0x42693a['offset']=qs[_0x8bb4('0x2a')](_0x1b51b1[_0x8bb4('0x42')]['offset']);}_0x42693a[_0x8bb4('0x49')]=qs[_0x8bb4('0x4a')](_0x1b51b1[_0x8bb4('0x42')][_0x8bb4('0x4a')]);_0x42693a['where']=qs[_0x8bb4('0x43')](_[_0x8bb4('0x4b')](_0x1b51b1['query'],_0x42746a['filters']),_0x165b7d);if(_0x1b51b1['query'][_0x8bb4('0x4c')]){_0x42693a[_0x8bb4('0x4d')]=_[_0x8bb4('0x4e')](_0x42693a[_0x8bb4('0x4d')],{'$or':_[_0x8bb4('0x40')](_0x165b7d,function(_0x3a3516){if(_0x3a3516[_0x8bb4('0x3e')]!==_0x8bb4('0x4f')){var _0x4ec5a6={};_0x4ec5a6[_0x3a3516[_0x8bb4('0x38')]]={'$like':'%'+_0x1b51b1[_0x8bb4('0x42')][_0x8bb4('0x4c')]+'%'};return _0x4ec5a6;}})});}_0x42693a=_[_0x8bb4('0x4e')]({},_0x42693a,_0x1b51b1['options']);var _0xf0609b={'where':_0x42693a['where']};return db[_0x8bb4('0x3b')][_0x8bb4('0x2d')](_0xf0609b)[_0x8bb4('0x32')](function(_0x525735){_0x29e2d7[_0x8bb4('0x2d')]=_0x525735;if(_0x1b51b1[_0x8bb4('0x42')][_0x8bb4('0x50')]){_0x42693a['include']=[{'all':!![]}];}return db[_0x8bb4('0x3b')]['findAll'](_0x42693a);})[_0x8bb4('0x32')](function(_0x62f180){_0x29e2d7['rows']=_0x62f180;return _0x29e2d7;})[_0x8bb4('0x32')](respondWithFilteredResult(_0x4208cb,_0x42693a))[_0x8bb4('0x25')](handleError(_0x4208cb,null));};exports[_0x8bb4('0x51')]=function(_0x9e528b,_0x243223){var _0x5db9c2={'raw':![],'where':{'id':_0x9e528b[_0x8bb4('0x52')]['id']}},_0x222939={};_0x222939['model']=_['keys'](db['VoiceContext'][_0x8bb4('0x3c')]);_0x222939[_0x8bb4('0x42')]=_[_0x8bb4('0x41')](_0x9e528b['query']);_0x222939[_0x8bb4('0x43')]=_['intersection'](_0x222939[_0x8bb4('0x3f')],_0x222939[_0x8bb4('0x42')]);_0x5db9c2['attributes']=_['intersection'](_0x222939[_0x8bb4('0x3f')],qs[_0x8bb4('0x46')](_0x9e528b[_0x8bb4('0x42')][_0x8bb4('0x46')]));_0x5db9c2[_0x8bb4('0x44')]=_0x5db9c2[_0x8bb4('0x44')]['length']?_0x5db9c2[_0x8bb4('0x44')]:_0x222939[_0x8bb4('0x3f')];if(_0x9e528b[_0x8bb4('0x42')][_0x8bb4('0x50')]){_0x5db9c2[_0x8bb4('0x53')]=[{'all':!![]}];}_0x5db9c2=_[_0x8bb4('0x4e')]({},_0x5db9c2,_0x9e528b[_0x8bb4('0x54')]);return db[_0x8bb4('0x3b')][_0x8bb4('0x55')](_0x5db9c2)[_0x8bb4('0x32')](handleEntityNotFound(_0x243223,null))[_0x8bb4('0x32')](respondWithResult(_0x243223,null))[_0x8bb4('0x25')](handleError(_0x243223,null));};exports[_0x8bb4('0x56')]=function(_0x1a500c,_0x123d2f,_0x11b27d){var _0x12bd68;return db[_0x8bb4('0x3b')][_0x8bb4('0x56')](_0x1a500c[_0x8bb4('0x57')],{'raw':!![]})[_0x8bb4('0x32')](function(_0x1233b5){_0x12bd68=_0x1233b5;return db[_0x8bb4('0x3b')][_0x8bb4('0x58')]({'raw':!![]});})[_0x8bb4('0x32')](function(_0x53e63b){var _0x3187e1=fs[_0x8bb4('0x59')](path['join'](config['root'],_0x8bb4('0x5a')),_0x8bb4('0x5b'));var _0x5ef708=ejs['render'](_0x3187e1,{'contexts':_0x53e63b||[]});fs[_0x8bb4('0x5c')](_0x8bb4('0x5d'),_0x5ef708);})['then'](function(){return respondWithRpcPromise('Reload',_0x8bb4('0x56'),{'module':'chan_sip.so'});})[_0x8bb4('0x32')](function(){var _0x3652f2=_0x1a500c[_0x8bb4('0x5e')][_0x8bb4('0x34')]({'plain':!![]});if(!_0x3652f2)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3652f2[_0x8bb4('0x5f')]===_0x8bb4('0x5e')){var _0x1d2af4=_0x12bd68[_0x8bb4('0x34')]({'plain':!![]});return db[_0x8bb4('0x60')]['find']({'where':{'name':_0x8bb4('0x35'),'userProfileId':_0x3652f2[_0x8bb4('0x61')]},'raw':!![]})['then'](function(_0x47a28f){if(_0x47a28f&&_0x47a28f[_0x8bb4('0x62')]===0x0){return db['UserProfileResource'][_0x8bb4('0x56')]({'name':_0x1d2af4[_0x8bb4('0x38')],'resourceId':_0x1d2af4['id'],'type':_0x47a28f[_0x8bb4('0x38')],'sectionId':_0x47a28f['id']},{})[_0x8bb4('0x32')](function(){return _0x12bd68;});}else{return _0x12bd68;}})[_0x8bb4('0x25')](function(_0x3632a0){logger[_0x8bb4('0x21')](_0x8bb4('0x63'),_0x3632a0);throw _0x3632a0;});}return _0x12bd68;})[_0x8bb4('0x32')](respondWithResult(_0x123d2f,0xc9))[_0x8bb4('0x25')](handleError(_0x123d2f,null));};exports['update']=function(_0x41bb2f,_0x56f19f,_0x3acaf2){var _0x1a4c67;return db[_0x8bb4('0x3b')][_0x8bb4('0x55')]({'where':{'id':_0x41bb2f[_0x8bb4('0x52')]['id']}})[_0x8bb4('0x32')](handleEntityNotFound(_0x56f19f,null))['then'](saveUpdates(_0x41bb2f[_0x8bb4('0x57')],null))['then'](function(_0x3fa879){if(_0x3fa879){_0x1a4c67=_0x3fa879;return db[_0x8bb4('0x3b')][_0x8bb4('0x58')]({'raw':!![]});}})[_0x8bb4('0x32')](function(_0x10fcbb){var _0x1ce7b9=fs[_0x8bb4('0x59')](path[_0x8bb4('0x64')](config['root'],_0x8bb4('0x5a')),_0x8bb4('0x5b'));var _0xf349=ejs[_0x8bb4('0x65')](_0x1ce7b9,{'contexts':_0x10fcbb||[]});fs[_0x8bb4('0x5c')](_0x8bb4('0x5d'),_0xf349);})[_0x8bb4('0x32')](function(){return respondWithRpcPromise(_0x8bb4('0x66'),'update',{'module':'chan_sip.so'});})['then'](function(){return _0x1a4c67;})['then'](respondWithResult(_0x56f19f,null))[_0x8bb4('0x25')](handleError(_0x56f19f,null));};exports[_0x8bb4('0x33')]=function(_0x765f96,_0x47aa29,_0x35105e){var _0x3164f1;return db[_0x8bb4('0x3b')][_0x8bb4('0x55')]({'where':{'id':_0x765f96[_0x8bb4('0x52')]['id']}})[_0x8bb4('0x32')](handleEntityNotFound(_0x47aa29,null))[_0x8bb4('0x32')](removeEntity(_0x47aa29,null))[_0x8bb4('0x32')](function(){return db[_0x8bb4('0x3b')]['findAll']({'raw':!![]});})[_0x8bb4('0x32')](function(_0x575e37){var _0x5b9358=fs[_0x8bb4('0x59')](path['join'](config['root'],_0x8bb4('0x5a')),_0x8bb4('0x5b'));var _0x1c44c1=ejs['render'](_0x5b9358,{'contexts':_0x575e37});fs[_0x8bb4('0x5c')](_0x8bb4('0x5d'),_0x1c44c1);return respondWithRpcPromise(_0x8bb4('0x66'),_0x8bb4('0x33'),{'module':'chan_sip.so'});})[_0x8bb4('0x25')](handleError(_0x47aa29,null));};