b56ce933d6bceb86bb45178ddbdce5f5e1543c6e
[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 _0xfc43=['filter','options','includeAll','include','findAll','catch','show','params','rawAttributes','find','create','body','getFields','findOne','ServicenowField','merge','hasOwnProperty','getSubjects','getDescriptions','eml-format','fast-json-patch','request-promise','moment','bluebird','util','path','to-csv','ejs','lodash','squel','crypto','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../mysqldb','status','end','json','limit','offset','count','set','Content-Range','apply','reject','save','update','then','destroy','sendStatus','error','stack','name','index','map','ServicenowConfiguration','fieldName','type','query','keys','filters','intersection','model','fields','attributes','length','nolimit','order','sort','where','pick','VIRTUAL'];(function(_0x51551d,_0x374eda){var _0x5957aa=function(_0x4c0b9b){while(--_0x4c0b9b){_0x51551d['push'](_0x51551d['shift']());}};_0x5957aa(++_0x374eda);}(_0xfc43,0x147));var _0x3fc4=function(_0x5020b0,_0x218f95){_0x5020b0=_0x5020b0-0x0;var _0x1b88a8=_0xfc43[_0x5020b0];return _0x1b88a8;};'use strict';var emlformat=require(_0x3fc4('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x3fc4('0x1'));var rp=require(_0x3fc4('0x2'));var moment=require(_0x3fc4('0x3'));var BPromise=require(_0x3fc4('0x4'));var Mustache=require('mustache');var util=require(_0x3fc4('0x5'));var path=require(_0x3fc4('0x6'));var sox=require('sox');var csv=require(_0x3fc4('0x7'));var ejs=require(_0x3fc4('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x3fc4('0x9'));var squel=require(_0x3fc4('0xa'));var crypto=require(_0x3fc4('0xb'));var jsforce=require('jsforce');var deskjs=require(_0x3fc4('0xc'));var toCsv=require(_0x3fc4('0x7'));var querystring=require('querystring');var Papa=require(_0x3fc4('0xd'));var Redis=require(_0x3fc4('0xe'));var authService=require(_0x3fc4('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x3fc4('0x10'));var hardwareService=require(_0x3fc4('0x11'));var logger=require('../../config/logger')(_0x3fc4('0x12'));var utils=require('../../config/utils');var config=require(_0x3fc4('0x13'));var licenseUtil=require('../../config/license/util');var db=require(_0x3fc4('0x14'))['db'];function respondWithStatusCode(_0x35c28f,_0x4dc807){_0x4dc807=_0x4dc807||0xcc;return function(_0x513a6c){if(_0x513a6c){return _0x35c28f['sendStatus'](_0x4dc807);}return _0x35c28f[_0x3fc4('0x15')](_0x4dc807)[_0x3fc4('0x16')]();};}function respondWithResult(_0x12a698,_0x1511b2){_0x1511b2=_0x1511b2||0xc8;return function(_0x3f8afa){if(_0x3f8afa){return _0x12a698[_0x3fc4('0x15')](_0x1511b2)[_0x3fc4('0x17')](_0x3f8afa);}};}function respondWithFilteredResult(_0x1b00d8,_0x1261f4){return function(_0x497973){if(_0x497973){var _0x3560a0=typeof _0x1261f4['offset']==='undefined'&&typeof _0x1261f4[_0x3fc4('0x18')]==='undefined';var _0x49fdd4=_0x497973['count'];var _0x482606=_0x3560a0?0x0:_0x1261f4[_0x3fc4('0x19')];var _0x475b88=_0x3560a0?_0x497973[_0x3fc4('0x1a')]:_0x1261f4[_0x3fc4('0x19')]+_0x1261f4[_0x3fc4('0x18')];var _0x5c9deb;if(_0x475b88>=_0x49fdd4){_0x475b88=_0x49fdd4;_0x5c9deb=0xc8;}else{_0x5c9deb=0xce;}_0x1b00d8[_0x3fc4('0x15')](_0x5c9deb);return _0x1b00d8[_0x3fc4('0x1b')](_0x3fc4('0x1c'),_0x482606+'-'+_0x475b88+'/'+_0x49fdd4)['json'](_0x497973);}return null;};}function patchUpdates(_0xe0a8c0){return function(_0x23c948){try{jsonpatch[_0x3fc4('0x1d')](_0x23c948,_0xe0a8c0,!![]);}catch(_0x53c4c9){return BPromise[_0x3fc4('0x1e')](_0x53c4c9);}return _0x23c948[_0x3fc4('0x1f')]();};}function saveUpdates(_0x43ee91,_0x2cac6b){return function(_0x2b3979){if(_0x2b3979){return _0x2b3979[_0x3fc4('0x20')](_0x43ee91)[_0x3fc4('0x21')](function(_0x515ff2){return _0x515ff2;});}return null;};}function removeEntity(_0x26704c,_0x3d13d1){return function(_0x1ec83c){if(_0x1ec83c){return _0x1ec83c[_0x3fc4('0x22')]()[_0x3fc4('0x21')](function(){_0x26704c[_0x3fc4('0x15')](0xcc)[_0x3fc4('0x16')]();});}};}function handleEntityNotFound(_0x37aa5b,_0x275353){return function(_0x205cfd){if(!_0x205cfd){_0x37aa5b[_0x3fc4('0x23')](0x194);}return _0x205cfd;};}function handleError(_0x52dc8a,_0x46ba49){_0x46ba49=_0x46ba49||0x1f4;return function(_0x5737f5){logger[_0x3fc4('0x24')](_0x5737f5[_0x3fc4('0x25')]);if(_0x5737f5[_0x3fc4('0x26')]){delete _0x5737f5[_0x3fc4('0x26')];}_0x52dc8a[_0x3fc4('0x15')](_0x46ba49)['send'](_0x5737f5);};}exports[_0x3fc4('0x27')]=function(_0x1bf990,_0x51f1fe){var _0xca57d1={},_0x1dc822={},_0x13b626={'count':0x0,'rows':[]};var _0x504a6c=_[_0x3fc4('0x28')](db[_0x3fc4('0x29')]['rawAttributes'],function(_0x40f575){return{'name':_0x40f575[_0x3fc4('0x2a')],'type':_0x40f575[_0x3fc4('0x2b')]['key']};});_0x1dc822['model']=_[_0x3fc4('0x28')](_0x504a6c,'name');_0x1dc822[_0x3fc4('0x2c')]=_[_0x3fc4('0x2d')](_0x1bf990['query']);_0x1dc822[_0x3fc4('0x2e')]=_[_0x3fc4('0x2f')](_0x1dc822[_0x3fc4('0x30')],_0x1dc822[_0x3fc4('0x2c')]);_0xca57d1['attributes']=_['intersection'](_0x1dc822['model'],qs[_0x3fc4('0x31')](_0x1bf990['query'][_0x3fc4('0x31')]));_0xca57d1[_0x3fc4('0x32')]=_0xca57d1[_0x3fc4('0x32')][_0x3fc4('0x33')]?_0xca57d1[_0x3fc4('0x32')]:_0x1dc822[_0x3fc4('0x30')];if(!_0x1bf990[_0x3fc4('0x2c')]['hasOwnProperty'](_0x3fc4('0x34'))){_0xca57d1['limit']=qs[_0x3fc4('0x18')](_0x1bf990[_0x3fc4('0x2c')][_0x3fc4('0x18')]);_0xca57d1['offset']=qs[_0x3fc4('0x19')](_0x1bf990[_0x3fc4('0x2c')][_0x3fc4('0x19')]);}_0xca57d1[_0x3fc4('0x35')]=qs[_0x3fc4('0x36')](_0x1bf990[_0x3fc4('0x2c')][_0x3fc4('0x36')]);_0xca57d1[_0x3fc4('0x37')]=qs['filters'](_[_0x3fc4('0x38')](_0x1bf990[_0x3fc4('0x2c')],_0x1dc822[_0x3fc4('0x2e')]),_0x504a6c);if(_0x1bf990[_0x3fc4('0x2c')]['filter']){_0xca57d1[_0x3fc4('0x37')]=_['merge'](_0xca57d1[_0x3fc4('0x37')],{'$or':_[_0x3fc4('0x28')](_0x504a6c,function(_0x1f743d){if(_0x1f743d[_0x3fc4('0x2b')]!==_0x3fc4('0x39')){var _0x29403a={};_0x29403a[_0x1f743d[_0x3fc4('0x26')]]={'$like':'%'+_0x1bf990[_0x3fc4('0x2c')][_0x3fc4('0x3a')]+'%'};return _0x29403a;}})});}_0xca57d1=_['merge']({},_0xca57d1,_0x1bf990[_0x3fc4('0x3b')]);var _0x48d02e={'where':_0xca57d1[_0x3fc4('0x37')]};return db[_0x3fc4('0x29')][_0x3fc4('0x1a')](_0x48d02e)['then'](function(_0x4db801){_0x13b626[_0x3fc4('0x1a')]=_0x4db801;if(_0x1bf990[_0x3fc4('0x2c')][_0x3fc4('0x3c')]){_0xca57d1[_0x3fc4('0x3d')]=[{'all':!![]}];}return db['ServicenowConfiguration'][_0x3fc4('0x3e')](_0xca57d1);})[_0x3fc4('0x21')](function(_0x463d61){_0x13b626['rows']=_0x463d61;return _0x13b626;})[_0x3fc4('0x21')](respondWithFilteredResult(_0x51f1fe,_0xca57d1))[_0x3fc4('0x3f')](handleError(_0x51f1fe,null));};exports[_0x3fc4('0x40')]=function(_0x3c00ab,_0x5471d8){var _0x3cd9f5={'raw':![],'where':{'id':_0x3c00ab[_0x3fc4('0x41')]['id']}},_0x781bd6={};_0x781bd6[_0x3fc4('0x30')]=_[_0x3fc4('0x2d')](db[_0x3fc4('0x29')][_0x3fc4('0x42')]);_0x781bd6[_0x3fc4('0x2c')]=_[_0x3fc4('0x2d')](_0x3c00ab[_0x3fc4('0x2c')]);_0x781bd6[_0x3fc4('0x2e')]=_[_0x3fc4('0x2f')](_0x781bd6[_0x3fc4('0x30')],_0x781bd6[_0x3fc4('0x2c')]);_0x3cd9f5['attributes']=_[_0x3fc4('0x2f')](_0x781bd6['model'],qs['fields'](_0x3c00ab['query']['fields']));_0x3cd9f5[_0x3fc4('0x32')]=_0x3cd9f5['attributes'][_0x3fc4('0x33')]?_0x3cd9f5[_0x3fc4('0x32')]:_0x781bd6[_0x3fc4('0x30')];if(_0x3c00ab[_0x3fc4('0x2c')][_0x3fc4('0x3c')]){_0x3cd9f5[_0x3fc4('0x3d')]=[{'all':!![]}];}_0x3cd9f5=_['merge']({},_0x3cd9f5,_0x3c00ab[_0x3fc4('0x3b')]);return db[_0x3fc4('0x29')][_0x3fc4('0x43')](_0x3cd9f5)[_0x3fc4('0x21')](handleEntityNotFound(_0x5471d8,null))[_0x3fc4('0x21')](respondWithResult(_0x5471d8,null))['catch'](handleError(_0x5471d8,null));};exports['create']=function(_0x44deee,_0xd46648){return db[_0x3fc4('0x29')][_0x3fc4('0x44')](_0x44deee[_0x3fc4('0x45')],{})[_0x3fc4('0x21')](respondWithResult(_0xd46648,0xc9))[_0x3fc4('0x3f')](handleError(_0xd46648,null));};exports[_0x3fc4('0x20')]=function(_0x3e6276,_0x450319){if(_0x3e6276[_0x3fc4('0x45')]['id']){delete _0x3e6276[_0x3fc4('0x45')]['id'];}return db[_0x3fc4('0x29')][_0x3fc4('0x43')]({'where':{'id':_0x3e6276[_0x3fc4('0x41')]['id']}})['then'](handleEntityNotFound(_0x450319,null))[_0x3fc4('0x21')](saveUpdates(_0x3e6276[_0x3fc4('0x45')],null))[_0x3fc4('0x21')](respondWithResult(_0x450319,null))[_0x3fc4('0x3f')](handleError(_0x450319,null));};exports[_0x3fc4('0x22')]=function(_0x7c8987,_0x47d6b2){return db[_0x3fc4('0x29')][_0x3fc4('0x43')]({'where':{'id':_0x7c8987['params']['id']}})['then'](handleEntityNotFound(_0x47d6b2,null))['then'](removeEntity(_0x47d6b2,null))[_0x3fc4('0x3f')](handleError(_0x47d6b2,null));};exports[_0x3fc4('0x46')]=function(_0x511f16,_0x452cbc,_0x53a772){var _0x16ea96={};var _0x308a5e={};var _0x43f4c2;var _0x4e3206;return db['ServicenowConfiguration'][_0x3fc4('0x47')]({'where':{'id':_0x511f16[_0x3fc4('0x41')]['id']}})[_0x3fc4('0x21')](handleEntityNotFound(_0x452cbc,null))[_0x3fc4('0x21')](function(_0xa84ace){if(_0xa84ace){_0x43f4c2=_0xa84ace;_0x308a5e[_0x3fc4('0x30')]=_['keys'](db[_0x3fc4('0x48')][_0x3fc4('0x42')]);_0x308a5e[_0x3fc4('0x2c')]=_[_0x3fc4('0x2d')](_0x511f16[_0x3fc4('0x2c')]);_0x308a5e['filters']=_[_0x3fc4('0x2f')](_0x308a5e[_0x3fc4('0x30')],_0x308a5e[_0x3fc4('0x2c')]);_0x16ea96['attributes']=_[_0x3fc4('0x2f')](_0x308a5e[_0x3fc4('0x30')],qs[_0x3fc4('0x31')](_0x511f16[_0x3fc4('0x2c')]['fields']));_0x16ea96[_0x3fc4('0x32')]=_0x16ea96[_0x3fc4('0x32')][_0x3fc4('0x33')]?_0x16ea96[_0x3fc4('0x32')]:_0x308a5e[_0x3fc4('0x30')];_0x16ea96[_0x3fc4('0x35')]=qs[_0x3fc4('0x36')](_0x511f16[_0x3fc4('0x2c')]['sort']);_0x16ea96[_0x3fc4('0x37')]=qs[_0x3fc4('0x2e')](_[_0x3fc4('0x38')](_0x511f16[_0x3fc4('0x2c')],_0x308a5e[_0x3fc4('0x2e')]));if(_0x511f16[_0x3fc4('0x2c')][_0x3fc4('0x3a')]){_0x16ea96[_0x3fc4('0x37')]=_['merge'](_0x16ea96[_0x3fc4('0x37')],{'$or':_[_0x3fc4('0x28')](_0x16ea96['attributes'],function(_0xd8f023){var _0x26bd61={};_0x26bd61[_0xd8f023]={'$like':'%'+_0x511f16['query']['filter']+'%'};return _0x26bd61;})});}_0x16ea96=_[_0x3fc4('0x49')]({},_0x16ea96,_0x511f16[_0x3fc4('0x3b')]);return _0x43f4c2[_0x3fc4('0x46')](_0x16ea96);}})[_0x3fc4('0x21')](function(_0x3e50f9){if(_0x3e50f9){_0x4e3206=_0x3e50f9[_0x3fc4('0x33')];if(!_0x511f16[_0x3fc4('0x2c')][_0x3fc4('0x4a')]('nolimit')){_0x16ea96[_0x3fc4('0x18')]=qs[_0x3fc4('0x18')](_0x511f16[_0x3fc4('0x2c')][_0x3fc4('0x18')]);_0x16ea96[_0x3fc4('0x19')]=qs[_0x3fc4('0x19')](_0x511f16[_0x3fc4('0x2c')][_0x3fc4('0x19')]);}return _0x43f4c2[_0x3fc4('0x46')](_0x16ea96);}})[_0x3fc4('0x21')](function(_0x5070bf){if(_0x5070bf){return _0x5070bf?{'count':_0x4e3206,'rows':_0x5070bf}:null;}})[_0x3fc4('0x21')](respondWithResult(_0x452cbc,null))[_0x3fc4('0x3f')](handleError(_0x452cbc,null));};exports['getSubjects']=function(_0x3466a7,_0x41469c,_0x16c974){var _0x148d6c={};var _0xda4f24={};var _0x18cf79;var _0x1dec72;return db['ServicenowConfiguration']['findOne']({'where':{'id':_0x3466a7['params']['id']}})['then'](handleEntityNotFound(_0x41469c,null))[_0x3fc4('0x21')](function(_0x113ce8){if(_0x113ce8){_0x18cf79=_0x113ce8;_0xda4f24[_0x3fc4('0x30')]=_[_0x3fc4('0x2d')](db['ServicenowField'][_0x3fc4('0x42')]);_0xda4f24[_0x3fc4('0x2c')]=_[_0x3fc4('0x2d')](_0x3466a7['query']);_0xda4f24['filters']=_[_0x3fc4('0x2f')](_0xda4f24[_0x3fc4('0x30')],_0xda4f24[_0x3fc4('0x2c')]);_0x148d6c[_0x3fc4('0x32')]=_['intersection'](_0xda4f24[_0x3fc4('0x30')],qs[_0x3fc4('0x31')](_0x3466a7[_0x3fc4('0x2c')]['fields']));_0x148d6c['attributes']=_0x148d6c[_0x3fc4('0x32')]['length']?_0x148d6c[_0x3fc4('0x32')]:_0xda4f24['model'];_0x148d6c[_0x3fc4('0x35')]=qs['sort'](_0x3466a7[_0x3fc4('0x2c')][_0x3fc4('0x36')]);_0x148d6c[_0x3fc4('0x37')]=qs[_0x3fc4('0x2e')](_[_0x3fc4('0x38')](_0x3466a7[_0x3fc4('0x2c')],_0xda4f24[_0x3fc4('0x2e')]));if(_0x3466a7[_0x3fc4('0x2c')][_0x3fc4('0x3a')]){_0x148d6c['where']=_[_0x3fc4('0x49')](_0x148d6c['where'],{'$or':_[_0x3fc4('0x28')](_0x148d6c[_0x3fc4('0x32')],function(_0x3e901b){var _0xffd4f0={};_0xffd4f0[_0x3e901b]={'$like':'%'+_0x3466a7[_0x3fc4('0x2c')]['filter']+'%'};return _0xffd4f0;})});}_0x148d6c=_[_0x3fc4('0x49')]({},_0x148d6c,_0x3466a7['options']);return _0x18cf79['getSubjects'](_0x148d6c);}})['then'](function(_0x2af9ad){if(_0x2af9ad){_0x1dec72=_0x2af9ad[_0x3fc4('0x33')];if(!_0x3466a7[_0x3fc4('0x2c')][_0x3fc4('0x4a')](_0x3fc4('0x34'))){_0x148d6c[_0x3fc4('0x18')]=qs[_0x3fc4('0x18')](_0x3466a7['query'][_0x3fc4('0x18')]);_0x148d6c['offset']=qs['offset'](_0x3466a7[_0x3fc4('0x2c')][_0x3fc4('0x19')]);}return _0x18cf79[_0x3fc4('0x4b')](_0x148d6c);}})['then'](function(_0x491e5b){if(_0x491e5b){return _0x491e5b?{'count':_0x1dec72,'rows':_0x491e5b}:null;}})[_0x3fc4('0x21')](respondWithResult(_0x41469c,null))[_0x3fc4('0x3f')](handleError(_0x41469c,null));};exports[_0x3fc4('0x4c')]=function(_0x53b0b7,_0x4692d6,_0x3bcc21){var _0x367d24={};var _0x4684e3={};var _0x14aa77;var _0x4f116d;return db[_0x3fc4('0x29')][_0x3fc4('0x47')]({'where':{'id':_0x53b0b7['params']['id']}})[_0x3fc4('0x21')](handleEntityNotFound(_0x4692d6,null))[_0x3fc4('0x21')](function(_0x7c59c8){if(_0x7c59c8){_0x14aa77=_0x7c59c8;_0x4684e3[_0x3fc4('0x30')]=_[_0x3fc4('0x2d')](db[_0x3fc4('0x48')]['rawAttributes']);_0x4684e3['query']=_['keys'](_0x53b0b7['query']);_0x4684e3['filters']=_[_0x3fc4('0x2f')](_0x4684e3[_0x3fc4('0x30')],_0x4684e3[_0x3fc4('0x2c')]);_0x367d24['attributes']=_['intersection'](_0x4684e3['model'],qs[_0x3fc4('0x31')](_0x53b0b7[_0x3fc4('0x2c')]['fields']));_0x367d24[_0x3fc4('0x32')]=_0x367d24[_0x3fc4('0x32')][_0x3fc4('0x33')]?_0x367d24[_0x3fc4('0x32')]:_0x4684e3[_0x3fc4('0x30')];_0x367d24[_0x3fc4('0x35')]=qs[_0x3fc4('0x36')](_0x53b0b7[_0x3fc4('0x2c')][_0x3fc4('0x36')]);_0x367d24[_0x3fc4('0x37')]=qs[_0x3fc4('0x2e')](_['pick'](_0x53b0b7[_0x3fc4('0x2c')],_0x4684e3[_0x3fc4('0x2e')]));if(_0x53b0b7[_0x3fc4('0x2c')][_0x3fc4('0x3a')]){_0x367d24[_0x3fc4('0x37')]=_[_0x3fc4('0x49')](_0x367d24[_0x3fc4('0x37')],{'$or':_['map'](_0x367d24['attributes'],function(_0x489b83){var _0x5a32e0={};_0x5a32e0[_0x489b83]={'$like':'%'+_0x53b0b7[_0x3fc4('0x2c')]['filter']+'%'};return _0x5a32e0;})});}_0x367d24=_[_0x3fc4('0x49')]({},_0x367d24,_0x53b0b7['options']);return _0x14aa77[_0x3fc4('0x4c')](_0x367d24);}})[_0x3fc4('0x21')](function(_0x1ada18){if(_0x1ada18){_0x4f116d=_0x1ada18[_0x3fc4('0x33')];if(!_0x53b0b7[_0x3fc4('0x2c')][_0x3fc4('0x4a')](_0x3fc4('0x34'))){_0x367d24['limit']=qs[_0x3fc4('0x18')](_0x53b0b7[_0x3fc4('0x2c')][_0x3fc4('0x18')]);_0x367d24[_0x3fc4('0x19')]=qs[_0x3fc4('0x19')](_0x53b0b7[_0x3fc4('0x2c')]['offset']);}return _0x14aa77[_0x3fc4('0x4c')](_0x367d24);}})[_0x3fc4('0x21')](function(_0x4a2f3d){if(_0x4a2f3d){return _0x4a2f3d?{'count':_0x4f116d,'rows':_0x4a2f3d}:null;}})[_0x3fc4('0x21')](respondWithResult(_0x4692d6,null))[_0x3fc4('0x3f')](handleError(_0x4692d6,null));};