Built motion from commit 10af8726.|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 _0x020f=['zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','ejs','fs-extra','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','jayson/promise','client','http','request','info','VoiceContext,\x20%s,\x20%s','debug','VoiceContext,\x20%s,\x20%s,\x20%s','stringify','error','code','message','request\x20sent','result','sendStatus','status','end','json','offset','undefined','limit','count','set','apply','save','update','then','destroy','get','Contexts','UserProfileResource','stack','name','send','map','VoiceContext','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','sort','where','filter','merge','include','catch','show','length','options','create','findAll','readFileSync','join','root','server/files/templates/context.ejs','utf8','/etc/asterisk/extensions_xcally_contexts.conf','Reload','chan_sip.so','user','role','UserProfileSection','autoAssociation','find','writeFileSync','params','render','eml-format'];(function(_0x42ebba,_0x4cbf87){var _0x3fd2af=function(_0x26d999){while(--_0x26d999){_0x42ebba['push'](_0x42ebba['shift']());}};_0x3fd2af(++_0x4cbf87);}(_0x020f,0x193));var _0xf020=function(_0x19ca93,_0xf1ed11){_0x19ca93=_0x19ca93-0x0;var _0x318631=_0x020f[_0x19ca93];return _0x318631;};'use strict';var emlformat=require(_0xf020('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xf020('0x1'));var jsonpatch=require(_0xf020('0x2'));var rp=require(_0xf020('0x3'));var moment=require(_0xf020('0x4'));var BPromise=require(_0xf020('0x5'));var Mustache=require(_0xf020('0x6'));var util=require('util');var path=require(_0xf020('0x7'));var sox=require('sox');var csv=require(_0xf020('0x8'));var ejs=require(_0xf020('0x9'));var fs=require('fs');var fs_extra=require(_0xf020('0xa'));var _=require('lodash');var squel=require(_0xf020('0xb'));var crypto=require(_0xf020('0xc'));var jsforce=require('jsforce');var deskjs=require(_0xf020('0xd'));var toCsv=require(_0xf020('0x8'));var querystring=require(_0xf020('0xe'));var Papa=require(_0xf020('0xf'));var Redis=require(_0xf020('0x10'));var authService=require(_0xf020('0x11'));var qs=require(_0xf020('0x12'));var as=require(_0xf020('0x13'));var hardwareService=require(_0xf020('0x14'));var logger=require(_0xf020('0x15'))(_0xf020('0x16'));var utils=require('../../config/utils');var config=require(_0xf020('0x17'));var licenseUtil=require(_0xf020('0x18'));var db=require(_0xf020('0x19'))['db'];var jayson=require(_0xf020('0x1a'));var client=jayson[_0xf020('0x1b')][_0xf020('0x1c')]({'port':0x232a});function respondWithRpcPromise(_0x2dda04,_0x2a512e,_0x377e11,_0x1e8fc9){return new BPromise(function(_0x40c8db,_0x3dab37){var _0x370abf=_0x1e8fc9||client;return _0x370abf[_0xf020('0x1d')](_0x2dda04,_0x377e11)['then'](function(_0x7a747f){logger[_0xf020('0x1e')](_0xf020('0x1f'),_0x2a512e,'request\x20sent');logger[_0xf020('0x20')](_0xf020('0x21'),_0x2a512e,'request\x20sent',JSON[_0xf020('0x22')](_0x7a747f));if(_0x7a747f[_0xf020('0x23')]){if(_0x7a747f['error'][_0xf020('0x24')]===0x1f4){logger[_0xf020('0x23')](_0xf020('0x1f'),_0x2a512e,_0x7a747f['error'][_0xf020('0x25')]);return _0x3dab37(_0x7a747f[_0xf020('0x23')]['message']);}logger['error'](_0xf020('0x1f'),_0x2a512e,_0x7a747f['error'][_0xf020('0x25')]);return _0x40c8db(_0x7a747f['error'][_0xf020('0x25')]);}else{logger[_0xf020('0x1e')](_0xf020('0x1f'),_0x2a512e,_0xf020('0x26'));_0x40c8db(_0x7a747f[_0xf020('0x27')]['message']);}})['catch'](function(_0x4bde1e){logger[_0xf020('0x23')](_0xf020('0x1f'),_0x2a512e,_0x4bde1e);_0x3dab37(_0x4bde1e);});});}function respondWithStatusCode(_0x12ee4a,_0x25d26f){_0x25d26f=_0x25d26f||0xcc;return function(_0x5997fa){if(_0x5997fa){return _0x12ee4a[_0xf020('0x28')](_0x25d26f);}return _0x12ee4a[_0xf020('0x29')](_0x25d26f)[_0xf020('0x2a')]();};}function respondWithResult(_0x1d6cf8,_0x214351){_0x214351=_0x214351||0xc8;return function(_0x49abb3){if(_0x49abb3){return _0x1d6cf8['status'](_0x214351)[_0xf020('0x2b')](_0x49abb3);}};}function respondWithFilteredResult(_0x43d45b,_0x563612){return function(_0x390eaa){if(_0x390eaa){var _0x33568f=typeof _0x563612[_0xf020('0x2c')]===_0xf020('0x2d')&&typeof _0x563612[_0xf020('0x2e')]===_0xf020('0x2d');var _0x1b26d1=_0x390eaa[_0xf020('0x2f')];var _0x30ac24=_0x33568f?0x0:_0x563612['offset'];var _0x16a457=_0x33568f?_0x390eaa[_0xf020('0x2f')]:_0x563612['offset']+_0x563612['limit'];var _0x526798;if(_0x16a457>=_0x1b26d1){_0x16a457=_0x1b26d1;_0x526798=0xc8;}else{_0x526798=0xce;}_0x43d45b[_0xf020('0x29')](_0x526798);return _0x43d45b[_0xf020('0x30')]('Content-Range',_0x30ac24+'-'+_0x16a457+'/'+_0x1b26d1)[_0xf020('0x2b')](_0x390eaa);}return null;};}function patchUpdates(_0x5c79bd){return function(_0x4d8ec8){try{jsonpatch[_0xf020('0x31')](_0x4d8ec8,_0x5c79bd,!![]);}catch(_0x168ee4){return BPromise['reject'](_0x168ee4);}return _0x4d8ec8[_0xf020('0x32')]();};}function saveUpdates(_0x22f8bf,_0x24648c){return function(_0x26d915){if(_0x26d915){return _0x26d915[_0xf020('0x33')](_0x22f8bf)[_0xf020('0x34')](function(_0x1482d1){return _0x1482d1;});}return null;};}function removeEntity(_0x5ead00,_0x35de3b){return function(_0x2210fa){if(_0x2210fa){return _0x2210fa[_0xf020('0x35')]()[_0xf020('0x34')](function(){var _0x3c6571=_0x2210fa[_0xf020('0x36')]({'plain':!![]});var _0xa53818=_0xf020('0x37');return db[_0xf020('0x38')][_0xf020('0x35')]({'where':{'type':_0xa53818,'resourceId':_0x3c6571['id']}})['then'](function(){return _0x2210fa;});})['then'](function(){_0x5ead00[_0xf020('0x29')](0xcc)[_0xf020('0x2a')]();});}};}function handleEntityNotFound(_0x4ac3d2,_0x1aad6e){return function(_0x4f45d7){if(!_0x4f45d7){_0x4ac3d2[_0xf020('0x28')](0x194);}return _0x4f45d7;};}function handleError(_0x149b15,_0x3d1ae3){_0x3d1ae3=_0x3d1ae3||0x1f4;return function(_0x1d699f){logger[_0xf020('0x23')](_0x1d699f[_0xf020('0x39')]);if(_0x1d699f[_0xf020('0x3a')]){delete _0x1d699f[_0xf020('0x3a')];}_0x149b15['status'](_0x3d1ae3)[_0xf020('0x3b')](_0x1d699f);};}exports['index']=function(_0x30c2c2,_0x3fe0d2){var _0x3f3d69={},_0x123684={},_0x41248c={'count':0x0,'rows':[]};var _0x225a8e=_[_0xf020('0x3c')](db[_0xf020('0x3d')][_0xf020('0x3e')],function(_0x1f656b){return{'name':_0x1f656b[_0xf020('0x3f')],'type':_0x1f656b[_0xf020('0x40')][_0xf020('0x41')]};});_0x123684[_0xf020('0x42')]=_['map'](_0x225a8e,_0xf020('0x3a'));_0x123684[_0xf020('0x43')]=_[_0xf020('0x44')](_0x30c2c2[_0xf020('0x43')]);_0x123684[_0xf020('0x45')]=_[_0xf020('0x46')](_0x123684[_0xf020('0x42')],_0x123684[_0xf020('0x43')]);_0x3f3d69[_0xf020('0x47')]=_[_0xf020('0x46')](_0x123684['model'],qs[_0xf020('0x48')](_0x30c2c2['query'][_0xf020('0x48')]));_0x3f3d69[_0xf020('0x47')]=_0x3f3d69[_0xf020('0x47')]['length']?_0x3f3d69['attributes']:_0x123684[_0xf020('0x42')];if(!_0x30c2c2[_0xf020('0x43')][_0xf020('0x49')]('nolimit')){_0x3f3d69['limit']=qs[_0xf020('0x2e')](_0x30c2c2[_0xf020('0x43')]['limit']);_0x3f3d69[_0xf020('0x2c')]=qs[_0xf020('0x2c')](_0x30c2c2['query'][_0xf020('0x2c')]);}_0x3f3d69['order']=qs[_0xf020('0x4a')](_0x30c2c2['query'][_0xf020('0x4a')]);_0x3f3d69[_0xf020('0x4b')]=qs[_0xf020('0x45')](_['pick'](_0x30c2c2[_0xf020('0x43')],_0x123684[_0xf020('0x45')]),_0x225a8e);if(_0x30c2c2[_0xf020('0x43')][_0xf020('0x4c')]){_0x3f3d69[_0xf020('0x4b')]=_[_0xf020('0x4d')](_0x3f3d69[_0xf020('0x4b')],{'$or':_['map'](_0x225a8e,function(_0x5b0acd){if(_0x5b0acd['type']!=='VIRTUAL'){var _0x356efb={};_0x356efb[_0x5b0acd[_0xf020('0x3a')]]={'$like':'%'+_0x30c2c2['query'][_0xf020('0x4c')]+'%'};return _0x356efb;}})});}_0x3f3d69=_[_0xf020('0x4d')]({},_0x3f3d69,_0x30c2c2['options']);var _0x23236e={'where':_0x3f3d69[_0xf020('0x4b')]};return db[_0xf020('0x3d')]['count'](_0x23236e)[_0xf020('0x34')](function(_0x2985fc){_0x41248c[_0xf020('0x2f')]=_0x2985fc;if(_0x30c2c2[_0xf020('0x43')]['includeAll']){_0x3f3d69[_0xf020('0x4e')]=[{'all':!![]}];}return db[_0xf020('0x3d')]['findAll'](_0x3f3d69);})[_0xf020('0x34')](function(_0x4b359d){_0x41248c['rows']=_0x4b359d;return _0x41248c;})['then'](respondWithFilteredResult(_0x3fe0d2,_0x3f3d69))[_0xf020('0x4f')](handleError(_0x3fe0d2,null));};exports[_0xf020('0x50')]=function(_0x2af4b8,_0x2b3fa0){var _0x470092={'raw':![],'where':{'id':_0x2af4b8['params']['id']}},_0x32a170={};_0x32a170[_0xf020('0x42')]=_['keys'](db[_0xf020('0x3d')][_0xf020('0x3e')]);_0x32a170[_0xf020('0x43')]=_[_0xf020('0x44')](_0x2af4b8[_0xf020('0x43')]);_0x32a170['filters']=_[_0xf020('0x46')](_0x32a170[_0xf020('0x42')],_0x32a170[_0xf020('0x43')]);_0x470092[_0xf020('0x47')]=_[_0xf020('0x46')](_0x32a170[_0xf020('0x42')],qs[_0xf020('0x48')](_0x2af4b8[_0xf020('0x43')][_0xf020('0x48')]));_0x470092[_0xf020('0x47')]=_0x470092[_0xf020('0x47')][_0xf020('0x51')]?_0x470092[_0xf020('0x47')]:_0x32a170[_0xf020('0x42')];if(_0x2af4b8[_0xf020('0x43')]['includeAll']){_0x470092[_0xf020('0x4e')]=[{'all':!![]}];}_0x470092=_[_0xf020('0x4d')]({},_0x470092,_0x2af4b8[_0xf020('0x52')]);return db[_0xf020('0x3d')]['find'](_0x470092)['then'](handleEntityNotFound(_0x2b3fa0,null))[_0xf020('0x34')](respondWithResult(_0x2b3fa0,null))[_0xf020('0x4f')](handleError(_0x2b3fa0,null));};exports[_0xf020('0x53')]=function(_0x2eb663,_0x5597d4,_0x51ffd3){var _0x16d272;return db[_0xf020('0x3d')][_0xf020('0x53')](_0x2eb663['body'],{'raw':!![]})[_0xf020('0x34')](function(_0x341132){_0x16d272=_0x341132;return db[_0xf020('0x3d')][_0xf020('0x54')]({'raw':!![]});})[_0xf020('0x34')](function(_0x491109){var _0x101c35=fs[_0xf020('0x55')](path[_0xf020('0x56')](config[_0xf020('0x57')],_0xf020('0x58')),_0xf020('0x59'));var _0x36359b=ejs['render'](_0x101c35,{'contexts':_0x491109||[]});fs['writeFileSync'](_0xf020('0x5a'),_0x36359b);})[_0xf020('0x34')](function(){return respondWithRpcPromise(_0xf020('0x5b'),_0xf020('0x53'),{'module':_0xf020('0x5c')});})[_0xf020('0x34')](function(){var _0x4fb76d=_0x2eb663[_0xf020('0x5d')]['get']({'plain':!![]});if(!_0x4fb76d)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x4fb76d[_0xf020('0x5e')]===_0xf020('0x5d')){var _0x2cbadd=_0x16d272[_0xf020('0x36')]({'plain':!![]});return db[_0xf020('0x5f')]['find']({'where':{'name':'Contexts','userProfileId':_0x4fb76d['userProfileId']},'raw':!![]})[_0xf020('0x34')](function(_0x2f77a5){if(_0x2f77a5&&_0x2f77a5[_0xf020('0x60')]===0x0){return db['UserProfileResource'][_0xf020('0x53')]({'name':_0x2cbadd[_0xf020('0x3a')],'resourceId':_0x2cbadd['id'],'type':_0x2f77a5[_0xf020('0x3a')],'sectionId':_0x2f77a5['id']},{})[_0xf020('0x34')](function(){return _0x16d272;});}else{return _0x16d272;}})[_0xf020('0x4f')](function(_0x523c4f){logger[_0xf020('0x23')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x523c4f);throw _0x523c4f;});}return _0x16d272;})[_0xf020('0x34')](respondWithResult(_0x5597d4,0xc9))[_0xf020('0x4f')](handleError(_0x5597d4,null));};exports['update']=function(_0x38e4d5,_0x2513ed,_0x3cb5c3){var _0x3216ef;return db[_0xf020('0x3d')][_0xf020('0x61')]({'where':{'id':_0x38e4d5['params']['id']}})['then'](handleEntityNotFound(_0x2513ed,null))[_0xf020('0x34')](saveUpdates(_0x38e4d5['body'],null))[_0xf020('0x34')](function(_0x3b667e){if(_0x3b667e){_0x3216ef=_0x3b667e;return db['VoiceContext'][_0xf020('0x54')]({'raw':!![]});}})[_0xf020('0x34')](function(_0x4be0dc){var _0x25ac9c=fs[_0xf020('0x55')](path['join'](config[_0xf020('0x57')],'server/files/templates/context.ejs'),'utf8');var _0x57d6c5=ejs['render'](_0x25ac9c,{'contexts':_0x4be0dc||[]});fs[_0xf020('0x62')](_0xf020('0x5a'),_0x57d6c5);})[_0xf020('0x34')](function(){return respondWithRpcPromise(_0xf020('0x5b'),_0xf020('0x33'),{'module':_0xf020('0x5c')});})[_0xf020('0x34')](function(){return _0x3216ef;})[_0xf020('0x34')](respondWithResult(_0x2513ed,null))[_0xf020('0x4f')](handleError(_0x2513ed,null));};exports[_0xf020('0x35')]=function(_0x204876,_0x4e47eb,_0x4fcf5a){var _0x38a6ee;return db[_0xf020('0x3d')][_0xf020('0x61')]({'where':{'id':_0x204876[_0xf020('0x63')]['id']}})[_0xf020('0x34')](handleEntityNotFound(_0x4e47eb,null))[_0xf020('0x34')](removeEntity(_0x4e47eb,null))['then'](function(){return db[_0xf020('0x3d')]['findAll']({'raw':!![]});})[_0xf020('0x34')](function(_0x44ea1b){var _0x2c6358=fs[_0xf020('0x55')](path[_0xf020('0x56')](config[_0xf020('0x57')],_0xf020('0x58')),_0xf020('0x59'));var _0x133e54=ejs[_0xf020('0x64')](_0x2c6358,{'contexts':_0x44ea1b});fs[_0xf020('0x62')](_0xf020('0x5a'),_0x133e54);return respondWithRpcPromise(_0xf020('0x5b'),_0xf020('0x35'),{'module':_0xf020('0x5c')});})['catch'](handleError(_0x4e47eb,null));};