Built motion from commit 3a7e82ea.|2.6.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 _0xaf8f=['type','key','model','keys','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','filters','pick','filter','merge','VIRTUAL','count','includeAll','include','findAll','rows','catch','params','options','find','create','body','join','server/files/templates/context.ejs','writeFileSync','/etc/asterisk/extensions_xcally_contexts.conf','Reload','chan_sip.so','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','update','readFileSync','utf8','render','root','eml-format','rimraf','zip-dir','request-promise','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','jayson/promise','client','http','request','then','info','request\x20sent','debug','stringify','error','code','message','VoiceContext,\x20%s,\x20%s','result','status','json','offset','undefined','limit','set','Content-Range','apply','reject','save','destroy','get','Contexts','UserProfileResource','end','sendStatus','name','send','index','map','VoiceContext','rawAttributes','fieldName'];(function(_0x30cc1a,_0x162bbc){var _0xb3f18f=function(_0x26bdfe){while(--_0x26bdfe){_0x30cc1a['push'](_0x30cc1a['shift']());}};_0xb3f18f(++_0x162bbc);}(_0xaf8f,0x102));var _0xfaf8=function(_0x25a8e2,_0x186f11){_0x25a8e2=_0x25a8e2-0x0;var _0x1d0949=_0xaf8f[_0x25a8e2];return _0x1d0949;};'use strict';var emlformat=require(_0xfaf8('0x0'));var rimraf=require(_0xfaf8('0x1'));var zipdir=require(_0xfaf8('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xfaf8('0x3'));var moment=require('moment');var BPromise=require(_0xfaf8('0x4'));var Mustache=require(_0xfaf8('0x5'));var util=require(_0xfaf8('0x6'));var path=require(_0xfaf8('0x7'));var sox=require(_0xfaf8('0x8'));var csv=require(_0xfaf8('0x9'));var ejs=require(_0xfaf8('0xa'));var fs=require('fs');var fs_extra=require(_0xfaf8('0xb'));var _=require(_0xfaf8('0xc'));var squel=require(_0xfaf8('0xd'));var crypto=require(_0xfaf8('0xe'));var jsforce=require(_0xfaf8('0xf'));var deskjs=require(_0xfaf8('0x10'));var toCsv=require(_0xfaf8('0x9'));var querystring=require(_0xfaf8('0x11'));var Papa=require(_0xfaf8('0x12'));var Redis=require(_0xfaf8('0x13'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xfaf8('0x14'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xfaf8('0x15'))(_0xfaf8('0x16'));var utils=require(_0xfaf8('0x17'));var config=require(_0xfaf8('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0xfaf8('0x19'))['db'];var jayson=require(_0xfaf8('0x1a'));var client=jayson[_0xfaf8('0x1b')][_0xfaf8('0x1c')]({'port':0x232a});function respondWithRpcPromise(_0x222514,_0x481fcf,_0x56937b,_0x5a81b3){return new BPromise(function(_0x1ec370,_0x2f6a40){var _0x2e2d68=_0x5a81b3||client;return _0x2e2d68[_0xfaf8('0x1d')](_0x222514,_0x56937b)[_0xfaf8('0x1e')](function(_0x2e159c){logger[_0xfaf8('0x1f')]('VoiceContext,\x20%s,\x20%s',_0x481fcf,_0xfaf8('0x20'));logger[_0xfaf8('0x21')]('VoiceContext,\x20%s,\x20%s,\x20%s',_0x481fcf,_0xfaf8('0x20'),JSON[_0xfaf8('0x22')](_0x2e159c));if(_0x2e159c[_0xfaf8('0x23')]){if(_0x2e159c[_0xfaf8('0x23')][_0xfaf8('0x24')]===0x1f4){logger['error']('VoiceContext,\x20%s,\x20%s',_0x481fcf,_0x2e159c[_0xfaf8('0x23')][_0xfaf8('0x25')]);return _0x2f6a40(_0x2e159c[_0xfaf8('0x23')][_0xfaf8('0x25')]);}logger['error']('VoiceContext,\x20%s,\x20%s',_0x481fcf,_0x2e159c[_0xfaf8('0x23')][_0xfaf8('0x25')]);return _0x1ec370(_0x2e159c[_0xfaf8('0x23')][_0xfaf8('0x25')]);}else{logger[_0xfaf8('0x1f')](_0xfaf8('0x26'),_0x481fcf,'request\x20sent');_0x1ec370(_0x2e159c[_0xfaf8('0x27')][_0xfaf8('0x25')]);}})['catch'](function(_0xb050b1){logger[_0xfaf8('0x23')]('VoiceContext,\x20%s,\x20%s',_0x481fcf,_0xb050b1);_0x2f6a40(_0xb050b1);});});}function respondWithStatusCode(_0x58842a,_0x4174cb){_0x4174cb=_0x4174cb||0xcc;return function(_0x6484fa){if(_0x6484fa){return _0x58842a['sendStatus'](_0x4174cb);}return _0x58842a[_0xfaf8('0x28')](_0x4174cb)['end']();};}function respondWithResult(_0x4a405a,_0x1ba81f){_0x1ba81f=_0x1ba81f||0xc8;return function(_0x247407){if(_0x247407){return _0x4a405a['status'](_0x1ba81f)[_0xfaf8('0x29')](_0x247407);}};}function respondWithFilteredResult(_0x51d28e,_0x19e58e){return function(_0x3e46f8){if(_0x3e46f8){var _0xdecf39=typeof _0x19e58e[_0xfaf8('0x2a')]===_0xfaf8('0x2b')&&typeof _0x19e58e[_0xfaf8('0x2c')]==='undefined';var _0x397e48=_0x3e46f8['count'];var _0x84e256=_0xdecf39?0x0:_0x19e58e[_0xfaf8('0x2a')];var _0x324573=_0xdecf39?_0x3e46f8['count']:_0x19e58e[_0xfaf8('0x2a')]+_0x19e58e['limit'];var _0x2263c5;if(_0x324573>=_0x397e48){_0x324573=_0x397e48;_0x2263c5=0xc8;}else{_0x2263c5=0xce;}_0x51d28e[_0xfaf8('0x28')](_0x2263c5);return _0x51d28e[_0xfaf8('0x2d')](_0xfaf8('0x2e'),_0x84e256+'-'+_0x324573+'/'+_0x397e48)[_0xfaf8('0x29')](_0x3e46f8);}return null;};}function patchUpdates(_0x474a68){return function(_0x452a1d){try{jsonpatch[_0xfaf8('0x2f')](_0x452a1d,_0x474a68,!![]);}catch(_0x4b5b0d){return BPromise[_0xfaf8('0x30')](_0x4b5b0d);}return _0x452a1d[_0xfaf8('0x31')]();};}function saveUpdates(_0x47076c,_0x2836c2){return function(_0x31d452){if(_0x31d452){return _0x31d452['update'](_0x47076c)[_0xfaf8('0x1e')](function(_0x3f9607){return _0x3f9607;});}return null;};}function removeEntity(_0x4dec29,_0x7f359c){return function(_0x5af295){if(_0x5af295){return _0x5af295[_0xfaf8('0x32')]()[_0xfaf8('0x1e')](function(){var _0x442880=_0x5af295[_0xfaf8('0x33')]({'plain':!![]});var _0x5e7cce=_0xfaf8('0x34');return db[_0xfaf8('0x35')][_0xfaf8('0x32')]({'where':{'type':_0x5e7cce,'resourceId':_0x442880['id']}})[_0xfaf8('0x1e')](function(){return _0x5af295;});})['then'](function(){_0x4dec29[_0xfaf8('0x28')](0xcc)[_0xfaf8('0x36')]();});}};}function handleEntityNotFound(_0x5e161b,_0xfd5761){return function(_0x2a8388){if(!_0x2a8388){_0x5e161b[_0xfaf8('0x37')](0x194);}return _0x2a8388;};}function handleError(_0x53ae20,_0x376f94){_0x376f94=_0x376f94||0x1f4;return function(_0x238c1e){logger[_0xfaf8('0x23')](_0x238c1e['stack']);if(_0x238c1e['name']){delete _0x238c1e[_0xfaf8('0x38')];}_0x53ae20[_0xfaf8('0x28')](_0x376f94)[_0xfaf8('0x39')](_0x238c1e);};}exports[_0xfaf8('0x3a')]=function(_0x21c7ef,_0x21d99f){var _0x53bcc1={},_0x273ed5={},_0x3160cf={'count':0x0,'rows':[]};var _0x3971b5=_[_0xfaf8('0x3b')](db[_0xfaf8('0x3c')][_0xfaf8('0x3d')],function(_0x127dc6){return{'name':_0x127dc6[_0xfaf8('0x3e')],'type':_0x127dc6[_0xfaf8('0x3f')][_0xfaf8('0x40')]};});_0x273ed5[_0xfaf8('0x41')]=_[_0xfaf8('0x3b')](_0x3971b5,_0xfaf8('0x38'));_0x273ed5['query']=_[_0xfaf8('0x42')](_0x21c7ef[_0xfaf8('0x43')]);_0x273ed5['filters']=_[_0xfaf8('0x44')](_0x273ed5[_0xfaf8('0x41')],_0x273ed5[_0xfaf8('0x43')]);_0x53bcc1[_0xfaf8('0x45')]=_[_0xfaf8('0x44')](_0x273ed5['model'],qs[_0xfaf8('0x46')](_0x21c7ef[_0xfaf8('0x43')][_0xfaf8('0x46')]));_0x53bcc1[_0xfaf8('0x45')]=_0x53bcc1[_0xfaf8('0x45')][_0xfaf8('0x47')]?_0x53bcc1[_0xfaf8('0x45')]:_0x273ed5[_0xfaf8('0x41')];if(!_0x21c7ef[_0xfaf8('0x43')][_0xfaf8('0x48')](_0xfaf8('0x49'))){_0x53bcc1[_0xfaf8('0x2c')]=qs[_0xfaf8('0x2c')](_0x21c7ef[_0xfaf8('0x43')][_0xfaf8('0x2c')]);_0x53bcc1['offset']=qs[_0xfaf8('0x2a')](_0x21c7ef[_0xfaf8('0x43')][_0xfaf8('0x2a')]);}_0x53bcc1['order']=qs[_0xfaf8('0x4a')](_0x21c7ef[_0xfaf8('0x43')][_0xfaf8('0x4a')]);_0x53bcc1[_0xfaf8('0x4b')]=qs[_0xfaf8('0x4c')](_[_0xfaf8('0x4d')](_0x21c7ef['query'],_0x273ed5[_0xfaf8('0x4c')]),_0x3971b5);if(_0x21c7ef['query'][_0xfaf8('0x4e')]){_0x53bcc1['where']=_[_0xfaf8('0x4f')](_0x53bcc1[_0xfaf8('0x4b')],{'$or':_['map'](_0x3971b5,function(_0x117251){if(_0x117251['type']!==_0xfaf8('0x50')){var _0x181d30={};_0x181d30[_0x117251[_0xfaf8('0x38')]]={'$like':'%'+_0x21c7ef['query'][_0xfaf8('0x4e')]+'%'};return _0x181d30;}})});}_0x53bcc1=_[_0xfaf8('0x4f')]({},_0x53bcc1,_0x21c7ef['options']);var _0xf984d9={'where':_0x53bcc1[_0xfaf8('0x4b')]};return db[_0xfaf8('0x3c')][_0xfaf8('0x51')](_0xf984d9)[_0xfaf8('0x1e')](function(_0xabed59){_0x3160cf['count']=_0xabed59;if(_0x21c7ef[_0xfaf8('0x43')][_0xfaf8('0x52')]){_0x53bcc1[_0xfaf8('0x53')]=[{'all':!![]}];}return db['VoiceContext'][_0xfaf8('0x54')](_0x53bcc1);})[_0xfaf8('0x1e')](function(_0x51c214){_0x3160cf[_0xfaf8('0x55')]=_0x51c214;return _0x3160cf;})[_0xfaf8('0x1e')](respondWithFilteredResult(_0x21d99f,_0x53bcc1))[_0xfaf8('0x56')](handleError(_0x21d99f,null));};exports['show']=function(_0x1b2d78,_0x53b59a){var _0x285a74={'raw':![],'where':{'id':_0x1b2d78[_0xfaf8('0x57')]['id']}},_0x51895d={};_0x51895d[_0xfaf8('0x41')]=_['keys'](db[_0xfaf8('0x3c')][_0xfaf8('0x3d')]);_0x51895d[_0xfaf8('0x43')]=_[_0xfaf8('0x42')](_0x1b2d78['query']);_0x51895d[_0xfaf8('0x4c')]=_[_0xfaf8('0x44')](_0x51895d[_0xfaf8('0x41')],_0x51895d['query']);_0x285a74['attributes']=_[_0xfaf8('0x44')](_0x51895d[_0xfaf8('0x41')],qs['fields'](_0x1b2d78['query'][_0xfaf8('0x46')]));_0x285a74['attributes']=_0x285a74[_0xfaf8('0x45')]['length']?_0x285a74[_0xfaf8('0x45')]:_0x51895d['model'];if(_0x1b2d78[_0xfaf8('0x43')]['includeAll']){_0x285a74[_0xfaf8('0x53')]=[{'all':!![]}];}_0x285a74=_[_0xfaf8('0x4f')]({},_0x285a74,_0x1b2d78[_0xfaf8('0x58')]);return db['VoiceContext'][_0xfaf8('0x59')](_0x285a74)[_0xfaf8('0x1e')](handleEntityNotFound(_0x53b59a,null))[_0xfaf8('0x1e')](respondWithResult(_0x53b59a,null))[_0xfaf8('0x56')](handleError(_0x53b59a,null));};exports['create']=function(_0x53fe50,_0x34b5ae,_0x519ed7){var _0x165fd1;return db[_0xfaf8('0x3c')][_0xfaf8('0x5a')](_0x53fe50[_0xfaf8('0x5b')],{'raw':!![]})[_0xfaf8('0x1e')](function(_0x1e017a){_0x165fd1=_0x1e017a;return db[_0xfaf8('0x3c')][_0xfaf8('0x54')]({'raw':!![]});})[_0xfaf8('0x1e')](function(_0x461915){var _0x35efbc=fs['readFileSync'](path[_0xfaf8('0x5c')](config['root'],_0xfaf8('0x5d')),'utf8');var _0x58c5b4=ejs['render'](_0x35efbc,{'contexts':_0x461915||[]});fs[_0xfaf8('0x5e')](_0xfaf8('0x5f'),_0x58c5b4);})[_0xfaf8('0x1e')](function(){return respondWithRpcPromise(_0xfaf8('0x60'),_0xfaf8('0x5a'),{'module':_0xfaf8('0x61')});})[_0xfaf8('0x1e')](function(){var _0x12476a=_0x53fe50[_0xfaf8('0x62')][_0xfaf8('0x33')]({'plain':!![]});if(!_0x12476a)throw new Error(_0xfaf8('0x63'));if(_0x12476a[_0xfaf8('0x64')]==='user'){var _0x1bec2a=_0x165fd1['get']({'plain':!![]});return db[_0xfaf8('0x65')][_0xfaf8('0x59')]({'where':{'name':'Contexts','userProfileId':_0x12476a['userProfileId']},'raw':!![]})[_0xfaf8('0x1e')](function(_0x3c5695){if(_0x3c5695&&_0x3c5695['autoAssociation']===0x0){return db['UserProfileResource'][_0xfaf8('0x5a')]({'name':_0x1bec2a[_0xfaf8('0x38')],'resourceId':_0x1bec2a['id'],'type':_0x3c5695[_0xfaf8('0x38')],'sectionId':_0x3c5695['id']},{})[_0xfaf8('0x1e')](function(){return _0x165fd1;});}else{return _0x165fd1;}})['catch'](function(_0x458a67){logger[_0xfaf8('0x23')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x458a67);throw _0x458a67;});}return _0x165fd1;})[_0xfaf8('0x1e')](respondWithResult(_0x34b5ae,0xc9))[_0xfaf8('0x56')](handleError(_0x34b5ae,null));};exports[_0xfaf8('0x66')]=function(_0x3532c2,_0x735f87,_0x518398){var _0x6c69dd;return db[_0xfaf8('0x3c')][_0xfaf8('0x59')]({'where':{'id':_0x3532c2[_0xfaf8('0x57')]['id']}})['then'](handleEntityNotFound(_0x735f87,null))[_0xfaf8('0x1e')](saveUpdates(_0x3532c2['body'],null))['then'](function(_0x59500a){if(_0x59500a){_0x6c69dd=_0x59500a;return db['VoiceContext'][_0xfaf8('0x54')]({'raw':!![]});}})[_0xfaf8('0x1e')](function(_0x159254){var _0x3838f4=fs[_0xfaf8('0x67')](path[_0xfaf8('0x5c')](config['root'],_0xfaf8('0x5d')),_0xfaf8('0x68'));var _0x50f706=ejs[_0xfaf8('0x69')](_0x3838f4,{'contexts':_0x159254||[]});fs[_0xfaf8('0x5e')](_0xfaf8('0x5f'),_0x50f706);})[_0xfaf8('0x1e')](function(){return respondWithRpcPromise(_0xfaf8('0x60'),'update',{'module':_0xfaf8('0x61')});})[_0xfaf8('0x1e')](function(){return _0x6c69dd;})[_0xfaf8('0x1e')](respondWithResult(_0x735f87,null))[_0xfaf8('0x56')](handleError(_0x735f87,null));};exports[_0xfaf8('0x32')]=function(_0x5d4b0b,_0x2bdcc5,_0x50f2fd){var _0x48250a;return db[_0xfaf8('0x3c')][_0xfaf8('0x59')]({'where':{'id':_0x5d4b0b[_0xfaf8('0x57')]['id']}})[_0xfaf8('0x1e')](handleEntityNotFound(_0x2bdcc5,null))[_0xfaf8('0x1e')](removeEntity(_0x2bdcc5,null))[_0xfaf8('0x1e')](function(){return db[_0xfaf8('0x3c')][_0xfaf8('0x54')]({'raw':!![]});})[_0xfaf8('0x1e')](function(_0x3658b4){var _0x55fb27=fs[_0xfaf8('0x67')](path['join'](config[_0xfaf8('0x6a')],_0xfaf8('0x5d')),_0xfaf8('0x68'));var _0x4f34a6=ejs[_0xfaf8('0x69')](_0x55fb27,{'contexts':_0x3658b4});fs[_0xfaf8('0x5e')](_0xfaf8('0x5f'),_0x4f34a6);return respondWithRpcPromise(_0xfaf8('0x60'),_0xfaf8('0x32'),{'module':_0xfaf8('0x61')});})[_0xfaf8('0x56')](handleError(_0x2bdcc5,null));};