Built motion from commit d13fb11b.|2.6.34
[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 _0xb794=['sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','jayson/promise','client','http','request','then','info','VoiceContext,\x20%s,\x20%s','request\x20sent','debug','stringify','error','code','message','result','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','Contexts','UserProfileResource','destroy','sendStatus','stack','name','map','VoiceContext','rawAttributes','fieldName','key','model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','type','VIRTUAL','options','include','rows','catch','includeAll','find','create','body','readFileSync','root','utf8','render','/etc/asterisk/extensions_xcally_contexts.conf','Reload','chan_sip.so','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','findAll','join','server/files/templates/context.ejs','params','writeFileSync','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path'];(function(_0x1d5208,_0xc47693){var _0xd021a3=function(_0x2b74b0){while(--_0x2b74b0){_0x1d5208['push'](_0x1d5208['shift']());}};_0xd021a3(++_0xc47693);}(_0xb794,0x138));var _0x4b79=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xb794[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0x4b79('0x0'));var rimraf=require(_0x4b79('0x1'));var zipdir=require(_0x4b79('0x2'));var jsonpatch=require(_0x4b79('0x3'));var rp=require(_0x4b79('0x4'));var moment=require(_0x4b79('0x5'));var BPromise=require(_0x4b79('0x6'));var Mustache=require(_0x4b79('0x7'));var util=require('util');var path=require(_0x4b79('0x8'));var sox=require(_0x4b79('0x9'));var csv=require(_0x4b79('0xa'));var ejs=require(_0x4b79('0xb'));var fs=require('fs');var fs_extra=require(_0x4b79('0xc'));var _=require(_0x4b79('0xd'));var squel=require('squel');var crypto=require(_0x4b79('0xe'));var jsforce=require(_0x4b79('0xf'));var deskjs=require(_0x4b79('0x10'));var toCsv=require(_0x4b79('0xa'));var querystring=require(_0x4b79('0x11'));var Papa=require(_0x4b79('0x12'));var Redis=require('ioredis');var authService=require(_0x4b79('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x4b79('0x14'));var hardwareService=require(_0x4b79('0x15'));var logger=require('../../config/logger')(_0x4b79('0x16'));var utils=require(_0x4b79('0x17'));var config=require(_0x4b79('0x18'));var licenseUtil=require(_0x4b79('0x19'));var db=require(_0x4b79('0x1a'))['db'];var jayson=require(_0x4b79('0x1b'));var client=jayson[_0x4b79('0x1c')][_0x4b79('0x1d')]({'port':0x232a});function respondWithRpcPromise(_0x85e2e1,_0x5bcdb7,_0x325aca,_0x2ae8f6){return new BPromise(function(_0x342bbb,_0x4568fb){var _0x66dc18=_0x2ae8f6||client;return _0x66dc18[_0x4b79('0x1e')](_0x85e2e1,_0x325aca)[_0x4b79('0x1f')](function(_0x53acc4){logger[_0x4b79('0x20')](_0x4b79('0x21'),_0x5bcdb7,_0x4b79('0x22'));logger[_0x4b79('0x23')]('VoiceContext,\x20%s,\x20%s,\x20%s',_0x5bcdb7,_0x4b79('0x22'),JSON[_0x4b79('0x24')](_0x53acc4));if(_0x53acc4['error']){if(_0x53acc4[_0x4b79('0x25')][_0x4b79('0x26')]===0x1f4){logger[_0x4b79('0x25')](_0x4b79('0x21'),_0x5bcdb7,_0x53acc4[_0x4b79('0x25')][_0x4b79('0x27')]);return _0x4568fb(_0x53acc4['error'][_0x4b79('0x27')]);}logger['error']('VoiceContext,\x20%s,\x20%s',_0x5bcdb7,_0x53acc4[_0x4b79('0x25')][_0x4b79('0x27')]);return _0x342bbb(_0x53acc4[_0x4b79('0x25')][_0x4b79('0x27')]);}else{logger[_0x4b79('0x20')](_0x4b79('0x21'),_0x5bcdb7,_0x4b79('0x22'));_0x342bbb(_0x53acc4[_0x4b79('0x28')]['message']);}})['catch'](function(_0x4e4fec){logger[_0x4b79('0x25')]('VoiceContext,\x20%s,\x20%s',_0x5bcdb7,_0x4e4fec);_0x4568fb(_0x4e4fec);});});}function respondWithStatusCode(_0x1bb562,_0x15703a){_0x15703a=_0x15703a||0xcc;return function(_0x553708){if(_0x553708){return _0x1bb562['sendStatus'](_0x15703a);}return _0x1bb562[_0x4b79('0x29')](_0x15703a)[_0x4b79('0x2a')]();};}function respondWithResult(_0x1e2b18,_0x672d0a){_0x672d0a=_0x672d0a||0xc8;return function(_0x29adc1){if(_0x29adc1){return _0x1e2b18['status'](_0x672d0a)[_0x4b79('0x2b')](_0x29adc1);}};}function respondWithFilteredResult(_0x3d60c0,_0x1009cc){return function(_0x451dd2){if(_0x451dd2){var _0xda7c90=typeof _0x1009cc[_0x4b79('0x2c')]===_0x4b79('0x2d')&&typeof _0x1009cc[_0x4b79('0x2e')]===_0x4b79('0x2d');var _0x807baf=_0x451dd2[_0x4b79('0x2f')];var _0xe6fb9b=_0xda7c90?0x0:_0x1009cc['offset'];var _0x4afcd3=_0xda7c90?_0x451dd2[_0x4b79('0x2f')]:_0x1009cc[_0x4b79('0x2c')]+_0x1009cc[_0x4b79('0x2e')];var _0x2fa82d;if(_0x4afcd3>=_0x807baf){_0x4afcd3=_0x807baf;_0x2fa82d=0xc8;}else{_0x2fa82d=0xce;}_0x3d60c0[_0x4b79('0x29')](_0x2fa82d);return _0x3d60c0[_0x4b79('0x30')](_0x4b79('0x31'),_0xe6fb9b+'-'+_0x4afcd3+'/'+_0x807baf)[_0x4b79('0x2b')](_0x451dd2);}return null;};}function patchUpdates(_0x302b8f){return function(_0x3576f0){try{jsonpatch['apply'](_0x3576f0,_0x302b8f,!![]);}catch(_0x14773e){return BPromise[_0x4b79('0x32')](_0x14773e);}return _0x3576f0[_0x4b79('0x33')]();};}function saveUpdates(_0x906b99,_0x11bb52){return function(_0x430896){if(_0x430896){return _0x430896[_0x4b79('0x34')](_0x906b99)[_0x4b79('0x1f')](function(_0x3bb082){return _0x3bb082;});}return null;};}function removeEntity(_0xf02c06,_0xba367f){return function(_0xe28818){if(_0xe28818){return _0xe28818['destroy']()['then'](function(){var _0x20e37f=_0xe28818['get']({'plain':!![]});var _0x563d68=_0x4b79('0x35');return db[_0x4b79('0x36')][_0x4b79('0x37')]({'where':{'type':_0x563d68,'resourceId':_0x20e37f['id']}})[_0x4b79('0x1f')](function(){return _0xe28818;});})[_0x4b79('0x1f')](function(){_0xf02c06[_0x4b79('0x29')](0xcc)[_0x4b79('0x2a')]();});}};}function handleEntityNotFound(_0x2d2f8b,_0x56f34c){return function(_0x19e4f1){if(!_0x19e4f1){_0x2d2f8b[_0x4b79('0x38')](0x194);}return _0x19e4f1;};}function handleError(_0x3eb0c8,_0x3afcb5){_0x3afcb5=_0x3afcb5||0x1f4;return function(_0x4a9913){logger['error'](_0x4a9913[_0x4b79('0x39')]);if(_0x4a9913['name']){delete _0x4a9913[_0x4b79('0x3a')];}_0x3eb0c8[_0x4b79('0x29')](_0x3afcb5)['send'](_0x4a9913);};}exports['index']=function(_0x180f75,_0x564c45){var _0x58e928={},_0x2021ae={},_0x53668b={'count':0x0,'rows':[]};var _0x5ae8a9=_[_0x4b79('0x3b')](db[_0x4b79('0x3c')][_0x4b79('0x3d')],function(_0x4631db){return{'name':_0x4631db[_0x4b79('0x3e')],'type':_0x4631db['type'][_0x4b79('0x3f')]};});_0x2021ae[_0x4b79('0x40')]=_[_0x4b79('0x3b')](_0x5ae8a9,_0x4b79('0x3a'));_0x2021ae[_0x4b79('0x41')]=_[_0x4b79('0x42')](_0x180f75['query']);_0x2021ae[_0x4b79('0x43')]=_[_0x4b79('0x44')](_0x2021ae[_0x4b79('0x40')],_0x2021ae[_0x4b79('0x41')]);_0x58e928[_0x4b79('0x45')]=_['intersection'](_0x2021ae[_0x4b79('0x40')],qs['fields'](_0x180f75['query'][_0x4b79('0x46')]));_0x58e928[_0x4b79('0x45')]=_0x58e928[_0x4b79('0x45')]['length']?_0x58e928['attributes']:_0x2021ae[_0x4b79('0x40')];if(!_0x180f75[_0x4b79('0x41')][_0x4b79('0x47')](_0x4b79('0x48'))){_0x58e928[_0x4b79('0x2e')]=qs[_0x4b79('0x2e')](_0x180f75[_0x4b79('0x41')][_0x4b79('0x2e')]);_0x58e928[_0x4b79('0x2c')]=qs[_0x4b79('0x2c')](_0x180f75[_0x4b79('0x41')][_0x4b79('0x2c')]);}_0x58e928[_0x4b79('0x49')]=qs[_0x4b79('0x4a')](_0x180f75[_0x4b79('0x41')][_0x4b79('0x4a')]);_0x58e928[_0x4b79('0x4b')]=qs['filters'](_[_0x4b79('0x4c')](_0x180f75['query'],_0x2021ae[_0x4b79('0x43')]),_0x5ae8a9);if(_0x180f75[_0x4b79('0x41')][_0x4b79('0x4d')]){_0x58e928['where']=_[_0x4b79('0x4e')](_0x58e928['where'],{'$or':_[_0x4b79('0x3b')](_0x5ae8a9,function(_0x16a1a0){if(_0x16a1a0[_0x4b79('0x4f')]!==_0x4b79('0x50')){var _0x3124d2={};_0x3124d2[_0x16a1a0[_0x4b79('0x3a')]]={'$like':'%'+_0x180f75[_0x4b79('0x41')][_0x4b79('0x4d')]+'%'};return _0x3124d2;}})});}_0x58e928=_['merge']({},_0x58e928,_0x180f75[_0x4b79('0x51')]);var _0x1cef94={'where':_0x58e928[_0x4b79('0x4b')]};return db[_0x4b79('0x3c')][_0x4b79('0x2f')](_0x1cef94)[_0x4b79('0x1f')](function(_0x3b9a79){_0x53668b[_0x4b79('0x2f')]=_0x3b9a79;if(_0x180f75[_0x4b79('0x41')]['includeAll']){_0x58e928[_0x4b79('0x52')]=[{'all':!![]}];}return db[_0x4b79('0x3c')]['findAll'](_0x58e928);})[_0x4b79('0x1f')](function(_0x41cad2){_0x53668b[_0x4b79('0x53')]=_0x41cad2;return _0x53668b;})['then'](respondWithFilteredResult(_0x564c45,_0x58e928))[_0x4b79('0x54')](handleError(_0x564c45,null));};exports['show']=function(_0x918392,_0x51174c){var _0x46fc88={'raw':![],'where':{'id':_0x918392['params']['id']}},_0x40974c={};_0x40974c[_0x4b79('0x40')]=_[_0x4b79('0x42')](db['VoiceContext'][_0x4b79('0x3d')]);_0x40974c[_0x4b79('0x41')]=_[_0x4b79('0x42')](_0x918392[_0x4b79('0x41')]);_0x40974c[_0x4b79('0x43')]=_[_0x4b79('0x44')](_0x40974c[_0x4b79('0x40')],_0x40974c[_0x4b79('0x41')]);_0x46fc88[_0x4b79('0x45')]=_[_0x4b79('0x44')](_0x40974c[_0x4b79('0x40')],qs[_0x4b79('0x46')](_0x918392[_0x4b79('0x41')][_0x4b79('0x46')]));_0x46fc88[_0x4b79('0x45')]=_0x46fc88[_0x4b79('0x45')]['length']?_0x46fc88[_0x4b79('0x45')]:_0x40974c['model'];if(_0x918392[_0x4b79('0x41')][_0x4b79('0x55')]){_0x46fc88[_0x4b79('0x52')]=[{'all':!![]}];}_0x46fc88=_[_0x4b79('0x4e')]({},_0x46fc88,_0x918392['options']);return db[_0x4b79('0x3c')][_0x4b79('0x56')](_0x46fc88)[_0x4b79('0x1f')](handleEntityNotFound(_0x51174c,null))[_0x4b79('0x1f')](respondWithResult(_0x51174c,null))[_0x4b79('0x54')](handleError(_0x51174c,null));};exports[_0x4b79('0x57')]=function(_0x364762,_0x2d5944,_0x2904d7){var _0x164090;return db[_0x4b79('0x3c')]['create'](_0x364762[_0x4b79('0x58')],{'raw':!![]})[_0x4b79('0x1f')](function(_0x52ade0){_0x164090=_0x52ade0;return db[_0x4b79('0x3c')]['findAll']({'raw':!![]});})[_0x4b79('0x1f')](function(_0x51bd02){var _0x4505f6=fs[_0x4b79('0x59')](path['join'](config[_0x4b79('0x5a')],'server/files/templates/context.ejs'),_0x4b79('0x5b'));var _0x469c77=ejs[_0x4b79('0x5c')](_0x4505f6,{'contexts':_0x51bd02||[]});fs['writeFileSync'](_0x4b79('0x5d'),_0x469c77);})['then'](function(){return respondWithRpcPromise(_0x4b79('0x5e'),_0x4b79('0x57'),{'module':_0x4b79('0x5f')});})['then'](function(){var _0x48e554=_0x364762[_0x4b79('0x60')][_0x4b79('0x61')]({'plain':!![]});if(!_0x48e554)throw new Error(_0x4b79('0x62'));if(_0x48e554[_0x4b79('0x63')]===_0x4b79('0x60')){var _0x1cf04b=_0x164090[_0x4b79('0x61')]({'plain':!![]});return db[_0x4b79('0x64')][_0x4b79('0x56')]({'where':{'name':_0x4b79('0x35'),'userProfileId':_0x48e554['userProfileId']},'raw':!![]})[_0x4b79('0x1f')](function(_0x2891d9){if(_0x2891d9&&_0x2891d9[_0x4b79('0x65')]===0x0){return db[_0x4b79('0x36')]['create']({'name':_0x1cf04b[_0x4b79('0x3a')],'resourceId':_0x1cf04b['id'],'type':_0x2891d9['name'],'sectionId':_0x2891d9['id']},{})[_0x4b79('0x1f')](function(){return _0x164090;});}else{return _0x164090;}})['catch'](function(_0x3ea1d3){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3ea1d3);throw _0x3ea1d3;});}return _0x164090;})[_0x4b79('0x1f')](respondWithResult(_0x2d5944,0xc9))['catch'](handleError(_0x2d5944,null));};exports[_0x4b79('0x34')]=function(_0x571ed0,_0x497ffa,_0x4bc229){var _0x3256fa;return db[_0x4b79('0x3c')][_0x4b79('0x56')]({'where':{'id':_0x571ed0['params']['id']}})['then'](handleEntityNotFound(_0x497ffa,null))[_0x4b79('0x1f')](saveUpdates(_0x571ed0[_0x4b79('0x58')],null))['then'](function(_0x2e3048){if(_0x2e3048){_0x3256fa=_0x2e3048;return db[_0x4b79('0x3c')][_0x4b79('0x66')]({'raw':!![]});}})[_0x4b79('0x1f')](function(_0x130dac){var _0x3d9562=fs['readFileSync'](path[_0x4b79('0x67')](config[_0x4b79('0x5a')],_0x4b79('0x68')),_0x4b79('0x5b'));var _0x363542=ejs['render'](_0x3d9562,{'contexts':_0x130dac||[]});fs['writeFileSync'](_0x4b79('0x5d'),_0x363542);})[_0x4b79('0x1f')](function(){return respondWithRpcPromise(_0x4b79('0x5e'),'update',{'module':_0x4b79('0x5f')});})[_0x4b79('0x1f')](function(){return _0x3256fa;})[_0x4b79('0x1f')](respondWithResult(_0x497ffa,null))[_0x4b79('0x54')](handleError(_0x497ffa,null));};exports[_0x4b79('0x37')]=function(_0x5ecb5e,_0x106362,_0x41173c){var _0x4b90aa;return db[_0x4b79('0x3c')][_0x4b79('0x56')]({'where':{'id':_0x5ecb5e[_0x4b79('0x69')]['id']}})[_0x4b79('0x1f')](handleEntityNotFound(_0x106362,null))[_0x4b79('0x1f')](removeEntity(_0x106362,null))[_0x4b79('0x1f')](function(){return db[_0x4b79('0x3c')][_0x4b79('0x66')]({'raw':!![]});})[_0x4b79('0x1f')](function(_0xe56bef){var _0x3353b3=fs[_0x4b79('0x59')](path[_0x4b79('0x67')](config['root'],'server/files/templates/context.ejs'),'utf8');var _0x2db4e4=ejs[_0x4b79('0x5c')](_0x3353b3,{'contexts':_0xe56bef});fs[_0x4b79('0x6a')]('/etc/asterisk/extensions_xcally_contexts.conf',_0x2db4e4);return respondWithRpcPromise('Reload','destroy',{'module':_0x4b79('0x5f')});})[_0x4b79('0x54')](handleError(_0x106362,null));};