Built motion from commit 0900f080.|2.5.45
[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 _0x040e=['ejs','fs-extra','squel','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','reject','update','then','destroy','get','ODBC','UserProfileResource','stack','name','index','map','SquareOdbc','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','filter','options','include','catch','show','params','includeAll','create','body','user','role','userProfileId','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','find','test','openSync','dsn','eml-format','zip-dir','fast-json-patch','moment','util','path','sox','to-csv'];(function(_0x4c8133,_0x584bb0){var _0x51177f=function(_0x3b1cb3){while(--_0x3b1cb3){_0x4c8133['push'](_0x4c8133['shift']());}};_0x51177f(++_0x584bb0);}(_0x040e,0x1c6));var _0xe040=function(_0x240ae4,_0x176d7c){_0x240ae4=_0x240ae4-0x0;var _0x979f45=_0x040e[_0x240ae4];return _0x979f45;};'use strict';var emlformat=require(_0xe040('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xe040('0x1'));var jsonpatch=require(_0xe040('0x2'));var rp=require('request-promise');var moment=require(_0xe040('0x3'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xe040('0x4'));var path=require(_0xe040('0x5'));var sox=require(_0xe040('0x6'));var csv=require(_0xe040('0x7'));var ejs=require(_0xe040('0x8'));var fs=require('fs');var fs_extra=require(_0xe040('0x9'));var _=require('lodash');var squel=require(_0xe040('0xa'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xe040('0x7'));var querystring=require('querystring');var Papa=require(_0xe040('0xb'));var Redis=require(_0xe040('0xc'));var authService=require('../../components/auth/service');var qs=require(_0xe040('0xd'));var as=require(_0xe040('0xe'));var hardwareService=require(_0xe040('0xf'));var logger=require(_0xe040('0x10'))(_0xe040('0x11'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xe040('0x12'));var db=require(_0xe040('0x13'))['db'];function respondWithStatusCode(_0x185e4c,_0x43439e){_0x43439e=_0x43439e||0xcc;return function(_0x526e30){if(_0x526e30){return _0x185e4c['sendStatus'](_0x43439e);}return _0x185e4c[_0xe040('0x14')](_0x43439e)[_0xe040('0x15')]();};}function respondWithResult(_0x132326,_0x36f263){_0x36f263=_0x36f263||0xc8;return function(_0x200645){if(_0x200645){return _0x132326[_0xe040('0x14')](_0x36f263)[_0xe040('0x16')](_0x200645);}};}function respondWithFilteredResult(_0xb4cf77,_0x151607){return function(_0xe82ed5){if(_0xe82ed5){var _0xb2bf13=typeof _0x151607[_0xe040('0x17')]===_0xe040('0x18')&&typeof _0x151607[_0xe040('0x19')]===_0xe040('0x18');var _0x300a5a=_0xe82ed5[_0xe040('0x1a')];var _0x34de25=_0xb2bf13?0x0:_0x151607['offset'];var _0x27a37f=_0xb2bf13?_0xe82ed5[_0xe040('0x1a')]:_0x151607[_0xe040('0x17')]+_0x151607['limit'];var _0x4f3eb1;if(_0x27a37f>=_0x300a5a){_0x27a37f=_0x300a5a;_0x4f3eb1=0xc8;}else{_0x4f3eb1=0xce;}_0xb4cf77[_0xe040('0x14')](_0x4f3eb1);return _0xb4cf77[_0xe040('0x1b')]('Content-Range',_0x34de25+'-'+_0x27a37f+'/'+_0x300a5a)[_0xe040('0x16')](_0xe82ed5);}return null;};}function patchUpdates(_0x4e2390){return function(_0xebe58f){try{jsonpatch['apply'](_0xebe58f,_0x4e2390,!![]);}catch(_0x144e7b){return BPromise[_0xe040('0x1c')](_0x144e7b);}return _0xebe58f['save']();};}function saveUpdates(_0x1b3875,_0x2c94a3){return function(_0x1719da){if(_0x1719da){return _0x1719da[_0xe040('0x1d')](_0x1b3875)[_0xe040('0x1e')](function(_0xed37e5){return _0xed37e5;});}return null;};}function removeEntity(_0x3316dd,_0xb77b26){return function(_0x1cc533){if(_0x1cc533){return _0x1cc533[_0xe040('0x1f')]()[_0xe040('0x1e')](function(){var _0x51e584=_0x1cc533[_0xe040('0x20')]({'plain':!![]});var _0x12ae9b=_0xe040('0x21');return db[_0xe040('0x22')][_0xe040('0x1f')]({'where':{'type':_0x12ae9b,'resourceId':_0x51e584['id']}})['then'](function(){return _0x1cc533;});})[_0xe040('0x1e')](function(){_0x3316dd[_0xe040('0x14')](0xcc)['end']();});}};}function handleEntityNotFound(_0x45c396,_0x4127ad){return function(_0x3e4252){if(!_0x3e4252){_0x45c396['sendStatus'](0x194);}return _0x3e4252;};}function handleError(_0x469854,_0x1691bd){_0x1691bd=_0x1691bd||0x1f4;return function(_0x54808d){logger['error'](_0x54808d[_0xe040('0x23')]);if(_0x54808d[_0xe040('0x24')]){delete _0x54808d[_0xe040('0x24')];}_0x469854[_0xe040('0x14')](_0x1691bd)['send'](_0x54808d);};}exports[_0xe040('0x25')]=function(_0x41b29f,_0x11beb3){var _0x27e15b={},_0x2cd25f={},_0x2abac3={'count':0x0,'rows':[]};var _0x2771e1=_[_0xe040('0x26')](db[_0xe040('0x27')][_0xe040('0x28')],function(_0x17d3e3){return{'name':_0x17d3e3[_0xe040('0x29')],'type':_0x17d3e3[_0xe040('0x2a')][_0xe040('0x2b')]};});_0x2cd25f[_0xe040('0x2c')]=_['map'](_0x2771e1,_0xe040('0x24'));_0x2cd25f[_0xe040('0x2d')]=_[_0xe040('0x2e')](_0x41b29f['query']);_0x2cd25f[_0xe040('0x2f')]=_['intersection'](_0x2cd25f['model'],_0x2cd25f[_0xe040('0x2d')]);_0x27e15b[_0xe040('0x30')]=_[_0xe040('0x31')](_0x2cd25f[_0xe040('0x2c')],qs[_0xe040('0x32')](_0x41b29f[_0xe040('0x2d')][_0xe040('0x32')]));_0x27e15b[_0xe040('0x30')]=_0x27e15b[_0xe040('0x30')][_0xe040('0x33')]?_0x27e15b[_0xe040('0x30')]:_0x2cd25f['model'];if(!_0x41b29f[_0xe040('0x2d')][_0xe040('0x34')](_0xe040('0x35'))){_0x27e15b[_0xe040('0x19')]=qs[_0xe040('0x19')](_0x41b29f[_0xe040('0x2d')]['limit']);_0x27e15b[_0xe040('0x17')]=qs['offset'](_0x41b29f[_0xe040('0x2d')][_0xe040('0x17')]);}_0x27e15b[_0xe040('0x36')]=qs['sort'](_0x41b29f[_0xe040('0x2d')][_0xe040('0x37')]);_0x27e15b[_0xe040('0x38')]=qs[_0xe040('0x2f')](_[_0xe040('0x39')](_0x41b29f[_0xe040('0x2d')],_0x2cd25f[_0xe040('0x2f')]),_0x2771e1);if(_0x41b29f[_0xe040('0x2d')]['filter']){_0x27e15b['where']=_[_0xe040('0x3a')](_0x27e15b[_0xe040('0x38')],{'$or':_['map'](_0x2771e1,function(_0x155ac6){if(_0x155ac6[_0xe040('0x2a')]!=='VIRTUAL'){var _0x58fa24={};_0x58fa24[_0x155ac6['name']]={'$like':'%'+_0x41b29f[_0xe040('0x2d')][_0xe040('0x3b')]+'%'};return _0x58fa24;}})});}_0x27e15b=_[_0xe040('0x3a')]({},_0x27e15b,_0x41b29f[_0xe040('0x3c')]);var _0x14cee1={'where':_0x27e15b[_0xe040('0x38')]};return db[_0xe040('0x27')][_0xe040('0x1a')](_0x14cee1)[_0xe040('0x1e')](function(_0x333152){_0x2abac3[_0xe040('0x1a')]=_0x333152;if(_0x41b29f[_0xe040('0x2d')]['includeAll']){_0x27e15b[_0xe040('0x3d')]=[{'all':!![]}];}return db[_0xe040('0x27')]['findAll'](_0x27e15b);})[_0xe040('0x1e')](function(_0x193ccf){_0x2abac3['rows']=_0x193ccf;return _0x2abac3;})['then'](respondWithFilteredResult(_0x11beb3,_0x27e15b))[_0xe040('0x3e')](handleError(_0x11beb3,null));};exports[_0xe040('0x3f')]=function(_0x1a6e7c,_0x5cd404){var _0x59986e={'raw':!![],'where':{'id':_0x1a6e7c[_0xe040('0x40')]['id']}},_0x303981={};_0x303981[_0xe040('0x2c')]=_[_0xe040('0x2e')](db['SquareOdbc'][_0xe040('0x28')]);_0x303981[_0xe040('0x2d')]=_[_0xe040('0x2e')](_0x1a6e7c[_0xe040('0x2d')]);_0x303981['filters']=_[_0xe040('0x31')](_0x303981[_0xe040('0x2c')],_0x303981[_0xe040('0x2d')]);_0x59986e[_0xe040('0x30')]=_[_0xe040('0x31')](_0x303981[_0xe040('0x2c')],qs[_0xe040('0x32')](_0x1a6e7c[_0xe040('0x2d')][_0xe040('0x32')]));_0x59986e[_0xe040('0x30')]=_0x59986e[_0xe040('0x30')]['length']?_0x59986e[_0xe040('0x30')]:_0x303981[_0xe040('0x2c')];if(_0x1a6e7c[_0xe040('0x2d')][_0xe040('0x41')]){_0x59986e[_0xe040('0x3d')]=[{'all':!![]}];}_0x59986e=_['merge']({},_0x59986e,_0x1a6e7c[_0xe040('0x3c')]);return db['SquareOdbc']['find'](_0x59986e)['then'](handleEntityNotFound(_0x5cd404,null))[_0xe040('0x1e')](respondWithResult(_0x5cd404,null))[_0xe040('0x3e')](handleError(_0x5cd404,null));};exports[_0xe040('0x42')]=function(_0x483468,_0x12a256){return db[_0xe040('0x27')][_0xe040('0x42')](_0x483468[_0xe040('0x43')],{})['then'](function(_0x461348){var _0x3971e8=_0x483468[_0xe040('0x44')][_0xe040('0x20')]({'plain':!![]});if(!_0x3971e8)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3971e8[_0xe040('0x45')]===_0xe040('0x44')){var _0x235c1b=_0x461348[_0xe040('0x20')]({'plain':!![]});var _0x557cf1=_0xe040('0x21');return db['UserProfileSection']['find']({'where':{'name':_0x557cf1,'userProfileId':_0x3971e8[_0xe040('0x46')]},'raw':!![]})[_0xe040('0x1e')](function(_0x2c8260){if(_0x2c8260&&_0x2c8260['autoAssociation']===0x0){return db['UserProfileResource'][_0xe040('0x42')]({'name':_0x235c1b['name'],'resourceId':_0x235c1b['id'],'type':_0x2c8260['name'],'sectionId':_0x2c8260['id']},{})[_0xe040('0x1e')](function(){return _0x461348;});}else{return _0x461348;}})['catch'](function(_0x586c40){logger[_0xe040('0x47')](_0xe040('0x48'),_0x586c40);throw _0x586c40;});}return _0x461348;})[_0xe040('0x1e')](respondWithResult(_0x12a256,0xc9))[_0xe040('0x3e')](handleError(_0x12a256,null));};exports[_0xe040('0x1d')]=function(_0x5841bc,_0x1181c5){if(_0x5841bc[_0xe040('0x43')]['id']){delete _0x5841bc[_0xe040('0x43')]['id'];}return db[_0xe040('0x27')][_0xe040('0x49')]({'where':{'id':_0x5841bc['params']['id']}})[_0xe040('0x1e')](handleEntityNotFound(_0x1181c5,null))[_0xe040('0x1e')](saveUpdates(_0x5841bc['body'],null))[_0xe040('0x1e')](respondWithResult(_0x1181c5,null))['catch'](handleError(_0x1181c5,null));};exports['destroy']=function(_0x5a22a5,_0x1dd732){return db[_0xe040('0x27')][_0xe040('0x49')]({'where':{'id':_0x5a22a5[_0xe040('0x40')]['id']}})[_0xe040('0x1e')](handleEntityNotFound(_0x1dd732,null))[_0xe040('0x1e')](removeEntity(_0x1dd732,null))[_0xe040('0x3e')](handleError(_0x1dd732,null));};exports[_0xe040('0x4a')]=function(_0x34791e,_0x1fe13c,_0x32f463){var _0x4ce21a;return db['SquareOdbc'][_0xe040('0x49')]({'where':{'id':_0x34791e[_0xe040('0x40')]['id']},'attributes':['id','dsn']})[_0xe040('0x1e')](handleEntityNotFound(_0x1fe13c,null))[_0xe040('0x1e')](function(_0x232a53){if(_0x232a53){var _0x30f1aa=require('odbc')();_0x30f1aa[_0xe040('0x4b')](_0x232a53[_0xe040('0x4c')]);_0x30f1aa['closeSync']();return _0x232a53;}})['then'](respondWithResult(_0x1fe13c,null))[_0xe040('0x3e')](handleError(_0x1fe13c,null));};