256d43e88e017eb014d15a33421457ec31689d16
[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 _0x949b=['then','error','name','send','index','map','rawAttributes','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','merge','VIRTUAL','options','ServicenowConfiguration','includeAll','include','rows','show','params','create','body','find','destroy','catch','getFields','findOne','ServicenowField','pick','filter','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','request-promise','bluebird','path','to-csv','ejs','fs-extra','squel','jsforce','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','limit','count','Content-Range','update'];(function(_0x5a0e20,_0x25dee4){var _0x20b0ac=function(_0x1a53cc){while(--_0x1a53cc){_0x5a0e20['push'](_0x5a0e20['shift']());}};_0x20b0ac(++_0x25dee4);}(_0x949b,0x72));var _0xb949=function(_0x1ed2ed,_0x374e63){_0x1ed2ed=_0x1ed2ed-0x0;var _0x3f0689=_0x949b[_0x1ed2ed];return _0x3f0689;};'use strict';var emlformat=require(_0xb949('0x0'));var rimraf=require(_0xb949('0x1'));var zipdir=require(_0xb949('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xb949('0x3'));var moment=require('moment');var BPromise=require(_0xb949('0x4'));var Mustache=require('mustache');var util=require('util');var path=require(_0xb949('0x5'));var sox=require('sox');var csv=require(_0xb949('0x6'));var ejs=require(_0xb949('0x7'));var fs=require('fs');var fs_extra=require(_0xb949('0x8'));var _=require('lodash');var squel=require(_0xb949('0x9'));var crypto=require('crypto');var jsforce=require(_0xb949('0xa'));var deskjs=require('desk.js');var toCsv=require(_0xb949('0x6'));var querystring=require(_0xb949('0xb'));var Papa=require(_0xb949('0xc'));var Redis=require('ioredis');var authService=require(_0xb949('0xd'));var qs=require(_0xb949('0xe'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0xb949('0xf'));var utils=require(_0xb949('0x10'));var config=require('../../config/environment');var licenseUtil=require(_0xb949('0x11'));var db=require(_0xb949('0x12'))['db'];function respondWithStatusCode(_0x2774d9,_0x44449c){_0x44449c=_0x44449c||0xcc;return function(_0x57dbef){if(_0x57dbef){return _0x2774d9[_0xb949('0x13')](_0x44449c);}return _0x2774d9[_0xb949('0x14')](_0x44449c)[_0xb949('0x15')]();};}function respondWithResult(_0x4d9a25,_0x103b18){_0x103b18=_0x103b18||0xc8;return function(_0x4b7dcf){if(_0x4b7dcf){return _0x4d9a25[_0xb949('0x14')](_0x103b18)[_0xb949('0x16')](_0x4b7dcf);}};}function respondWithFilteredResult(_0x30cbe1,_0x6b403e){return function(_0x23dbb7){if(_0x23dbb7){var _0x407af7=typeof _0x6b403e[_0xb949('0x17')]==='undefined'&&typeof _0x6b403e[_0xb949('0x18')]==='undefined';var _0x34f2f3=_0x23dbb7[_0xb949('0x19')];var _0x9166e9=_0x407af7?0x0:_0x6b403e['offset'];var _0x22d7cb=_0x407af7?_0x23dbb7[_0xb949('0x19')]:_0x6b403e[_0xb949('0x17')]+_0x6b403e[_0xb949('0x18')];var _0x150fd6;if(_0x22d7cb>=_0x34f2f3){_0x22d7cb=_0x34f2f3;_0x150fd6=0xc8;}else{_0x150fd6=0xce;}_0x30cbe1['status'](_0x150fd6);return _0x30cbe1['set'](_0xb949('0x1a'),_0x9166e9+'-'+_0x22d7cb+'/'+_0x34f2f3)['json'](_0x23dbb7);}return null;};}function patchUpdates(_0x30a7d6){return function(_0x3d7a85){try{jsonpatch['apply'](_0x3d7a85,_0x30a7d6,!![]);}catch(_0x16f1d5){return BPromise['reject'](_0x16f1d5);}return _0x3d7a85['save']();};}function saveUpdates(_0x1dbeb3,_0x23ceca){return function(_0x1fe771){if(_0x1fe771){return _0x1fe771[_0xb949('0x1b')](_0x1dbeb3)['then'](function(_0xbf4e24){return _0xbf4e24;});}return null;};}function removeEntity(_0x43496e,_0x23a6fd){return function(_0x5948be){if(_0x5948be){return _0x5948be['destroy']()[_0xb949('0x1c')](function(){_0x43496e['status'](0xcc)[_0xb949('0x15')]();});}};}function handleEntityNotFound(_0x1481ac,_0x26a4e4){return function(_0xd2efa6){if(!_0xd2efa6){_0x1481ac[_0xb949('0x13')](0x194);}return _0xd2efa6;};}function handleError(_0x1e7ab5,_0x17a82a){_0x17a82a=_0x17a82a||0x1f4;return function(_0x56b925){logger[_0xb949('0x1d')](_0x56b925['stack']);if(_0x56b925[_0xb949('0x1e')]){delete _0x56b925[_0xb949('0x1e')];}_0x1e7ab5[_0xb949('0x14')](_0x17a82a)[_0xb949('0x1f')](_0x56b925);};}exports[_0xb949('0x20')]=function(_0x575a92,_0x31e149){var _0x2e0222={},_0x2d6fb4={},_0x50abc1={'count':0x0,'rows':[]};var _0x5350fd=_[_0xb949('0x21')](db['ServicenowConfiguration'][_0xb949('0x22')],function(_0x25faa7){return{'name':_0x25faa7['fieldName'],'type':_0x25faa7[_0xb949('0x23')][_0xb949('0x24')]};});_0x2d6fb4[_0xb949('0x25')]=_[_0xb949('0x21')](_0x5350fd,_0xb949('0x1e'));_0x2d6fb4[_0xb949('0x26')]=_[_0xb949('0x27')](_0x575a92['query']);_0x2d6fb4[_0xb949('0x28')]=_[_0xb949('0x29')](_0x2d6fb4[_0xb949('0x25')],_0x2d6fb4[_0xb949('0x26')]);_0x2e0222[_0xb949('0x2a')]=_[_0xb949('0x29')](_0x2d6fb4[_0xb949('0x25')],qs[_0xb949('0x2b')](_0x575a92[_0xb949('0x26')][_0xb949('0x2b')]));_0x2e0222['attributes']=_0x2e0222[_0xb949('0x2a')][_0xb949('0x2c')]?_0x2e0222[_0xb949('0x2a')]:_0x2d6fb4[_0xb949('0x25')];if(!_0x575a92[_0xb949('0x26')][_0xb949('0x2d')](_0xb949('0x2e'))){_0x2e0222[_0xb949('0x18')]=qs[_0xb949('0x18')](_0x575a92[_0xb949('0x26')]['limit']);_0x2e0222[_0xb949('0x17')]=qs[_0xb949('0x17')](_0x575a92[_0xb949('0x26')][_0xb949('0x17')]);}_0x2e0222[_0xb949('0x2f')]=qs[_0xb949('0x30')](_0x575a92[_0xb949('0x26')]['sort']);_0x2e0222['where']=qs[_0xb949('0x28')](_['pick'](_0x575a92['query'],_0x2d6fb4['filters']),_0x5350fd);if(_0x575a92[_0xb949('0x26')]['filter']){_0x2e0222[_0xb949('0x31')]=_[_0xb949('0x32')](_0x2e0222['where'],{'$or':_[_0xb949('0x21')](_0x5350fd,function(_0x3cc6ee){if(_0x3cc6ee[_0xb949('0x23')]!==_0xb949('0x33')){var _0x33fac6={};_0x33fac6[_0x3cc6ee[_0xb949('0x1e')]]={'$like':'%'+_0x575a92['query']['filter']+'%'};return _0x33fac6;}})});}_0x2e0222=_['merge']({},_0x2e0222,_0x575a92[_0xb949('0x34')]);var _0x4f3631={'where':_0x2e0222[_0xb949('0x31')]};return db[_0xb949('0x35')][_0xb949('0x19')](_0x4f3631)['then'](function(_0x49b9ec){_0x50abc1[_0xb949('0x19')]=_0x49b9ec;if(_0x575a92[_0xb949('0x26')][_0xb949('0x36')]){_0x2e0222[_0xb949('0x37')]=[{'all':!![]}];}return db[_0xb949('0x35')]['findAll'](_0x2e0222);})[_0xb949('0x1c')](function(_0x36ee24){_0x50abc1[_0xb949('0x38')]=_0x36ee24;return _0x50abc1;})['then'](respondWithFilteredResult(_0x31e149,_0x2e0222))['catch'](handleError(_0x31e149,null));};exports[_0xb949('0x39')]=function(_0x542cf2,_0x38b732){var _0x210d48={'raw':![],'where':{'id':_0x542cf2[_0xb949('0x3a')]['id']}},_0x2cf155={};_0x2cf155[_0xb949('0x25')]=_[_0xb949('0x27')](db[_0xb949('0x35')][_0xb949('0x22')]);_0x2cf155[_0xb949('0x26')]=_[_0xb949('0x27')](_0x542cf2[_0xb949('0x26')]);_0x2cf155['filters']=_[_0xb949('0x29')](_0x2cf155[_0xb949('0x25')],_0x2cf155[_0xb949('0x26')]);_0x210d48[_0xb949('0x2a')]=_[_0xb949('0x29')](_0x2cf155[_0xb949('0x25')],qs[_0xb949('0x2b')](_0x542cf2['query']['fields']));_0x210d48[_0xb949('0x2a')]=_0x210d48[_0xb949('0x2a')][_0xb949('0x2c')]?_0x210d48[_0xb949('0x2a')]:_0x2cf155[_0xb949('0x25')];if(_0x542cf2[_0xb949('0x26')][_0xb949('0x36')]){_0x210d48[_0xb949('0x37')]=[{'all':!![]}];}_0x210d48=_[_0xb949('0x32')]({},_0x210d48,_0x542cf2[_0xb949('0x34')]);return db[_0xb949('0x35')]['find'](_0x210d48)['then'](handleEntityNotFound(_0x38b732,null))[_0xb949('0x1c')](respondWithResult(_0x38b732,null))['catch'](handleError(_0x38b732,null));};exports[_0xb949('0x3b')]=function(_0x5909a1,_0x28f026){return db[_0xb949('0x35')][_0xb949('0x3b')](_0x5909a1[_0xb949('0x3c')],{})[_0xb949('0x1c')](respondWithResult(_0x28f026,0xc9))['catch'](handleError(_0x28f026,null));};exports[_0xb949('0x1b')]=function(_0x45477c,_0x44077d){if(_0x45477c[_0xb949('0x3c')]['id']){delete _0x45477c[_0xb949('0x3c')]['id'];}return db['ServicenowConfiguration'][_0xb949('0x3d')]({'where':{'id':_0x45477c['params']['id']}})['then'](handleEntityNotFound(_0x44077d,null))[_0xb949('0x1c')](saveUpdates(_0x45477c['body'],null))['then'](respondWithResult(_0x44077d,null))['catch'](handleError(_0x44077d,null));};exports[_0xb949('0x3e')]=function(_0x483341,_0x306774){return db['ServicenowConfiguration'][_0xb949('0x3d')]({'where':{'id':_0x483341[_0xb949('0x3a')]['id']}})[_0xb949('0x1c')](handleEntityNotFound(_0x306774,null))[_0xb949('0x1c')](removeEntity(_0x306774,null))[_0xb949('0x3f')](handleError(_0x306774,null));};exports[_0xb949('0x40')]=function(_0x2e29e1,_0x319fc5,_0x3dab1d){var _0x51702d={};var _0x32eeff={};var _0x16e9b4;var _0x4e3b0c;return db[_0xb949('0x35')][_0xb949('0x41')]({'where':{'id':_0x2e29e1['params']['id']}})[_0xb949('0x1c')](handleEntityNotFound(_0x319fc5,null))[_0xb949('0x1c')](function(_0x3772d6){if(_0x3772d6){_0x16e9b4=_0x3772d6;_0x32eeff[_0xb949('0x25')]=_[_0xb949('0x27')](db[_0xb949('0x42')][_0xb949('0x22')]);_0x32eeff[_0xb949('0x26')]=_[_0xb949('0x27')](_0x2e29e1[_0xb949('0x26')]);_0x32eeff[_0xb949('0x28')]=_[_0xb949('0x29')](_0x32eeff['model'],_0x32eeff[_0xb949('0x26')]);_0x51702d['attributes']=_['intersection'](_0x32eeff[_0xb949('0x25')],qs[_0xb949('0x2b')](_0x2e29e1['query']['fields']));_0x51702d['attributes']=_0x51702d['attributes']['length']?_0x51702d[_0xb949('0x2a')]:_0x32eeff[_0xb949('0x25')];_0x51702d[_0xb949('0x2f')]=qs[_0xb949('0x30')](_0x2e29e1[_0xb949('0x26')][_0xb949('0x30')]);_0x51702d[_0xb949('0x31')]=qs[_0xb949('0x28')](_[_0xb949('0x43')](_0x2e29e1[_0xb949('0x26')],_0x32eeff['filters']));if(_0x2e29e1[_0xb949('0x26')][_0xb949('0x44')]){_0x51702d[_0xb949('0x31')]=_[_0xb949('0x32')](_0x51702d[_0xb949('0x31')],{'$or':_[_0xb949('0x21')](_0x51702d[_0xb949('0x2a')],function(_0x23d7f5){var _0x1eee2a={};_0x1eee2a[_0x23d7f5]={'$like':'%'+_0x2e29e1[_0xb949('0x26')][_0xb949('0x44')]+'%'};return _0x1eee2a;})});}_0x51702d=_[_0xb949('0x32')]({},_0x51702d,_0x2e29e1['options']);return _0x16e9b4[_0xb949('0x40')](_0x51702d);}})[_0xb949('0x1c')](function(_0x53d3e6){if(_0x53d3e6){_0x4e3b0c=_0x53d3e6[_0xb949('0x2c')];if(!_0x2e29e1[_0xb949('0x26')][_0xb949('0x2d')]('nolimit')){_0x51702d['limit']=qs[_0xb949('0x18')](_0x2e29e1[_0xb949('0x26')][_0xb949('0x18')]);_0x51702d['offset']=qs['offset'](_0x2e29e1[_0xb949('0x26')][_0xb949('0x17')]);}return _0x16e9b4[_0xb949('0x40')](_0x51702d);}})['then'](function(_0x5d47fa){if(_0x5d47fa){return _0x5d47fa?{'count':_0x4e3b0c,'rows':_0x5d47fa}:null;}})[_0xb949('0x1c')](respondWithResult(_0x319fc5,null))['catch'](handleError(_0x319fc5,null));};exports[_0xb949('0x45')]=function(_0x3d7d2c,_0x1b3f8e,_0x5c01a2){var _0x11355d={};var _0x1637d2={};var _0x188cf1;var _0xc5b29d;return db[_0xb949('0x35')]['findOne']({'where':{'id':_0x3d7d2c[_0xb949('0x3a')]['id']}})[_0xb949('0x1c')](handleEntityNotFound(_0x1b3f8e,null))[_0xb949('0x1c')](function(_0x1ebee2){if(_0x1ebee2){_0x188cf1=_0x1ebee2;_0x1637d2[_0xb949('0x25')]=_['keys'](db[_0xb949('0x42')][_0xb949('0x22')]);_0x1637d2[_0xb949('0x26')]=_[_0xb949('0x27')](_0x3d7d2c['query']);_0x1637d2[_0xb949('0x28')]=_[_0xb949('0x29')](_0x1637d2[_0xb949('0x25')],_0x1637d2[_0xb949('0x26')]);_0x11355d['attributes']=_[_0xb949('0x29')](_0x1637d2[_0xb949('0x25')],qs[_0xb949('0x2b')](_0x3d7d2c['query'][_0xb949('0x2b')]));_0x11355d[_0xb949('0x2a')]=_0x11355d[_0xb949('0x2a')][_0xb949('0x2c')]?_0x11355d[_0xb949('0x2a')]:_0x1637d2[_0xb949('0x25')];_0x11355d[_0xb949('0x2f')]=qs[_0xb949('0x30')](_0x3d7d2c['query'][_0xb949('0x30')]);_0x11355d['where']=qs['filters'](_[_0xb949('0x43')](_0x3d7d2c[_0xb949('0x26')],_0x1637d2[_0xb949('0x28')]));if(_0x3d7d2c[_0xb949('0x26')][_0xb949('0x44')]){_0x11355d[_0xb949('0x31')]=_['merge'](_0x11355d[_0xb949('0x31')],{'$or':_['map'](_0x11355d[_0xb949('0x2a')],function(_0x3facec){var _0x5bad6b={};_0x5bad6b[_0x3facec]={'$like':'%'+_0x3d7d2c['query'][_0xb949('0x44')]+'%'};return _0x5bad6b;})});}_0x11355d=_[_0xb949('0x32')]({},_0x11355d,_0x3d7d2c[_0xb949('0x34')]);return _0x188cf1[_0xb949('0x45')](_0x11355d);}})[_0xb949('0x1c')](function(_0x11b76b){if(_0x11b76b){_0xc5b29d=_0x11b76b[_0xb949('0x2c')];if(!_0x3d7d2c[_0xb949('0x26')]['hasOwnProperty'](_0xb949('0x2e'))){_0x11355d[_0xb949('0x18')]=qs[_0xb949('0x18')](_0x3d7d2c[_0xb949('0x26')][_0xb949('0x18')]);_0x11355d[_0xb949('0x17')]=qs['offset'](_0x3d7d2c[_0xb949('0x26')][_0xb949('0x17')]);}return _0x188cf1[_0xb949('0x45')](_0x11355d);}})[_0xb949('0x1c')](function(_0x191c7f){if(_0x191c7f){return _0x191c7f?{'count':_0xc5b29d,'rows':_0x191c7f}:null;}})[_0xb949('0x1c')](respondWithResult(_0x1b3f8e,null))[_0xb949('0x3f')](handleError(_0x1b3f8e,null));};exports[_0xb949('0x46')]=function(_0x231725,_0x2e5b32,_0x1c9008){var _0x142d62={};var _0x39a970={};var _0x491ea1;var _0x10d470;return db['ServicenowConfiguration'][_0xb949('0x41')]({'where':{'id':_0x231725[_0xb949('0x3a')]['id']}})[_0xb949('0x1c')](handleEntityNotFound(_0x2e5b32,null))['then'](function(_0x29d396){if(_0x29d396){_0x491ea1=_0x29d396;_0x39a970[_0xb949('0x25')]=_[_0xb949('0x27')](db[_0xb949('0x42')]['rawAttributes']);_0x39a970[_0xb949('0x26')]=_[_0xb949('0x27')](_0x231725['query']);_0x39a970[_0xb949('0x28')]=_['intersection'](_0x39a970[_0xb949('0x25')],_0x39a970[_0xb949('0x26')]);_0x142d62[_0xb949('0x2a')]=_[_0xb949('0x29')](_0x39a970[_0xb949('0x25')],qs[_0xb949('0x2b')](_0x231725[_0xb949('0x26')]['fields']));_0x142d62[_0xb949('0x2a')]=_0x142d62[_0xb949('0x2a')][_0xb949('0x2c')]?_0x142d62[_0xb949('0x2a')]:_0x39a970[_0xb949('0x25')];_0x142d62[_0xb949('0x2f')]=qs[_0xb949('0x30')](_0x231725[_0xb949('0x26')][_0xb949('0x30')]);_0x142d62[_0xb949('0x31')]=qs['filters'](_['pick'](_0x231725[_0xb949('0x26')],_0x39a970['filters']));if(_0x231725['query']['filter']){_0x142d62[_0xb949('0x31')]=_[_0xb949('0x32')](_0x142d62[_0xb949('0x31')],{'$or':_[_0xb949('0x21')](_0x142d62['attributes'],function(_0x157d77){var _0x55b69c={};_0x55b69c[_0x157d77]={'$like':'%'+_0x231725[_0xb949('0x26')][_0xb949('0x44')]+'%'};return _0x55b69c;})});}_0x142d62=_[_0xb949('0x32')]({},_0x142d62,_0x231725[_0xb949('0x34')]);return _0x491ea1[_0xb949('0x46')](_0x142d62);}})[_0xb949('0x1c')](function(_0x21180f){if(_0x21180f){_0x10d470=_0x21180f[_0xb949('0x2c')];if(!_0x231725[_0xb949('0x26')]['hasOwnProperty'](_0xb949('0x2e'))){_0x142d62[_0xb949('0x18')]=qs[_0xb949('0x18')](_0x231725[_0xb949('0x26')]['limit']);_0x142d62[_0xb949('0x17')]=qs[_0xb949('0x17')](_0x231725[_0xb949('0x26')]['offset']);}return _0x491ea1[_0xb949('0x46')](_0x142d62);}})[_0xb949('0x1c')](function(_0x41c906){if(_0x41c906){return _0x41c906?{'count':_0x10d470,'rows':_0x41c906}:null;}})[_0xb949('0x1c')](respondWithResult(_0x2e5b32,null))[_0xb949('0x3f')](handleError(_0x2e5b32,null));};