Built motion from commit (unavailable).|2.5.13
[motion2.git] / server / api / trunk / trunk.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 _0xff2b=['omit','createdAt','updatedAt','body','create','role','user','UserProfileSection','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','readFileSync','root','server/files/templates/trunk.ejs','server/files/templates/registry.ejs','utf8','render','writeFileSync','/etc/asterisk/sip_xcally_trunks.conf','registry','/etc/asterisk/sip_xcally_registers.conf','Reload','chan_sip.so','autoAssociation','join','html-pdf','eml-format','zip-dir','fast-json-patch','request-promise','moment','util','sox','to-csv','squel','crypto','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/environment','../../config/license/util','jayson/promise','client','http','request','info','Trunk,\x20%s,\x20%s','request\x20sent','debug','stringify','code','error','message','catch','sendStatus','status','json','count','offset','limit','set','Content-Range','apply','reject','save','update','then','destroy','get','Trunks','end','stack','name','index','Trunk','rawAttributes','fieldName','type','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','includeAll','include','findAll','rows','show','merge','options','find','clone'];(function(_0x1aa3fe,_0x27c5fa){var _0x163cee=function(_0x2a1505){while(--_0x2a1505){_0x1aa3fe['push'](_0x1aa3fe['shift']());}};_0x163cee(++_0x27c5fa);}(_0xff2b,0x159));var _0xbff2=function(_0x5c854f,_0x557516){_0x5c854f=_0x5c854f-0x0;var _0x5e1f97=_0xff2b[_0x5c854f];return _0x5e1f97;};'use strict';var pdf=require(_0xbff2('0x0'));var emlformat=require(_0xbff2('0x1'));var rimraf=require('rimraf');var zipdir=require(_0xbff2('0x2'));var jsonpatch=require(_0xbff2('0x3'));var rp=require(_0xbff2('0x4'));var moment=require(_0xbff2('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xbff2('0x6'));var path=require('path');var sox=require(_0xbff2('0x7'));var csv=require(_0xbff2('0x8'));var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var squel=require(_0xbff2('0x9'));var crypto=require(_0xbff2('0xa'));var jsforce=require('jsforce');var deskjs=require(_0xbff2('0xb'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0xbff2('0xc'));var Redis=require(_0xbff2('0xd'));var authService=require(_0xbff2('0xe'));var qs=require(_0xbff2('0xf'));var as=require(_0xbff2('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xbff2('0x11'))(_0xbff2('0x12'));var utils=require('../../config/utils');var config=require(_0xbff2('0x13'));var licenseUtil=require(_0xbff2('0x14'));var db=require('../../mysqldb')['db'];var jayson=require(_0xbff2('0x15'));var client=jayson[_0xbff2('0x16')][_0xbff2('0x17')]({'port':0x232a});function respondWithRpcPromise(_0x31b35c,_0x194e79,_0x46d380,_0x99d874){return new BPromise(function(_0x19dfea,_0x293cd5){var _0x410541=_0x99d874||client;return _0x410541[_0xbff2('0x18')](_0x31b35c,_0x46d380)['then'](function(_0x11d689){logger[_0xbff2('0x19')](_0xbff2('0x1a'),_0x194e79,_0xbff2('0x1b'));logger[_0xbff2('0x1c')]('Trunk,\x20%s,\x20%s,\x20%s',_0x194e79,_0xbff2('0x1b'),JSON[_0xbff2('0x1d')](_0x11d689));if(_0x11d689['error']){if(_0x11d689['error'][_0xbff2('0x1e')]===0x1f4){logger[_0xbff2('0x1f')](_0xbff2('0x1a'),_0x194e79,_0x11d689[_0xbff2('0x1f')][_0xbff2('0x20')]);return _0x293cd5(_0x11d689[_0xbff2('0x1f')][_0xbff2('0x20')]);}logger[_0xbff2('0x1f')](_0xbff2('0x1a'),_0x194e79,_0x11d689[_0xbff2('0x1f')][_0xbff2('0x20')]);return _0x19dfea(_0x11d689['error'][_0xbff2('0x20')]);}else{logger[_0xbff2('0x19')](_0xbff2('0x1a'),_0x194e79,'request\x20sent');_0x19dfea(_0x11d689['result'][_0xbff2('0x20')]);}})[_0xbff2('0x21')](function(_0x2707c5){logger[_0xbff2('0x1f')](_0xbff2('0x1a'),_0x194e79,_0x2707c5);_0x293cd5(_0x2707c5);});});}function respondWithStatusCode(_0x5a9194,_0xbb3b72){_0xbb3b72=_0xbb3b72||0xcc;return function(_0x4d5019){if(_0x4d5019){return _0x5a9194[_0xbff2('0x22')](_0xbb3b72);}return _0x5a9194[_0xbff2('0x23')](_0xbb3b72)['end']();};}function respondWithResult(_0x45dc72,_0x395e65){_0x395e65=_0x395e65||0xc8;return function(_0x3954d4){if(_0x3954d4){return _0x45dc72['status'](_0x395e65)[_0xbff2('0x24')](_0x3954d4);}};}function respondWithFilteredResult(_0x3048ff,_0x18e516){return function(_0x1d02cf){if(_0x1d02cf){var _0x65c481=_0x1d02cf[_0xbff2('0x25')],_0x120ddc=_0x18e516['offset'],_0x138071=_0x18e516[_0xbff2('0x26')]+_0x18e516[_0xbff2('0x27')],_0x471d3a;if(_0x138071>=_0x65c481){_0x138071=_0x65c481;_0x471d3a=0xc8;}else{_0x471d3a=0xce;}_0x3048ff[_0xbff2('0x23')](_0x471d3a);return _0x3048ff[_0xbff2('0x28')](_0xbff2('0x29'),_0x120ddc+'-'+_0x138071+'/'+_0x65c481)['json'](_0x1d02cf);}return null;};}function patchUpdates(_0x271883){return function(_0x5604b7){try{jsonpatch[_0xbff2('0x2a')](_0x5604b7,_0x271883,!![]);}catch(_0x29912e){return BPromise[_0xbff2('0x2b')](_0x29912e);}return _0x5604b7[_0xbff2('0x2c')]();};}function saveUpdates(_0x3bf1b3,_0x3fbdfd){return function(_0x168929){if(_0x168929){return _0x168929[_0xbff2('0x2d')](_0x3bf1b3)[_0xbff2('0x2e')](function(_0x459c7e){return _0x459c7e;});}return null;};}function removeEntity(_0x48d73e,_0x3d9860){return function(_0x489ff5){if(_0x489ff5){return _0x489ff5[_0xbff2('0x2f')]()[_0xbff2('0x2e')](function(){var _0x5312cb=_0x489ff5[_0xbff2('0x30')]({'plain':!![]});var _0x171928=_0xbff2('0x31');return db['UserProfileResource'][_0xbff2('0x2f')]({'where':{'type':_0x171928,'resourceId':_0x5312cb['id']}})[_0xbff2('0x2e')](function(){return _0x489ff5;});})['then'](function(){_0x48d73e[_0xbff2('0x23')](0xcc)[_0xbff2('0x32')]();});}};}function handleEntityNotFound(_0x3db3da,_0x4d4b91){return function(_0x588b10){if(!_0x588b10){_0x3db3da[_0xbff2('0x22')](0x194);}return _0x588b10;};}function handleError(_0x3e5eb5,_0x2489c1){_0x2489c1=_0x2489c1||0x1f4;return function(_0x5edaaa){logger[_0xbff2('0x1f')](_0x5edaaa[_0xbff2('0x33')]);if(_0x5edaaa[_0xbff2('0x34')]){delete _0x5edaaa[_0xbff2('0x34')];}_0x3e5eb5[_0xbff2('0x23')](_0x2489c1)['send'](_0x5edaaa);};}exports[_0xbff2('0x35')]=function(_0x26faf6,_0x3c3581){var _0x3482ae={},_0x2d8fb5={},_0x148119={'count':0x0,'rows':[]};var _0x2718b5=_['map'](db[_0xbff2('0x36')][_0xbff2('0x37')],function(_0x30348d){return{'name':_0x30348d[_0xbff2('0x38')],'type':_0x30348d[_0xbff2('0x39')]['key']};});_0x2d8fb5[_0xbff2('0x3a')]=_['map'](_0x2718b5,_0xbff2('0x34'));_0x2d8fb5[_0xbff2('0x3b')]=_[_0xbff2('0x3c')](_0x26faf6[_0xbff2('0x3b')]);_0x2d8fb5[_0xbff2('0x3d')]=_['intersection'](_0x2d8fb5[_0xbff2('0x3a')],_0x2d8fb5[_0xbff2('0x3b')]);_0x3482ae[_0xbff2('0x3e')]=_[_0xbff2('0x3f')](_0x2d8fb5[_0xbff2('0x3a')],qs[_0xbff2('0x40')](_0x26faf6['query']['fields']));_0x3482ae[_0xbff2('0x3e')]=_0x3482ae[_0xbff2('0x3e')][_0xbff2('0x41')]?_0x3482ae[_0xbff2('0x3e')]:_0x2d8fb5[_0xbff2('0x3a')];if(!_0x26faf6['query'][_0xbff2('0x42')](_0xbff2('0x43'))){_0x3482ae[_0xbff2('0x27')]=qs[_0xbff2('0x27')](_0x26faf6[_0xbff2('0x3b')][_0xbff2('0x27')]);_0x3482ae['offset']=qs[_0xbff2('0x26')](_0x26faf6[_0xbff2('0x3b')][_0xbff2('0x26')]);}_0x3482ae[_0xbff2('0x44')]=qs[_0xbff2('0x45')](_0x26faf6[_0xbff2('0x3b')][_0xbff2('0x45')]);_0x3482ae[_0xbff2('0x46')]=qs[_0xbff2('0x3d')](_[_0xbff2('0x47')](_0x26faf6[_0xbff2('0x3b')],_0x2d8fb5[_0xbff2('0x3d')]),_0x2718b5);if(_0x26faf6[_0xbff2('0x3b')][_0xbff2('0x48')]){_0x3482ae['where']=_['merge'](_0x3482ae[_0xbff2('0x46')],{'$or':_['map'](_0x2718b5,function(_0x42d854){if(_0x42d854['type']!==_0xbff2('0x49')){var _0x12a27b={};_0x12a27b[_0x42d854[_0xbff2('0x34')]]={'$like':'%'+_0x26faf6['query'][_0xbff2('0x48')]+'%'};return _0x12a27b;}})});}_0x3482ae=_['merge']({},_0x3482ae,_0x26faf6['options']);var _0x1e8ffb={'where':_0x3482ae[_0xbff2('0x46')]};return db[_0xbff2('0x36')][_0xbff2('0x25')](_0x1e8ffb)[_0xbff2('0x2e')](function(_0x38389f){_0x148119[_0xbff2('0x25')]=_0x38389f;if(_0x26faf6['query'][_0xbff2('0x4a')]){_0x3482ae[_0xbff2('0x4b')]=[{'all':!![]}];}return db[_0xbff2('0x36')][_0xbff2('0x4c')](_0x3482ae);})[_0xbff2('0x2e')](function(_0x1afcf9){_0x148119[_0xbff2('0x4d')]=_0x1afcf9;return _0x148119;})[_0xbff2('0x2e')](respondWithFilteredResult(_0x3c3581,_0x3482ae))[_0xbff2('0x21')](handleError(_0x3c3581,null));};exports[_0xbff2('0x4e')]=function(_0x3d8efd,_0x431afb){var _0x354bb5={'raw':![],'where':{'id':_0x3d8efd['params']['id']}},_0x180e94={};_0x180e94['model']=_[_0xbff2('0x3c')](db['Trunk']['rawAttributes']);_0x180e94[_0xbff2('0x3b')]=_['keys'](_0x3d8efd[_0xbff2('0x3b')]);_0x180e94[_0xbff2('0x3d')]=_['intersection'](_0x180e94['model'],_0x180e94['query']);_0x354bb5[_0xbff2('0x3e')]=_[_0xbff2('0x3f')](_0x180e94[_0xbff2('0x3a')],qs[_0xbff2('0x40')](_0x3d8efd[_0xbff2('0x3b')][_0xbff2('0x40')]));_0x354bb5[_0xbff2('0x3e')]=_0x354bb5[_0xbff2('0x3e')][_0xbff2('0x41')]?_0x354bb5[_0xbff2('0x3e')]:_0x180e94['model'];if(_0x3d8efd[_0xbff2('0x3b')][_0xbff2('0x4a')]){_0x354bb5[_0xbff2('0x4b')]=[{'all':!![]}];}_0x354bb5=_[_0xbff2('0x4f')]({},_0x354bb5,_0x3d8efd[_0xbff2('0x50')]);return db['Trunk'][_0xbff2('0x51')](_0x354bb5)[_0xbff2('0x2e')](handleEntityNotFound(_0x431afb,null))[_0xbff2('0x2e')](respondWithResult(_0x431afb,null))[_0xbff2('0x21')](handleError(_0x431afb,null));};exports[_0xbff2('0x52')]=function(_0x1f1573,_0x149d49){var _0x4b3501={'where':{'id':_0x1f1573['params']['id']}},_0x3d4b44={};_0x3d4b44[_0xbff2('0x3a')]=_[_0xbff2('0x3c')](db[_0xbff2('0x36')]['rawAttributes']);_0x4b3501[_0xbff2('0x3e')]=_[_0xbff2('0x3f')](_0x3d4b44[_0xbff2('0x3a')],qs[_0xbff2('0x40')](_0x1f1573[_0xbff2('0x3b')][_0xbff2('0x40')]));_0x4b3501['attributes']=_0x4b3501['attributes']['length']?_0x4b3501[_0xbff2('0x3e')]:_0x3d4b44[_0xbff2('0x3a')];if(_0x1f1573[_0xbff2('0x3b')]['includeAll']){_0x4b3501['include']=[{'all':!![]}];}_0x4b3501=_['merge']({},_0x4b3501,_0x1f1573[_0xbff2('0x50')]);return db['Trunk']['find'](_0x4b3501)[_0xbff2('0x2e')](handleEntityNotFound(_0x149d49,null))[_0xbff2('0x2e')](function(_0x5109d2){if(_0x5109d2){var _0x133ae3=_0x5109d2[_0xbff2('0x30')]({'plain':!![]});_0x133ae3=qs[_0xbff2('0x53')](_0x133ae3,['id',_0xbff2('0x54'),_0xbff2('0x55')]);_0x1f1573[_0xbff2('0x56')]=_['omit'](_0x1f1573['body'],['id','createdAt',_0xbff2('0x55')]);return db[_0xbff2('0x36')][_0xbff2('0x57')](_[_0xbff2('0x4f')](_0x133ae3,_0x1f1573[_0xbff2('0x56')]),{'include':_0x1f1573[_0xbff2('0x3b')][_0xbff2('0x4a')]?[{'all':!![]}]:undefined})[_0xbff2('0x2e')](function(_0x12907f){var _0x58bcde=_0x1f1573['user'][_0xbff2('0x30')]({'plain':!![]});if(!_0x58bcde)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x58bcde[_0xbff2('0x58')]===_0xbff2('0x59')){var _0x3c3472=_0x12907f[_0xbff2('0x30')]({'plain':!![]});var _0x3e90d5='Trunks';return db[_0xbff2('0x5a')][_0xbff2('0x51')]({'where':{'name':_0x3e90d5,'userProfileId':_0x58bcde[_0xbff2('0x5b')]},'raw':!![]})[_0xbff2('0x2e')](function(_0x264e12){if(_0x264e12&&_0x264e12['autoAssociation']===0x0){return db['UserProfileResource'][_0xbff2('0x57')]({'name':_0x3c3472[_0xbff2('0x34')],'resourceId':_0x3c3472['id'],'type':_0x264e12[_0xbff2('0x34')],'sectionId':_0x264e12['id']},{})[_0xbff2('0x2e')](function(){return _0x12907f;});}else{return _0x12907f;}})['catch'](function(_0x594e07){logger[_0xbff2('0x1f')](_0xbff2('0x5c'),_0x594e07);throw _0x594e07;});}return _0x12907f;});}})[_0xbff2('0x2e')](respondWithResult(_0x149d49,0xc9))[_0xbff2('0x21')](handleError(_0x149d49,null));};exports[_0xbff2('0x57')]=function(_0x2af76e,_0x4de19e,_0x119108){var _0x13d217;return db['Trunk'][_0xbff2('0x57')](_0x2af76e[_0xbff2('0x56')],{'raw':!![]})[_0xbff2('0x2e')](function(_0x42129d){_0x13d217=_0x42129d;return db[_0xbff2('0x36')]['findAll']({'raw':!![]});})[_0xbff2('0x2e')](function(_0x3860be){var _0xf3f450=fs[_0xbff2('0x5d')](path['join'](config[_0xbff2('0x5e')],_0xbff2('0x5f')),'utf8');var _0x4ae37c=fs[_0xbff2('0x5d')](path['join'](config[_0xbff2('0x5e')],_0xbff2('0x60')),_0xbff2('0x61'));var _0x203f5a=ejs[_0xbff2('0x62')](_0xf3f450,{'trunks':_0x3860be||[]});fs[_0xbff2('0x63')](_0xbff2('0x64'),_0x203f5a);if(_0x13d217[_0xbff2('0x65')]){var _0x27e08f=ejs[_0xbff2('0x62')](_0x4ae37c,{'trunks':_0x3860be||[]});fs['writeFileSync'](_0xbff2('0x66'),_0x27e08f);}})[_0xbff2('0x2e')](function(){return respondWithRpcPromise(_0xbff2('0x67'),_0xbff2('0x57'),{'module':_0xbff2('0x68')});})[_0xbff2('0x2e')](function(){var _0x296fd8=_0x2af76e[_0xbff2('0x59')][_0xbff2('0x30')]({'plain':!![]});if(!_0x296fd8)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x296fd8[_0xbff2('0x58')]===_0xbff2('0x59')){var _0xb3d272=_0x13d217[_0xbff2('0x30')]({'plain':!![]});return db[_0xbff2('0x5a')][_0xbff2('0x51')]({'where':{'name':'Trunks','userProfileId':_0x296fd8[_0xbff2('0x5b')]},'raw':!![]})[_0xbff2('0x2e')](function(_0x5c1f18){if(_0x5c1f18&&_0x5c1f18[_0xbff2('0x69')]===0x0){return db['UserProfileResource'][_0xbff2('0x57')]({'name':_0xb3d272['name'],'resourceId':_0xb3d272['id'],'type':_0x5c1f18[_0xbff2('0x34')],'sectionId':_0x5c1f18['id']},{})['then'](function(){return _0x13d217;});}else{return _0x13d217;}})[_0xbff2('0x21')](function(_0x5bb5c0){logger[_0xbff2('0x1f')](_0xbff2('0x5c'),_0x5bb5c0);throw _0x5bb5c0;});}return _0x13d217;})[_0xbff2('0x2e')](respondWithResult(_0x4de19e,0xc9))[_0xbff2('0x21')](handleError(_0x4de19e,null));};exports[_0xbff2('0x2d')]=function(_0x383a7,_0x2d8927,_0x144f2d){var _0x1680f2;return db['Trunk']['find']({'where':{'id':_0x383a7['params']['id']}})['then'](handleEntityNotFound(_0x2d8927,null))[_0xbff2('0x2e')](saveUpdates(_0x383a7[_0xbff2('0x56')],null))[_0xbff2('0x2e')](function(_0x1daacd){if(_0x1daacd){_0x1680f2=_0x1daacd;return db[_0xbff2('0x36')]['findAll']({'raw':!![]});}})[_0xbff2('0x2e')](function(_0x27a743){if(_0x27a743){var _0x1268aa=fs[_0xbff2('0x5d')](path[_0xbff2('0x6a')](config['root'],'server/files/templates/trunk.ejs'),_0xbff2('0x61'));var _0xccd026=fs[_0xbff2('0x5d')](path['join'](config[_0xbff2('0x5e')],_0xbff2('0x60')),_0xbff2('0x61'));var _0xdb35c4=ejs[_0xbff2('0x62')](_0x1268aa,{'trunks':_0x27a743||[]});fs[_0xbff2('0x63')]('/etc/asterisk/sip_xcally_trunks.conf',_0xdb35c4);var _0x289737=ejs[_0xbff2('0x62')](_0xccd026,{'trunks':_0x27a743||[]});fs[_0xbff2('0x63')](_0xbff2('0x66'),_0x289737);return respondWithRpcPromise(_0xbff2('0x67'),_0xbff2('0x2d'),{'module':_0xbff2('0x68')});}return null;})[_0xbff2('0x2e')](function(_0x17a1ef){if(_0x17a1ef){return _0x1680f2;}})['then'](respondWithResult(_0x2d8927,null))['catch'](handleError(_0x2d8927,null));};exports[_0xbff2('0x2f')]=function(_0x3e1bab,_0x28697c,_0x7c9f50){var _0x121455;return db[_0xbff2('0x36')][_0xbff2('0x51')]({'where':{'id':_0x3e1bab['params']['id']}})[_0xbff2('0x2e')](handleEntityNotFound(_0x28697c,null))['then'](removeEntity(_0x28697c,null))[_0xbff2('0x2e')](function(){return db[_0xbff2('0x36')][_0xbff2('0x4c')]({'raw':!![]});})[_0xbff2('0x2e')](function(_0x373fc4){var _0xb58276=fs[_0xbff2('0x5d')](path[_0xbff2('0x6a')](config[_0xbff2('0x5e')],_0xbff2('0x5f')),_0xbff2('0x61'));var _0x3aa6c1=fs[_0xbff2('0x5d')](path[_0xbff2('0x6a')](config[_0xbff2('0x5e')],'server/files/templates/registry.ejs'),_0xbff2('0x61'));var _0x2bf1c6=ejs[_0xbff2('0x62')](_0xb58276,{'trunks':_0x373fc4});var _0x523ce6=ejs['render'](_0x3aa6c1,{'trunks':_0x373fc4});fs[_0xbff2('0x63')]('/etc/asterisk/sip_xcally_trunks.conf',_0x2bf1c6);fs[_0xbff2('0x63')](_0xbff2('0x66'),_0x523ce6);return respondWithRpcPromise('Reload','destroy',{'module':_0xbff2('0x68')});})[_0xbff2('0x21')](handleError(_0x28697c,null));};