aa331647c30ec566f5dd7c4640a57ea4767d362d
[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 _0xf405=['Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','findAll','utf8','chan_sip.so','eml-format','zip-dir','request-promise','moment','mustache','sox','ejs','lodash','squel','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','client','request','info','VoiceContext,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','status','undefined','limit','count','Content-Range','apply','save','update','then','get','Contexts','UserProfileResource','destroy','end','stack','name','index','rawAttributes','fieldName','type','key','map','keys','filters','intersection','model','query','fields','attributes','length','nolimit','offset','sort','merge','where','VIRTUAL','filter','options','VoiceContext','includeAll','include','rows','show','params','find','create','readFileSync','join','root','server/files/templates/context.ejs','render','writeFileSync','/etc/asterisk/extensions_xcally_contexts.conf','Reload','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId'];(function(_0x96691d,_0x544a9d){var _0x5a79ca=function(_0x48421a){while(--_0x48421a){_0x96691d['push'](_0x96691d['shift']());}};_0x5a79ca(++_0x544a9d);}(_0xf405,0xc9));var _0x5f40=function(_0x4e00b7,_0x59e15a){_0x4e00b7=_0x4e00b7-0x0;var _0x297f6f=_0xf405[_0x4e00b7];return _0x297f6f;};'use strict';var emlformat=require(_0x5f40('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x5f40('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x5f40('0x2'));var moment=require(_0x5f40('0x3'));var BPromise=require('bluebird');var Mustache=require(_0x5f40('0x4'));var util=require('util');var path=require('path');var sox=require(_0x5f40('0x5'));var csv=require('to-csv');var ejs=require(_0x5f40('0x6'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x5f40('0x7'));var squel=require(_0x5f40('0x8'));var crypto=require('crypto');var jsforce=require(_0x5f40('0x9'));var deskjs=require(_0x5f40('0xa'));var toCsv=require(_0x5f40('0xb'));var querystring=require(_0x5f40('0xc'));var Papa=require(_0x5f40('0xd'));var Redis=require(_0x5f40('0xe'));var authService=require('../../components/auth/service');var qs=require(_0x5f40('0xf'));var as=require(_0x5f40('0x10'));var hardwareService=require(_0x5f40('0x11'));var logger=require('../../config/logger')(_0x5f40('0x12'));var utils=require(_0x5f40('0x13'));var config=require(_0x5f40('0x14'));var licenseUtil=require(_0x5f40('0x15'));var db=require(_0x5f40('0x16'))['db'];var jayson=require('jayson/promise');var client=jayson[_0x5f40('0x17')]['http']({'port':0x232a});function respondWithRpcPromise(_0x19fcea,_0x1acde8,_0x5a20e1,_0x37ee8e){return new BPromise(function(_0x1205ae,_0x32e5c0){var _0x3e55aa=_0x37ee8e||client;return _0x3e55aa[_0x5f40('0x18')](_0x19fcea,_0x5a20e1)['then'](function(_0x2e6fce){logger[_0x5f40('0x19')](_0x5f40('0x1a'),_0x1acde8,'request\x20sent');logger['debug']('VoiceContext,\x20%s,\x20%s,\x20%s',_0x1acde8,_0x5f40('0x1b'),JSON[_0x5f40('0x1c')](_0x2e6fce));if(_0x2e6fce[_0x5f40('0x1d')]){if(_0x2e6fce[_0x5f40('0x1d')][_0x5f40('0x1e')]===0x1f4){logger['error'](_0x5f40('0x1a'),_0x1acde8,_0x2e6fce[_0x5f40('0x1d')][_0x5f40('0x1f')]);return _0x32e5c0(_0x2e6fce[_0x5f40('0x1d')][_0x5f40('0x1f')]);}logger[_0x5f40('0x1d')](_0x5f40('0x1a'),_0x1acde8,_0x2e6fce[_0x5f40('0x1d')]['message']);return _0x1205ae(_0x2e6fce[_0x5f40('0x1d')]['message']);}else{logger[_0x5f40('0x19')]('VoiceContext,\x20%s,\x20%s',_0x1acde8,_0x5f40('0x1b'));_0x1205ae(_0x2e6fce[_0x5f40('0x20')][_0x5f40('0x1f')]);}})[_0x5f40('0x21')](function(_0x402666){logger[_0x5f40('0x1d')](_0x5f40('0x1a'),_0x1acde8,_0x402666);_0x32e5c0(_0x402666);});});}function respondWithStatusCode(_0x556aa0,_0x2906e6){_0x2906e6=_0x2906e6||0xcc;return function(_0x16cd9e){if(_0x16cd9e){return _0x556aa0['sendStatus'](_0x2906e6);}return _0x556aa0[_0x5f40('0x22')](_0x2906e6)['end']();};}function respondWithResult(_0x13e26a,_0x455df9){_0x455df9=_0x455df9||0xc8;return function(_0x473bc9){if(_0x473bc9){return _0x13e26a[_0x5f40('0x22')](_0x455df9)['json'](_0x473bc9);}};}function respondWithFilteredResult(_0x2382e5,_0x7a6a61){return function(_0x38320a){if(_0x38320a){var _0x9fe561=typeof _0x7a6a61['offset']===_0x5f40('0x23')&&typeof _0x7a6a61[_0x5f40('0x24')]===_0x5f40('0x23');var _0x366b26=_0x38320a[_0x5f40('0x25')];var _0x3b9bc9=_0x9fe561?0x0:_0x7a6a61['offset'];var _0xcf5f8b=_0x9fe561?_0x38320a[_0x5f40('0x25')]:_0x7a6a61['offset']+_0x7a6a61[_0x5f40('0x24')];var _0x3848a;if(_0xcf5f8b>=_0x366b26){_0xcf5f8b=_0x366b26;_0x3848a=0xc8;}else{_0x3848a=0xce;}_0x2382e5[_0x5f40('0x22')](_0x3848a);return _0x2382e5['set'](_0x5f40('0x26'),_0x3b9bc9+'-'+_0xcf5f8b+'/'+_0x366b26)['json'](_0x38320a);}return null;};}function patchUpdates(_0x370aed){return function(_0x54ea24){try{jsonpatch[_0x5f40('0x27')](_0x54ea24,_0x370aed,!![]);}catch(_0x4bd8ec){return BPromise['reject'](_0x4bd8ec);}return _0x54ea24[_0x5f40('0x28')]();};}function saveUpdates(_0x4fc44a,_0x12615b){return function(_0x359cf8){if(_0x359cf8){return _0x359cf8[_0x5f40('0x29')](_0x4fc44a)[_0x5f40('0x2a')](function(_0x333350){return _0x333350;});}return null;};}function removeEntity(_0x21afcf,_0x53edb0){return function(_0x3fe4cc){if(_0x3fe4cc){return _0x3fe4cc['destroy']()[_0x5f40('0x2a')](function(){var _0x3217ec=_0x3fe4cc[_0x5f40('0x2b')]({'plain':!![]});var _0x532f79=_0x5f40('0x2c');return db[_0x5f40('0x2d')][_0x5f40('0x2e')]({'where':{'type':_0x532f79,'resourceId':_0x3217ec['id']}})[_0x5f40('0x2a')](function(){return _0x3fe4cc;});})[_0x5f40('0x2a')](function(){_0x21afcf['status'](0xcc)[_0x5f40('0x2f')]();});}};}function handleEntityNotFound(_0x508cb4,_0x51f7d9){return function(_0x266488){if(!_0x266488){_0x508cb4['sendStatus'](0x194);}return _0x266488;};}function handleError(_0x5bc510,_0x442cf4){_0x442cf4=_0x442cf4||0x1f4;return function(_0x342fe5){logger['error'](_0x342fe5[_0x5f40('0x30')]);if(_0x342fe5[_0x5f40('0x31')]){delete _0x342fe5[_0x5f40('0x31')];}_0x5bc510[_0x5f40('0x22')](_0x442cf4)['send'](_0x342fe5);};}exports[_0x5f40('0x32')]=function(_0x3f6cd3,_0x366934){var _0xf5753d={},_0x2380e0={},_0x360b54={'count':0x0,'rows':[]};var _0x131ef9=_['map'](db['VoiceContext'][_0x5f40('0x33')],function(_0x232eb5){return{'name':_0x232eb5[_0x5f40('0x34')],'type':_0x232eb5[_0x5f40('0x35')][_0x5f40('0x36')]};});_0x2380e0['model']=_[_0x5f40('0x37')](_0x131ef9,_0x5f40('0x31'));_0x2380e0['query']=_[_0x5f40('0x38')](_0x3f6cd3['query']);_0x2380e0[_0x5f40('0x39')]=_[_0x5f40('0x3a')](_0x2380e0[_0x5f40('0x3b')],_0x2380e0[_0x5f40('0x3c')]);_0xf5753d['attributes']=_['intersection'](_0x2380e0['model'],qs[_0x5f40('0x3d')](_0x3f6cd3[_0x5f40('0x3c')]['fields']));_0xf5753d[_0x5f40('0x3e')]=_0xf5753d[_0x5f40('0x3e')][_0x5f40('0x3f')]?_0xf5753d[_0x5f40('0x3e')]:_0x2380e0[_0x5f40('0x3b')];if(!_0x3f6cd3['query']['hasOwnProperty'](_0x5f40('0x40'))){_0xf5753d['limit']=qs['limit'](_0x3f6cd3[_0x5f40('0x3c')]['limit']);_0xf5753d[_0x5f40('0x41')]=qs[_0x5f40('0x41')](_0x3f6cd3[_0x5f40('0x3c')]['offset']);}_0xf5753d['order']=qs[_0x5f40('0x42')](_0x3f6cd3[_0x5f40('0x3c')][_0x5f40('0x42')]);_0xf5753d['where']=qs[_0x5f40('0x39')](_['pick'](_0x3f6cd3[_0x5f40('0x3c')],_0x2380e0[_0x5f40('0x39')]),_0x131ef9);if(_0x3f6cd3[_0x5f40('0x3c')]['filter']){_0xf5753d['where']=_[_0x5f40('0x43')](_0xf5753d[_0x5f40('0x44')],{'$or':_[_0x5f40('0x37')](_0x131ef9,function(_0x100cb2){if(_0x100cb2[_0x5f40('0x35')]!==_0x5f40('0x45')){var _0x433af0={};_0x433af0[_0x100cb2[_0x5f40('0x31')]]={'$like':'%'+_0x3f6cd3[_0x5f40('0x3c')][_0x5f40('0x46')]+'%'};return _0x433af0;}})});}_0xf5753d=_[_0x5f40('0x43')]({},_0xf5753d,_0x3f6cd3[_0x5f40('0x47')]);var _0x295697={'where':_0xf5753d[_0x5f40('0x44')]};return db[_0x5f40('0x48')][_0x5f40('0x25')](_0x295697)[_0x5f40('0x2a')](function(_0x103c16){_0x360b54['count']=_0x103c16;if(_0x3f6cd3[_0x5f40('0x3c')][_0x5f40('0x49')]){_0xf5753d[_0x5f40('0x4a')]=[{'all':!![]}];}return db[_0x5f40('0x48')]['findAll'](_0xf5753d);})[_0x5f40('0x2a')](function(_0x45fa0f){_0x360b54[_0x5f40('0x4b')]=_0x45fa0f;return _0x360b54;})[_0x5f40('0x2a')](respondWithFilteredResult(_0x366934,_0xf5753d))[_0x5f40('0x21')](handleError(_0x366934,null));};exports[_0x5f40('0x4c')]=function(_0x40a6a7,_0x3bd459){var _0x1f9e8b={'raw':![],'where':{'id':_0x40a6a7[_0x5f40('0x4d')]['id']}},_0x5799a2={};_0x5799a2[_0x5f40('0x3b')]=_[_0x5f40('0x38')](db[_0x5f40('0x48')][_0x5f40('0x33')]);_0x5799a2[_0x5f40('0x3c')]=_[_0x5f40('0x38')](_0x40a6a7[_0x5f40('0x3c')]);_0x5799a2['filters']=_['intersection'](_0x5799a2[_0x5f40('0x3b')],_0x5799a2['query']);_0x1f9e8b[_0x5f40('0x3e')]=_[_0x5f40('0x3a')](_0x5799a2[_0x5f40('0x3b')],qs[_0x5f40('0x3d')](_0x40a6a7['query'][_0x5f40('0x3d')]));_0x1f9e8b[_0x5f40('0x3e')]=_0x1f9e8b[_0x5f40('0x3e')][_0x5f40('0x3f')]?_0x1f9e8b[_0x5f40('0x3e')]:_0x5799a2[_0x5f40('0x3b')];if(_0x40a6a7[_0x5f40('0x3c')][_0x5f40('0x49')]){_0x1f9e8b[_0x5f40('0x4a')]=[{'all':!![]}];}_0x1f9e8b=_[_0x5f40('0x43')]({},_0x1f9e8b,_0x40a6a7[_0x5f40('0x47')]);return db[_0x5f40('0x48')][_0x5f40('0x4e')](_0x1f9e8b)['then'](handleEntityNotFound(_0x3bd459,null))['then'](respondWithResult(_0x3bd459,null))[_0x5f40('0x21')](handleError(_0x3bd459,null));};exports[_0x5f40('0x4f')]=function(_0x2d6280,_0x4196a2,_0x1d12b3){var _0x16dee6;return db['VoiceContext'][_0x5f40('0x4f')](_0x2d6280['body'],{'raw':!![]})['then'](function(_0x1a6ccc){_0x16dee6=_0x1a6ccc;return db[_0x5f40('0x48')]['findAll']({'raw':!![]});})[_0x5f40('0x2a')](function(_0x3d0185){var _0x251bcf=fs[_0x5f40('0x50')](path[_0x5f40('0x51')](config[_0x5f40('0x52')],_0x5f40('0x53')),'utf8');var _0x3b9708=ejs[_0x5f40('0x54')](_0x251bcf,{'contexts':_0x3d0185||[]});fs[_0x5f40('0x55')](_0x5f40('0x56'),_0x3b9708);})[_0x5f40('0x2a')](function(){return respondWithRpcPromise(_0x5f40('0x57'),'create',{'module':'chan_sip.so'});})['then'](function(){var _0x5786e1=_0x2d6280[_0x5f40('0x58')]['get']({'plain':!![]});if(!_0x5786e1)throw new Error(_0x5f40('0x59'));if(_0x5786e1[_0x5f40('0x5a')]===_0x5f40('0x58')){var _0x2bf30e=_0x16dee6['get']({'plain':!![]});return db[_0x5f40('0x5b')][_0x5f40('0x4e')]({'where':{'name':_0x5f40('0x2c'),'userProfileId':_0x5786e1[_0x5f40('0x5c')]},'raw':!![]})[_0x5f40('0x2a')](function(_0xb22f0a){if(_0xb22f0a&&_0xb22f0a['autoAssociation']===0x0){return db[_0x5f40('0x2d')][_0x5f40('0x4f')]({'name':_0x2bf30e['name'],'resourceId':_0x2bf30e['id'],'type':_0xb22f0a[_0x5f40('0x31')],'sectionId':_0xb22f0a['id']},{})['then'](function(){return _0x16dee6;});}else{return _0x16dee6;}})['catch'](function(_0x246416){logger['error'](_0x5f40('0x5d'),_0x246416);throw _0x246416;});}return _0x16dee6;})[_0x5f40('0x2a')](respondWithResult(_0x4196a2,0xc9))['catch'](handleError(_0x4196a2,null));};exports['update']=function(_0x23d836,_0x1b2f3e,_0x5dd535){var _0x3eda55;return db[_0x5f40('0x48')][_0x5f40('0x4e')]({'where':{'id':_0x23d836[_0x5f40('0x4d')]['id']}})[_0x5f40('0x2a')](handleEntityNotFound(_0x1b2f3e,null))['then'](saveUpdates(_0x23d836[_0x5f40('0x5e')],null))['then'](function(_0x3327ca){if(_0x3327ca){_0x3eda55=_0x3327ca;return db[_0x5f40('0x48')][_0x5f40('0x5f')]({'raw':!![]});}})['then'](function(_0x1ae4aa){var _0xc5615a=fs[_0x5f40('0x50')](path[_0x5f40('0x51')](config[_0x5f40('0x52')],_0x5f40('0x53')),_0x5f40('0x60'));var _0x1f1224=ejs[_0x5f40('0x54')](_0xc5615a,{'contexts':_0x1ae4aa||[]});fs[_0x5f40('0x55')](_0x5f40('0x56'),_0x1f1224);})[_0x5f40('0x2a')](function(){return respondWithRpcPromise('Reload',_0x5f40('0x29'),{'module':_0x5f40('0x61')});})[_0x5f40('0x2a')](function(){return _0x3eda55;})[_0x5f40('0x2a')](respondWithResult(_0x1b2f3e,null))[_0x5f40('0x21')](handleError(_0x1b2f3e,null));};exports[_0x5f40('0x2e')]=function(_0x21f901,_0x54a2ae,_0x5216dd){var _0x13aafb;return db[_0x5f40('0x48')][_0x5f40('0x4e')]({'where':{'id':_0x21f901[_0x5f40('0x4d')]['id']}})[_0x5f40('0x2a')](handleEntityNotFound(_0x54a2ae,null))[_0x5f40('0x2a')](removeEntity(_0x54a2ae,null))[_0x5f40('0x2a')](function(){return db[_0x5f40('0x48')][_0x5f40('0x5f')]({'raw':!![]});})[_0x5f40('0x2a')](function(_0x545124){var _0x4cf03f=fs[_0x5f40('0x50')](path[_0x5f40('0x51')](config[_0x5f40('0x52')],_0x5f40('0x53')),_0x5f40('0x60'));var _0x4fbde4=ejs[_0x5f40('0x54')](_0x4cf03f,{'contexts':_0x545124});fs['writeFileSync'](_0x5f40('0x56'),_0x4fbde4);return respondWithRpcPromise('Reload',_0x5f40('0x2e'),{'module':_0x5f40('0x61')});})[_0x5f40('0x21')](handleError(_0x54a2ae,null));};