Built motion from commit e6806ed6.|2.6.0
[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 _0x91f2=['request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','jsforce','desk.js','to-csv','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','api','../../config/environment','sendStatus','status','json','limit','undefined','count','offset','set','Content-Range','reject','save','update','then','get','destroy','end','error','name','send','index','map','SquareOdbc','fieldName','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','sort','where','pick','filter','merge','type','VIRTUAL','options','includeAll','include','rows','show','find','catch','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','ODBC','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','test','dsn','odbc','openSync','closeSync','eml-format','rimraf','zip-dir','fast-json-patch'];(function(_0x588dd2,_0x5c8152){var _0x4f208e=function(_0x12c652){while(--_0x12c652){_0x588dd2['push'](_0x588dd2['shift']());}};_0x4f208e(++_0x5c8152);}(_0x91f2,0xac));var _0x291f=function(_0x375883,_0x2877cc){_0x375883=_0x375883-0x0;var _0x11992c=_0x91f2[_0x375883];return _0x11992c;};'use strict';var emlformat=require(_0x291f('0x0'));var rimraf=require(_0x291f('0x1'));var zipdir=require(_0x291f('0x2'));var jsonpatch=require(_0x291f('0x3'));var rp=require(_0x291f('0x4'));var moment=require(_0x291f('0x5'));var BPromise=require(_0x291f('0x6'));var Mustache=require(_0x291f('0x7'));var util=require(_0x291f('0x8'));var path=require(_0x291f('0x9'));var sox=require(_0x291f('0xa'));var csv=require('to-csv');var ejs=require(_0x291f('0xb'));var fs=require('fs');var fs_extra=require(_0x291f('0xc'));var _=require(_0x291f('0xd'));var squel=require(_0x291f('0xe'));var crypto=require('crypto');var jsforce=require(_0x291f('0xf'));var deskjs=require(_0x291f('0x10'));var toCsv=require(_0x291f('0x11'));var querystring=require('querystring');var Papa=require(_0x291f('0x12'));var Redis=require(_0x291f('0x13'));var authService=require(_0x291f('0x14'));var qs=require('../../components/parsers/qs');var as=require(_0x291f('0x15'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x291f('0x16'))(_0x291f('0x17'));var utils=require('../../config/utils');var config=require(_0x291f('0x18'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x8ac3a4,_0x5cb422){_0x5cb422=_0x5cb422||0xcc;return function(_0x482260){if(_0x482260){return _0x8ac3a4[_0x291f('0x19')](_0x5cb422);}return _0x8ac3a4[_0x291f('0x1a')](_0x5cb422)['end']();};}function respondWithResult(_0x4faab1,_0x505b48){_0x505b48=_0x505b48||0xc8;return function(_0x50c6de){if(_0x50c6de){return _0x4faab1['status'](_0x505b48)[_0x291f('0x1b')](_0x50c6de);}};}function respondWithFilteredResult(_0x2d8346,_0x473792){return function(_0x2748ca){if(_0x2748ca){var _0x7a42c3=typeof _0x473792['offset']==='undefined'&&typeof _0x473792[_0x291f('0x1c')]===_0x291f('0x1d');var _0x442300=_0x2748ca[_0x291f('0x1e')];var _0x7ef1e6=_0x7a42c3?0x0:_0x473792[_0x291f('0x1f')];var _0x4f49e2=_0x7a42c3?_0x2748ca['count']:_0x473792[_0x291f('0x1f')]+_0x473792['limit'];var _0x406767;if(_0x4f49e2>=_0x442300){_0x4f49e2=_0x442300;_0x406767=0xc8;}else{_0x406767=0xce;}_0x2d8346['status'](_0x406767);return _0x2d8346[_0x291f('0x20')](_0x291f('0x21'),_0x7ef1e6+'-'+_0x4f49e2+'/'+_0x442300)[_0x291f('0x1b')](_0x2748ca);}return null;};}function patchUpdates(_0x34fceb){return function(_0x3278){try{jsonpatch['apply'](_0x3278,_0x34fceb,!![]);}catch(_0x23d514){return BPromise[_0x291f('0x22')](_0x23d514);}return _0x3278[_0x291f('0x23')]();};}function saveUpdates(_0x2f28c4,_0x37e42d){return function(_0x540056){if(_0x540056){return _0x540056[_0x291f('0x24')](_0x2f28c4)[_0x291f('0x25')](function(_0x3776cb){return _0x3776cb;});}return null;};}function removeEntity(_0x4c0672,_0x580266){return function(_0x1bac25){if(_0x1bac25){return _0x1bac25['destroy']()[_0x291f('0x25')](function(){var _0x233b0a=_0x1bac25[_0x291f('0x26')]({'plain':!![]});var _0x4d0902='ODBC';return db['UserProfileResource'][_0x291f('0x27')]({'where':{'type':_0x4d0902,'resourceId':_0x233b0a['id']}})[_0x291f('0x25')](function(){return _0x1bac25;});})[_0x291f('0x25')](function(){_0x4c0672[_0x291f('0x1a')](0xcc)[_0x291f('0x28')]();});}};}function handleEntityNotFound(_0x458a92,_0x442f53){return function(_0x22701e){if(!_0x22701e){_0x458a92[_0x291f('0x19')](0x194);}return _0x22701e;};}function handleError(_0x1618fd,_0x2745d1){_0x2745d1=_0x2745d1||0x1f4;return function(_0x539671){logger[_0x291f('0x29')](_0x539671['stack']);if(_0x539671[_0x291f('0x2a')]){delete _0x539671['name'];}_0x1618fd[_0x291f('0x1a')](_0x2745d1)[_0x291f('0x2b')](_0x539671);};}exports[_0x291f('0x2c')]=function(_0x2a5617,_0x2fb8ac){var _0x6e4e27={},_0x33163c={},_0x4b5cce={'count':0x0,'rows':[]};var _0x12641c=_[_0x291f('0x2d')](db[_0x291f('0x2e')]['rawAttributes'],function(_0x50a248){return{'name':_0x50a248[_0x291f('0x2f')],'type':_0x50a248['type']['key']};});_0x33163c[_0x291f('0x30')]=_['map'](_0x12641c,'name');_0x33163c[_0x291f('0x31')]=_[_0x291f('0x32')](_0x2a5617[_0x291f('0x31')]);_0x33163c[_0x291f('0x33')]=_['intersection'](_0x33163c[_0x291f('0x30')],_0x33163c[_0x291f('0x31')]);_0x6e4e27[_0x291f('0x34')]=_[_0x291f('0x35')](_0x33163c[_0x291f('0x30')],qs['fields'](_0x2a5617[_0x291f('0x31')][_0x291f('0x36')]));_0x6e4e27['attributes']=_0x6e4e27['attributes'][_0x291f('0x37')]?_0x6e4e27[_0x291f('0x34')]:_0x33163c[_0x291f('0x30')];if(!_0x2a5617[_0x291f('0x31')][_0x291f('0x38')]('nolimit')){_0x6e4e27[_0x291f('0x1c')]=qs['limit'](_0x2a5617['query'][_0x291f('0x1c')]);_0x6e4e27[_0x291f('0x1f')]=qs[_0x291f('0x1f')](_0x2a5617[_0x291f('0x31')][_0x291f('0x1f')]);}_0x6e4e27['order']=qs[_0x291f('0x39')](_0x2a5617[_0x291f('0x31')][_0x291f('0x39')]);_0x6e4e27[_0x291f('0x3a')]=qs[_0x291f('0x33')](_[_0x291f('0x3b')](_0x2a5617[_0x291f('0x31')],_0x33163c[_0x291f('0x33')]),_0x12641c);if(_0x2a5617[_0x291f('0x31')][_0x291f('0x3c')]){_0x6e4e27[_0x291f('0x3a')]=_[_0x291f('0x3d')](_0x6e4e27['where'],{'$or':_[_0x291f('0x2d')](_0x12641c,function(_0x2ba837){if(_0x2ba837[_0x291f('0x3e')]!==_0x291f('0x3f')){var _0x581076={};_0x581076[_0x2ba837[_0x291f('0x2a')]]={'$like':'%'+_0x2a5617['query'][_0x291f('0x3c')]+'%'};return _0x581076;}})});}_0x6e4e27=_['merge']({},_0x6e4e27,_0x2a5617[_0x291f('0x40')]);var _0x1e7e70={'where':_0x6e4e27[_0x291f('0x3a')]};return db['SquareOdbc']['count'](_0x1e7e70)[_0x291f('0x25')](function(_0x331153){_0x4b5cce[_0x291f('0x1e')]=_0x331153;if(_0x2a5617['query'][_0x291f('0x41')]){_0x6e4e27[_0x291f('0x42')]=[{'all':!![]}];}return db['SquareOdbc']['findAll'](_0x6e4e27);})['then'](function(_0x16746e){_0x4b5cce[_0x291f('0x43')]=_0x16746e;return _0x4b5cce;})[_0x291f('0x25')](respondWithFilteredResult(_0x2fb8ac,_0x6e4e27))['catch'](handleError(_0x2fb8ac,null));};exports[_0x291f('0x44')]=function(_0x83df92,_0x4562b4){var _0x13a079={'raw':!![],'where':{'id':_0x83df92['params']['id']}},_0x5bd6c5={};_0x5bd6c5['model']=_[_0x291f('0x32')](db[_0x291f('0x2e')]['rawAttributes']);_0x5bd6c5[_0x291f('0x31')]=_[_0x291f('0x32')](_0x83df92['query']);_0x5bd6c5[_0x291f('0x33')]=_['intersection'](_0x5bd6c5[_0x291f('0x30')],_0x5bd6c5[_0x291f('0x31')]);_0x13a079['attributes']=_[_0x291f('0x35')](_0x5bd6c5['model'],qs['fields'](_0x83df92['query']['fields']));_0x13a079[_0x291f('0x34')]=_0x13a079[_0x291f('0x34')][_0x291f('0x37')]?_0x13a079[_0x291f('0x34')]:_0x5bd6c5[_0x291f('0x30')];if(_0x83df92['query'][_0x291f('0x41')]){_0x13a079[_0x291f('0x42')]=[{'all':!![]}];}_0x13a079=_[_0x291f('0x3d')]({},_0x13a079,_0x83df92[_0x291f('0x40')]);return db[_0x291f('0x2e')][_0x291f('0x45')](_0x13a079)['then'](handleEntityNotFound(_0x4562b4,null))[_0x291f('0x25')](respondWithResult(_0x4562b4,null))[_0x291f('0x46')](handleError(_0x4562b4,null));};exports['create']=function(_0x2030de,_0x34460d){return db[_0x291f('0x2e')][_0x291f('0x47')](_0x2030de[_0x291f('0x48')],{})[_0x291f('0x25')](function(_0x140df1){var _0x1d90f9=_0x2030de[_0x291f('0x49')][_0x291f('0x26')]({'plain':!![]});if(!_0x1d90f9)throw new Error(_0x291f('0x4a'));if(_0x1d90f9[_0x291f('0x4b')]===_0x291f('0x49')){var _0x40d286=_0x140df1['get']({'plain':!![]});var _0xcc8bd8=_0x291f('0x4c');return db[_0x291f('0x4d')][_0x291f('0x45')]({'where':{'name':_0xcc8bd8,'userProfileId':_0x1d90f9[_0x291f('0x4e')]},'raw':!![]})['then'](function(_0x3ae32f){if(_0x3ae32f&&_0x3ae32f[_0x291f('0x4f')]===0x0){return db[_0x291f('0x50')][_0x291f('0x47')]({'name':_0x40d286[_0x291f('0x2a')],'resourceId':_0x40d286['id'],'type':_0x3ae32f['name'],'sectionId':_0x3ae32f['id']},{})['then'](function(){return _0x140df1;});}else{return _0x140df1;}})[_0x291f('0x46')](function(_0x36885b){logger[_0x291f('0x29')](_0x291f('0x51'),_0x36885b);throw _0x36885b;});}return _0x140df1;})['then'](respondWithResult(_0x34460d,0xc9))[_0x291f('0x46')](handleError(_0x34460d,null));};exports[_0x291f('0x24')]=function(_0x3a92b6,_0x224153){if(_0x3a92b6[_0x291f('0x48')]['id']){delete _0x3a92b6[_0x291f('0x48')]['id'];}return db[_0x291f('0x2e')][_0x291f('0x45')]({'where':{'id':_0x3a92b6[_0x291f('0x52')]['id']}})['then'](handleEntityNotFound(_0x224153,null))['then'](saveUpdates(_0x3a92b6['body'],null))[_0x291f('0x25')](respondWithResult(_0x224153,null))['catch'](handleError(_0x224153,null));};exports['destroy']=function(_0x5c0c99,_0x4f0ece){return db[_0x291f('0x2e')][_0x291f('0x45')]({'where':{'id':_0x5c0c99[_0x291f('0x52')]['id']}})[_0x291f('0x25')](handleEntityNotFound(_0x4f0ece,null))[_0x291f('0x25')](removeEntity(_0x4f0ece,null))[_0x291f('0x46')](handleError(_0x4f0ece,null));};exports[_0x291f('0x53')]=function(_0x4a1d33,_0x45ab04,_0x5b1147){var _0x141119;return db[_0x291f('0x2e')][_0x291f('0x45')]({'where':{'id':_0x4a1d33[_0x291f('0x52')]['id']},'attributes':['id',_0x291f('0x54')]})['then'](handleEntityNotFound(_0x45ab04,null))['then'](function(_0x5d558e){if(_0x5d558e){var _0x443447=require(_0x291f('0x55'))();_0x443447[_0x291f('0x56')](_0x5d558e['dsn']);_0x443447[_0x291f('0x57')]();return _0x5d558e;}})[_0x291f('0x25')](respondWithResult(_0x45ab04,null))['catch'](handleError(_0x45ab04,null));};