Built motion from commit bab6f49e.|2.6.17
[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 _0x4bf0=['then','get','ODBC','UserProfileResource','sendStatus','stack','name','index','rawAttributes','type','key','model','map','query','filters','attributes','intersection','fields','length','nolimit','order','sort','where','VIRTUAL','merge','options','SquareOdbc','includeAll','include','findAll','catch','show','params','keys','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','test','odbc','openSync','dsn','eml-format','zip-dir','request-promise','bluebird','path','to-csv','fs-extra','squel','crypto','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','end','offset','undefined','count','limit','set','Content-Range','apply','reject','save','update','destroy'];(function(_0x2ab5e1,_0x47ce7a){var _0x3c1c34=function(_0x9bde4c){while(--_0x9bde4c){_0x2ab5e1['push'](_0x2ab5e1['shift']());}};_0x3c1c34(++_0x47ce7a);}(_0x4bf0,0x129));var _0x04bf=function(_0x29e193,_0x21d138){_0x29e193=_0x29e193-0x0;var _0x277581=_0x4bf0[_0x29e193];return _0x277581;};'use strict';var emlformat=require(_0x04bf('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x04bf('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x04bf('0x2'));var moment=require('moment');var BPromise=require(_0x04bf('0x3'));var Mustache=require('mustache');var util=require('util');var path=require(_0x04bf('0x4'));var sox=require('sox');var csv=require(_0x04bf('0x5'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x04bf('0x6'));var _=require('lodash');var squel=require(_0x04bf('0x7'));var crypto=require(_0x04bf('0x8'));var jsforce=require(_0x04bf('0x9'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x04bf('0xa'));var Papa=require(_0x04bf('0xb'));var Redis=require(_0x04bf('0xc'));var authService=require(_0x04bf('0xd'));var qs=require(_0x04bf('0xe'));var as=require(_0x04bf('0xf'));var hardwareService=require(_0x04bf('0x10'));var logger=require(_0x04bf('0x11'))(_0x04bf('0x12'));var utils=require(_0x04bf('0x13'));var config=require(_0x04bf('0x14'));var licenseUtil=require(_0x04bf('0x15'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0xa89b43,_0x8b8f1f){_0x8b8f1f=_0x8b8f1f||0xcc;return function(_0x59026d){if(_0x59026d){return _0xa89b43['sendStatus'](_0x8b8f1f);}return _0xa89b43[_0x04bf('0x16')](_0x8b8f1f)[_0x04bf('0x17')]();};}function respondWithResult(_0x145878,_0x26fbc2){_0x26fbc2=_0x26fbc2||0xc8;return function(_0x26d35e){if(_0x26d35e){return _0x145878[_0x04bf('0x16')](_0x26fbc2)['json'](_0x26d35e);}};}function respondWithFilteredResult(_0x202ccc,_0x26668d){return function(_0x1f8325){if(_0x1f8325){var _0x2e9295=typeof _0x26668d[_0x04bf('0x18')]===_0x04bf('0x19')&&typeof _0x26668d['limit']===_0x04bf('0x19');var _0x3d249c=_0x1f8325['count'];var _0x44dcf0=_0x2e9295?0x0:_0x26668d[_0x04bf('0x18')];var _0x12fe11=_0x2e9295?_0x1f8325[_0x04bf('0x1a')]:_0x26668d[_0x04bf('0x18')]+_0x26668d[_0x04bf('0x1b')];var _0x3d9dca;if(_0x12fe11>=_0x3d249c){_0x12fe11=_0x3d249c;_0x3d9dca=0xc8;}else{_0x3d9dca=0xce;}_0x202ccc[_0x04bf('0x16')](_0x3d9dca);return _0x202ccc[_0x04bf('0x1c')](_0x04bf('0x1d'),_0x44dcf0+'-'+_0x12fe11+'/'+_0x3d249c)['json'](_0x1f8325);}return null;};}function patchUpdates(_0x3e7a16){return function(_0x3ec593){try{jsonpatch[_0x04bf('0x1e')](_0x3ec593,_0x3e7a16,!![]);}catch(_0x57ed8b){return BPromise[_0x04bf('0x1f')](_0x57ed8b);}return _0x3ec593[_0x04bf('0x20')]();};}function saveUpdates(_0x550304,_0x241ee0){return function(_0x203b48){if(_0x203b48){return _0x203b48[_0x04bf('0x21')](_0x550304)['then'](function(_0x243509){return _0x243509;});}return null;};}function removeEntity(_0x4347d5,_0x4ac6eb){return function(_0x221bba){if(_0x221bba){return _0x221bba[_0x04bf('0x22')]()[_0x04bf('0x23')](function(){var _0x5761a6=_0x221bba[_0x04bf('0x24')]({'plain':!![]});var _0x407d01=_0x04bf('0x25');return db[_0x04bf('0x26')][_0x04bf('0x22')]({'where':{'type':_0x407d01,'resourceId':_0x5761a6['id']}})[_0x04bf('0x23')](function(){return _0x221bba;});})[_0x04bf('0x23')](function(){_0x4347d5[_0x04bf('0x16')](0xcc)[_0x04bf('0x17')]();});}};}function handleEntityNotFound(_0x464ccb,_0x1c2ba2){return function(_0x5f2839){if(!_0x5f2839){_0x464ccb[_0x04bf('0x27')](0x194);}return _0x5f2839;};}function handleError(_0x4a5435,_0x54fbe0){_0x54fbe0=_0x54fbe0||0x1f4;return function(_0x229ac2){logger['error'](_0x229ac2[_0x04bf('0x28')]);if(_0x229ac2['name']){delete _0x229ac2[_0x04bf('0x29')];}_0x4a5435[_0x04bf('0x16')](_0x54fbe0)['send'](_0x229ac2);};}exports[_0x04bf('0x2a')]=function(_0x175346,_0x5f3a27){var _0x2df097={},_0x2731d4={},_0x4aa8ee={'count':0x0,'rows':[]};var _0x5c8aa4=_['map'](db['SquareOdbc'][_0x04bf('0x2b')],function(_0x4d50de){return{'name':_0x4d50de['fieldName'],'type':_0x4d50de[_0x04bf('0x2c')][_0x04bf('0x2d')]};});_0x2731d4[_0x04bf('0x2e')]=_[_0x04bf('0x2f')](_0x5c8aa4,_0x04bf('0x29'));_0x2731d4[_0x04bf('0x30')]=_['keys'](_0x175346[_0x04bf('0x30')]);_0x2731d4[_0x04bf('0x31')]=_['intersection'](_0x2731d4['model'],_0x2731d4[_0x04bf('0x30')]);_0x2df097[_0x04bf('0x32')]=_[_0x04bf('0x33')](_0x2731d4[_0x04bf('0x2e')],qs[_0x04bf('0x34')](_0x175346[_0x04bf('0x30')][_0x04bf('0x34')]));_0x2df097[_0x04bf('0x32')]=_0x2df097['attributes'][_0x04bf('0x35')]?_0x2df097[_0x04bf('0x32')]:_0x2731d4[_0x04bf('0x2e')];if(!_0x175346['query']['hasOwnProperty'](_0x04bf('0x36'))){_0x2df097[_0x04bf('0x1b')]=qs['limit'](_0x175346[_0x04bf('0x30')][_0x04bf('0x1b')]);_0x2df097[_0x04bf('0x18')]=qs[_0x04bf('0x18')](_0x175346[_0x04bf('0x30')][_0x04bf('0x18')]);}_0x2df097[_0x04bf('0x37')]=qs['sort'](_0x175346[_0x04bf('0x30')][_0x04bf('0x38')]);_0x2df097['where']=qs[_0x04bf('0x31')](_['pick'](_0x175346['query'],_0x2731d4[_0x04bf('0x31')]),_0x5c8aa4);if(_0x175346[_0x04bf('0x30')]['filter']){_0x2df097['where']=_['merge'](_0x2df097[_0x04bf('0x39')],{'$or':_[_0x04bf('0x2f')](_0x5c8aa4,function(_0x5a1a46){if(_0x5a1a46['type']!==_0x04bf('0x3a')){var _0x2e4c02={};_0x2e4c02[_0x5a1a46[_0x04bf('0x29')]]={'$like':'%'+_0x175346[_0x04bf('0x30')]['filter']+'%'};return _0x2e4c02;}})});}_0x2df097=_[_0x04bf('0x3b')]({},_0x2df097,_0x175346[_0x04bf('0x3c')]);var _0x3418b0={'where':_0x2df097['where']};return db[_0x04bf('0x3d')][_0x04bf('0x1a')](_0x3418b0)[_0x04bf('0x23')](function(_0x42fa84){_0x4aa8ee[_0x04bf('0x1a')]=_0x42fa84;if(_0x175346[_0x04bf('0x30')][_0x04bf('0x3e')]){_0x2df097[_0x04bf('0x3f')]=[{'all':!![]}];}return db[_0x04bf('0x3d')][_0x04bf('0x40')](_0x2df097);})[_0x04bf('0x23')](function(_0x1c117d){_0x4aa8ee['rows']=_0x1c117d;return _0x4aa8ee;})[_0x04bf('0x23')](respondWithFilteredResult(_0x5f3a27,_0x2df097))[_0x04bf('0x41')](handleError(_0x5f3a27,null));};exports[_0x04bf('0x42')]=function(_0x55d4e4,_0x3f9edb){var _0x4e4828={'raw':!![],'where':{'id':_0x55d4e4[_0x04bf('0x43')]['id']}},_0x402110={};_0x402110[_0x04bf('0x2e')]=_[_0x04bf('0x44')](db[_0x04bf('0x3d')][_0x04bf('0x2b')]);_0x402110[_0x04bf('0x30')]=_[_0x04bf('0x44')](_0x55d4e4['query']);_0x402110[_0x04bf('0x31')]=_[_0x04bf('0x33')](_0x402110[_0x04bf('0x2e')],_0x402110[_0x04bf('0x30')]);_0x4e4828['attributes']=_[_0x04bf('0x33')](_0x402110[_0x04bf('0x2e')],qs[_0x04bf('0x34')](_0x55d4e4['query'][_0x04bf('0x34')]));_0x4e4828[_0x04bf('0x32')]=_0x4e4828[_0x04bf('0x32')]['length']?_0x4e4828[_0x04bf('0x32')]:_0x402110[_0x04bf('0x2e')];if(_0x55d4e4[_0x04bf('0x30')][_0x04bf('0x3e')]){_0x4e4828[_0x04bf('0x3f')]=[{'all':!![]}];}_0x4e4828=_[_0x04bf('0x3b')]({},_0x4e4828,_0x55d4e4[_0x04bf('0x3c')]);return db[_0x04bf('0x3d')][_0x04bf('0x45')](_0x4e4828)['then'](handleEntityNotFound(_0x3f9edb,null))[_0x04bf('0x23')](respondWithResult(_0x3f9edb,null))[_0x04bf('0x41')](handleError(_0x3f9edb,null));};exports[_0x04bf('0x46')]=function(_0xa5f81e,_0x537bdc){return db['SquareOdbc'][_0x04bf('0x46')](_0xa5f81e[_0x04bf('0x47')],{})[_0x04bf('0x23')](function(_0x321149){var _0x248ebe=_0xa5f81e[_0x04bf('0x48')]['get']({'plain':!![]});if(!_0x248ebe)throw new Error(_0x04bf('0x49'));if(_0x248ebe[_0x04bf('0x4a')]===_0x04bf('0x48')){var _0xaa3b83=_0x321149[_0x04bf('0x24')]({'plain':!![]});var _0xded0d0=_0x04bf('0x25');return db[_0x04bf('0x4b')][_0x04bf('0x45')]({'where':{'name':_0xded0d0,'userProfileId':_0x248ebe['userProfileId']},'raw':!![]})['then'](function(_0x4d2cd1){if(_0x4d2cd1&&_0x4d2cd1[_0x04bf('0x4c')]===0x0){return db[_0x04bf('0x26')]['create']({'name':_0xaa3b83['name'],'resourceId':_0xaa3b83['id'],'type':_0x4d2cd1[_0x04bf('0x29')],'sectionId':_0x4d2cd1['id']},{})[_0x04bf('0x23')](function(){return _0x321149;});}else{return _0x321149;}})['catch'](function(_0x324e0c){logger[_0x04bf('0x4d')](_0x04bf('0x4e'),_0x324e0c);throw _0x324e0c;});}return _0x321149;})['then'](respondWithResult(_0x537bdc,0xc9))['catch'](handleError(_0x537bdc,null));};exports[_0x04bf('0x21')]=function(_0xecdb61,_0xc265e1){if(_0xecdb61[_0x04bf('0x47')]['id']){delete _0xecdb61[_0x04bf('0x47')]['id'];}return db['SquareOdbc'][_0x04bf('0x45')]({'where':{'id':_0xecdb61[_0x04bf('0x43')]['id']}})['then'](handleEntityNotFound(_0xc265e1,null))[_0x04bf('0x23')](saveUpdates(_0xecdb61[_0x04bf('0x47')],null))[_0x04bf('0x23')](respondWithResult(_0xc265e1,null))[_0x04bf('0x41')](handleError(_0xc265e1,null));};exports['destroy']=function(_0x5ed660,_0x118c92){return db['SquareOdbc'][_0x04bf('0x45')]({'where':{'id':_0x5ed660[_0x04bf('0x43')]['id']}})[_0x04bf('0x23')](handleEntityNotFound(_0x118c92,null))[_0x04bf('0x23')](removeEntity(_0x118c92,null))[_0x04bf('0x41')](handleError(_0x118c92,null));};exports[_0x04bf('0x4f')]=function(_0x589815,_0x16261f,_0x3c9cef){var _0x4d148e;return db[_0x04bf('0x3d')][_0x04bf('0x45')]({'where':{'id':_0x589815[_0x04bf('0x43')]['id']},'attributes':['id','dsn']})['then'](handleEntityNotFound(_0x16261f,null))['then'](function(_0x35d71a){if(_0x35d71a){var _0x2c3182=require(_0x04bf('0x50'))();_0x2c3182[_0x04bf('0x51')](_0x35d71a[_0x04bf('0x52')]);_0x2c3182['closeSync']();return _0x35d71a;}})[_0x04bf('0x23')](respondWithResult(_0x16261f,null))[_0x04bf('0x41')](handleError(_0x16261f,null));};