9a84851e2abdbc0411fa94f3ce4886abcc4025d2
[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 _0xbcf0=['client','http','info','request\x20sent','debug','VoiceContext,\x20%s,\x20%s,\x20%s','error','code','VoiceContext,\x20%s,\x20%s','message','result','catch','sendStatus','status','end','count','offset','limit','Content-Range','apply','reject','update','then','destroy','get','Contexts','UserProfileResource','stack','name','index','VoiceContext','rawAttributes','fieldName','key','model','map','keys','query','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','type','filter','options','includeAll','include','findAll','rows','show','params','find','create','readFileSync','join','root','server/files/templates/context.ejs','utf8','render','writeFileSync','Reload','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','/etc/asterisk/extensions_xcally_contexts.conf','chan_sip.so','eml-format','rimraf','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','jayson/promise'];(function(_0x38ded6,_0x260e8c){var _0x404a50=function(_0x316b34){while(--_0x316b34){_0x38ded6['push'](_0x38ded6['shift']());}};_0x404a50(++_0x260e8c);}(_0xbcf0,0xb7));var _0x0bcf=function(_0x173441,_0x26b0d9){_0x173441=_0x173441-0x0;var _0x2423a8=_0xbcf0[_0x173441];return _0x2423a8;};'use strict';var emlformat=require(_0x0bcf('0x0'));var rimraf=require(_0x0bcf('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x0bcf('0x2'));var moment=require(_0x0bcf('0x3'));var BPromise=require(_0x0bcf('0x4'));var Mustache=require(_0x0bcf('0x5'));var util=require(_0x0bcf('0x6'));var path=require(_0x0bcf('0x7'));var sox=require(_0x0bcf('0x8'));var csv=require('to-csv');var ejs=require(_0x0bcf('0x9'));var fs=require('fs');var fs_extra=require(_0x0bcf('0xa'));var _=require(_0x0bcf('0xb'));var squel=require(_0x0bcf('0xc'));var crypto=require(_0x0bcf('0xd'));var jsforce=require(_0x0bcf('0xe'));var deskjs=require(_0x0bcf('0xf'));var toCsv=require(_0x0bcf('0x10'));var querystring=require(_0x0bcf('0x11'));var Papa=require('papaparse');var Redis=require(_0x0bcf('0x12'));var authService=require(_0x0bcf('0x13'));var qs=require(_0x0bcf('0x14'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x0bcf('0x15'));var logger=require(_0x0bcf('0x16'))(_0x0bcf('0x17'));var utils=require(_0x0bcf('0x18'));var config=require(_0x0bcf('0x19'));var licenseUtil=require(_0x0bcf('0x1a'));var db=require(_0x0bcf('0x1b'))['db'];var jayson=require(_0x0bcf('0x1c'));var client=jayson[_0x0bcf('0x1d')][_0x0bcf('0x1e')]({'port':0x232a});function respondWithRpcPromise(_0x376ba4,_0x1d2795,_0x2d0f65,_0x1e3221){return new BPromise(function(_0x2cbaae,_0x1c3210){var _0x4ca8ea=_0x1e3221||client;return _0x4ca8ea['request'](_0x376ba4,_0x2d0f65)['then'](function(_0x39363f){logger[_0x0bcf('0x1f')]('VoiceContext,\x20%s,\x20%s',_0x1d2795,_0x0bcf('0x20'));logger[_0x0bcf('0x21')](_0x0bcf('0x22'),_0x1d2795,_0x0bcf('0x20'),JSON['stringify'](_0x39363f));if(_0x39363f['error']){if(_0x39363f[_0x0bcf('0x23')][_0x0bcf('0x24')]===0x1f4){logger[_0x0bcf('0x23')](_0x0bcf('0x25'),_0x1d2795,_0x39363f[_0x0bcf('0x23')][_0x0bcf('0x26')]);return _0x1c3210(_0x39363f[_0x0bcf('0x23')][_0x0bcf('0x26')]);}logger[_0x0bcf('0x23')]('VoiceContext,\x20%s,\x20%s',_0x1d2795,_0x39363f['error']['message']);return _0x2cbaae(_0x39363f['error'][_0x0bcf('0x26')]);}else{logger[_0x0bcf('0x1f')](_0x0bcf('0x25'),_0x1d2795,'request\x20sent');_0x2cbaae(_0x39363f[_0x0bcf('0x27')][_0x0bcf('0x26')]);}})[_0x0bcf('0x28')](function(_0x2fc280){logger['error'](_0x0bcf('0x25'),_0x1d2795,_0x2fc280);_0x1c3210(_0x2fc280);});});}function respondWithStatusCode(_0x4f1ec9,_0x45b98e){_0x45b98e=_0x45b98e||0xcc;return function(_0x3f97ce){if(_0x3f97ce){return _0x4f1ec9[_0x0bcf('0x29')](_0x45b98e);}return _0x4f1ec9[_0x0bcf('0x2a')](_0x45b98e)[_0x0bcf('0x2b')]();};}function respondWithResult(_0x48bb85,_0x3a527d){_0x3a527d=_0x3a527d||0xc8;return function(_0xf8cdcd){if(_0xf8cdcd){return _0x48bb85['status'](_0x3a527d)['json'](_0xf8cdcd);}};}function respondWithFilteredResult(_0x380f86,_0x5a2b97){return function(_0x2ccc6f){if(_0x2ccc6f){var _0x3fd9c0=typeof _0x5a2b97['offset']==='undefined'&&typeof _0x5a2b97['limit']==='undefined';var _0xc18cc7=_0x2ccc6f[_0x0bcf('0x2c')];var _0x8ff78b=_0x3fd9c0?0x0:_0x5a2b97[_0x0bcf('0x2d')];var _0x8a95c1=_0x3fd9c0?_0x2ccc6f[_0x0bcf('0x2c')]:_0x5a2b97['offset']+_0x5a2b97[_0x0bcf('0x2e')];var _0xea2870;if(_0x8a95c1>=_0xc18cc7){_0x8a95c1=_0xc18cc7;_0xea2870=0xc8;}else{_0xea2870=0xce;}_0x380f86[_0x0bcf('0x2a')](_0xea2870);return _0x380f86['set'](_0x0bcf('0x2f'),_0x8ff78b+'-'+_0x8a95c1+'/'+_0xc18cc7)['json'](_0x2ccc6f);}return null;};}function patchUpdates(_0x3a603f){return function(_0x2464b1){try{jsonpatch[_0x0bcf('0x30')](_0x2464b1,_0x3a603f,!![]);}catch(_0x14a8f0){return BPromise[_0x0bcf('0x31')](_0x14a8f0);}return _0x2464b1['save']();};}function saveUpdates(_0x10bf15,_0x3dac6f){return function(_0x6e3a93){if(_0x6e3a93){return _0x6e3a93[_0x0bcf('0x32')](_0x10bf15)[_0x0bcf('0x33')](function(_0x5ad303){return _0x5ad303;});}return null;};}function removeEntity(_0xb8543e,_0x5f21e2){return function(_0x1a29a2){if(_0x1a29a2){return _0x1a29a2[_0x0bcf('0x34')]()['then'](function(){var _0x190c49=_0x1a29a2[_0x0bcf('0x35')]({'plain':!![]});var _0x417d8d=_0x0bcf('0x36');return db[_0x0bcf('0x37')][_0x0bcf('0x34')]({'where':{'type':_0x417d8d,'resourceId':_0x190c49['id']}})[_0x0bcf('0x33')](function(){return _0x1a29a2;});})[_0x0bcf('0x33')](function(){_0xb8543e[_0x0bcf('0x2a')](0xcc)[_0x0bcf('0x2b')]();});}};}function handleEntityNotFound(_0x1597b3,_0x376072){return function(_0x41290c){if(!_0x41290c){_0x1597b3[_0x0bcf('0x29')](0x194);}return _0x41290c;};}function handleError(_0x46fdf5,_0x459865){_0x459865=_0x459865||0x1f4;return function(_0x38251b){logger[_0x0bcf('0x23')](_0x38251b[_0x0bcf('0x38')]);if(_0x38251b[_0x0bcf('0x39')]){delete _0x38251b['name'];}_0x46fdf5[_0x0bcf('0x2a')](_0x459865)['send'](_0x38251b);};}exports[_0x0bcf('0x3a')]=function(_0x118e02,_0x5e26ee){var _0x72ec5c={},_0x51ed19={},_0x253c59={'count':0x0,'rows':[]};var _0x115d27=_['map'](db[_0x0bcf('0x3b')][_0x0bcf('0x3c')],function(_0x24ee28){return{'name':_0x24ee28[_0x0bcf('0x3d')],'type':_0x24ee28['type'][_0x0bcf('0x3e')]};});_0x51ed19[_0x0bcf('0x3f')]=_[_0x0bcf('0x40')](_0x115d27,'name');_0x51ed19['query']=_[_0x0bcf('0x41')](_0x118e02[_0x0bcf('0x42')]);_0x51ed19[_0x0bcf('0x43')]=_['intersection'](_0x51ed19[_0x0bcf('0x3f')],_0x51ed19['query']);_0x72ec5c[_0x0bcf('0x44')]=_[_0x0bcf('0x45')](_0x51ed19['model'],qs[_0x0bcf('0x46')](_0x118e02[_0x0bcf('0x42')][_0x0bcf('0x46')]));_0x72ec5c[_0x0bcf('0x44')]=_0x72ec5c['attributes'][_0x0bcf('0x47')]?_0x72ec5c[_0x0bcf('0x44')]:_0x51ed19[_0x0bcf('0x3f')];if(!_0x118e02[_0x0bcf('0x42')][_0x0bcf('0x48')](_0x0bcf('0x49'))){_0x72ec5c[_0x0bcf('0x2e')]=qs[_0x0bcf('0x2e')](_0x118e02[_0x0bcf('0x42')][_0x0bcf('0x2e')]);_0x72ec5c[_0x0bcf('0x2d')]=qs['offset'](_0x118e02[_0x0bcf('0x42')][_0x0bcf('0x2d')]);}_0x72ec5c[_0x0bcf('0x4a')]=qs[_0x0bcf('0x4b')](_0x118e02[_0x0bcf('0x42')]['sort']);_0x72ec5c[_0x0bcf('0x4c')]=qs['filters'](_[_0x0bcf('0x4d')](_0x118e02[_0x0bcf('0x42')],_0x51ed19[_0x0bcf('0x43')]),_0x115d27);if(_0x118e02['query']['filter']){_0x72ec5c[_0x0bcf('0x4c')]=_[_0x0bcf('0x4e')](_0x72ec5c[_0x0bcf('0x4c')],{'$or':_['map'](_0x115d27,function(_0x1efa28){if(_0x1efa28[_0x0bcf('0x4f')]!=='VIRTUAL'){var _0x3e3f14={};_0x3e3f14[_0x1efa28['name']]={'$like':'%'+_0x118e02[_0x0bcf('0x42')][_0x0bcf('0x50')]+'%'};return _0x3e3f14;}})});}_0x72ec5c=_['merge']({},_0x72ec5c,_0x118e02[_0x0bcf('0x51')]);var _0x4245c0={'where':_0x72ec5c[_0x0bcf('0x4c')]};return db[_0x0bcf('0x3b')][_0x0bcf('0x2c')](_0x4245c0)['then'](function(_0x58d89d){_0x253c59[_0x0bcf('0x2c')]=_0x58d89d;if(_0x118e02['query'][_0x0bcf('0x52')]){_0x72ec5c[_0x0bcf('0x53')]=[{'all':!![]}];}return db['VoiceContext'][_0x0bcf('0x54')](_0x72ec5c);})['then'](function(_0xd59162){_0x253c59[_0x0bcf('0x55')]=_0xd59162;return _0x253c59;})['then'](respondWithFilteredResult(_0x5e26ee,_0x72ec5c))[_0x0bcf('0x28')](handleError(_0x5e26ee,null));};exports[_0x0bcf('0x56')]=function(_0x75609b,_0x42f768){var _0xb6109b={'raw':![],'where':{'id':_0x75609b[_0x0bcf('0x57')]['id']}},_0x5cf517={};_0x5cf517[_0x0bcf('0x3f')]=_[_0x0bcf('0x41')](db['VoiceContext'][_0x0bcf('0x3c')]);_0x5cf517[_0x0bcf('0x42')]=_['keys'](_0x75609b[_0x0bcf('0x42')]);_0x5cf517['filters']=_[_0x0bcf('0x45')](_0x5cf517['model'],_0x5cf517[_0x0bcf('0x42')]);_0xb6109b['attributes']=_[_0x0bcf('0x45')](_0x5cf517[_0x0bcf('0x3f')],qs[_0x0bcf('0x46')](_0x75609b['query'][_0x0bcf('0x46')]));_0xb6109b[_0x0bcf('0x44')]=_0xb6109b[_0x0bcf('0x44')]['length']?_0xb6109b[_0x0bcf('0x44')]:_0x5cf517['model'];if(_0x75609b['query'][_0x0bcf('0x52')]){_0xb6109b[_0x0bcf('0x53')]=[{'all':!![]}];}_0xb6109b=_[_0x0bcf('0x4e')]({},_0xb6109b,_0x75609b[_0x0bcf('0x51')]);return db['VoiceContext'][_0x0bcf('0x58')](_0xb6109b)[_0x0bcf('0x33')](handleEntityNotFound(_0x42f768,null))[_0x0bcf('0x33')](respondWithResult(_0x42f768,null))[_0x0bcf('0x28')](handleError(_0x42f768,null));};exports[_0x0bcf('0x59')]=function(_0x485754,_0x1bf0dd,_0x329e1f){var _0x21d1e2;return db['VoiceContext'][_0x0bcf('0x59')](_0x485754['body'],{'raw':!![]})[_0x0bcf('0x33')](function(_0x9f7fd8){_0x21d1e2=_0x9f7fd8;return db['VoiceContext'][_0x0bcf('0x54')]({'raw':!![]});})[_0x0bcf('0x33')](function(_0xf31bb){var _0x6e5bbc=fs[_0x0bcf('0x5a')](path[_0x0bcf('0x5b')](config[_0x0bcf('0x5c')],_0x0bcf('0x5d')),_0x0bcf('0x5e'));var _0x55c3cb=ejs[_0x0bcf('0x5f')](_0x6e5bbc,{'contexts':_0xf31bb||[]});fs[_0x0bcf('0x60')]('/etc/asterisk/extensions_xcally_contexts.conf',_0x55c3cb);})[_0x0bcf('0x33')](function(){return respondWithRpcPromise(_0x0bcf('0x61'),'create',{'module':'chan_sip.so'});})[_0x0bcf('0x33')](function(){var _0x3609dd=_0x485754['user'][_0x0bcf('0x35')]({'plain':!![]});if(!_0x3609dd)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3609dd[_0x0bcf('0x62')]==='user'){var _0x2aeed8=_0x21d1e2[_0x0bcf('0x35')]({'plain':!![]});return db[_0x0bcf('0x63')]['find']({'where':{'name':_0x0bcf('0x36'),'userProfileId':_0x3609dd[_0x0bcf('0x64')]},'raw':!![]})[_0x0bcf('0x33')](function(_0x1a7f6b){if(_0x1a7f6b&&_0x1a7f6b[_0x0bcf('0x65')]===0x0){return db['UserProfileResource'][_0x0bcf('0x59')]({'name':_0x2aeed8[_0x0bcf('0x39')],'resourceId':_0x2aeed8['id'],'type':_0x1a7f6b['name'],'sectionId':_0x1a7f6b['id']},{})[_0x0bcf('0x33')](function(){return _0x21d1e2;});}else{return _0x21d1e2;}})[_0x0bcf('0x28')](function(_0x48321f){logger[_0x0bcf('0x23')](_0x0bcf('0x66'),_0x48321f);throw _0x48321f;});}return _0x21d1e2;})[_0x0bcf('0x33')](respondWithResult(_0x1bf0dd,0xc9))['catch'](handleError(_0x1bf0dd,null));};exports[_0x0bcf('0x32')]=function(_0x107755,_0xbe4bc1,_0x5a0c99){var _0x35c6e1;return db['VoiceContext'][_0x0bcf('0x58')]({'where':{'id':_0x107755[_0x0bcf('0x57')]['id']}})[_0x0bcf('0x33')](handleEntityNotFound(_0xbe4bc1,null))[_0x0bcf('0x33')](saveUpdates(_0x107755[_0x0bcf('0x67')],null))[_0x0bcf('0x33')](function(_0x1a56ca){if(_0x1a56ca){_0x35c6e1=_0x1a56ca;return db[_0x0bcf('0x3b')][_0x0bcf('0x54')]({'raw':!![]});}})['then'](function(_0xef4981){var _0x3c68f9=fs[_0x0bcf('0x5a')](path[_0x0bcf('0x5b')](config[_0x0bcf('0x5c')],_0x0bcf('0x5d')),_0x0bcf('0x5e'));var _0x230eac=ejs[_0x0bcf('0x5f')](_0x3c68f9,{'contexts':_0xef4981||[]});fs[_0x0bcf('0x60')](_0x0bcf('0x68'),_0x230eac);})['then'](function(){return respondWithRpcPromise(_0x0bcf('0x61'),_0x0bcf('0x32'),{'module':_0x0bcf('0x69')});})[_0x0bcf('0x33')](function(){return _0x35c6e1;})[_0x0bcf('0x33')](respondWithResult(_0xbe4bc1,null))[_0x0bcf('0x28')](handleError(_0xbe4bc1,null));};exports['destroy']=function(_0x5da3d6,_0x35d579,_0x51ea28){var _0x7778ce;return db[_0x0bcf('0x3b')]['find']({'where':{'id':_0x5da3d6['params']['id']}})[_0x0bcf('0x33')](handleEntityNotFound(_0x35d579,null))[_0x0bcf('0x33')](removeEntity(_0x35d579,null))[_0x0bcf('0x33')](function(){return db['VoiceContext']['findAll']({'raw':!![]});})[_0x0bcf('0x33')](function(_0x326274){var _0x33f1be=fs[_0x0bcf('0x5a')](path[_0x0bcf('0x5b')](config[_0x0bcf('0x5c')],_0x0bcf('0x5d')),_0x0bcf('0x5e'));var _0x19ce08=ejs[_0x0bcf('0x5f')](_0x33f1be,{'contexts':_0x326274});fs['writeFileSync'](_0x0bcf('0x68'),_0x19ce08);return respondWithRpcPromise('Reload','destroy',{'module':_0x0bcf('0x69')});})[_0x0bcf('0x28')](handleError(_0x35d579,null));};