57dff2c50e7bf5c2c6086e9adcb303a1556cc3a3
[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 _0xb3b8=['render','writeFileSync','/etc/asterisk/extensions_xcally_contexts.conf','Reload','chan_sip.so','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Contexts','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','root','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','client','request','info','VoiceContext,\x20%s,\x20%s','request\x20sent','error','code','message','sendStatus','end','status','json','offset','undefined','limit','count','set','Content-Range','apply','update','then','destroy','get','UserProfileResource','stack','name','send','index','map','fieldName','type','key','model','query','keys','filters','attributes','intersection','fields','length','nolimit','order','sort','where','pick','VIRTUAL','merge','options','VoiceContext','includeAll','include','findAll','catch','show','params','rawAttributes','find','create','body','readFileSync','join','server/files/templates/context.ejs','utf8'];(function(_0xa1f13a,_0x2ba089){var _0x58c53c=function(_0x40cb0d){while(--_0x40cb0d){_0xa1f13a['push'](_0xa1f13a['shift']());}};_0x58c53c(++_0x2ba089);}(_0xb3b8,0x72));var _0x8b3b=function(_0x4fdcc0,_0x560a01){_0x4fdcc0=_0x4fdcc0-0x0;var _0xc0ce94=_0xb3b8[_0x4fdcc0];return _0xc0ce94;};'use strict';var emlformat=require(_0x8b3b('0x0'));var rimraf=require(_0x8b3b('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x8b3b('0x2'));var rp=require('request-promise');var moment=require(_0x8b3b('0x3'));var BPromise=require(_0x8b3b('0x4'));var Mustache=require(_0x8b3b('0x5'));var util=require('util');var path=require(_0x8b3b('0x6'));var sox=require(_0x8b3b('0x7'));var csv=require('to-csv');var ejs=require(_0x8b3b('0x8'));var fs=require('fs');var fs_extra=require(_0x8b3b('0x9'));var _=require(_0x8b3b('0xa'));var squel=require(_0x8b3b('0xb'));var crypto=require(_0x8b3b('0xc'));var jsforce=require(_0x8b3b('0xd'));var deskjs=require(_0x8b3b('0xe'));var toCsv=require(_0x8b3b('0xf'));var querystring=require(_0x8b3b('0x10'));var Papa=require(_0x8b3b('0x11'));var Redis=require('ioredis');var authService=require(_0x8b3b('0x12'));var qs=require(_0x8b3b('0x13'));var as=require(_0x8b3b('0x14'));var hardwareService=require(_0x8b3b('0x15'));var logger=require(_0x8b3b('0x16'))(_0x8b3b('0x17'));var utils=require('../../config/utils');var config=require(_0x8b3b('0x18'));var licenseUtil=require(_0x8b3b('0x19'));var db=require(_0x8b3b('0x1a'))['db'];var jayson=require('jayson/promise');var client=jayson[_0x8b3b('0x1b')]['http']({'port':0x232a});function respondWithRpcPromise(_0x4b0c14,_0x41985a,_0x17e9a0,_0x30777a){return new BPromise(function(_0x198f2b,_0x2ac535){var _0x2ab54f=_0x30777a||client;return _0x2ab54f[_0x8b3b('0x1c')](_0x4b0c14,_0x17e9a0)['then'](function(_0x1d994b){logger[_0x8b3b('0x1d')](_0x8b3b('0x1e'),_0x41985a,_0x8b3b('0x1f'));logger['debug']('VoiceContext,\x20%s,\x20%s,\x20%s',_0x41985a,'request\x20sent',JSON['stringify'](_0x1d994b));if(_0x1d994b[_0x8b3b('0x20')]){if(_0x1d994b[_0x8b3b('0x20')][_0x8b3b('0x21')]===0x1f4){logger[_0x8b3b('0x20')](_0x8b3b('0x1e'),_0x41985a,_0x1d994b[_0x8b3b('0x20')]['message']);return _0x2ac535(_0x1d994b['error'][_0x8b3b('0x22')]);}logger[_0x8b3b('0x20')]('VoiceContext,\x20%s,\x20%s',_0x41985a,_0x1d994b[_0x8b3b('0x20')][_0x8b3b('0x22')]);return _0x198f2b(_0x1d994b[_0x8b3b('0x20')][_0x8b3b('0x22')]);}else{logger[_0x8b3b('0x1d')]('VoiceContext,\x20%s,\x20%s',_0x41985a,_0x8b3b('0x1f'));_0x198f2b(_0x1d994b['result']['message']);}})['catch'](function(_0x5ad5f2){logger[_0x8b3b('0x20')](_0x8b3b('0x1e'),_0x41985a,_0x5ad5f2);_0x2ac535(_0x5ad5f2);});});}function respondWithStatusCode(_0x3d737c,_0x4890f7){_0x4890f7=_0x4890f7||0xcc;return function(_0x4ea101){if(_0x4ea101){return _0x3d737c[_0x8b3b('0x23')](_0x4890f7);}return _0x3d737c['status'](_0x4890f7)[_0x8b3b('0x24')]();};}function respondWithResult(_0x239096,_0x16f5b3){_0x16f5b3=_0x16f5b3||0xc8;return function(_0x57f2ae){if(_0x57f2ae){return _0x239096[_0x8b3b('0x25')](_0x16f5b3)[_0x8b3b('0x26')](_0x57f2ae);}};}function respondWithFilteredResult(_0x4cb8c5,_0x18f6b1){return function(_0x285462){if(_0x285462){var _0x31244b=typeof _0x18f6b1[_0x8b3b('0x27')]===_0x8b3b('0x28')&&typeof _0x18f6b1[_0x8b3b('0x29')]===_0x8b3b('0x28');var _0x433dbd=_0x285462[_0x8b3b('0x2a')];var _0x5b61c6=_0x31244b?0x0:_0x18f6b1[_0x8b3b('0x27')];var _0x113b30=_0x31244b?_0x285462[_0x8b3b('0x2a')]:_0x18f6b1['offset']+_0x18f6b1[_0x8b3b('0x29')];var _0xfb115e;if(_0x113b30>=_0x433dbd){_0x113b30=_0x433dbd;_0xfb115e=0xc8;}else{_0xfb115e=0xce;}_0x4cb8c5[_0x8b3b('0x25')](_0xfb115e);return _0x4cb8c5[_0x8b3b('0x2b')](_0x8b3b('0x2c'),_0x5b61c6+'-'+_0x113b30+'/'+_0x433dbd)[_0x8b3b('0x26')](_0x285462);}return null;};}function patchUpdates(_0x5ef37a){return function(_0x5d5b49){try{jsonpatch[_0x8b3b('0x2d')](_0x5d5b49,_0x5ef37a,!![]);}catch(_0x2594c1){return BPromise['reject'](_0x2594c1);}return _0x5d5b49['save']();};}function saveUpdates(_0x3daf24,_0x483ac9){return function(_0x31f352){if(_0x31f352){return _0x31f352[_0x8b3b('0x2e')](_0x3daf24)[_0x8b3b('0x2f')](function(_0x1d808e){return _0x1d808e;});}return null;};}function removeEntity(_0x219c1c,_0x3426cc){return function(_0x14baca){if(_0x14baca){return _0x14baca[_0x8b3b('0x30')]()[_0x8b3b('0x2f')](function(){var _0x27b01f=_0x14baca[_0x8b3b('0x31')]({'plain':!![]});var _0x3e64a6='Contexts';return db[_0x8b3b('0x32')]['destroy']({'where':{'type':_0x3e64a6,'resourceId':_0x27b01f['id']}})[_0x8b3b('0x2f')](function(){return _0x14baca;});})[_0x8b3b('0x2f')](function(){_0x219c1c[_0x8b3b('0x25')](0xcc)[_0x8b3b('0x24')]();});}};}function handleEntityNotFound(_0x3012f3,_0x12dd12){return function(_0x208869){if(!_0x208869){_0x3012f3[_0x8b3b('0x23')](0x194);}return _0x208869;};}function handleError(_0x487a22,_0x29be8b){_0x29be8b=_0x29be8b||0x1f4;return function(_0x5464af){logger[_0x8b3b('0x20')](_0x5464af[_0x8b3b('0x33')]);if(_0x5464af[_0x8b3b('0x34')]){delete _0x5464af[_0x8b3b('0x34')];}_0x487a22[_0x8b3b('0x25')](_0x29be8b)[_0x8b3b('0x35')](_0x5464af);};}exports[_0x8b3b('0x36')]=function(_0x548744,_0x5f0b5c){var _0xb20483={},_0x4efa20={},_0x3bd149={'count':0x0,'rows':[]};var _0x2dcdd7=_[_0x8b3b('0x37')](db['VoiceContext']['rawAttributes'],function(_0x2ba74d){return{'name':_0x2ba74d[_0x8b3b('0x38')],'type':_0x2ba74d[_0x8b3b('0x39')][_0x8b3b('0x3a')]};});_0x4efa20[_0x8b3b('0x3b')]=_['map'](_0x2dcdd7,'name');_0x4efa20[_0x8b3b('0x3c')]=_[_0x8b3b('0x3d')](_0x548744[_0x8b3b('0x3c')]);_0x4efa20[_0x8b3b('0x3e')]=_['intersection'](_0x4efa20['model'],_0x4efa20[_0x8b3b('0x3c')]);_0xb20483[_0x8b3b('0x3f')]=_[_0x8b3b('0x40')](_0x4efa20['model'],qs[_0x8b3b('0x41')](_0x548744['query'][_0x8b3b('0x41')]));_0xb20483[_0x8b3b('0x3f')]=_0xb20483[_0x8b3b('0x3f')][_0x8b3b('0x42')]?_0xb20483[_0x8b3b('0x3f')]:_0x4efa20[_0x8b3b('0x3b')];if(!_0x548744['query']['hasOwnProperty'](_0x8b3b('0x43'))){_0xb20483[_0x8b3b('0x29')]=qs['limit'](_0x548744[_0x8b3b('0x3c')][_0x8b3b('0x29')]);_0xb20483[_0x8b3b('0x27')]=qs[_0x8b3b('0x27')](_0x548744['query']['offset']);}_0xb20483[_0x8b3b('0x44')]=qs[_0x8b3b('0x45')](_0x548744[_0x8b3b('0x3c')][_0x8b3b('0x45')]);_0xb20483[_0x8b3b('0x46')]=qs['filters'](_[_0x8b3b('0x47')](_0x548744[_0x8b3b('0x3c')],_0x4efa20[_0x8b3b('0x3e')]),_0x2dcdd7);if(_0x548744[_0x8b3b('0x3c')]['filter']){_0xb20483['where']=_['merge'](_0xb20483[_0x8b3b('0x46')],{'$or':_[_0x8b3b('0x37')](_0x2dcdd7,function(_0x26dfab){if(_0x26dfab['type']!==_0x8b3b('0x48')){var _0x48d8dd={};_0x48d8dd[_0x26dfab[_0x8b3b('0x34')]]={'$like':'%'+_0x548744['query']['filter']+'%'};return _0x48d8dd;}})});}_0xb20483=_[_0x8b3b('0x49')]({},_0xb20483,_0x548744[_0x8b3b('0x4a')]);var _0x44a7b7={'where':_0xb20483[_0x8b3b('0x46')]};return db[_0x8b3b('0x4b')][_0x8b3b('0x2a')](_0x44a7b7)[_0x8b3b('0x2f')](function(_0x4bf338){_0x3bd149[_0x8b3b('0x2a')]=_0x4bf338;if(_0x548744[_0x8b3b('0x3c')][_0x8b3b('0x4c')]){_0xb20483[_0x8b3b('0x4d')]=[{'all':!![]}];}return db[_0x8b3b('0x4b')][_0x8b3b('0x4e')](_0xb20483);})[_0x8b3b('0x2f')](function(_0x513ec6){_0x3bd149['rows']=_0x513ec6;return _0x3bd149;})[_0x8b3b('0x2f')](respondWithFilteredResult(_0x5f0b5c,_0xb20483))[_0x8b3b('0x4f')](handleError(_0x5f0b5c,null));};exports[_0x8b3b('0x50')]=function(_0x493c40,_0x45598d){var _0x1f946a={'raw':![],'where':{'id':_0x493c40[_0x8b3b('0x51')]['id']}},_0x5d5f68={};_0x5d5f68[_0x8b3b('0x3b')]=_[_0x8b3b('0x3d')](db[_0x8b3b('0x4b')][_0x8b3b('0x52')]);_0x5d5f68[_0x8b3b('0x3c')]=_[_0x8b3b('0x3d')](_0x493c40[_0x8b3b('0x3c')]);_0x5d5f68['filters']=_[_0x8b3b('0x40')](_0x5d5f68['model'],_0x5d5f68['query']);_0x1f946a['attributes']=_[_0x8b3b('0x40')](_0x5d5f68[_0x8b3b('0x3b')],qs['fields'](_0x493c40['query'][_0x8b3b('0x41')]));_0x1f946a[_0x8b3b('0x3f')]=_0x1f946a[_0x8b3b('0x3f')]['length']?_0x1f946a[_0x8b3b('0x3f')]:_0x5d5f68[_0x8b3b('0x3b')];if(_0x493c40[_0x8b3b('0x3c')][_0x8b3b('0x4c')]){_0x1f946a['include']=[{'all':!![]}];}_0x1f946a=_[_0x8b3b('0x49')]({},_0x1f946a,_0x493c40[_0x8b3b('0x4a')]);return db[_0x8b3b('0x4b')][_0x8b3b('0x53')](_0x1f946a)[_0x8b3b('0x2f')](handleEntityNotFound(_0x45598d,null))[_0x8b3b('0x2f')](respondWithResult(_0x45598d,null))[_0x8b3b('0x4f')](handleError(_0x45598d,null));};exports[_0x8b3b('0x54')]=function(_0x5c5286,_0x2264a8,_0x4a083c){var _0x3c77ac;return db[_0x8b3b('0x4b')][_0x8b3b('0x54')](_0x5c5286[_0x8b3b('0x55')],{'raw':!![]})[_0x8b3b('0x2f')](function(_0x420bc3){_0x3c77ac=_0x420bc3;return db[_0x8b3b('0x4b')][_0x8b3b('0x4e')]({'raw':!![]});})[_0x8b3b('0x2f')](function(_0x2df4d6){var _0x590a4f=fs[_0x8b3b('0x56')](path[_0x8b3b('0x57')](config['root'],_0x8b3b('0x58')),_0x8b3b('0x59'));var _0xc8212b=ejs[_0x8b3b('0x5a')](_0x590a4f,{'contexts':_0x2df4d6||[]});fs[_0x8b3b('0x5b')](_0x8b3b('0x5c'),_0xc8212b);})[_0x8b3b('0x2f')](function(){return respondWithRpcPromise(_0x8b3b('0x5d'),_0x8b3b('0x54'),{'module':_0x8b3b('0x5e')});})[_0x8b3b('0x2f')](function(){var _0xef29f8=_0x5c5286['user'][_0x8b3b('0x31')]({'plain':!![]});if(!_0xef29f8)throw new Error(_0x8b3b('0x5f'));if(_0xef29f8['role']==='user'){var _0x5cc87d=_0x3c77ac['get']({'plain':!![]});return db[_0x8b3b('0x60')]['find']({'where':{'name':_0x8b3b('0x61'),'userProfileId':_0xef29f8[_0x8b3b('0x62')]},'raw':!![]})[_0x8b3b('0x2f')](function(_0x49eabd){if(_0x49eabd&&_0x49eabd[_0x8b3b('0x63')]===0x0){return db[_0x8b3b('0x32')][_0x8b3b('0x54')]({'name':_0x5cc87d[_0x8b3b('0x34')],'resourceId':_0x5cc87d['id'],'type':_0x49eabd[_0x8b3b('0x34')],'sectionId':_0x49eabd['id']},{})[_0x8b3b('0x2f')](function(){return _0x3c77ac;});}else{return _0x3c77ac;}})[_0x8b3b('0x4f')](function(_0x6e7bd8){logger[_0x8b3b('0x20')](_0x8b3b('0x64'),_0x6e7bd8);throw _0x6e7bd8;});}return _0x3c77ac;})[_0x8b3b('0x2f')](respondWithResult(_0x2264a8,0xc9))[_0x8b3b('0x4f')](handleError(_0x2264a8,null));};exports[_0x8b3b('0x2e')]=function(_0x35590e,_0x5b5d09,_0x3d0781){var _0x3c928d;return db['VoiceContext']['find']({'where':{'id':_0x35590e[_0x8b3b('0x51')]['id']}})[_0x8b3b('0x2f')](handleEntityNotFound(_0x5b5d09,null))['then'](saveUpdates(_0x35590e[_0x8b3b('0x55')],null))[_0x8b3b('0x2f')](function(_0x4358c8){if(_0x4358c8){_0x3c928d=_0x4358c8;return db['VoiceContext'][_0x8b3b('0x4e')]({'raw':!![]});}})[_0x8b3b('0x2f')](function(_0x2b9d6c){var _0xb4c784=fs[_0x8b3b('0x56')](path[_0x8b3b('0x57')](config['root'],'server/files/templates/context.ejs'),_0x8b3b('0x59'));var _0x49666f=ejs[_0x8b3b('0x5a')](_0xb4c784,{'contexts':_0x2b9d6c||[]});fs['writeFileSync'](_0x8b3b('0x5c'),_0x49666f);})[_0x8b3b('0x2f')](function(){return respondWithRpcPromise(_0x8b3b('0x5d'),_0x8b3b('0x2e'),{'module':_0x8b3b('0x5e')});})[_0x8b3b('0x2f')](function(){return _0x3c928d;})['then'](respondWithResult(_0x5b5d09,null))[_0x8b3b('0x4f')](handleError(_0x5b5d09,null));};exports[_0x8b3b('0x30')]=function(_0x56b8ab,_0x30a24f,_0x1e92fc){var _0x2ca69c;return db[_0x8b3b('0x4b')][_0x8b3b('0x53')]({'where':{'id':_0x56b8ab[_0x8b3b('0x51')]['id']}})[_0x8b3b('0x2f')](handleEntityNotFound(_0x30a24f,null))[_0x8b3b('0x2f')](removeEntity(_0x30a24f,null))[_0x8b3b('0x2f')](function(){return db['VoiceContext'][_0x8b3b('0x4e')]({'raw':!![]});})[_0x8b3b('0x2f')](function(_0x428be7){var _0x37b552=fs[_0x8b3b('0x56')](path['join'](config[_0x8b3b('0x65')],_0x8b3b('0x58')),_0x8b3b('0x59'));var _0x148dd6=ejs[_0x8b3b('0x5a')](_0x37b552,{'contexts':_0x428be7});fs[_0x8b3b('0x5b')]('/etc/asterisk/extensions_xcally_contexts.conf',_0x148dd6);return respondWithRpcPromise(_0x8b3b('0x5d'),_0x8b3b('0x30'),{'module':_0x8b3b('0x5e')});})[_0x8b3b('0x4f')](handleError(_0x30a24f,null));};