751eebce97ebb64a81e2d8be58deb0f850ac017a
[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 _0x83b5=['../../config/environment','sendStatus','status','end','json','offset','undefined','count','set','reject','stack','name','index','map','ServicenowConfiguration','rawAttributes','key','model','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','merge','type','VIRTUAL','filter','options','then','include','findAll','rows','catch','show','params','keys','includeAll','find','create','body','destroy','getFields','findOne','getSubjects','ServicenowField','getDescriptions','rimraf','zip-dir','moment','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils'];(function(_0xdd827a,_0x482ad2){var _0x2f46f1=function(_0x221046){while(--_0x221046){_0xdd827a['push'](_0xdd827a['shift']());}};_0x2f46f1(++_0x482ad2);}(_0x83b5,0xd2));var _0x583b=function(_0x59b439,_0x14ae7b){_0x59b439=_0x59b439-0x0;var _0x521ea3=_0x83b5[_0x59b439];return _0x521ea3;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x583b('0x0'));var zipdir=require(_0x583b('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x583b('0x2'));var BPromise=require(_0x583b('0x3'));var Mustache=require(_0x583b('0x4'));var util=require(_0x583b('0x5'));var path=require(_0x583b('0x6'));var sox=require(_0x583b('0x7'));var csv=require(_0x583b('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x583b('0x9'));var _=require(_0x583b('0xa'));var squel=require(_0x583b('0xb'));var crypto=require(_0x583b('0xc'));var jsforce=require(_0x583b('0xd'));var deskjs=require(_0x583b('0xe'));var toCsv=require('to-csv');var querystring=require(_0x583b('0xf'));var Papa=require(_0x583b('0x10'));var Redis=require(_0x583b('0x11'));var authService=require(_0x583b('0x12'));var qs=require(_0x583b('0x13'));var as=require(_0x583b('0x14'));var hardwareService=require(_0x583b('0x15'));var logger=require(_0x583b('0x16'))('api');var utils=require(_0x583b('0x17'));var config=require(_0x583b('0x18'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x22b1b0,_0x5990c3){_0x5990c3=_0x5990c3||0xcc;return function(_0x4e2039){if(_0x4e2039){return _0x22b1b0[_0x583b('0x19')](_0x5990c3);}return _0x22b1b0[_0x583b('0x1a')](_0x5990c3)[_0x583b('0x1b')]();};}function respondWithResult(_0x1a8a10,_0x3a5500){_0x3a5500=_0x3a5500||0xc8;return function(_0x349eb7){if(_0x349eb7){return _0x1a8a10[_0x583b('0x1a')](_0x3a5500)[_0x583b('0x1c')](_0x349eb7);}};}function respondWithFilteredResult(_0x2f28f7,_0x4463c9){return function(_0x210191){if(_0x210191){var _0x47e7c0=typeof _0x4463c9[_0x583b('0x1d')]===_0x583b('0x1e')&&typeof _0x4463c9['limit']===_0x583b('0x1e');var _0x39341b=_0x210191['count'];var _0x2596b8=_0x47e7c0?0x0:_0x4463c9[_0x583b('0x1d')];var _0x2b6fd0=_0x47e7c0?_0x210191[_0x583b('0x1f')]:_0x4463c9[_0x583b('0x1d')]+_0x4463c9['limit'];var _0x470370;if(_0x2b6fd0>=_0x39341b){_0x2b6fd0=_0x39341b;_0x470370=0xc8;}else{_0x470370=0xce;}_0x2f28f7[_0x583b('0x1a')](_0x470370);return _0x2f28f7[_0x583b('0x20')]('Content-Range',_0x2596b8+'-'+_0x2b6fd0+'/'+_0x39341b)[_0x583b('0x1c')](_0x210191);}return null;};}function patchUpdates(_0x5286e6){return function(_0x23d09d){try{jsonpatch['apply'](_0x23d09d,_0x5286e6,!![]);}catch(_0x31f8be){return BPromise[_0x583b('0x21')](_0x31f8be);}return _0x23d09d['save']();};}function saveUpdates(_0xdcfdc3,_0x460a80){return function(_0x11c6e6){if(_0x11c6e6){return _0x11c6e6['update'](_0xdcfdc3)['then'](function(_0x46f10f){return _0x46f10f;});}return null;};}function removeEntity(_0xe35b5a,_0x26da4c){return function(_0x5e5ac7){if(_0x5e5ac7){return _0x5e5ac7['destroy']()['then'](function(){_0xe35b5a[_0x583b('0x1a')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4de822,_0xd2cfd5){return function(_0x47bfdc){if(!_0x47bfdc){_0x4de822['sendStatus'](0x194);}return _0x47bfdc;};}function handleError(_0x344880,_0x1919f7){_0x1919f7=_0x1919f7||0x1f4;return function(_0x5bf570){logger['error'](_0x5bf570[_0x583b('0x22')]);if(_0x5bf570[_0x583b('0x23')]){delete _0x5bf570[_0x583b('0x23')];}_0x344880[_0x583b('0x1a')](_0x1919f7)['send'](_0x5bf570);};}exports[_0x583b('0x24')]=function(_0x34b680,_0x36c97c){var _0x56d7af={},_0x39ac21={},_0x42dcc4={'count':0x0,'rows':[]};var _0x593505=_[_0x583b('0x25')](db[_0x583b('0x26')][_0x583b('0x27')],function(_0x5fa928){return{'name':_0x5fa928['fieldName'],'type':_0x5fa928['type'][_0x583b('0x28')]};});_0x39ac21[_0x583b('0x29')]=_[_0x583b('0x25')](_0x593505,_0x583b('0x23'));_0x39ac21[_0x583b('0x2a')]=_['keys'](_0x34b680[_0x583b('0x2a')]);_0x39ac21[_0x583b('0x2b')]=_[_0x583b('0x2c')](_0x39ac21[_0x583b('0x29')],_0x39ac21['query']);_0x56d7af['attributes']=_[_0x583b('0x2c')](_0x39ac21[_0x583b('0x29')],qs[_0x583b('0x2d')](_0x34b680[_0x583b('0x2a')][_0x583b('0x2d')]));_0x56d7af['attributes']=_0x56d7af[_0x583b('0x2e')][_0x583b('0x2f')]?_0x56d7af['attributes']:_0x39ac21[_0x583b('0x29')];if(!_0x34b680['query'][_0x583b('0x30')](_0x583b('0x31'))){_0x56d7af[_0x583b('0x32')]=qs['limit'](_0x34b680[_0x583b('0x2a')][_0x583b('0x32')]);_0x56d7af[_0x583b('0x1d')]=qs['offset'](_0x34b680[_0x583b('0x2a')][_0x583b('0x1d')]);}_0x56d7af[_0x583b('0x33')]=qs[_0x583b('0x34')](_0x34b680[_0x583b('0x2a')][_0x583b('0x34')]);_0x56d7af[_0x583b('0x35')]=qs['filters'](_[_0x583b('0x36')](_0x34b680['query'],_0x39ac21['filters']),_0x593505);if(_0x34b680[_0x583b('0x2a')]['filter']){_0x56d7af['where']=_[_0x583b('0x37')](_0x56d7af[_0x583b('0x35')],{'$or':_['map'](_0x593505,function(_0x25a0aa){if(_0x25a0aa[_0x583b('0x38')]!==_0x583b('0x39')){var _0x536ba0={};_0x536ba0[_0x25a0aa[_0x583b('0x23')]]={'$like':'%'+_0x34b680[_0x583b('0x2a')][_0x583b('0x3a')]+'%'};return _0x536ba0;}})});}_0x56d7af=_[_0x583b('0x37')]({},_0x56d7af,_0x34b680[_0x583b('0x3b')]);var _0x26d284={'where':_0x56d7af[_0x583b('0x35')]};return db[_0x583b('0x26')][_0x583b('0x1f')](_0x26d284)[_0x583b('0x3c')](function(_0x13cb7e){_0x42dcc4['count']=_0x13cb7e;if(_0x34b680[_0x583b('0x2a')]['includeAll']){_0x56d7af[_0x583b('0x3d')]=[{'all':!![]}];}return db[_0x583b('0x26')][_0x583b('0x3e')](_0x56d7af);})[_0x583b('0x3c')](function(_0x42e030){_0x42dcc4[_0x583b('0x3f')]=_0x42e030;return _0x42dcc4;})['then'](respondWithFilteredResult(_0x36c97c,_0x56d7af))[_0x583b('0x40')](handleError(_0x36c97c,null));};exports[_0x583b('0x41')]=function(_0x234a0e,_0x942e2a){var _0x5afef5={'raw':![],'where':{'id':_0x234a0e[_0x583b('0x42')]['id']}},_0x221652={};_0x221652[_0x583b('0x29')]=_[_0x583b('0x43')](db[_0x583b('0x26')]['rawAttributes']);_0x221652[_0x583b('0x2a')]=_[_0x583b('0x43')](_0x234a0e['query']);_0x221652[_0x583b('0x2b')]=_[_0x583b('0x2c')](_0x221652['model'],_0x221652[_0x583b('0x2a')]);_0x5afef5[_0x583b('0x2e')]=_['intersection'](_0x221652[_0x583b('0x29')],qs[_0x583b('0x2d')](_0x234a0e[_0x583b('0x2a')][_0x583b('0x2d')]));_0x5afef5['attributes']=_0x5afef5[_0x583b('0x2e')]['length']?_0x5afef5[_0x583b('0x2e')]:_0x221652[_0x583b('0x29')];if(_0x234a0e[_0x583b('0x2a')][_0x583b('0x44')]){_0x5afef5[_0x583b('0x3d')]=[{'all':!![]}];}_0x5afef5=_[_0x583b('0x37')]({},_0x5afef5,_0x234a0e[_0x583b('0x3b')]);return db['ServicenowConfiguration'][_0x583b('0x45')](_0x5afef5)[_0x583b('0x3c')](handleEntityNotFound(_0x942e2a,null))[_0x583b('0x3c')](respondWithResult(_0x942e2a,null))['catch'](handleError(_0x942e2a,null));};exports[_0x583b('0x46')]=function(_0x329f2b,_0xa5098d){return db[_0x583b('0x26')][_0x583b('0x46')](_0x329f2b[_0x583b('0x47')],{})[_0x583b('0x3c')](respondWithResult(_0xa5098d,0xc9))[_0x583b('0x40')](handleError(_0xa5098d,null));};exports['update']=function(_0x542379,_0x28487a){if(_0x542379[_0x583b('0x47')]['id']){delete _0x542379[_0x583b('0x47')]['id'];}return db['ServicenowConfiguration'][_0x583b('0x45')]({'where':{'id':_0x542379['params']['id']}})[_0x583b('0x3c')](handleEntityNotFound(_0x28487a,null))[_0x583b('0x3c')](saveUpdates(_0x542379[_0x583b('0x47')],null))[_0x583b('0x3c')](respondWithResult(_0x28487a,null))[_0x583b('0x40')](handleError(_0x28487a,null));};exports[_0x583b('0x48')]=function(_0x193c75,_0x56312a){return db[_0x583b('0x26')]['find']({'where':{'id':_0x193c75['params']['id']}})[_0x583b('0x3c')](handleEntityNotFound(_0x56312a,null))[_0x583b('0x3c')](removeEntity(_0x56312a,null))[_0x583b('0x40')](handleError(_0x56312a,null));};exports[_0x583b('0x49')]=function(_0x25d48c,_0x2b080f,_0x4b460a){var _0x3e87d7={};var _0x399f50={};var _0x44a4d0;var _0x5cbf77;return db[_0x583b('0x26')][_0x583b('0x4a')]({'where':{'id':_0x25d48c[_0x583b('0x42')]['id']}})[_0x583b('0x3c')](handleEntityNotFound(_0x2b080f,null))['then'](function(_0x23b66d){if(_0x23b66d){_0x44a4d0=_0x23b66d;_0x399f50[_0x583b('0x29')]=_[_0x583b('0x43')](db['ServicenowField'][_0x583b('0x27')]);_0x399f50[_0x583b('0x2a')]=_[_0x583b('0x43')](_0x25d48c[_0x583b('0x2a')]);_0x399f50[_0x583b('0x2b')]=_[_0x583b('0x2c')](_0x399f50['model'],_0x399f50[_0x583b('0x2a')]);_0x3e87d7['attributes']=_[_0x583b('0x2c')](_0x399f50[_0x583b('0x29')],qs[_0x583b('0x2d')](_0x25d48c[_0x583b('0x2a')][_0x583b('0x2d')]));_0x3e87d7['attributes']=_0x3e87d7[_0x583b('0x2e')]['length']?_0x3e87d7[_0x583b('0x2e')]:_0x399f50['model'];_0x3e87d7[_0x583b('0x33')]=qs['sort'](_0x25d48c[_0x583b('0x2a')][_0x583b('0x34')]);_0x3e87d7[_0x583b('0x35')]=qs[_0x583b('0x2b')](_[_0x583b('0x36')](_0x25d48c[_0x583b('0x2a')],_0x399f50['filters']));if(_0x25d48c[_0x583b('0x2a')][_0x583b('0x3a')]){_0x3e87d7[_0x583b('0x35')]=_[_0x583b('0x37')](_0x3e87d7[_0x583b('0x35')],{'$or':_['map'](_0x3e87d7[_0x583b('0x2e')],function(_0x425610){var _0x4d0677={};_0x4d0677[_0x425610]={'$like':'%'+_0x25d48c[_0x583b('0x2a')]['filter']+'%'};return _0x4d0677;})});}_0x3e87d7=_['merge']({},_0x3e87d7,_0x25d48c[_0x583b('0x3b')]);return _0x44a4d0[_0x583b('0x49')](_0x3e87d7);}})[_0x583b('0x3c')](function(_0x4c9773){if(_0x4c9773){_0x5cbf77=_0x4c9773[_0x583b('0x2f')];if(!_0x25d48c[_0x583b('0x2a')][_0x583b('0x30')](_0x583b('0x31'))){_0x3e87d7[_0x583b('0x32')]=qs[_0x583b('0x32')](_0x25d48c[_0x583b('0x2a')][_0x583b('0x32')]);_0x3e87d7[_0x583b('0x1d')]=qs[_0x583b('0x1d')](_0x25d48c['query'][_0x583b('0x1d')]);}return _0x44a4d0['getFields'](_0x3e87d7);}})[_0x583b('0x3c')](function(_0xa7452e){if(_0xa7452e){return _0xa7452e?{'count':_0x5cbf77,'rows':_0xa7452e}:null;}})[_0x583b('0x3c')](respondWithResult(_0x2b080f,null))[_0x583b('0x40')](handleError(_0x2b080f,null));};exports[_0x583b('0x4b')]=function(_0x1f5db2,_0x28f3a1,_0x415c9d){var _0x27d01e={};var _0x26263b={};var _0x33a98f;var _0x31f600;return db['ServicenowConfiguration'][_0x583b('0x4a')]({'where':{'id':_0x1f5db2[_0x583b('0x42')]['id']}})[_0x583b('0x3c')](handleEntityNotFound(_0x28f3a1,null))[_0x583b('0x3c')](function(_0x174b2c){if(_0x174b2c){_0x33a98f=_0x174b2c;_0x26263b[_0x583b('0x29')]=_[_0x583b('0x43')](db[_0x583b('0x4c')]['rawAttributes']);_0x26263b[_0x583b('0x2a')]=_[_0x583b('0x43')](_0x1f5db2[_0x583b('0x2a')]);_0x26263b[_0x583b('0x2b')]=_['intersection'](_0x26263b[_0x583b('0x29')],_0x26263b[_0x583b('0x2a')]);_0x27d01e[_0x583b('0x2e')]=_[_0x583b('0x2c')](_0x26263b[_0x583b('0x29')],qs[_0x583b('0x2d')](_0x1f5db2['query'][_0x583b('0x2d')]));_0x27d01e[_0x583b('0x2e')]=_0x27d01e['attributes'][_0x583b('0x2f')]?_0x27d01e[_0x583b('0x2e')]:_0x26263b[_0x583b('0x29')];_0x27d01e[_0x583b('0x33')]=qs[_0x583b('0x34')](_0x1f5db2[_0x583b('0x2a')][_0x583b('0x34')]);_0x27d01e['where']=qs[_0x583b('0x2b')](_[_0x583b('0x36')](_0x1f5db2['query'],_0x26263b[_0x583b('0x2b')]));if(_0x1f5db2[_0x583b('0x2a')]['filter']){_0x27d01e['where']=_[_0x583b('0x37')](_0x27d01e[_0x583b('0x35')],{'$or':_[_0x583b('0x25')](_0x27d01e[_0x583b('0x2e')],function(_0x7f9055){var _0x13cc3a={};_0x13cc3a[_0x7f9055]={'$like':'%'+_0x1f5db2['query'][_0x583b('0x3a')]+'%'};return _0x13cc3a;})});}_0x27d01e=_['merge']({},_0x27d01e,_0x1f5db2[_0x583b('0x3b')]);return _0x33a98f[_0x583b('0x4b')](_0x27d01e);}})[_0x583b('0x3c')](function(_0x2cf802){if(_0x2cf802){_0x31f600=_0x2cf802[_0x583b('0x2f')];if(!_0x1f5db2[_0x583b('0x2a')][_0x583b('0x30')](_0x583b('0x31'))){_0x27d01e[_0x583b('0x32')]=qs[_0x583b('0x32')](_0x1f5db2['query'][_0x583b('0x32')]);_0x27d01e[_0x583b('0x1d')]=qs[_0x583b('0x1d')](_0x1f5db2[_0x583b('0x2a')][_0x583b('0x1d')]);}return _0x33a98f[_0x583b('0x4b')](_0x27d01e);}})[_0x583b('0x3c')](function(_0x347270){if(_0x347270){return _0x347270?{'count':_0x31f600,'rows':_0x347270}:null;}})[_0x583b('0x3c')](respondWithResult(_0x28f3a1,null))[_0x583b('0x40')](handleError(_0x28f3a1,null));};exports[_0x583b('0x4d')]=function(_0x24966d,_0x23cf5a,_0xc60c89){var _0x475e2b={};var _0x41f24e={};var _0x488007;var _0x26260e;return db[_0x583b('0x26')][_0x583b('0x4a')]({'where':{'id':_0x24966d[_0x583b('0x42')]['id']}})[_0x583b('0x3c')](handleEntityNotFound(_0x23cf5a,null))[_0x583b('0x3c')](function(_0x25e163){if(_0x25e163){_0x488007=_0x25e163;_0x41f24e['model']=_[_0x583b('0x43')](db['ServicenowField']['rawAttributes']);_0x41f24e[_0x583b('0x2a')]=_[_0x583b('0x43')](_0x24966d['query']);_0x41f24e['filters']=_['intersection'](_0x41f24e['model'],_0x41f24e[_0x583b('0x2a')]);_0x475e2b['attributes']=_[_0x583b('0x2c')](_0x41f24e['model'],qs['fields'](_0x24966d[_0x583b('0x2a')][_0x583b('0x2d')]));_0x475e2b['attributes']=_0x475e2b[_0x583b('0x2e')][_0x583b('0x2f')]?_0x475e2b[_0x583b('0x2e')]:_0x41f24e[_0x583b('0x29')];_0x475e2b['order']=qs[_0x583b('0x34')](_0x24966d['query']['sort']);_0x475e2b['where']=qs[_0x583b('0x2b')](_[_0x583b('0x36')](_0x24966d['query'],_0x41f24e[_0x583b('0x2b')]));if(_0x24966d[_0x583b('0x2a')]['filter']){_0x475e2b[_0x583b('0x35')]=_[_0x583b('0x37')](_0x475e2b[_0x583b('0x35')],{'$or':_['map'](_0x475e2b[_0x583b('0x2e')],function(_0x511967){var _0x3b7ba7={};_0x3b7ba7[_0x511967]={'$like':'%'+_0x24966d[_0x583b('0x2a')]['filter']+'%'};return _0x3b7ba7;})});}_0x475e2b=_[_0x583b('0x37')]({},_0x475e2b,_0x24966d['options']);return _0x488007[_0x583b('0x4d')](_0x475e2b);}})['then'](function(_0x168b40){if(_0x168b40){_0x26260e=_0x168b40[_0x583b('0x2f')];if(!_0x24966d[_0x583b('0x2a')][_0x583b('0x30')](_0x583b('0x31'))){_0x475e2b[_0x583b('0x32')]=qs[_0x583b('0x32')](_0x24966d[_0x583b('0x2a')][_0x583b('0x32')]);_0x475e2b[_0x583b('0x1d')]=qs[_0x583b('0x1d')](_0x24966d[_0x583b('0x2a')][_0x583b('0x1d')]);}return _0x488007[_0x583b('0x4d')](_0x475e2b);}})[_0x583b('0x3c')](function(_0x16a765){if(_0x16a765){return _0x16a765?{'count':_0x26260e,'rows':_0x16a765}:null;}})[_0x583b('0x3c')](respondWithResult(_0x23cf5a,null))[_0x583b('0x40')](handleError(_0x23cf5a,null));};