Built motion from commit 47436d59.|2.6.7
[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 _0x15f5=['eml-format','rimraf','request-promise','moment','util','path','to-csv','ejs','fs-extra','lodash','crypto','querystring','papaparse','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','then','ODBC','UserProfileResource','stack','name','send','index','map','fieldName','type','key','model','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','filter','where','merge','VIRTUAL','options','SquareOdbc','include','findAll','rows','catch','keys','rawAttributes','includeAll','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','get','UserProfileSection','userProfileId','autoAssociation','error','update','params','destroy','dsn','odbc'];(function(_0x5fe69a,_0x5484f5){var _0x3f1aee=function(_0x54387f){while(--_0x54387f){_0x5fe69a['push'](_0x5fe69a['shift']());}};_0x3f1aee(++_0x5484f5);}(_0x15f5,0xa4));var _0x515f=function(_0x6559c0,_0x3b930f){_0x6559c0=_0x6559c0-0x0;var _0x486026=_0x15f5[_0x6559c0];return _0x486026;};'use strict';var emlformat=require(_0x515f('0x0'));var rimraf=require(_0x515f('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x515f('0x2'));var moment=require(_0x515f('0x3'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x515f('0x4'));var path=require(_0x515f('0x5'));var sox=require('sox');var csv=require(_0x515f('0x6'));var ejs=require(_0x515f('0x7'));var fs=require('fs');var fs_extra=require(_0x515f('0x8'));var _=require(_0x515f('0x9'));var squel=require('squel');var crypto=require(_0x515f('0xa'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x515f('0x6'));var querystring=require(_0x515f('0xb'));var Papa=require(_0x515f('0xc'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x515f('0xd'));var hardwareService=require(_0x515f('0xe'));var logger=require(_0x515f('0xf'))(_0x515f('0x10'));var utils=require(_0x515f('0x11'));var config=require(_0x515f('0x12'));var licenseUtil=require('../../config/license/util');var db=require(_0x515f('0x13'))['db'];function respondWithStatusCode(_0x33cd19,_0x36bd89){_0x36bd89=_0x36bd89||0xcc;return function(_0x5a275f){if(_0x5a275f){return _0x33cd19[_0x515f('0x14')](_0x36bd89);}return _0x33cd19[_0x515f('0x15')](_0x36bd89)[_0x515f('0x16')]();};}function respondWithResult(_0x3a1316,_0x472cf6){_0x472cf6=_0x472cf6||0xc8;return function(_0x141182){if(_0x141182){return _0x3a1316[_0x515f('0x15')](_0x472cf6)[_0x515f('0x17')](_0x141182);}};}function respondWithFilteredResult(_0x34bfef,_0x5c8237){return function(_0xa156d0){if(_0xa156d0){var _0x511ab5=typeof _0x5c8237[_0x515f('0x18')]===_0x515f('0x19')&&typeof _0x5c8237[_0x515f('0x1a')]==='undefined';var _0x484a5c=_0xa156d0[_0x515f('0x1b')];var _0xdf3ab0=_0x511ab5?0x0:_0x5c8237[_0x515f('0x18')];var _0x3973fc=_0x511ab5?_0xa156d0[_0x515f('0x1b')]:_0x5c8237[_0x515f('0x18')]+_0x5c8237[_0x515f('0x1a')];var _0x354464;if(_0x3973fc>=_0x484a5c){_0x3973fc=_0x484a5c;_0x354464=0xc8;}else{_0x354464=0xce;}_0x34bfef[_0x515f('0x15')](_0x354464);return _0x34bfef[_0x515f('0x1c')](_0x515f('0x1d'),_0xdf3ab0+'-'+_0x3973fc+'/'+_0x484a5c)[_0x515f('0x17')](_0xa156d0);}return null;};}function patchUpdates(_0xed698b){return function(_0x36de2e){try{jsonpatch['apply'](_0x36de2e,_0xed698b,!![]);}catch(_0x46a341){return BPromise[_0x515f('0x1e')](_0x46a341);}return _0x36de2e['save']();};}function saveUpdates(_0x5a0e5b,_0x34dcf0){return function(_0x43b364){if(_0x43b364){return _0x43b364['update'](_0x5a0e5b)[_0x515f('0x1f')](function(_0x4bd0d9){return _0x4bd0d9;});}return null;};}function removeEntity(_0x3d15ff,_0x57b4a6){return function(_0x56bdc9){if(_0x56bdc9){return _0x56bdc9['destroy']()[_0x515f('0x1f')](function(){var _0x6b87b5=_0x56bdc9['get']({'plain':!![]});var _0x36af49=_0x515f('0x20');return db[_0x515f('0x21')]['destroy']({'where':{'type':_0x36af49,'resourceId':_0x6b87b5['id']}})[_0x515f('0x1f')](function(){return _0x56bdc9;});})['then'](function(){_0x3d15ff['status'](0xcc)[_0x515f('0x16')]();});}};}function handleEntityNotFound(_0x2857f7,_0x1d2fa1){return function(_0x2d5e80){if(!_0x2d5e80){_0x2857f7['sendStatus'](0x194);}return _0x2d5e80;};}function handleError(_0x185376,_0x3a2786){_0x3a2786=_0x3a2786||0x1f4;return function(_0x265a33){logger['error'](_0x265a33[_0x515f('0x22')]);if(_0x265a33[_0x515f('0x23')]){delete _0x265a33['name'];}_0x185376[_0x515f('0x15')](_0x3a2786)[_0x515f('0x24')](_0x265a33);};}exports[_0x515f('0x25')]=function(_0xbb6752,_0x35c89b){var _0xc70d12={},_0x56d468={},_0x27f724={'count':0x0,'rows':[]};var _0x129769=_[_0x515f('0x26')](db['SquareOdbc']['rawAttributes'],function(_0x3c0307){return{'name':_0x3c0307[_0x515f('0x27')],'type':_0x3c0307[_0x515f('0x28')][_0x515f('0x29')]};});_0x56d468[_0x515f('0x2a')]=_[_0x515f('0x26')](_0x129769,_0x515f('0x23'));_0x56d468[_0x515f('0x2b')]=_['keys'](_0xbb6752[_0x515f('0x2b')]);_0x56d468['filters']=_[_0x515f('0x2c')](_0x56d468[_0x515f('0x2a')],_0x56d468[_0x515f('0x2b')]);_0xc70d12[_0x515f('0x2d')]=_[_0x515f('0x2c')](_0x56d468[_0x515f('0x2a')],qs[_0x515f('0x2e')](_0xbb6752[_0x515f('0x2b')][_0x515f('0x2e')]));_0xc70d12[_0x515f('0x2d')]=_0xc70d12['attributes'][_0x515f('0x2f')]?_0xc70d12[_0x515f('0x2d')]:_0x56d468[_0x515f('0x2a')];if(!_0xbb6752[_0x515f('0x2b')][_0x515f('0x30')](_0x515f('0x31'))){_0xc70d12['limit']=qs[_0x515f('0x1a')](_0xbb6752[_0x515f('0x2b')][_0x515f('0x1a')]);_0xc70d12[_0x515f('0x18')]=qs[_0x515f('0x18')](_0xbb6752[_0x515f('0x2b')]['offset']);}_0xc70d12[_0x515f('0x32')]=qs[_0x515f('0x33')](_0xbb6752[_0x515f('0x2b')][_0x515f('0x33')]);_0xc70d12['where']=qs[_0x515f('0x34')](_['pick'](_0xbb6752[_0x515f('0x2b')],_0x56d468[_0x515f('0x34')]),_0x129769);if(_0xbb6752[_0x515f('0x2b')][_0x515f('0x35')]){_0xc70d12[_0x515f('0x36')]=_[_0x515f('0x37')](_0xc70d12[_0x515f('0x36')],{'$or':_['map'](_0x129769,function(_0x53a408){if(_0x53a408[_0x515f('0x28')]!==_0x515f('0x38')){var _0x4748e6={};_0x4748e6[_0x53a408[_0x515f('0x23')]]={'$like':'%'+_0xbb6752[_0x515f('0x2b')][_0x515f('0x35')]+'%'};return _0x4748e6;}})});}_0xc70d12=_[_0x515f('0x37')]({},_0xc70d12,_0xbb6752[_0x515f('0x39')]);var _0x133566={'where':_0xc70d12['where']};return db[_0x515f('0x3a')]['count'](_0x133566)[_0x515f('0x1f')](function(_0x6422e6){_0x27f724[_0x515f('0x1b')]=_0x6422e6;if(_0xbb6752[_0x515f('0x2b')]['includeAll']){_0xc70d12[_0x515f('0x3b')]=[{'all':!![]}];}return db[_0x515f('0x3a')][_0x515f('0x3c')](_0xc70d12);})['then'](function(_0xf7c852){_0x27f724[_0x515f('0x3d')]=_0xf7c852;return _0x27f724;})[_0x515f('0x1f')](respondWithFilteredResult(_0x35c89b,_0xc70d12))[_0x515f('0x3e')](handleError(_0x35c89b,null));};exports['show']=function(_0x3e1041,_0xde288c){var _0x3c2d57={'raw':!![],'where':{'id':_0x3e1041['params']['id']}},_0x4f66c2={};_0x4f66c2[_0x515f('0x2a')]=_[_0x515f('0x3f')](db['SquareOdbc'][_0x515f('0x40')]);_0x4f66c2[_0x515f('0x2b')]=_[_0x515f('0x3f')](_0x3e1041[_0x515f('0x2b')]);_0x4f66c2['filters']=_[_0x515f('0x2c')](_0x4f66c2[_0x515f('0x2a')],_0x4f66c2[_0x515f('0x2b')]);_0x3c2d57[_0x515f('0x2d')]=_[_0x515f('0x2c')](_0x4f66c2[_0x515f('0x2a')],qs[_0x515f('0x2e')](_0x3e1041[_0x515f('0x2b')][_0x515f('0x2e')]));_0x3c2d57['attributes']=_0x3c2d57[_0x515f('0x2d')][_0x515f('0x2f')]?_0x3c2d57[_0x515f('0x2d')]:_0x4f66c2[_0x515f('0x2a')];if(_0x3e1041[_0x515f('0x2b')][_0x515f('0x41')]){_0x3c2d57[_0x515f('0x3b')]=[{'all':!![]}];}_0x3c2d57=_[_0x515f('0x37')]({},_0x3c2d57,_0x3e1041[_0x515f('0x39')]);return db[_0x515f('0x3a')][_0x515f('0x42')](_0x3c2d57)[_0x515f('0x1f')](handleEntityNotFound(_0xde288c,null))['then'](respondWithResult(_0xde288c,null))['catch'](handleError(_0xde288c,null));};exports[_0x515f('0x43')]=function(_0x24eac5,_0x34d22e){return db[_0x515f('0x3a')][_0x515f('0x43')](_0x24eac5[_0x515f('0x44')],{})['then'](function(_0x361881){var _0x3767a4=_0x24eac5[_0x515f('0x45')]['get']({'plain':!![]});if(!_0x3767a4)throw new Error(_0x515f('0x46'));if(_0x3767a4[_0x515f('0x47')]===_0x515f('0x45')){var _0x3c8b76=_0x361881[_0x515f('0x48')]({'plain':!![]});var _0x51e944=_0x515f('0x20');return db[_0x515f('0x49')][_0x515f('0x42')]({'where':{'name':_0x51e944,'userProfileId':_0x3767a4[_0x515f('0x4a')]},'raw':!![]})['then'](function(_0x25ab1e){if(_0x25ab1e&&_0x25ab1e[_0x515f('0x4b')]===0x0){return db[_0x515f('0x21')][_0x515f('0x43')]({'name':_0x3c8b76[_0x515f('0x23')],'resourceId':_0x3c8b76['id'],'type':_0x25ab1e[_0x515f('0x23')],'sectionId':_0x25ab1e['id']},{})[_0x515f('0x1f')](function(){return _0x361881;});}else{return _0x361881;}})[_0x515f('0x3e')](function(_0x4f3999){logger[_0x515f('0x4c')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x4f3999);throw _0x4f3999;});}return _0x361881;})['then'](respondWithResult(_0x34d22e,0xc9))[_0x515f('0x3e')](handleError(_0x34d22e,null));};exports[_0x515f('0x4d')]=function(_0x21e798,_0x446828){if(_0x21e798[_0x515f('0x44')]['id']){delete _0x21e798[_0x515f('0x44')]['id'];}return db[_0x515f('0x3a')][_0x515f('0x42')]({'where':{'id':_0x21e798[_0x515f('0x4e')]['id']}})[_0x515f('0x1f')](handleEntityNotFound(_0x446828,null))['then'](saveUpdates(_0x21e798[_0x515f('0x44')],null))[_0x515f('0x1f')](respondWithResult(_0x446828,null))[_0x515f('0x3e')](handleError(_0x446828,null));};exports[_0x515f('0x4f')]=function(_0x44be69,_0x54dede){return db[_0x515f('0x3a')][_0x515f('0x42')]({'where':{'id':_0x44be69[_0x515f('0x4e')]['id']}})[_0x515f('0x1f')](handleEntityNotFound(_0x54dede,null))[_0x515f('0x1f')](removeEntity(_0x54dede,null))[_0x515f('0x3e')](handleError(_0x54dede,null));};exports['test']=function(_0x337813,_0x1b6c11,_0x1ba312){var _0x3efc07;return db[_0x515f('0x3a')]['find']({'where':{'id':_0x337813[_0x515f('0x4e')]['id']},'attributes':['id',_0x515f('0x50')]})[_0x515f('0x1f')](handleEntityNotFound(_0x1b6c11,null))[_0x515f('0x1f')](function(_0x5559cd){if(_0x5559cd){var _0x335845=require(_0x515f('0x51'))();_0x335845['openSync'](_0x5559cd[_0x515f('0x50')]);_0x335845['closeSync']();return _0x5559cd;}})['then'](respondWithResult(_0x1b6c11,null))['catch'](handleError(_0x1b6c11,null));};