07f1668cab0cbba0ba3005d7e8914856317c1319
[motion2.git] / server / api / intDynamics365Configuration / intDynamics365Configuration.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 _0xcbcb=['model','fields','attributes','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','params','find','create','body','update','findOne','Dynamics365Field','getFields','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','jsforce','desk.js','querystring','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range','apply','destroy','then','stack','name','index','map','Dynamics365Configuration','rawAttributes','type','query','keys','filters','intersection'];(function(_0x21a9d9,_0x4d5ac8){var _0x46adce=function(_0xfdd336){while(--_0xfdd336){_0x21a9d9['push'](_0x21a9d9['shift']());}};_0x46adce(++_0x4d5ac8);}(_0xcbcb,0x6d));var _0xbcbc=function(_0x2b26c3,_0x514fd1){_0x2b26c3=_0x2b26c3-0x0;var _0x3cbde4=_0xcbcb[_0x2b26c3];return _0x3cbde4;};'use strict';var emlformat=require(_0xbcbc('0x0'));var rimraf=require(_0xbcbc('0x1'));var zipdir=require(_0xbcbc('0x2'));var jsonpatch=require(_0xbcbc('0x3'));var rp=require(_0xbcbc('0x4'));var moment=require(_0xbcbc('0x5'));var BPromise=require(_0xbcbc('0x6'));var Mustache=require(_0xbcbc('0x7'));var util=require(_0xbcbc('0x8'));var path=require(_0xbcbc('0x9'));var sox=require(_0xbcbc('0xa'));var csv=require(_0xbcbc('0xb'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xbcbc('0xc'));var _=require(_0xbcbc('0xd'));var squel=require(_0xbcbc('0xe'));var crypto=require('crypto');var jsforce=require(_0xbcbc('0xf'));var deskjs=require(_0xbcbc('0x10'));var toCsv=require(_0xbcbc('0xb'));var querystring=require(_0xbcbc('0x11'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0xbcbc('0x12'));var qs=require('../../components/parsers/qs');var as=require(_0xbcbc('0x13'));var hardwareService=require(_0xbcbc('0x14'));var logger=require(_0xbcbc('0x15'))(_0xbcbc('0x16'));var utils=require(_0xbcbc('0x17'));var config=require(_0xbcbc('0x18'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2710a6,_0x1d8d51){_0x1d8d51=_0x1d8d51||0xcc;return function(_0x4635fa){if(_0x4635fa){return _0x2710a6[_0xbcbc('0x19')](_0x1d8d51);}return _0x2710a6[_0xbcbc('0x1a')](_0x1d8d51)[_0xbcbc('0x1b')]();};}function respondWithResult(_0x1d3ec7,_0x447eb1){_0x447eb1=_0x447eb1||0xc8;return function(_0xbf69f1){if(_0xbf69f1){return _0x1d3ec7[_0xbcbc('0x1a')](_0x447eb1)[_0xbcbc('0x1c')](_0xbf69f1);}};}function respondWithFilteredResult(_0x43cc55,_0xedde1b){return function(_0x42e123){if(_0x42e123){var _0x3420d7=typeof _0xedde1b[_0xbcbc('0x1d')]==='undefined'&&typeof _0xedde1b[_0xbcbc('0x1e')]===_0xbcbc('0x1f');var _0x28ce09=_0x42e123[_0xbcbc('0x20')];var _0x27979d=_0x3420d7?0x0:_0xedde1b[_0xbcbc('0x1d')];var _0x154ae0=_0x3420d7?_0x42e123[_0xbcbc('0x20')]:_0xedde1b[_0xbcbc('0x1d')]+_0xedde1b[_0xbcbc('0x1e')];var _0x23469e;if(_0x154ae0>=_0x28ce09){_0x154ae0=_0x28ce09;_0x23469e=0xc8;}else{_0x23469e=0xce;}_0x43cc55[_0xbcbc('0x1a')](_0x23469e);return _0x43cc55[_0xbcbc('0x21')](_0xbcbc('0x22'),_0x27979d+'-'+_0x154ae0+'/'+_0x28ce09)[_0xbcbc('0x1c')](_0x42e123);}return null;};}function patchUpdates(_0x3fb0b2){return function(_0x28089e){try{jsonpatch[_0xbcbc('0x23')](_0x28089e,_0x3fb0b2,!![]);}catch(_0xcfe843){return BPromise['reject'](_0xcfe843);}return _0x28089e['save']();};}function saveUpdates(_0x5ddc03,_0x913ddc){return function(_0xd6c923){if(_0xd6c923){return _0xd6c923['update'](_0x5ddc03)['then'](function(_0x309103){return _0x309103;});}return null;};}function removeEntity(_0x3c45ab,_0x31725f){return function(_0x487058){if(_0x487058){return _0x487058[_0xbcbc('0x24')]()[_0xbcbc('0x25')](function(){_0x3c45ab['status'](0xcc)[_0xbcbc('0x1b')]();});}};}function handleEntityNotFound(_0x220046,_0x3745a4){return function(_0x54f162){if(!_0x54f162){_0x220046[_0xbcbc('0x19')](0x194);}return _0x54f162;};}function handleError(_0x2c499e,_0x2dad96){_0x2dad96=_0x2dad96||0x1f4;return function(_0x218834){logger['error'](_0x218834[_0xbcbc('0x26')]);if(_0x218834[_0xbcbc('0x27')]){delete _0x218834[_0xbcbc('0x27')];}_0x2c499e[_0xbcbc('0x1a')](_0x2dad96)['send'](_0x218834);};}exports[_0xbcbc('0x28')]=function(_0x41acdb,_0x251f38){var _0x4a8335={},_0x12a537={},_0x141dcb={'count':0x0,'rows':[]};var _0x5a7d9e=_[_0xbcbc('0x29')](db[_0xbcbc('0x2a')][_0xbcbc('0x2b')],function(_0x5b6a53){return{'name':_0x5b6a53['fieldName'],'type':_0x5b6a53[_0xbcbc('0x2c')]['key']};});_0x12a537['model']=_[_0xbcbc('0x29')](_0x5a7d9e,'name');_0x12a537[_0xbcbc('0x2d')]=_[_0xbcbc('0x2e')](_0x41acdb[_0xbcbc('0x2d')]);_0x12a537[_0xbcbc('0x2f')]=_[_0xbcbc('0x30')](_0x12a537[_0xbcbc('0x31')],_0x12a537[_0xbcbc('0x2d')]);_0x4a8335['attributes']=_['intersection'](_0x12a537[_0xbcbc('0x31')],qs[_0xbcbc('0x32')](_0x41acdb[_0xbcbc('0x2d')][_0xbcbc('0x32')]));_0x4a8335['attributes']=_0x4a8335[_0xbcbc('0x33')][_0xbcbc('0x34')]?_0x4a8335['attributes']:_0x12a537['model'];if(!_0x41acdb[_0xbcbc('0x2d')][_0xbcbc('0x35')](_0xbcbc('0x36'))){_0x4a8335[_0xbcbc('0x1e')]=qs[_0xbcbc('0x1e')](_0x41acdb[_0xbcbc('0x2d')]['limit']);_0x4a8335['offset']=qs[_0xbcbc('0x1d')](_0x41acdb[_0xbcbc('0x2d')]['offset']);}_0x4a8335[_0xbcbc('0x37')]=qs['sort'](_0x41acdb[_0xbcbc('0x2d')][_0xbcbc('0x38')]);_0x4a8335['where']=qs[_0xbcbc('0x2f')](_[_0xbcbc('0x39')](_0x41acdb[_0xbcbc('0x2d')],_0x12a537[_0xbcbc('0x2f')]),_0x5a7d9e);if(_0x41acdb[_0xbcbc('0x2d')][_0xbcbc('0x3a')]){_0x4a8335[_0xbcbc('0x3b')]=_[_0xbcbc('0x3c')](_0x4a8335['where'],{'$or':_[_0xbcbc('0x29')](_0x5a7d9e,function(_0x35c21d){if(_0x35c21d['type']!==_0xbcbc('0x3d')){var _0x339215={};_0x339215[_0x35c21d[_0xbcbc('0x27')]]={'$like':'%'+_0x41acdb[_0xbcbc('0x2d')]['filter']+'%'};return _0x339215;}})});}_0x4a8335=_[_0xbcbc('0x3c')]({},_0x4a8335,_0x41acdb[_0xbcbc('0x3e')]);var _0x4aa73a={'where':_0x4a8335['where']};return db['Dynamics365Configuration'][_0xbcbc('0x20')](_0x4aa73a)[_0xbcbc('0x25')](function(_0x3e1c57){_0x141dcb[_0xbcbc('0x20')]=_0x3e1c57;if(_0x41acdb[_0xbcbc('0x2d')][_0xbcbc('0x3f')]){_0x4a8335[_0xbcbc('0x40')]=[{'all':!![]}];}return db[_0xbcbc('0x2a')][_0xbcbc('0x41')](_0x4a8335);})['then'](function(_0x5e87a8){_0x141dcb[_0xbcbc('0x42')]=_0x5e87a8;return _0x141dcb;})[_0xbcbc('0x25')](respondWithFilteredResult(_0x251f38,_0x4a8335))[_0xbcbc('0x43')](handleError(_0x251f38,null));};exports[_0xbcbc('0x44')]=function(_0x578a3b,_0x335e3f){var _0x31e9b0={'raw':![],'where':{'id':_0x578a3b[_0xbcbc('0x45')]['id']}},_0x402198={};_0x402198['model']=_[_0xbcbc('0x2e')](db[_0xbcbc('0x2a')][_0xbcbc('0x2b')]);_0x402198[_0xbcbc('0x2d')]=_[_0xbcbc('0x2e')](_0x578a3b[_0xbcbc('0x2d')]);_0x402198[_0xbcbc('0x2f')]=_['intersection'](_0x402198[_0xbcbc('0x31')],_0x402198['query']);_0x31e9b0[_0xbcbc('0x33')]=_[_0xbcbc('0x30')](_0x402198[_0xbcbc('0x31')],qs[_0xbcbc('0x32')](_0x578a3b[_0xbcbc('0x2d')][_0xbcbc('0x32')]));_0x31e9b0[_0xbcbc('0x33')]=_0x31e9b0[_0xbcbc('0x33')][_0xbcbc('0x34')]?_0x31e9b0[_0xbcbc('0x33')]:_0x402198['model'];if(_0x578a3b[_0xbcbc('0x2d')][_0xbcbc('0x3f')]){_0x31e9b0[_0xbcbc('0x40')]=[{'all':!![]}];}_0x31e9b0=_[_0xbcbc('0x3c')]({},_0x31e9b0,_0x578a3b['options']);return db[_0xbcbc('0x2a')][_0xbcbc('0x46')](_0x31e9b0)['then'](handleEntityNotFound(_0x335e3f,null))['then'](respondWithResult(_0x335e3f,null))[_0xbcbc('0x43')](handleError(_0x335e3f,null));};exports['create']=function(_0x2ed45f,_0x21df2e){return db[_0xbcbc('0x2a')][_0xbcbc('0x47')](_0x2ed45f[_0xbcbc('0x48')],{})[_0xbcbc('0x25')](respondWithResult(_0x21df2e,0xc9))[_0xbcbc('0x43')](handleError(_0x21df2e,null));};exports[_0xbcbc('0x49')]=function(_0x1e9468,_0x273529){if(_0x1e9468[_0xbcbc('0x48')]['id']){delete _0x1e9468[_0xbcbc('0x48')]['id'];}return db[_0xbcbc('0x2a')]['find']({'where':{'id':_0x1e9468['params']['id']}})[_0xbcbc('0x25')](handleEntityNotFound(_0x273529,null))[_0xbcbc('0x25')](saveUpdates(_0x1e9468[_0xbcbc('0x48')],null))[_0xbcbc('0x25')](respondWithResult(_0x273529,null))[_0xbcbc('0x43')](handleError(_0x273529,null));};exports[_0xbcbc('0x24')]=function(_0x4be12c,_0x14411e){return db['Dynamics365Configuration'][_0xbcbc('0x46')]({'where':{'id':_0x4be12c[_0xbcbc('0x45')]['id']}})[_0xbcbc('0x25')](handleEntityNotFound(_0x14411e,null))['then'](removeEntity(_0x14411e,null))[_0xbcbc('0x43')](handleError(_0x14411e,null));};exports['getFields']=function(_0x105dd9,_0xcc9dad,_0x435d9f){var _0x4765eb={};var _0x2fd2ed={};var _0x235205;var _0x12ac26;return db['Dynamics365Configuration'][_0xbcbc('0x4a')]({'where':{'id':_0x105dd9[_0xbcbc('0x45')]['id']}})[_0xbcbc('0x25')](handleEntityNotFound(_0xcc9dad,null))[_0xbcbc('0x25')](function(_0x517440){if(_0x517440){_0x235205=_0x517440;_0x2fd2ed['model']=_[_0xbcbc('0x2e')](db[_0xbcbc('0x4b')]['rawAttributes']);_0x2fd2ed['query']=_[_0xbcbc('0x2e')](_0x105dd9['query']);_0x2fd2ed[_0xbcbc('0x2f')]=_[_0xbcbc('0x30')](_0x2fd2ed[_0xbcbc('0x31')],_0x2fd2ed[_0xbcbc('0x2d')]);_0x4765eb[_0xbcbc('0x33')]=_['intersection'](_0x2fd2ed['model'],qs['fields'](_0x105dd9[_0xbcbc('0x2d')][_0xbcbc('0x32')]));_0x4765eb[_0xbcbc('0x33')]=_0x4765eb[_0xbcbc('0x33')]['length']?_0x4765eb[_0xbcbc('0x33')]:_0x2fd2ed[_0xbcbc('0x31')];_0x4765eb[_0xbcbc('0x37')]=qs[_0xbcbc('0x38')](_0x105dd9[_0xbcbc('0x2d')][_0xbcbc('0x38')]);_0x4765eb[_0xbcbc('0x3b')]=qs[_0xbcbc('0x2f')](_[_0xbcbc('0x39')](_0x105dd9[_0xbcbc('0x2d')],_0x2fd2ed[_0xbcbc('0x2f')]));if(_0x105dd9[_0xbcbc('0x2d')]['filter']){_0x4765eb[_0xbcbc('0x3b')]=_['merge'](_0x4765eb['where'],{'$or':_[_0xbcbc('0x29')](_0x4765eb[_0xbcbc('0x33')],function(_0x15f968){var _0x41d775={};_0x41d775[_0x15f968]={'$like':'%'+_0x105dd9[_0xbcbc('0x2d')][_0xbcbc('0x3a')]+'%'};return _0x41d775;})});}_0x4765eb=_[_0xbcbc('0x3c')]({},_0x4765eb,_0x105dd9['options']);return _0x235205[_0xbcbc('0x4c')](_0x4765eb);}})['then'](function(_0x648c08){if(_0x648c08){_0x12ac26=_0x648c08[_0xbcbc('0x34')];if(!_0x105dd9[_0xbcbc('0x2d')][_0xbcbc('0x35')](_0xbcbc('0x36'))){_0x4765eb[_0xbcbc('0x1e')]=qs[_0xbcbc('0x1e')](_0x105dd9[_0xbcbc('0x2d')][_0xbcbc('0x1e')]);_0x4765eb[_0xbcbc('0x1d')]=qs[_0xbcbc('0x1d')](_0x105dd9[_0xbcbc('0x2d')][_0xbcbc('0x1d')]);}return _0x235205['getFields'](_0x4765eb);}})[_0xbcbc('0x25')](function(_0x16122b){if(_0x16122b){return _0x16122b?{'count':_0x12ac26,'rows':_0x16122b}:null;}})['then'](respondWithResult(_0xcc9dad,null))['catch'](handleError(_0xcc9dad,null));};exports[_0xbcbc('0x4d')]=function(_0x101b5a,_0x4b290a,_0x2940ee){var _0xbcf8da={};var _0xc625fb={};var _0x5133a2;var _0x14cd24;return db[_0xbcbc('0x2a')][_0xbcbc('0x4a')]({'where':{'id':_0x101b5a['params']['id']}})[_0xbcbc('0x25')](handleEntityNotFound(_0x4b290a,null))[_0xbcbc('0x25')](function(_0x3b494f){if(_0x3b494f){_0x5133a2=_0x3b494f;_0xc625fb[_0xbcbc('0x31')]=_[_0xbcbc('0x2e')](db[_0xbcbc('0x4b')][_0xbcbc('0x2b')]);_0xc625fb[_0xbcbc('0x2d')]=_[_0xbcbc('0x2e')](_0x101b5a[_0xbcbc('0x2d')]);_0xc625fb['filters']=_[_0xbcbc('0x30')](_0xc625fb[_0xbcbc('0x31')],_0xc625fb['query']);_0xbcf8da['attributes']=_['intersection'](_0xc625fb[_0xbcbc('0x31')],qs[_0xbcbc('0x32')](_0x101b5a['query']['fields']));_0xbcf8da['attributes']=_0xbcf8da[_0xbcbc('0x33')][_0xbcbc('0x34')]?_0xbcf8da[_0xbcbc('0x33')]:_0xc625fb['model'];_0xbcf8da[_0xbcbc('0x37')]=qs[_0xbcbc('0x38')](_0x101b5a[_0xbcbc('0x2d')][_0xbcbc('0x38')]);_0xbcf8da[_0xbcbc('0x3b')]=qs['filters'](_[_0xbcbc('0x39')](_0x101b5a[_0xbcbc('0x2d')],_0xc625fb[_0xbcbc('0x2f')]));if(_0x101b5a['query'][_0xbcbc('0x3a')]){_0xbcf8da[_0xbcbc('0x3b')]=_['merge'](_0xbcf8da['where'],{'$or':_[_0xbcbc('0x29')](_0xbcf8da['attributes'],function(_0x3d3fd7){var _0x487b14={};_0x487b14[_0x3d3fd7]={'$like':'%'+_0x101b5a[_0xbcbc('0x2d')]['filter']+'%'};return _0x487b14;})});}_0xbcf8da=_[_0xbcbc('0x3c')]({},_0xbcf8da,_0x101b5a['options']);return _0x5133a2[_0xbcbc('0x4d')](_0xbcf8da);}})[_0xbcbc('0x25')](function(_0x3afff2){if(_0x3afff2){_0x14cd24=_0x3afff2['length'];if(!_0x101b5a[_0xbcbc('0x2d')][_0xbcbc('0x35')]('nolimit')){_0xbcf8da['limit']=qs[_0xbcbc('0x1e')](_0x101b5a[_0xbcbc('0x2d')][_0xbcbc('0x1e')]);_0xbcf8da['offset']=qs[_0xbcbc('0x1d')](_0x101b5a[_0xbcbc('0x2d')][_0xbcbc('0x1d')]);}return _0x5133a2['getSubjects'](_0xbcf8da);}})[_0xbcbc('0x25')](function(_0x37c3c5){if(_0x37c3c5){return _0x37c3c5?{'count':_0x14cd24,'rows':_0x37c3c5}:null;}})[_0xbcbc('0x25')](respondWithResult(_0x4b290a,null))[_0xbcbc('0x43')](handleError(_0x4b290a,null));};exports[_0xbcbc('0x4e')]=function(_0x1c25bd,_0x544a8c,_0xb62e1e){var _0x33f1f4={};var _0x45254c={};var _0x1b4675;var _0x34ad0e;return db[_0xbcbc('0x2a')]['findOne']({'where':{'id':_0x1c25bd[_0xbcbc('0x45')]['id']}})[_0xbcbc('0x25')](handleEntityNotFound(_0x544a8c,null))[_0xbcbc('0x25')](function(_0x2b0946){if(_0x2b0946){_0x1b4675=_0x2b0946;_0x45254c[_0xbcbc('0x31')]=_['keys'](db['Dynamics365Field'][_0xbcbc('0x2b')]);_0x45254c[_0xbcbc('0x2d')]=_['keys'](_0x1c25bd['query']);_0x45254c['filters']=_['intersection'](_0x45254c[_0xbcbc('0x31')],_0x45254c[_0xbcbc('0x2d')]);_0x33f1f4['attributes']=_[_0xbcbc('0x30')](_0x45254c[_0xbcbc('0x31')],qs[_0xbcbc('0x32')](_0x1c25bd['query'][_0xbcbc('0x32')]));_0x33f1f4[_0xbcbc('0x33')]=_0x33f1f4[_0xbcbc('0x33')][_0xbcbc('0x34')]?_0x33f1f4['attributes']:_0x45254c[_0xbcbc('0x31')];_0x33f1f4['order']=qs[_0xbcbc('0x38')](_0x1c25bd[_0xbcbc('0x2d')][_0xbcbc('0x38')]);_0x33f1f4[_0xbcbc('0x3b')]=qs['filters'](_[_0xbcbc('0x39')](_0x1c25bd[_0xbcbc('0x2d')],_0x45254c['filters']));if(_0x1c25bd[_0xbcbc('0x2d')][_0xbcbc('0x3a')]){_0x33f1f4['where']=_[_0xbcbc('0x3c')](_0x33f1f4[_0xbcbc('0x3b')],{'$or':_['map'](_0x33f1f4[_0xbcbc('0x33')],function(_0x3c5ea6){var _0x581c04={};_0x581c04[_0x3c5ea6]={'$like':'%'+_0x1c25bd[_0xbcbc('0x2d')]['filter']+'%'};return _0x581c04;})});}_0x33f1f4=_[_0xbcbc('0x3c')]({},_0x33f1f4,_0x1c25bd[_0xbcbc('0x3e')]);return _0x1b4675[_0xbcbc('0x4e')](_0x33f1f4);}})['then'](function(_0x554441){if(_0x554441){_0x34ad0e=_0x554441[_0xbcbc('0x34')];if(!_0x1c25bd[_0xbcbc('0x2d')][_0xbcbc('0x35')](_0xbcbc('0x36'))){_0x33f1f4['limit']=qs[_0xbcbc('0x1e')](_0x1c25bd['query'][_0xbcbc('0x1e')]);_0x33f1f4[_0xbcbc('0x1d')]=qs[_0xbcbc('0x1d')](_0x1c25bd[_0xbcbc('0x2d')][_0xbcbc('0x1d')]);}return _0x1b4675[_0xbcbc('0x4e')](_0x33f1f4);}})[_0xbcbc('0x25')](function(_0x205bdb){if(_0x205bdb){return _0x205bdb?{'count':_0x34ad0e,'rows':_0x205bdb}:null;}})[_0xbcbc('0x25')](respondWithResult(_0x544a8c,null))['catch'](handleError(_0x544a8c,null));};