Built motion from commit c738b9ac.|2.6.25
[motion2.git] / server / api / squareOdbc / squareOdbc.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 _0xbb00=['name','send','map','rawAttributes','type','key','model','query','filters','intersection','attributes','fields','hasOwnProperty','order','sort','where','filter','merge','SquareOdbc','includeAll','include','findAll','rows','catch','show','params','keys','length','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','ODBC','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','test','dsn','odbc','openSync','closeSync','eml-format','fast-json-patch','request-promise','moment','bluebird','util','sox','ejs','fs-extra','lodash','squel','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','status','json','undefined','limit','offset','count','set','Content-Range','apply','reject','save','update','then','get','UserProfileResource','destroy','end','sendStatus','error'];(function(_0x2f4f07,_0x554421){var _0x58721c=function(_0x270f72){while(--_0x270f72){_0x2f4f07['push'](_0x2f4f07['shift']());}};_0x58721c(++_0x554421);}(_0xbb00,0x1d9));var _0x0bb0=function(_0x46a38b,_0x16ffb5){_0x46a38b=_0x46a38b-0x0;var _0x346e75=_0xbb00[_0x46a38b];return _0x346e75;};'use strict';var emlformat=require(_0x0bb0('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x0bb0('0x1'));var rp=require(_0x0bb0('0x2'));var moment=require(_0x0bb0('0x3'));var BPromise=require(_0x0bb0('0x4'));var Mustache=require('mustache');var util=require(_0x0bb0('0x5'));var path=require('path');var sox=require(_0x0bb0('0x6'));var csv=require('to-csv');var ejs=require(_0x0bb0('0x7'));var fs=require('fs');var fs_extra=require(_0x0bb0('0x8'));var _=require(_0x0bb0('0x9'));var squel=require(_0x0bb0('0xa'));var crypto=require('crypto');var jsforce=require(_0x0bb0('0xb'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x0bb0('0xc'));var Papa=require(_0x0bb0('0xd'));var Redis=require(_0x0bb0('0xe'));var authService=require(_0x0bb0('0xf'));var qs=require(_0x0bb0('0x10'));var as=require(_0x0bb0('0x11'));var hardwareService=require(_0x0bb0('0x12'));var logger=require(_0x0bb0('0x13'))(_0x0bb0('0x14'));var utils=require(_0x0bb0('0x15'));var config=require(_0x0bb0('0x16'));var licenseUtil=require(_0x0bb0('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x866313,_0x4ec80c){_0x4ec80c=_0x4ec80c||0xcc;return function(_0x1147fa){if(_0x1147fa){return _0x866313['sendStatus'](_0x4ec80c);}return _0x866313['status'](_0x4ec80c)['end']();};}function respondWithResult(_0xfa5237,_0x33eff2){_0x33eff2=_0x33eff2||0xc8;return function(_0x162f75){if(_0x162f75){return _0xfa5237[_0x0bb0('0x18')](_0x33eff2)[_0x0bb0('0x19')](_0x162f75);}};}function respondWithFilteredResult(_0x21d0e7,_0xde7f57){return function(_0x2ba398){if(_0x2ba398){var _0x14a8ac=typeof _0xde7f57['offset']===_0x0bb0('0x1a')&&typeof _0xde7f57[_0x0bb0('0x1b')]===_0x0bb0('0x1a');var _0x456743=_0x2ba398['count'];var _0x3ad870=_0x14a8ac?0x0:_0xde7f57[_0x0bb0('0x1c')];var _0x15b218=_0x14a8ac?_0x2ba398[_0x0bb0('0x1d')]:_0xde7f57[_0x0bb0('0x1c')]+_0xde7f57['limit'];var _0x54da55;if(_0x15b218>=_0x456743){_0x15b218=_0x456743;_0x54da55=0xc8;}else{_0x54da55=0xce;}_0x21d0e7[_0x0bb0('0x18')](_0x54da55);return _0x21d0e7[_0x0bb0('0x1e')](_0x0bb0('0x1f'),_0x3ad870+'-'+_0x15b218+'/'+_0x456743)[_0x0bb0('0x19')](_0x2ba398);}return null;};}function patchUpdates(_0x2dd964){return function(_0xc4b506){try{jsonpatch[_0x0bb0('0x20')](_0xc4b506,_0x2dd964,!![]);}catch(_0x23b55b){return BPromise[_0x0bb0('0x21')](_0x23b55b);}return _0xc4b506[_0x0bb0('0x22')]();};}function saveUpdates(_0x4c2aca,_0x1fc3fa){return function(_0x408218){if(_0x408218){return _0x408218[_0x0bb0('0x23')](_0x4c2aca)[_0x0bb0('0x24')](function(_0x382f0f){return _0x382f0f;});}return null;};}function removeEntity(_0x5b2b81,_0x3de61a){return function(_0xd88286){if(_0xd88286){return _0xd88286['destroy']()[_0x0bb0('0x24')](function(){var _0x5402fe=_0xd88286[_0x0bb0('0x25')]({'plain':!![]});var _0x2c30a6='ODBC';return db[_0x0bb0('0x26')][_0x0bb0('0x27')]({'where':{'type':_0x2c30a6,'resourceId':_0x5402fe['id']}})[_0x0bb0('0x24')](function(){return _0xd88286;});})['then'](function(){_0x5b2b81[_0x0bb0('0x18')](0xcc)[_0x0bb0('0x28')]();});}};}function handleEntityNotFound(_0x579654,_0x1eac73){return function(_0x32a92a){if(!_0x32a92a){_0x579654[_0x0bb0('0x29')](0x194);}return _0x32a92a;};}function handleError(_0x6d1240,_0xe0d216){_0xe0d216=_0xe0d216||0x1f4;return function(_0x1258f6){logger[_0x0bb0('0x2a')](_0x1258f6['stack']);if(_0x1258f6[_0x0bb0('0x2b')]){delete _0x1258f6['name'];}_0x6d1240['status'](_0xe0d216)[_0x0bb0('0x2c')](_0x1258f6);};}exports['index']=function(_0x81bd11,_0x4aec35){var _0x262666={},_0x1c4063={},_0x4867a5={'count':0x0,'rows':[]};var _0x5056b1=_[_0x0bb0('0x2d')](db['SquareOdbc'][_0x0bb0('0x2e')],function(_0x5737e4){return{'name':_0x5737e4['fieldName'],'type':_0x5737e4[_0x0bb0('0x2f')][_0x0bb0('0x30')]};});_0x1c4063[_0x0bb0('0x31')]=_[_0x0bb0('0x2d')](_0x5056b1,_0x0bb0('0x2b'));_0x1c4063[_0x0bb0('0x32')]=_['keys'](_0x81bd11[_0x0bb0('0x32')]);_0x1c4063[_0x0bb0('0x33')]=_[_0x0bb0('0x34')](_0x1c4063[_0x0bb0('0x31')],_0x1c4063['query']);_0x262666[_0x0bb0('0x35')]=_[_0x0bb0('0x34')](_0x1c4063[_0x0bb0('0x31')],qs[_0x0bb0('0x36')](_0x81bd11[_0x0bb0('0x32')][_0x0bb0('0x36')]));_0x262666[_0x0bb0('0x35')]=_0x262666[_0x0bb0('0x35')]['length']?_0x262666[_0x0bb0('0x35')]:_0x1c4063[_0x0bb0('0x31')];if(!_0x81bd11[_0x0bb0('0x32')][_0x0bb0('0x37')]('nolimit')){_0x262666[_0x0bb0('0x1b')]=qs['limit'](_0x81bd11['query'][_0x0bb0('0x1b')]);_0x262666['offset']=qs[_0x0bb0('0x1c')](_0x81bd11[_0x0bb0('0x32')][_0x0bb0('0x1c')]);}_0x262666[_0x0bb0('0x38')]=qs[_0x0bb0('0x39')](_0x81bd11[_0x0bb0('0x32')][_0x0bb0('0x39')]);_0x262666[_0x0bb0('0x3a')]=qs[_0x0bb0('0x33')](_['pick'](_0x81bd11[_0x0bb0('0x32')],_0x1c4063['filters']),_0x5056b1);if(_0x81bd11['query'][_0x0bb0('0x3b')]){_0x262666[_0x0bb0('0x3a')]=_['merge'](_0x262666[_0x0bb0('0x3a')],{'$or':_[_0x0bb0('0x2d')](_0x5056b1,function(_0x36d776){if(_0x36d776[_0x0bb0('0x2f')]!=='VIRTUAL'){var _0x347836={};_0x347836[_0x36d776[_0x0bb0('0x2b')]]={'$like':'%'+_0x81bd11[_0x0bb0('0x32')]['filter']+'%'};return _0x347836;}})});}_0x262666=_[_0x0bb0('0x3c')]({},_0x262666,_0x81bd11['options']);var _0x4a405c={'where':_0x262666[_0x0bb0('0x3a')]};return db[_0x0bb0('0x3d')][_0x0bb0('0x1d')](_0x4a405c)['then'](function(_0x5c0574){_0x4867a5['count']=_0x5c0574;if(_0x81bd11[_0x0bb0('0x32')][_0x0bb0('0x3e')]){_0x262666[_0x0bb0('0x3f')]=[{'all':!![]}];}return db['SquareOdbc'][_0x0bb0('0x40')](_0x262666);})[_0x0bb0('0x24')](function(_0x1961a3){_0x4867a5[_0x0bb0('0x41')]=_0x1961a3;return _0x4867a5;})[_0x0bb0('0x24')](respondWithFilteredResult(_0x4aec35,_0x262666))[_0x0bb0('0x42')](handleError(_0x4aec35,null));};exports[_0x0bb0('0x43')]=function(_0x42ba36,_0x40acb1){var _0x4e4ebb={'raw':!![],'where':{'id':_0x42ba36[_0x0bb0('0x44')]['id']}},_0x19692c={};_0x19692c[_0x0bb0('0x31')]=_[_0x0bb0('0x45')](db[_0x0bb0('0x3d')]['rawAttributes']);_0x19692c[_0x0bb0('0x32')]=_[_0x0bb0('0x45')](_0x42ba36[_0x0bb0('0x32')]);_0x19692c['filters']=_[_0x0bb0('0x34')](_0x19692c[_0x0bb0('0x31')],_0x19692c[_0x0bb0('0x32')]);_0x4e4ebb[_0x0bb0('0x35')]=_[_0x0bb0('0x34')](_0x19692c[_0x0bb0('0x31')],qs[_0x0bb0('0x36')](_0x42ba36[_0x0bb0('0x32')][_0x0bb0('0x36')]));_0x4e4ebb['attributes']=_0x4e4ebb[_0x0bb0('0x35')][_0x0bb0('0x46')]?_0x4e4ebb[_0x0bb0('0x35')]:_0x19692c['model'];if(_0x42ba36[_0x0bb0('0x32')][_0x0bb0('0x3e')]){_0x4e4ebb[_0x0bb0('0x3f')]=[{'all':!![]}];}_0x4e4ebb=_[_0x0bb0('0x3c')]({},_0x4e4ebb,_0x42ba36['options']);return db[_0x0bb0('0x3d')][_0x0bb0('0x47')](_0x4e4ebb)[_0x0bb0('0x24')](handleEntityNotFound(_0x40acb1,null))['then'](respondWithResult(_0x40acb1,null))[_0x0bb0('0x42')](handleError(_0x40acb1,null));};exports[_0x0bb0('0x48')]=function(_0x35b863,_0x5e1f56){return db[_0x0bb0('0x3d')][_0x0bb0('0x48')](_0x35b863[_0x0bb0('0x49')],{})[_0x0bb0('0x24')](function(_0x4ad79d){var _0x455a88=_0x35b863[_0x0bb0('0x4a')][_0x0bb0('0x25')]({'plain':!![]});if(!_0x455a88)throw new Error(_0x0bb0('0x4b'));if(_0x455a88['role']==='user'){var _0x4c081d=_0x4ad79d['get']({'plain':!![]});var _0x21e1b9=_0x0bb0('0x4c');return db[_0x0bb0('0x4d')][_0x0bb0('0x47')]({'where':{'name':_0x21e1b9,'userProfileId':_0x455a88[_0x0bb0('0x4e')]},'raw':!![]})[_0x0bb0('0x24')](function(_0x2e2e4f){if(_0x2e2e4f&&_0x2e2e4f[_0x0bb0('0x4f')]===0x0){return db['UserProfileResource']['create']({'name':_0x4c081d[_0x0bb0('0x2b')],'resourceId':_0x4c081d['id'],'type':_0x2e2e4f[_0x0bb0('0x2b')],'sectionId':_0x2e2e4f['id']},{})[_0x0bb0('0x24')](function(){return _0x4ad79d;});}else{return _0x4ad79d;}})['catch'](function(_0x37b7c4){logger[_0x0bb0('0x2a')](_0x0bb0('0x50'),_0x37b7c4);throw _0x37b7c4;});}return _0x4ad79d;})[_0x0bb0('0x24')](respondWithResult(_0x5e1f56,0xc9))['catch'](handleError(_0x5e1f56,null));};exports[_0x0bb0('0x23')]=function(_0x543ab2,_0x537e6d){if(_0x543ab2[_0x0bb0('0x49')]['id']){delete _0x543ab2['body']['id'];}return db[_0x0bb0('0x3d')][_0x0bb0('0x47')]({'where':{'id':_0x543ab2[_0x0bb0('0x44')]['id']}})[_0x0bb0('0x24')](handleEntityNotFound(_0x537e6d,null))[_0x0bb0('0x24')](saveUpdates(_0x543ab2['body'],null))[_0x0bb0('0x24')](respondWithResult(_0x537e6d,null))[_0x0bb0('0x42')](handleError(_0x537e6d,null));};exports[_0x0bb0('0x27')]=function(_0x26ff9a,_0x5ed5cb){return db[_0x0bb0('0x3d')][_0x0bb0('0x47')]({'where':{'id':_0x26ff9a[_0x0bb0('0x44')]['id']}})['then'](handleEntityNotFound(_0x5ed5cb,null))['then'](removeEntity(_0x5ed5cb,null))['catch'](handleError(_0x5ed5cb,null));};exports[_0x0bb0('0x51')]=function(_0x165183,_0x3b8093,_0x50dd84){var _0x4bfb4c;return db[_0x0bb0('0x3d')][_0x0bb0('0x47')]({'where':{'id':_0x165183[_0x0bb0('0x44')]['id']},'attributes':['id',_0x0bb0('0x52')]})[_0x0bb0('0x24')](handleEntityNotFound(_0x3b8093,null))[_0x0bb0('0x24')](function(_0x2a6b34){if(_0x2a6b34){var _0x3e22d0=require(_0x0bb0('0x53'))();_0x3e22d0[_0x0bb0('0x54')](_0x2a6b34[_0x0bb0('0x52')]);_0x3e22d0[_0x0bb0('0x55')]();return _0x2a6b34;}})[_0x0bb0('0x24')](respondWithResult(_0x3b8093,null))[_0x0bb0('0x42')](handleError(_0x3b8093,null));};