Built motion from commit fe909640.|2.6.8
[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 _0xfac2=['create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','userProfileId','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','test','odbc','openSync','dsn','closeSync','fast-json-patch','request-promise','moment','mustache','util','sox','to-csv','ejs','lodash','squel','jsforce','querystring','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','status','end','json','offset','limit','count','Content-Range','apply','reject','save','update','then','destroy','get','UserProfileResource','sendStatus','stack','name','index','map','SquareOdbc','type','key','model','query','keys','intersection','attributes','fields','length','nolimit','order','sort','where','filters','pick','filter','VIRTUAL','merge','options','include','findAll','rows','show','params','rawAttributes','includeAll','find','catch'];(function(_0x3ff085,_0x27c92e){var _0x2b68c3=function(_0x37811e){while(--_0x37811e){_0x3ff085['push'](_0x3ff085['shift']());}};_0x2b68c3(++_0x27c92e);}(_0xfac2,0x151));var _0x2fac=function(_0x26d3e5,_0xb01e63){_0x26d3e5=_0x26d3e5-0x0;var _0x4a871b=_0xfac2[_0x26d3e5];return _0x4a871b;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x2fac('0x0'));var rp=require(_0x2fac('0x1'));var moment=require(_0x2fac('0x2'));var BPromise=require('bluebird');var Mustache=require(_0x2fac('0x3'));var util=require(_0x2fac('0x4'));var path=require('path');var sox=require(_0x2fac('0x5'));var csv=require(_0x2fac('0x6'));var ejs=require(_0x2fac('0x7'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x2fac('0x8'));var squel=require(_0x2fac('0x9'));var crypto=require('crypto');var jsforce=require(_0x2fac('0xa'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x2fac('0xb'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x2fac('0xc'));var qs=require(_0x2fac('0xd'));var as=require(_0x2fac('0xe'));var hardwareService=require(_0x2fac('0xf'));var logger=require(_0x2fac('0x10'))(_0x2fac('0x11'));var utils=require(_0x2fac('0x12'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2ee70b,_0x4d33ea){_0x4d33ea=_0x4d33ea||0xcc;return function(_0x5b5a7f){if(_0x5b5a7f){return _0x2ee70b['sendStatus'](_0x4d33ea);}return _0x2ee70b[_0x2fac('0x13')](_0x4d33ea)[_0x2fac('0x14')]();};}function respondWithResult(_0x277f46,_0x59999b){_0x59999b=_0x59999b||0xc8;return function(_0x42917f){if(_0x42917f){return _0x277f46[_0x2fac('0x13')](_0x59999b)[_0x2fac('0x15')](_0x42917f);}};}function respondWithFilteredResult(_0x33f2d3,_0xf272b2){return function(_0x4bb939){if(_0x4bb939){var _0x5e4983=typeof _0xf272b2[_0x2fac('0x16')]==='undefined'&&typeof _0xf272b2[_0x2fac('0x17')]==='undefined';var _0x5f20b4=_0x4bb939[_0x2fac('0x18')];var _0x42d0e0=_0x5e4983?0x0:_0xf272b2[_0x2fac('0x16')];var _0xbdc3f2=_0x5e4983?_0x4bb939['count']:_0xf272b2[_0x2fac('0x16')]+_0xf272b2['limit'];var _0x46fd3c;if(_0xbdc3f2>=_0x5f20b4){_0xbdc3f2=_0x5f20b4;_0x46fd3c=0xc8;}else{_0x46fd3c=0xce;}_0x33f2d3['status'](_0x46fd3c);return _0x33f2d3['set'](_0x2fac('0x19'),_0x42d0e0+'-'+_0xbdc3f2+'/'+_0x5f20b4)[_0x2fac('0x15')](_0x4bb939);}return null;};}function patchUpdates(_0x3ddda9){return function(_0x30ce76){try{jsonpatch[_0x2fac('0x1a')](_0x30ce76,_0x3ddda9,!![]);}catch(_0x52f8fe){return BPromise[_0x2fac('0x1b')](_0x52f8fe);}return _0x30ce76[_0x2fac('0x1c')]();};}function saveUpdates(_0x27e15b,_0x3e3b92){return function(_0x547c9d){if(_0x547c9d){return _0x547c9d[_0x2fac('0x1d')](_0x27e15b)[_0x2fac('0x1e')](function(_0xb5c5cf){return _0xb5c5cf;});}return null;};}function removeEntity(_0x2656a1,_0x30fc03){return function(_0xf2c7a3){if(_0xf2c7a3){return _0xf2c7a3[_0x2fac('0x1f')]()[_0x2fac('0x1e')](function(){var _0x15e2fe=_0xf2c7a3[_0x2fac('0x20')]({'plain':!![]});var _0x2a7942='ODBC';return db[_0x2fac('0x21')][_0x2fac('0x1f')]({'where':{'type':_0x2a7942,'resourceId':_0x15e2fe['id']}})[_0x2fac('0x1e')](function(){return _0xf2c7a3;});})['then'](function(){_0x2656a1[_0x2fac('0x13')](0xcc)[_0x2fac('0x14')]();});}};}function handleEntityNotFound(_0x5322a4,_0x327ef8){return function(_0x4d8eba){if(!_0x4d8eba){_0x5322a4[_0x2fac('0x22')](0x194);}return _0x4d8eba;};}function handleError(_0x2640bd,_0x53e13d){_0x53e13d=_0x53e13d||0x1f4;return function(_0xabf2de){logger['error'](_0xabf2de[_0x2fac('0x23')]);if(_0xabf2de[_0x2fac('0x24')]){delete _0xabf2de[_0x2fac('0x24')];}_0x2640bd[_0x2fac('0x13')](_0x53e13d)['send'](_0xabf2de);};}exports[_0x2fac('0x25')]=function(_0x1ba4c3,_0x4a9bcb){var _0x1c0761={},_0x127972={},_0x135420={'count':0x0,'rows':[]};var _0x3d6aa4=_[_0x2fac('0x26')](db[_0x2fac('0x27')]['rawAttributes'],function(_0xd08b9f){return{'name':_0xd08b9f['fieldName'],'type':_0xd08b9f[_0x2fac('0x28')][_0x2fac('0x29')]};});_0x127972[_0x2fac('0x2a')]=_[_0x2fac('0x26')](_0x3d6aa4,'name');_0x127972[_0x2fac('0x2b')]=_[_0x2fac('0x2c')](_0x1ba4c3[_0x2fac('0x2b')]);_0x127972['filters']=_[_0x2fac('0x2d')](_0x127972[_0x2fac('0x2a')],_0x127972['query']);_0x1c0761[_0x2fac('0x2e')]=_[_0x2fac('0x2d')](_0x127972[_0x2fac('0x2a')],qs['fields'](_0x1ba4c3['query'][_0x2fac('0x2f')]));_0x1c0761['attributes']=_0x1c0761[_0x2fac('0x2e')][_0x2fac('0x30')]?_0x1c0761[_0x2fac('0x2e')]:_0x127972['model'];if(!_0x1ba4c3[_0x2fac('0x2b')]['hasOwnProperty'](_0x2fac('0x31'))){_0x1c0761[_0x2fac('0x17')]=qs[_0x2fac('0x17')](_0x1ba4c3[_0x2fac('0x2b')][_0x2fac('0x17')]);_0x1c0761[_0x2fac('0x16')]=qs['offset'](_0x1ba4c3[_0x2fac('0x2b')][_0x2fac('0x16')]);}_0x1c0761[_0x2fac('0x32')]=qs[_0x2fac('0x33')](_0x1ba4c3[_0x2fac('0x2b')]['sort']);_0x1c0761[_0x2fac('0x34')]=qs[_0x2fac('0x35')](_[_0x2fac('0x36')](_0x1ba4c3[_0x2fac('0x2b')],_0x127972['filters']),_0x3d6aa4);if(_0x1ba4c3[_0x2fac('0x2b')][_0x2fac('0x37')]){_0x1c0761['where']=_['merge'](_0x1c0761[_0x2fac('0x34')],{'$or':_[_0x2fac('0x26')](_0x3d6aa4,function(_0x27489d){if(_0x27489d[_0x2fac('0x28')]!==_0x2fac('0x38')){var _0x7398c8={};_0x7398c8[_0x27489d[_0x2fac('0x24')]]={'$like':'%'+_0x1ba4c3[_0x2fac('0x2b')][_0x2fac('0x37')]+'%'};return _0x7398c8;}})});}_0x1c0761=_[_0x2fac('0x39')]({},_0x1c0761,_0x1ba4c3[_0x2fac('0x3a')]);var _0x565db6={'where':_0x1c0761[_0x2fac('0x34')]};return db['SquareOdbc']['count'](_0x565db6)[_0x2fac('0x1e')](function(_0x11625d){_0x135420[_0x2fac('0x18')]=_0x11625d;if(_0x1ba4c3[_0x2fac('0x2b')]['includeAll']){_0x1c0761[_0x2fac('0x3b')]=[{'all':!![]}];}return db[_0x2fac('0x27')][_0x2fac('0x3c')](_0x1c0761);})[_0x2fac('0x1e')](function(_0x138310){_0x135420[_0x2fac('0x3d')]=_0x138310;return _0x135420;})[_0x2fac('0x1e')](respondWithFilteredResult(_0x4a9bcb,_0x1c0761))['catch'](handleError(_0x4a9bcb,null));};exports[_0x2fac('0x3e')]=function(_0x5d9e17,_0x121bc2){var _0x3c870d={'raw':!![],'where':{'id':_0x5d9e17[_0x2fac('0x3f')]['id']}},_0x2a5c5a={};_0x2a5c5a['model']=_[_0x2fac('0x2c')](db[_0x2fac('0x27')][_0x2fac('0x40')]);_0x2a5c5a[_0x2fac('0x2b')]=_['keys'](_0x5d9e17['query']);_0x2a5c5a[_0x2fac('0x35')]=_[_0x2fac('0x2d')](_0x2a5c5a[_0x2fac('0x2a')],_0x2a5c5a[_0x2fac('0x2b')]);_0x3c870d[_0x2fac('0x2e')]=_[_0x2fac('0x2d')](_0x2a5c5a[_0x2fac('0x2a')],qs['fields'](_0x5d9e17[_0x2fac('0x2b')]['fields']));_0x3c870d[_0x2fac('0x2e')]=_0x3c870d[_0x2fac('0x2e')]['length']?_0x3c870d['attributes']:_0x2a5c5a['model'];if(_0x5d9e17[_0x2fac('0x2b')][_0x2fac('0x41')]){_0x3c870d[_0x2fac('0x3b')]=[{'all':!![]}];}_0x3c870d=_[_0x2fac('0x39')]({},_0x3c870d,_0x5d9e17[_0x2fac('0x3a')]);return db[_0x2fac('0x27')][_0x2fac('0x42')](_0x3c870d)['then'](handleEntityNotFound(_0x121bc2,null))[_0x2fac('0x1e')](respondWithResult(_0x121bc2,null))[_0x2fac('0x43')](handleError(_0x121bc2,null));};exports[_0x2fac('0x44')]=function(_0x198718,_0x4862ea){return db[_0x2fac('0x27')]['create'](_0x198718[_0x2fac('0x45')],{})[_0x2fac('0x1e')](function(_0x5b6ca9){var _0x20a743=_0x198718[_0x2fac('0x46')][_0x2fac('0x20')]({'plain':!![]});if(!_0x20a743)throw new Error(_0x2fac('0x47'));if(_0x20a743['role']===_0x2fac('0x46')){var _0x2b1255=_0x5b6ca9['get']({'plain':!![]});var _0x3deb52='ODBC';return db[_0x2fac('0x48')]['find']({'where':{'name':_0x3deb52,'userProfileId':_0x20a743[_0x2fac('0x49')]},'raw':!![]})[_0x2fac('0x1e')](function(_0x5d5319){if(_0x5d5319&&_0x5d5319['autoAssociation']===0x0){return db[_0x2fac('0x21')][_0x2fac('0x44')]({'name':_0x2b1255[_0x2fac('0x24')],'resourceId':_0x2b1255['id'],'type':_0x5d5319[_0x2fac('0x24')],'sectionId':_0x5d5319['id']},{})[_0x2fac('0x1e')](function(){return _0x5b6ca9;});}else{return _0x5b6ca9;}})['catch'](function(_0x3fc66d){logger[_0x2fac('0x4a')](_0x2fac('0x4b'),_0x3fc66d);throw _0x3fc66d;});}return _0x5b6ca9;})[_0x2fac('0x1e')](respondWithResult(_0x4862ea,0xc9))[_0x2fac('0x43')](handleError(_0x4862ea,null));};exports[_0x2fac('0x1d')]=function(_0x1ff741,_0x5c8bde){if(_0x1ff741[_0x2fac('0x45')]['id']){delete _0x1ff741[_0x2fac('0x45')]['id'];}return db['SquareOdbc'][_0x2fac('0x42')]({'where':{'id':_0x1ff741[_0x2fac('0x3f')]['id']}})[_0x2fac('0x1e')](handleEntityNotFound(_0x5c8bde,null))[_0x2fac('0x1e')](saveUpdates(_0x1ff741[_0x2fac('0x45')],null))['then'](respondWithResult(_0x5c8bde,null))[_0x2fac('0x43')](handleError(_0x5c8bde,null));};exports['destroy']=function(_0xdf7194,_0x4ad326){return db['SquareOdbc'][_0x2fac('0x42')]({'where':{'id':_0xdf7194[_0x2fac('0x3f')]['id']}})[_0x2fac('0x1e')](handleEntityNotFound(_0x4ad326,null))[_0x2fac('0x1e')](removeEntity(_0x4ad326,null))['catch'](handleError(_0x4ad326,null));};exports[_0x2fac('0x4c')]=function(_0x3f3ba5,_0x45aaa4,_0x34eefb){var _0x41b440;return db[_0x2fac('0x27')][_0x2fac('0x42')]({'where':{'id':_0x3f3ba5['params']['id']},'attributes':['id','dsn']})[_0x2fac('0x1e')](handleEntityNotFound(_0x45aaa4,null))[_0x2fac('0x1e')](function(_0xae3608){if(_0xae3608){var _0x42719f=require(_0x2fac('0x4d'))();_0x42719f[_0x2fac('0x4e')](_0xae3608[_0x2fac('0x4f')]);_0x42719f[_0x2fac('0x50')]();return _0xae3608;}})[_0x2fac('0x1e')](respondWithResult(_0x45aaa4,null))[_0x2fac('0x43')](handleError(_0x45aaa4,null));};