53e02f41a0304a335925beb3f3005f03789cfbf4
[motion2.git] / server / api / intServicenowConfiguration / intServicenowConfiguration.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 _0x341f=['fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','set','Content-Range','apply','destroy','then','error','stack','name','send','index','map','ServicenowConfiguration','fieldName','type','key','query','keys','model','attributes','intersection','length','nolimit','order','where','filters','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','show','params','rawAttributes','fields','find','catch','update','body','getFields','findOne','ServicenowField','sort','pick','hasOwnProperty','getSubjects','getDescriptions','eml-format','rimraf'];(function(_0x497bc3,_0x26e5d9){var _0x5285c0=function(_0x344f11){while(--_0x344f11){_0x497bc3['push'](_0x497bc3['shift']());}};_0x5285c0(++_0x26e5d9);}(_0x341f,0x13e));var _0xf341=function(_0x190e66,_0x5e2e71){_0x190e66=_0x190e66-0x0;var _0x124779=_0x341f[_0x190e66];return _0x124779;};'use strict';var emlformat=require(_0xf341('0x0'));var rimraf=require(_0xf341('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xf341('0x2'));var rp=require(_0xf341('0x3'));var moment=require(_0xf341('0x4'));var BPromise=require('bluebird');var Mustache=require(_0xf341('0x5'));var util=require(_0xf341('0x6'));var path=require(_0xf341('0x7'));var sox=require(_0xf341('0x8'));var csv=require(_0xf341('0x9'));var ejs=require(_0xf341('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xf341('0xb'));var squel=require('squel');var crypto=require(_0xf341('0xc'));var jsforce=require('jsforce');var deskjs=require(_0xf341('0xd'));var toCsv=require(_0xf341('0x9'));var querystring=require(_0xf341('0xe'));var Papa=require(_0xf341('0xf'));var Redis=require(_0xf341('0x10'));var authService=require(_0xf341('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0xf341('0x12'));var hardwareService=require(_0xf341('0x13'));var logger=require(_0xf341('0x14'))(_0xf341('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xf341('0x16'));var db=require(_0xf341('0x17'))['db'];function respondWithStatusCode(_0x3feb09,_0x452eeb){_0x452eeb=_0x452eeb||0xcc;return function(_0x47cc3a){if(_0x47cc3a){return _0x3feb09[_0xf341('0x18')](_0x452eeb);}return _0x3feb09[_0xf341('0x19')](_0x452eeb)[_0xf341('0x1a')]();};}function respondWithResult(_0x37849e,_0x2610f2){_0x2610f2=_0x2610f2||0xc8;return function(_0x36308f){if(_0x36308f){return _0x37849e[_0xf341('0x19')](_0x2610f2)[_0xf341('0x1b')](_0x36308f);}};}function respondWithFilteredResult(_0x2b3239,_0x2e088b){return function(_0x52251d){if(_0x52251d){var _0x3a0d3e=typeof _0x2e088b[_0xf341('0x1c')]===_0xf341('0x1d')&&typeof _0x2e088b[_0xf341('0x1e')]===_0xf341('0x1d');var _0x43d1ee=_0x52251d['count'];var _0xddc4ec=_0x3a0d3e?0x0:_0x2e088b['offset'];var _0x546ce7=_0x3a0d3e?_0x52251d['count']:_0x2e088b[_0xf341('0x1c')]+_0x2e088b[_0xf341('0x1e')];var _0x7e9196;if(_0x546ce7>=_0x43d1ee){_0x546ce7=_0x43d1ee;_0x7e9196=0xc8;}else{_0x7e9196=0xce;}_0x2b3239['status'](_0x7e9196);return _0x2b3239[_0xf341('0x1f')](_0xf341('0x20'),_0xddc4ec+'-'+_0x546ce7+'/'+_0x43d1ee)['json'](_0x52251d);}return null;};}function patchUpdates(_0x236ae8){return function(_0x3f2a51){try{jsonpatch[_0xf341('0x21')](_0x3f2a51,_0x236ae8,!![]);}catch(_0x1b638f){return BPromise['reject'](_0x1b638f);}return _0x3f2a51['save']();};}function saveUpdates(_0x13be02,_0x3eaa4b){return function(_0x59ed4e){if(_0x59ed4e){return _0x59ed4e['update'](_0x13be02)['then'](function(_0x1df6f0){return _0x1df6f0;});}return null;};}function removeEntity(_0x34b1ca,_0x43deee){return function(_0x22c3ef){if(_0x22c3ef){return _0x22c3ef[_0xf341('0x22')]()[_0xf341('0x23')](function(){_0x34b1ca['status'](0xcc)[_0xf341('0x1a')]();});}};}function handleEntityNotFound(_0x5dfeee,_0x24a2a1){return function(_0x79d44c){if(!_0x79d44c){_0x5dfeee[_0xf341('0x18')](0x194);}return _0x79d44c;};}function handleError(_0x3ae9ce,_0x52c3a4){_0x52c3a4=_0x52c3a4||0x1f4;return function(_0x47025){logger[_0xf341('0x24')](_0x47025[_0xf341('0x25')]);if(_0x47025[_0xf341('0x26')]){delete _0x47025[_0xf341('0x26')];}_0x3ae9ce[_0xf341('0x19')](_0x52c3a4)[_0xf341('0x27')](_0x47025);};}exports[_0xf341('0x28')]=function(_0x49c656,_0x5ef570){var _0x1de3d7={},_0x3e8ac8={},_0x2644c0={'count':0x0,'rows':[]};var _0x3c868f=_[_0xf341('0x29')](db[_0xf341('0x2a')]['rawAttributes'],function(_0x12e875){return{'name':_0x12e875[_0xf341('0x2b')],'type':_0x12e875[_0xf341('0x2c')][_0xf341('0x2d')]};});_0x3e8ac8['model']=_[_0xf341('0x29')](_0x3c868f,_0xf341('0x26'));_0x3e8ac8[_0xf341('0x2e')]=_[_0xf341('0x2f')](_0x49c656[_0xf341('0x2e')]);_0x3e8ac8['filters']=_['intersection'](_0x3e8ac8[_0xf341('0x30')],_0x3e8ac8[_0xf341('0x2e')]);_0x1de3d7[_0xf341('0x31')]=_[_0xf341('0x32')](_0x3e8ac8[_0xf341('0x30')],qs['fields'](_0x49c656[_0xf341('0x2e')]['fields']));_0x1de3d7[_0xf341('0x31')]=_0x1de3d7[_0xf341('0x31')][_0xf341('0x33')]?_0x1de3d7[_0xf341('0x31')]:_0x3e8ac8[_0xf341('0x30')];if(!_0x49c656[_0xf341('0x2e')]['hasOwnProperty'](_0xf341('0x34'))){_0x1de3d7[_0xf341('0x1e')]=qs[_0xf341('0x1e')](_0x49c656[_0xf341('0x2e')]['limit']);_0x1de3d7[_0xf341('0x1c')]=qs[_0xf341('0x1c')](_0x49c656[_0xf341('0x2e')][_0xf341('0x1c')]);}_0x1de3d7[_0xf341('0x35')]=qs['sort'](_0x49c656[_0xf341('0x2e')]['sort']);_0x1de3d7[_0xf341('0x36')]=qs['filters'](_['pick'](_0x49c656['query'],_0x3e8ac8[_0xf341('0x37')]),_0x3c868f);if(_0x49c656['query'][_0xf341('0x38')]){_0x1de3d7[_0xf341('0x36')]=_[_0xf341('0x39')](_0x1de3d7[_0xf341('0x36')],{'$or':_[_0xf341('0x29')](_0x3c868f,function(_0xecd90f){if(_0xecd90f[_0xf341('0x2c')]!==_0xf341('0x3a')){var _0x31b6b2={};_0x31b6b2[_0xecd90f['name']]={'$like':'%'+_0x49c656[_0xf341('0x2e')][_0xf341('0x38')]+'%'};return _0x31b6b2;}})});}_0x1de3d7=_[_0xf341('0x39')]({},_0x1de3d7,_0x49c656[_0xf341('0x3b')]);var _0x289787={'where':_0x1de3d7['where']};return db['ServicenowConfiguration']['count'](_0x289787)[_0xf341('0x23')](function(_0x257d55){_0x2644c0['count']=_0x257d55;if(_0x49c656['query'][_0xf341('0x3c')]){_0x1de3d7[_0xf341('0x3d')]=[{'all':!![]}];}return db[_0xf341('0x2a')][_0xf341('0x3e')](_0x1de3d7);})[_0xf341('0x23')](function(_0x17c8a7){_0x2644c0[_0xf341('0x3f')]=_0x17c8a7;return _0x2644c0;})[_0xf341('0x23')](respondWithFilteredResult(_0x5ef570,_0x1de3d7))['catch'](handleError(_0x5ef570,null));};exports[_0xf341('0x40')]=function(_0x3f8f94,_0x513b11){var _0x391a8c={'raw':![],'where':{'id':_0x3f8f94[_0xf341('0x41')]['id']}},_0x3e28b9={};_0x3e28b9[_0xf341('0x30')]=_['keys'](db['ServicenowConfiguration'][_0xf341('0x42')]);_0x3e28b9[_0xf341('0x2e')]=_[_0xf341('0x2f')](_0x3f8f94['query']);_0x3e28b9[_0xf341('0x37')]=_[_0xf341('0x32')](_0x3e28b9[_0xf341('0x30')],_0x3e28b9['query']);_0x391a8c[_0xf341('0x31')]=_[_0xf341('0x32')](_0x3e28b9[_0xf341('0x30')],qs[_0xf341('0x43')](_0x3f8f94['query'][_0xf341('0x43')]));_0x391a8c[_0xf341('0x31')]=_0x391a8c[_0xf341('0x31')][_0xf341('0x33')]?_0x391a8c[_0xf341('0x31')]:_0x3e28b9[_0xf341('0x30')];if(_0x3f8f94['query'][_0xf341('0x3c')]){_0x391a8c['include']=[{'all':!![]}];}_0x391a8c=_[_0xf341('0x39')]({},_0x391a8c,_0x3f8f94[_0xf341('0x3b')]);return db[_0xf341('0x2a')][_0xf341('0x44')](_0x391a8c)['then'](handleEntityNotFound(_0x513b11,null))[_0xf341('0x23')](respondWithResult(_0x513b11,null))['catch'](handleError(_0x513b11,null));};exports['create']=function(_0x1a8e92,_0x30b744){return db['ServicenowConfiguration']['create'](_0x1a8e92['body'],{})[_0xf341('0x23')](respondWithResult(_0x30b744,0xc9))[_0xf341('0x45')](handleError(_0x30b744,null));};exports[_0xf341('0x46')]=function(_0x556b1e,_0x3124f7){if(_0x556b1e[_0xf341('0x47')]['id']){delete _0x556b1e[_0xf341('0x47')]['id'];}return db['ServicenowConfiguration'][_0xf341('0x44')]({'where':{'id':_0x556b1e[_0xf341('0x41')]['id']}})[_0xf341('0x23')](handleEntityNotFound(_0x3124f7,null))[_0xf341('0x23')](saveUpdates(_0x556b1e[_0xf341('0x47')],null))[_0xf341('0x23')](respondWithResult(_0x3124f7,null))['catch'](handleError(_0x3124f7,null));};exports['destroy']=function(_0x134be4,_0x13e37e){return db[_0xf341('0x2a')][_0xf341('0x44')]({'where':{'id':_0x134be4[_0xf341('0x41')]['id']}})['then'](handleEntityNotFound(_0x13e37e,null))[_0xf341('0x23')](removeEntity(_0x13e37e,null))[_0xf341('0x45')](handleError(_0x13e37e,null));};exports[_0xf341('0x48')]=function(_0x25ac9d,_0x52725f,_0xa945eb){var _0x5cc6c0={};var _0x2e4d07={};var _0x5360e7;var _0x29810f;return db[_0xf341('0x2a')][_0xf341('0x49')]({'where':{'id':_0x25ac9d[_0xf341('0x41')]['id']}})[_0xf341('0x23')](handleEntityNotFound(_0x52725f,null))[_0xf341('0x23')](function(_0x5695f5){if(_0x5695f5){_0x5360e7=_0x5695f5;_0x2e4d07[_0xf341('0x30')]=_[_0xf341('0x2f')](db[_0xf341('0x4a')]['rawAttributes']);_0x2e4d07['query']=_[_0xf341('0x2f')](_0x25ac9d[_0xf341('0x2e')]);_0x2e4d07[_0xf341('0x37')]=_[_0xf341('0x32')](_0x2e4d07['model'],_0x2e4d07[_0xf341('0x2e')]);_0x5cc6c0[_0xf341('0x31')]=_[_0xf341('0x32')](_0x2e4d07[_0xf341('0x30')],qs['fields'](_0x25ac9d[_0xf341('0x2e')]['fields']));_0x5cc6c0[_0xf341('0x31')]=_0x5cc6c0[_0xf341('0x31')][_0xf341('0x33')]?_0x5cc6c0[_0xf341('0x31')]:_0x2e4d07['model'];_0x5cc6c0['order']=qs[_0xf341('0x4b')](_0x25ac9d[_0xf341('0x2e')][_0xf341('0x4b')]);_0x5cc6c0[_0xf341('0x36')]=qs[_0xf341('0x37')](_[_0xf341('0x4c')](_0x25ac9d[_0xf341('0x2e')],_0x2e4d07[_0xf341('0x37')]));if(_0x25ac9d[_0xf341('0x2e')]['filter']){_0x5cc6c0[_0xf341('0x36')]=_[_0xf341('0x39')](_0x5cc6c0[_0xf341('0x36')],{'$or':_['map'](_0x5cc6c0['attributes'],function(_0x13cc40){var _0x1efd5a={};_0x1efd5a[_0x13cc40]={'$like':'%'+_0x25ac9d[_0xf341('0x2e')][_0xf341('0x38')]+'%'};return _0x1efd5a;})});}_0x5cc6c0=_[_0xf341('0x39')]({},_0x5cc6c0,_0x25ac9d[_0xf341('0x3b')]);return _0x5360e7[_0xf341('0x48')](_0x5cc6c0);}})['then'](function(_0x5e2d0e){if(_0x5e2d0e){_0x29810f=_0x5e2d0e[_0xf341('0x33')];if(!_0x25ac9d[_0xf341('0x2e')][_0xf341('0x4d')]('nolimit')){_0x5cc6c0[_0xf341('0x1e')]=qs[_0xf341('0x1e')](_0x25ac9d[_0xf341('0x2e')]['limit']);_0x5cc6c0[_0xf341('0x1c')]=qs['offset'](_0x25ac9d[_0xf341('0x2e')][_0xf341('0x1c')]);}return _0x5360e7[_0xf341('0x48')](_0x5cc6c0);}})[_0xf341('0x23')](function(_0x319832){if(_0x319832){return _0x319832?{'count':_0x29810f,'rows':_0x319832}:null;}})['then'](respondWithResult(_0x52725f,null))[_0xf341('0x45')](handleError(_0x52725f,null));};exports[_0xf341('0x4e')]=function(_0x4506f6,_0x1e22a8,_0x219ea7){var _0x5bbc4a={};var _0x3753b4={};var _0x3960bf;var _0x51b7a6;return db[_0xf341('0x2a')][_0xf341('0x49')]({'where':{'id':_0x4506f6[_0xf341('0x41')]['id']}})[_0xf341('0x23')](handleEntityNotFound(_0x1e22a8,null))[_0xf341('0x23')](function(_0x16ad76){if(_0x16ad76){_0x3960bf=_0x16ad76;_0x3753b4['model']=_[_0xf341('0x2f')](db['ServicenowField'][_0xf341('0x42')]);_0x3753b4[_0xf341('0x2e')]=_[_0xf341('0x2f')](_0x4506f6[_0xf341('0x2e')]);_0x3753b4[_0xf341('0x37')]=_[_0xf341('0x32')](_0x3753b4[_0xf341('0x30')],_0x3753b4['query']);_0x5bbc4a[_0xf341('0x31')]=_['intersection'](_0x3753b4[_0xf341('0x30')],qs[_0xf341('0x43')](_0x4506f6[_0xf341('0x2e')][_0xf341('0x43')]));_0x5bbc4a[_0xf341('0x31')]=_0x5bbc4a['attributes'][_0xf341('0x33')]?_0x5bbc4a[_0xf341('0x31')]:_0x3753b4[_0xf341('0x30')];_0x5bbc4a[_0xf341('0x35')]=qs[_0xf341('0x4b')](_0x4506f6[_0xf341('0x2e')]['sort']);_0x5bbc4a[_0xf341('0x36')]=qs[_0xf341('0x37')](_[_0xf341('0x4c')](_0x4506f6[_0xf341('0x2e')],_0x3753b4['filters']));if(_0x4506f6[_0xf341('0x2e')][_0xf341('0x38')]){_0x5bbc4a[_0xf341('0x36')]=_[_0xf341('0x39')](_0x5bbc4a[_0xf341('0x36')],{'$or':_['map'](_0x5bbc4a[_0xf341('0x31')],function(_0x41964b){var _0x3fe1f0={};_0x3fe1f0[_0x41964b]={'$like':'%'+_0x4506f6['query'][_0xf341('0x38')]+'%'};return _0x3fe1f0;})});}_0x5bbc4a=_[_0xf341('0x39')]({},_0x5bbc4a,_0x4506f6[_0xf341('0x3b')]);return _0x3960bf[_0xf341('0x4e')](_0x5bbc4a);}})['then'](function(_0x599bfe){if(_0x599bfe){_0x51b7a6=_0x599bfe[_0xf341('0x33')];if(!_0x4506f6[_0xf341('0x2e')]['hasOwnProperty'](_0xf341('0x34'))){_0x5bbc4a[_0xf341('0x1e')]=qs[_0xf341('0x1e')](_0x4506f6['query'][_0xf341('0x1e')]);_0x5bbc4a[_0xf341('0x1c')]=qs[_0xf341('0x1c')](_0x4506f6[_0xf341('0x2e')][_0xf341('0x1c')]);}return _0x3960bf['getSubjects'](_0x5bbc4a);}})[_0xf341('0x23')](function(_0x2899c4){if(_0x2899c4){return _0x2899c4?{'count':_0x51b7a6,'rows':_0x2899c4}:null;}})[_0xf341('0x23')](respondWithResult(_0x1e22a8,null))[_0xf341('0x45')](handleError(_0x1e22a8,null));};exports['getDescriptions']=function(_0xab4d6,_0x3a469c,_0x2e54d0){var _0x4b82d7={};var _0x5c0c67={};var _0x51d563;var _0x2dddf6;return db['ServicenowConfiguration']['findOne']({'where':{'id':_0xab4d6[_0xf341('0x41')]['id']}})[_0xf341('0x23')](handleEntityNotFound(_0x3a469c,null))[_0xf341('0x23')](function(_0xcd4afc){if(_0xcd4afc){_0x51d563=_0xcd4afc;_0x5c0c67[_0xf341('0x30')]=_[_0xf341('0x2f')](db[_0xf341('0x4a')][_0xf341('0x42')]);_0x5c0c67[_0xf341('0x2e')]=_[_0xf341('0x2f')](_0xab4d6[_0xf341('0x2e')]);_0x5c0c67['filters']=_['intersection'](_0x5c0c67[_0xf341('0x30')],_0x5c0c67[_0xf341('0x2e')]);_0x4b82d7[_0xf341('0x31')]=_[_0xf341('0x32')](_0x5c0c67[_0xf341('0x30')],qs[_0xf341('0x43')](_0xab4d6['query']['fields']));_0x4b82d7['attributes']=_0x4b82d7[_0xf341('0x31')][_0xf341('0x33')]?_0x4b82d7['attributes']:_0x5c0c67[_0xf341('0x30')];_0x4b82d7[_0xf341('0x35')]=qs[_0xf341('0x4b')](_0xab4d6['query']['sort']);_0x4b82d7[_0xf341('0x36')]=qs[_0xf341('0x37')](_[_0xf341('0x4c')](_0xab4d6[_0xf341('0x2e')],_0x5c0c67[_0xf341('0x37')]));if(_0xab4d6[_0xf341('0x2e')]['filter']){_0x4b82d7[_0xf341('0x36')]=_[_0xf341('0x39')](_0x4b82d7[_0xf341('0x36')],{'$or':_[_0xf341('0x29')](_0x4b82d7[_0xf341('0x31')],function(_0x22cfca){var _0x2f3954={};_0x2f3954[_0x22cfca]={'$like':'%'+_0xab4d6[_0xf341('0x2e')][_0xf341('0x38')]+'%'};return _0x2f3954;})});}_0x4b82d7=_[_0xf341('0x39')]({},_0x4b82d7,_0xab4d6[_0xf341('0x3b')]);return _0x51d563[_0xf341('0x4f')](_0x4b82d7);}})[_0xf341('0x23')](function(_0x1745fe){if(_0x1745fe){_0x2dddf6=_0x1745fe[_0xf341('0x33')];if(!_0xab4d6[_0xf341('0x2e')][_0xf341('0x4d')](_0xf341('0x34'))){_0x4b82d7[_0xf341('0x1e')]=qs[_0xf341('0x1e')](_0xab4d6['query'][_0xf341('0x1e')]);_0x4b82d7[_0xf341('0x1c')]=qs['offset'](_0xab4d6[_0xf341('0x2e')][_0xf341('0x1c')]);}return _0x51d563[_0xf341('0x4f')](_0x4b82d7);}})[_0xf341('0x23')](function(_0x376ab1){if(_0x376ab1){return _0x376ab1?{'count':_0x2dddf6,'rows':_0x376ab1}:null;}})[_0xf341('0x23')](respondWithResult(_0x3a469c,null))[_0xf341('0x45')](handleError(_0x3a469c,null));};