9a08221cbe8cdaff09b5b0865382060220259267
[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 _0xbcc9=['error','stack','name','send','index','map','SquareOdbc','rawAttributes','fieldName','type','key','query','keys','intersection','model','attributes','fields','length','hasOwnProperty','order','sort','where','filters','pick','merge','VIRTUAL','filter','options','includeAll','include','rows','catch','find','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','get','UserProfileSection','userProfileId','autoAssociation','params','test','dsn','odbc','closeSync','html-pdf','eml-format','zip-dir','fast-json-patch','moment','bluebird','util','sox','to-csv','ejs','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','api','sendStatus','status','end','json','count','offset','limit','set','Content-Range','apply','reject','save','update','then','destroy','ODBC'];(function(_0xc0e2c4,_0xa4c81e){var _0x3e378c=function(_0x18bc29){while(--_0x18bc29){_0xc0e2c4['push'](_0xc0e2c4['shift']());}};_0x3e378c(++_0xa4c81e);}(_0xbcc9,0x172));var _0x9bcc=function(_0x19d728,_0x58a500){_0x19d728=_0x19d728-0x0;var _0xc23d6f=_0xbcc9[_0x19d728];return _0xc23d6f;};'use strict';var pdf=require(_0x9bcc('0x0'));var emlformat=require(_0x9bcc('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x9bcc('0x2'));var jsonpatch=require(_0x9bcc('0x3'));var rp=require('request-promise');var moment=require(_0x9bcc('0x4'));var BPromise=require(_0x9bcc('0x5'));var Mustache=require('mustache');var util=require(_0x9bcc('0x6'));var path=require('path');var sox=require(_0x9bcc('0x7'));var csv=require(_0x9bcc('0x8'));var ejs=require(_0x9bcc('0x9'));var fs=require('fs');var _=require('lodash');var squel=require(_0x9bcc('0xa'));var crypto=require(_0x9bcc('0xb'));var jsforce=require(_0x9bcc('0xc'));var deskjs=require(_0x9bcc('0xd'));var toCsv=require(_0x9bcc('0x8'));var querystring=require(_0x9bcc('0xe'));var Papa=require('papaparse');var Redis=require(_0x9bcc('0xf'));var authService=require(_0x9bcc('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0x9bcc('0x11'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x9bcc('0x12'));var utils=require('../../config/utils');var config=require('../../config/environment');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x11f141,_0x4e0d3a){_0x4e0d3a=_0x4e0d3a||0xcc;return function(_0x889cf5){if(_0x889cf5){return _0x11f141[_0x9bcc('0x13')](_0x4e0d3a);}return _0x11f141[_0x9bcc('0x14')](_0x4e0d3a)[_0x9bcc('0x15')]();};}function respondWithResult(_0x4688e5,_0x1438f9){_0x1438f9=_0x1438f9||0xc8;return function(_0x2d152a){if(_0x2d152a){return _0x4688e5['status'](_0x1438f9)[_0x9bcc('0x16')](_0x2d152a);}};}function respondWithFilteredResult(_0x11ae7c,_0x592b69){return function(_0x5b4474){if(_0x5b4474){var _0x4cef3c=_0x5b4474[_0x9bcc('0x17')],_0xba7573=_0x592b69[_0x9bcc('0x18')],_0x2f550e=_0x592b69['offset']+_0x592b69[_0x9bcc('0x19')],_0x7852eb;if(_0x2f550e>=_0x4cef3c){_0x2f550e=_0x4cef3c;_0x7852eb=0xc8;}else{_0x7852eb=0xce;}_0x11ae7c['status'](_0x7852eb);return _0x11ae7c[_0x9bcc('0x1a')](_0x9bcc('0x1b'),_0xba7573+'-'+_0x2f550e+'/'+_0x4cef3c)[_0x9bcc('0x16')](_0x5b4474);}return null;};}function patchUpdates(_0x3c300a){return function(_0x5f3482){try{jsonpatch[_0x9bcc('0x1c')](_0x5f3482,_0x3c300a,!![]);}catch(_0x43b3cf){return BPromise[_0x9bcc('0x1d')](_0x43b3cf);}return _0x5f3482[_0x9bcc('0x1e')]();};}function saveUpdates(_0x56f2bb,_0x3d3424){return function(_0x7bcece){if(_0x7bcece){return _0x7bcece[_0x9bcc('0x1f')](_0x56f2bb)[_0x9bcc('0x20')](function(_0x4eb0b7){return _0x4eb0b7;});}return null;};}function removeEntity(_0x280dd3,_0x478fd7){return function(_0x2ebb8d){if(_0x2ebb8d){return _0x2ebb8d[_0x9bcc('0x21')]()[_0x9bcc('0x20')](function(){var _0x1cb582=_0x2ebb8d['get']({'plain':!![]});var _0xa2cf48=_0x9bcc('0x22');return db['UserProfileResource']['destroy']({'where':{'type':_0xa2cf48,'resourceId':_0x1cb582['id']}})[_0x9bcc('0x20')](function(){return _0x2ebb8d;});})[_0x9bcc('0x20')](function(){_0x280dd3[_0x9bcc('0x14')](0xcc)[_0x9bcc('0x15')]();});}};}function handleEntityNotFound(_0x522da6,_0xdfd6bc){return function(_0x84f611){if(!_0x84f611){_0x522da6[_0x9bcc('0x13')](0x194);}return _0x84f611;};}function handleError(_0x33d48f,_0x2617a7){_0x2617a7=_0x2617a7||0x1f4;return function(_0x2dc3b9){logger[_0x9bcc('0x23')](_0x2dc3b9[_0x9bcc('0x24')]);if(_0x2dc3b9[_0x9bcc('0x25')]){delete _0x2dc3b9[_0x9bcc('0x25')];}_0x33d48f[_0x9bcc('0x14')](_0x2617a7)[_0x9bcc('0x26')](_0x2dc3b9);};}exports[_0x9bcc('0x27')]=function(_0x1bf288,_0x1e104b){var _0x3db4f7={},_0xf0ffe0={},_0x4110a9={'count':0x0,'rows':[]};var _0x42f6af=_[_0x9bcc('0x28')](db[_0x9bcc('0x29')][_0x9bcc('0x2a')],function(_0x988b66){return{'name':_0x988b66[_0x9bcc('0x2b')],'type':_0x988b66[_0x9bcc('0x2c')][_0x9bcc('0x2d')]};});_0xf0ffe0['model']=_['map'](_0x42f6af,'name');_0xf0ffe0[_0x9bcc('0x2e')]=_[_0x9bcc('0x2f')](_0x1bf288['query']);_0xf0ffe0['filters']=_[_0x9bcc('0x30')](_0xf0ffe0[_0x9bcc('0x31')],_0xf0ffe0[_0x9bcc('0x2e')]);_0x3db4f7[_0x9bcc('0x32')]=_['intersection'](_0xf0ffe0['model'],qs[_0x9bcc('0x33')](_0x1bf288['query']['fields']));_0x3db4f7[_0x9bcc('0x32')]=_0x3db4f7['attributes'][_0x9bcc('0x34')]?_0x3db4f7[_0x9bcc('0x32')]:_0xf0ffe0[_0x9bcc('0x31')];if(!_0x1bf288[_0x9bcc('0x2e')][_0x9bcc('0x35')]('nolimit')){_0x3db4f7['limit']=qs[_0x9bcc('0x19')](_0x1bf288[_0x9bcc('0x2e')][_0x9bcc('0x19')]);_0x3db4f7[_0x9bcc('0x18')]=qs[_0x9bcc('0x18')](_0x1bf288['query']['offset']);}_0x3db4f7[_0x9bcc('0x36')]=qs[_0x9bcc('0x37')](_0x1bf288[_0x9bcc('0x2e')]['sort']);_0x3db4f7[_0x9bcc('0x38')]=qs[_0x9bcc('0x39')](_[_0x9bcc('0x3a')](_0x1bf288[_0x9bcc('0x2e')],_0xf0ffe0[_0x9bcc('0x39')]),_0x42f6af);if(_0x1bf288[_0x9bcc('0x2e')]['filter']){_0x3db4f7[_0x9bcc('0x38')]=_[_0x9bcc('0x3b')](_0x3db4f7[_0x9bcc('0x38')],{'$or':_[_0x9bcc('0x28')](_0x42f6af,function(_0x4a1f1a){if(_0x4a1f1a['type']!==_0x9bcc('0x3c')){var _0x521202={};_0x521202[_0x4a1f1a[_0x9bcc('0x25')]]={'$like':'%'+_0x1bf288[_0x9bcc('0x2e')][_0x9bcc('0x3d')]+'%'};return _0x521202;}})});}_0x3db4f7=_['merge']({},_0x3db4f7,_0x1bf288[_0x9bcc('0x3e')]);var _0x2a9345={'where':_0x3db4f7['where']};return db[_0x9bcc('0x29')][_0x9bcc('0x17')](_0x2a9345)[_0x9bcc('0x20')](function(_0x2e1dbc){_0x4110a9[_0x9bcc('0x17')]=_0x2e1dbc;if(_0x1bf288[_0x9bcc('0x2e')][_0x9bcc('0x3f')]){_0x3db4f7[_0x9bcc('0x40')]=[{'all':!![]}];}return db['SquareOdbc']['findAll'](_0x3db4f7);})[_0x9bcc('0x20')](function(_0x29968a){_0x4110a9[_0x9bcc('0x41')]=_0x29968a;return _0x4110a9;})[_0x9bcc('0x20')](respondWithFilteredResult(_0x1e104b,_0x3db4f7))[_0x9bcc('0x42')](handleError(_0x1e104b,null));};exports['show']=function(_0x55875d,_0x1070ed){var _0x2f6e27={'raw':!![],'where':{'id':_0x55875d['params']['id']}},_0x13644e={};_0x13644e[_0x9bcc('0x31')]=_[_0x9bcc('0x2f')](db[_0x9bcc('0x29')]['rawAttributes']);_0x13644e[_0x9bcc('0x2e')]=_[_0x9bcc('0x2f')](_0x55875d[_0x9bcc('0x2e')]);_0x13644e[_0x9bcc('0x39')]=_[_0x9bcc('0x30')](_0x13644e['model'],_0x13644e[_0x9bcc('0x2e')]);_0x2f6e27[_0x9bcc('0x32')]=_[_0x9bcc('0x30')](_0x13644e[_0x9bcc('0x31')],qs[_0x9bcc('0x33')](_0x55875d['query'][_0x9bcc('0x33')]));_0x2f6e27[_0x9bcc('0x32')]=_0x2f6e27[_0x9bcc('0x32')][_0x9bcc('0x34')]?_0x2f6e27['attributes']:_0x13644e[_0x9bcc('0x31')];if(_0x55875d[_0x9bcc('0x2e')][_0x9bcc('0x3f')]){_0x2f6e27[_0x9bcc('0x40')]=[{'all':!![]}];}_0x2f6e27=_[_0x9bcc('0x3b')]({},_0x2f6e27,_0x55875d['options']);return db[_0x9bcc('0x29')][_0x9bcc('0x43')](_0x2f6e27)[_0x9bcc('0x20')](handleEntityNotFound(_0x1070ed,null))[_0x9bcc('0x20')](respondWithResult(_0x1070ed,null))[_0x9bcc('0x42')](handleError(_0x1070ed,null));};exports[_0x9bcc('0x44')]=function(_0x2d4aec,_0x239d53){return db[_0x9bcc('0x29')]['create'](_0x2d4aec[_0x9bcc('0x45')],{})[_0x9bcc('0x20')](function(_0x5b3047){var _0x4d0b4e=_0x2d4aec['user']['get']({'plain':!![]});if(!_0x4d0b4e)throw new Error(_0x9bcc('0x46'));if(_0x4d0b4e['role']===_0x9bcc('0x47')){var _0x25e25c=_0x5b3047[_0x9bcc('0x48')]({'plain':!![]});var _0x14b352=_0x9bcc('0x22');return db[_0x9bcc('0x49')][_0x9bcc('0x43')]({'where':{'name':_0x14b352,'userProfileId':_0x4d0b4e[_0x9bcc('0x4a')]},'raw':!![]})[_0x9bcc('0x20')](function(_0xec8f81){if(_0xec8f81&&_0xec8f81[_0x9bcc('0x4b')]===0x0){return db['UserProfileResource'][_0x9bcc('0x44')]({'name':_0x25e25c['name'],'resourceId':_0x25e25c['id'],'type':_0xec8f81[_0x9bcc('0x25')],'sectionId':_0xec8f81['id']},{})[_0x9bcc('0x20')](function(){return _0x5b3047;});}else{return _0x5b3047;}})[_0x9bcc('0x42')](function(_0x22dc6a){logger[_0x9bcc('0x23')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x22dc6a);throw _0x22dc6a;});}return _0x5b3047;})[_0x9bcc('0x20')](respondWithResult(_0x239d53,0xc9))['catch'](handleError(_0x239d53,null));};exports[_0x9bcc('0x1f')]=function(_0x1523d7,_0x508870){if(_0x1523d7[_0x9bcc('0x45')]['id']){delete _0x1523d7[_0x9bcc('0x45')]['id'];}return db[_0x9bcc('0x29')][_0x9bcc('0x43')]({'where':{'id':_0x1523d7[_0x9bcc('0x4c')]['id']}})[_0x9bcc('0x20')](handleEntityNotFound(_0x508870,null))['then'](saveUpdates(_0x1523d7[_0x9bcc('0x45')],null))[_0x9bcc('0x20')](respondWithResult(_0x508870,null))[_0x9bcc('0x42')](handleError(_0x508870,null));};exports[_0x9bcc('0x21')]=function(_0x1dbc3f,_0x52f4c6){return db['SquareOdbc']['find']({'where':{'id':_0x1dbc3f[_0x9bcc('0x4c')]['id']}})['then'](handleEntityNotFound(_0x52f4c6,null))[_0x9bcc('0x20')](removeEntity(_0x52f4c6,null))['catch'](handleError(_0x52f4c6,null));};exports[_0x9bcc('0x4d')]=function(_0x24b31c,_0x1c5dde,_0x1681ef){var _0x4cd882;return db[_0x9bcc('0x29')][_0x9bcc('0x43')]({'where':{'id':_0x24b31c[_0x9bcc('0x4c')]['id']},'attributes':['id',_0x9bcc('0x4e')]})[_0x9bcc('0x20')](handleEntityNotFound(_0x1c5dde,null))['then'](function(_0x1fe340){if(_0x1fe340){var _0x2bfbaf=require(_0x9bcc('0x4f'))();_0x2bfbaf['openSync'](_0x1fe340[_0x9bcc('0x4e')]);_0x2bfbaf[_0x9bcc('0x50')]();return _0x1fe340;}})[_0x9bcc('0x20')](respondWithResult(_0x1c5dde,null))['catch'](handleError(_0x1c5dde,null));};