Built motion from commit 1fa89557.|2.6.10
[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 _0x05f4=['VIRTUAL','options','includeAll','findAll','rows','catch','show','include','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','ODBC','UserProfileSection','find','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','test','dsn','odbc','openSync','request-promise','moment','bluebird','mustache','util','sox','to-csv','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','then','destroy','get','UserProfileResource','end','stack','name','send','index','map','SquareOdbc','rawAttributes','fieldName','key','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filters','filter','merge','type'];(function(_0x5d31ee,_0x183e43){var _0x21c2e6=function(_0x176343){while(--_0x176343){_0x5d31ee['push'](_0x5d31ee['shift']());}};_0x21c2e6(++_0x183e43);}(_0x05f4,0x174));var _0x405f=function(_0x3782f1,_0xdb9da7){_0x3782f1=_0x3782f1-0x0;var _0x59aa2c=_0x05f4[_0x3782f1];return _0x59aa2c;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x405f('0x0'));var moment=require(_0x405f('0x1'));var BPromise=require(_0x405f('0x2'));var Mustache=require(_0x405f('0x3'));var util=require(_0x405f('0x4'));var path=require('path');var sox=require(_0x405f('0x5'));var csv=require(_0x405f('0x6'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x405f('0x7'));var squel=require(_0x405f('0x8'));var crypto=require(_0x405f('0x9'));var jsforce=require('jsforce');var deskjs=require(_0x405f('0xa'));var toCsv=require('to-csv');var querystring=require(_0x405f('0xb'));var Papa=require(_0x405f('0xc'));var Redis=require(_0x405f('0xd'));var authService=require(_0x405f('0xe'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0x405f('0xf'))('api');var utils=require(_0x405f('0x10'));var config=require(_0x405f('0x11'));var licenseUtil=require(_0x405f('0x12'));var db=require(_0x405f('0x13'))['db'];function respondWithStatusCode(_0x176992,_0x482f06){_0x482f06=_0x482f06||0xcc;return function(_0xbfd734){if(_0xbfd734){return _0x176992[_0x405f('0x14')](_0x482f06);}return _0x176992[_0x405f('0x15')](_0x482f06)['end']();};}function respondWithResult(_0x40ba42,_0x1ed48a){_0x1ed48a=_0x1ed48a||0xc8;return function(_0x5d01ba){if(_0x5d01ba){return _0x40ba42['status'](_0x1ed48a)[_0x405f('0x16')](_0x5d01ba);}};}function respondWithFilteredResult(_0x53d8ad,_0xe57c45){return function(_0x105632){if(_0x105632){var _0x3ff8fd=typeof _0xe57c45[_0x405f('0x17')]===_0x405f('0x18')&&typeof _0xe57c45[_0x405f('0x19')]==='undefined';var _0x281987=_0x105632[_0x405f('0x1a')];var _0x48ab63=_0x3ff8fd?0x0:_0xe57c45[_0x405f('0x17')];var _0x11eb21=_0x3ff8fd?_0x105632[_0x405f('0x1a')]:_0xe57c45['offset']+_0xe57c45[_0x405f('0x19')];var _0x18195;if(_0x11eb21>=_0x281987){_0x11eb21=_0x281987;_0x18195=0xc8;}else{_0x18195=0xce;}_0x53d8ad['status'](_0x18195);return _0x53d8ad[_0x405f('0x1b')](_0x405f('0x1c'),_0x48ab63+'-'+_0x11eb21+'/'+_0x281987)[_0x405f('0x16')](_0x105632);}return null;};}function patchUpdates(_0x893791){return function(_0x82b1ef){try{jsonpatch[_0x405f('0x1d')](_0x82b1ef,_0x893791,!![]);}catch(_0x1e4c18){return BPromise[_0x405f('0x1e')](_0x1e4c18);}return _0x82b1ef[_0x405f('0x1f')]();};}function saveUpdates(_0x2adf7d,_0x5a9a5b){return function(_0x537f43){if(_0x537f43){return _0x537f43['update'](_0x2adf7d)[_0x405f('0x20')](function(_0x51b23a){return _0x51b23a;});}return null;};}function removeEntity(_0x12063f,_0x472e18){return function(_0x4ae03a){if(_0x4ae03a){return _0x4ae03a[_0x405f('0x21')]()[_0x405f('0x20')](function(){var _0xe551c5=_0x4ae03a[_0x405f('0x22')]({'plain':!![]});var _0x23bc64='ODBC';return db[_0x405f('0x23')]['destroy']({'where':{'type':_0x23bc64,'resourceId':_0xe551c5['id']}})[_0x405f('0x20')](function(){return _0x4ae03a;});})[_0x405f('0x20')](function(){_0x12063f['status'](0xcc)[_0x405f('0x24')]();});}};}function handleEntityNotFound(_0x569964,_0x322dcf){return function(_0x3296ee){if(!_0x3296ee){_0x569964[_0x405f('0x14')](0x194);}return _0x3296ee;};}function handleError(_0xe301fb,_0x503024){_0x503024=_0x503024||0x1f4;return function(_0x14074f){logger['error'](_0x14074f[_0x405f('0x25')]);if(_0x14074f[_0x405f('0x26')]){delete _0x14074f[_0x405f('0x26')];}_0xe301fb[_0x405f('0x15')](_0x503024)[_0x405f('0x27')](_0x14074f);};}exports[_0x405f('0x28')]=function(_0x4698c4,_0x4476c2){var _0x497b4e={},_0x1f248b={},_0x249f4d={'count':0x0,'rows':[]};var _0x2b98b2=_[_0x405f('0x29')](db[_0x405f('0x2a')][_0x405f('0x2b')],function(_0x28a0bf){return{'name':_0x28a0bf[_0x405f('0x2c')],'type':_0x28a0bf['type'][_0x405f('0x2d')]};});_0x1f248b[_0x405f('0x2e')]=_['map'](_0x2b98b2,_0x405f('0x26'));_0x1f248b[_0x405f('0x2f')]=_[_0x405f('0x30')](_0x4698c4[_0x405f('0x2f')]);_0x1f248b['filters']=_[_0x405f('0x31')](_0x1f248b[_0x405f('0x2e')],_0x1f248b[_0x405f('0x2f')]);_0x497b4e[_0x405f('0x32')]=_[_0x405f('0x31')](_0x1f248b['model'],qs[_0x405f('0x33')](_0x4698c4[_0x405f('0x2f')]['fields']));_0x497b4e[_0x405f('0x32')]=_0x497b4e[_0x405f('0x32')][_0x405f('0x34')]?_0x497b4e[_0x405f('0x32')]:_0x1f248b['model'];if(!_0x4698c4[_0x405f('0x2f')][_0x405f('0x35')](_0x405f('0x36'))){_0x497b4e[_0x405f('0x19')]=qs[_0x405f('0x19')](_0x4698c4[_0x405f('0x2f')][_0x405f('0x19')]);_0x497b4e['offset']=qs['offset'](_0x4698c4[_0x405f('0x2f')][_0x405f('0x17')]);}_0x497b4e[_0x405f('0x37')]=qs[_0x405f('0x38')](_0x4698c4[_0x405f('0x2f')][_0x405f('0x38')]);_0x497b4e[_0x405f('0x39')]=qs['filters'](_[_0x405f('0x3a')](_0x4698c4[_0x405f('0x2f')],_0x1f248b[_0x405f('0x3b')]),_0x2b98b2);if(_0x4698c4[_0x405f('0x2f')][_0x405f('0x3c')]){_0x497b4e['where']=_[_0x405f('0x3d')](_0x497b4e[_0x405f('0x39')],{'$or':_['map'](_0x2b98b2,function(_0x32a44a){if(_0x32a44a[_0x405f('0x3e')]!==_0x405f('0x3f')){var _0xc333fd={};_0xc333fd[_0x32a44a[_0x405f('0x26')]]={'$like':'%'+_0x4698c4[_0x405f('0x2f')][_0x405f('0x3c')]+'%'};return _0xc333fd;}})});}_0x497b4e=_[_0x405f('0x3d')]({},_0x497b4e,_0x4698c4[_0x405f('0x40')]);var _0x2bf170={'where':_0x497b4e['where']};return db[_0x405f('0x2a')][_0x405f('0x1a')](_0x2bf170)[_0x405f('0x20')](function(_0x356a08){_0x249f4d[_0x405f('0x1a')]=_0x356a08;if(_0x4698c4[_0x405f('0x2f')][_0x405f('0x41')]){_0x497b4e['include']=[{'all':!![]}];}return db[_0x405f('0x2a')][_0x405f('0x42')](_0x497b4e);})['then'](function(_0x301036){_0x249f4d[_0x405f('0x43')]=_0x301036;return _0x249f4d;})[_0x405f('0x20')](respondWithFilteredResult(_0x4476c2,_0x497b4e))[_0x405f('0x44')](handleError(_0x4476c2,null));};exports[_0x405f('0x45')]=function(_0x45a560,_0x5af088){var _0x475f5a={'raw':!![],'where':{'id':_0x45a560['params']['id']}},_0x2eb830={};_0x2eb830['model']=_['keys'](db[_0x405f('0x2a')][_0x405f('0x2b')]);_0x2eb830[_0x405f('0x2f')]=_[_0x405f('0x30')](_0x45a560['query']);_0x2eb830[_0x405f('0x3b')]=_[_0x405f('0x31')](_0x2eb830[_0x405f('0x2e')],_0x2eb830[_0x405f('0x2f')]);_0x475f5a[_0x405f('0x32')]=_['intersection'](_0x2eb830[_0x405f('0x2e')],qs[_0x405f('0x33')](_0x45a560[_0x405f('0x2f')][_0x405f('0x33')]));_0x475f5a['attributes']=_0x475f5a[_0x405f('0x32')][_0x405f('0x34')]?_0x475f5a[_0x405f('0x32')]:_0x2eb830[_0x405f('0x2e')];if(_0x45a560[_0x405f('0x2f')][_0x405f('0x41')]){_0x475f5a[_0x405f('0x46')]=[{'all':!![]}];}_0x475f5a=_['merge']({},_0x475f5a,_0x45a560[_0x405f('0x40')]);return db['SquareOdbc']['find'](_0x475f5a)['then'](handleEntityNotFound(_0x5af088,null))[_0x405f('0x20')](respondWithResult(_0x5af088,null))[_0x405f('0x44')](handleError(_0x5af088,null));};exports['create']=function(_0x3d9510,_0x5775c7){return db[_0x405f('0x2a')][_0x405f('0x47')](_0x3d9510[_0x405f('0x48')],{})['then'](function(_0x12436b){var _0x2fb036=_0x3d9510[_0x405f('0x49')][_0x405f('0x22')]({'plain':!![]});if(!_0x2fb036)throw new Error(_0x405f('0x4a'));if(_0x2fb036[_0x405f('0x4b')]==='user'){var _0x3429dd=_0x12436b[_0x405f('0x22')]({'plain':!![]});var _0x22b177=_0x405f('0x4c');return db[_0x405f('0x4d')][_0x405f('0x4e')]({'where':{'name':_0x22b177,'userProfileId':_0x2fb036['userProfileId']},'raw':!![]})[_0x405f('0x20')](function(_0x4ea4d8){if(_0x4ea4d8&&_0x4ea4d8[_0x405f('0x4f')]===0x0){return db['UserProfileResource'][_0x405f('0x47')]({'name':_0x3429dd[_0x405f('0x26')],'resourceId':_0x3429dd['id'],'type':_0x4ea4d8[_0x405f('0x26')],'sectionId':_0x4ea4d8['id']},{})[_0x405f('0x20')](function(){return _0x12436b;});}else{return _0x12436b;}})[_0x405f('0x44')](function(_0xfa8522){logger[_0x405f('0x50')](_0x405f('0x51'),_0xfa8522);throw _0xfa8522;});}return _0x12436b;})[_0x405f('0x20')](respondWithResult(_0x5775c7,0xc9))[_0x405f('0x44')](handleError(_0x5775c7,null));};exports['update']=function(_0x322c0b,_0x5758c6){if(_0x322c0b[_0x405f('0x48')]['id']){delete _0x322c0b[_0x405f('0x48')]['id'];}return db[_0x405f('0x2a')][_0x405f('0x4e')]({'where':{'id':_0x322c0b['params']['id']}})['then'](handleEntityNotFound(_0x5758c6,null))[_0x405f('0x20')](saveUpdates(_0x322c0b[_0x405f('0x48')],null))[_0x405f('0x20')](respondWithResult(_0x5758c6,null))[_0x405f('0x44')](handleError(_0x5758c6,null));};exports[_0x405f('0x21')]=function(_0x493cf0,_0x52634c){return db['SquareOdbc']['find']({'where':{'id':_0x493cf0[_0x405f('0x52')]['id']}})['then'](handleEntityNotFound(_0x52634c,null))[_0x405f('0x20')](removeEntity(_0x52634c,null))['catch'](handleError(_0x52634c,null));};exports[_0x405f('0x53')]=function(_0x1c4ba4,_0x545360,_0x4d9ab9){var _0x3e37dd;return db[_0x405f('0x2a')][_0x405f('0x4e')]({'where':{'id':_0x1c4ba4[_0x405f('0x52')]['id']},'attributes':['id',_0x405f('0x54')]})['then'](handleEntityNotFound(_0x545360,null))['then'](function(_0x5d9633){if(_0x5d9633){var _0x2774d8=require(_0x405f('0x55'))();_0x2774d8[_0x405f('0x56')](_0x5d9633['dsn']);_0x2774d8['closeSync']();return _0x5d9633;}})[_0x405f('0x20')](respondWithResult(_0x545360,null))[_0x405f('0x44')](handleError(_0x545360,null));};