19040ed82f285070f067cd5acce588aa2d0accff
[motion2.git] / server / api / squareProject / squareProject.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 _0x12d7=['update','then','destroy','get','SquareProjects','publish','agi','stringify','sendStatus','name','send','index','map','SquareProject','fieldName','type','key','query','keys','filters','model','intersection','fields','attributes','length','hasOwnProperty','order','sort','pick','filter','merge','VIRTUAL','where','includeAll','include','findAll','rows','catch','params','rawAttributes','options','find','create','user','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','production','lodash','ioredis','../../config/logger','api','../../config/environment','../../mysqldb','defaults','localhost','redis','./squareProject.socket','status','json','offset','limit','undefined','count','set'];(function(_0x713978,_0x1a9ddf){var _0x2730ee=function(_0x35d6df){while(--_0x35d6df){_0x713978['push'](_0x713978['shift']());}};_0x2730ee(++_0x1a9ddf);}(_0x12d7,0x139));var _0x712d=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x12d7[_0x3dd15e];return _0x231fd0;};'use strict';var _=require(_0x712d('0x0'));var Redis=require(_0x712d('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x712d('0x2'))(_0x712d('0x3'));var config=require(_0x712d('0x4'));var db=require(_0x712d('0x5'))['db'];config['redis']=_[_0x712d('0x6')](config['redis'],{'host':_0x712d('0x7'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x712d('0x8')]));require(_0x712d('0x9'))['register'](socket);function respondWithResult(_0x1c7a59,_0x2e642b){_0x2e642b=_0x2e642b||0xc8;return function(_0x1038e9){if(_0x1038e9){return _0x1c7a59[_0x712d('0xa')](_0x2e642b)[_0x712d('0xb')](_0x1038e9);}};}function respondWithFilteredResult(_0x5e5491,_0xd2ba46){return function(_0x3e9056){if(_0x3e9056){var _0x17436a=typeof _0xd2ba46[_0x712d('0xc')]==='undefined'&&typeof _0xd2ba46[_0x712d('0xd')]===_0x712d('0xe');var _0x3825cb=_0x3e9056['count'];var _0x88f9c8=_0x17436a?0x0:_0xd2ba46[_0x712d('0xc')];var _0x5027dc=_0x17436a?_0x3e9056[_0x712d('0xf')]:_0xd2ba46['offset']+_0xd2ba46['limit'];var _0x100166;if(_0x5027dc>=_0x3825cb){_0x5027dc=_0x3825cb;_0x100166=0xc8;}else{_0x100166=0xce;}_0x5e5491[_0x712d('0xa')](_0x100166);return _0x5e5491[_0x712d('0x10')]('Content-Range',_0x88f9c8+'-'+_0x5027dc+'/'+_0x3825cb)[_0x712d('0xb')](_0x3e9056);}return null;};}function saveUpdates(_0xfcba3b){return function(_0x20d0c1){if(_0x20d0c1){return _0x20d0c1[_0x712d('0x11')](_0xfcba3b)[_0x712d('0x12')](function(_0x4c7cd3){return _0x4c7cd3;});}return null;};}function removeEntity(_0xb9e8a){return function(_0x48c388){if(_0x48c388){return _0x48c388[_0x712d('0x13')]()[_0x712d('0x12')](function(){var _0x576216=_0x48c388[_0x712d('0x14')]({'plain':!![]});var _0x50adcb=_0x712d('0x15');return db['UserProfileResource'][_0x712d('0x13')]({'where':{'type':_0x50adcb,'resourceId':_0x576216['id']}})['then'](function(){return _0x48c388;});})[_0x712d('0x12')](function(){var _0x3557f5=new Redis(config['redis']);_0x3557f5[_0x712d('0x16')](_0x712d('0x17'),JSON[_0x712d('0x18')]({'id':_0x48c388['id'],'deleted':!![]}));})[_0x712d('0x12')](function(){_0xb9e8a['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x1c5d8a){return function(_0x5980e8){if(!_0x5980e8){_0x1c5d8a[_0x712d('0x19')](0x194);}return _0x5980e8;};}function handleError(_0x338cbc,_0x4c0a45){_0x4c0a45=_0x4c0a45||0x1f4;return function(_0x2720e9){logger['error'](_0x2720e9['stack']);if(_0x2720e9[_0x712d('0x1a')]){delete _0x2720e9[_0x712d('0x1a')];}_0x338cbc[_0x712d('0xa')](_0x4c0a45)[_0x712d('0x1b')](_0x2720e9);};}exports[_0x712d('0x1c')]=function(_0x32b467,_0x4f7ee1){var _0x294b38={},_0x5a8069={},_0x4d3ab8={'count':0x0,'rows':[]};var _0x657952=_[_0x712d('0x1d')](db[_0x712d('0x1e')]['rawAttributes'],function(_0x4f0600){return{'name':_0x4f0600[_0x712d('0x1f')],'type':_0x4f0600[_0x712d('0x20')][_0x712d('0x21')]};});_0x5a8069['model']=_['map'](_0x657952,_0x712d('0x1a'));_0x5a8069[_0x712d('0x22')]=_[_0x712d('0x23')](_0x32b467[_0x712d('0x22')]);_0x5a8069[_0x712d('0x24')]=_['intersection'](_0x5a8069[_0x712d('0x25')],_0x5a8069['query']);_0x294b38['attributes']=_[_0x712d('0x26')](_0x5a8069['model'],qs[_0x712d('0x27')](_0x32b467[_0x712d('0x22')]['fields']));_0x294b38[_0x712d('0x28')]=_0x294b38[_0x712d('0x28')][_0x712d('0x29')]?_0x294b38['attributes']:_0x5a8069[_0x712d('0x25')];if(!_0x32b467[_0x712d('0x22')][_0x712d('0x2a')]('nolimit')){_0x294b38[_0x712d('0xd')]=qs['limit'](_0x32b467['query'][_0x712d('0xd')]);_0x294b38[_0x712d('0xc')]=qs['offset'](_0x32b467['query']['offset']);}_0x294b38[_0x712d('0x2b')]=qs[_0x712d('0x2c')](_0x32b467[_0x712d('0x22')][_0x712d('0x2c')]);_0x294b38['where']=qs[_0x712d('0x24')](_[_0x712d('0x2d')](_0x32b467[_0x712d('0x22')],_0x5a8069[_0x712d('0x24')]),_0x657952);if(_0x32b467['query'][_0x712d('0x2e')]){_0x294b38['where']=_[_0x712d('0x2f')](_0x294b38['where'],{'$or':_[_0x712d('0x1d')](_0x657952,function(_0xf1566d){if(_0xf1566d[_0x712d('0x20')]!==_0x712d('0x30')){var _0x2831fa={};_0x2831fa[_0xf1566d['name']]={'$like':'%'+_0x32b467['query'][_0x712d('0x2e')]+'%'};return _0x2831fa;}})});}_0x294b38=_['merge']({},_0x294b38,_0x32b467['options']);var _0x4d6986={'where':_0x294b38[_0x712d('0x31')]};return db['SquareProject'][_0x712d('0xf')](_0x4d6986)[_0x712d('0x12')](function(_0x1e9aa1){_0x4d3ab8[_0x712d('0xf')]=_0x1e9aa1;if(_0x32b467['query'][_0x712d('0x32')]){_0x294b38[_0x712d('0x33')]=[{'all':!![]}];}return db[_0x712d('0x1e')][_0x712d('0x34')](_0x294b38);})[_0x712d('0x12')](function(_0x5b7b51){_0x4d3ab8[_0x712d('0x35')]=_0x5b7b51;return _0x4d3ab8;})['then'](respondWithFilteredResult(_0x4f7ee1,_0x294b38))[_0x712d('0x36')](handleError(_0x4f7ee1,null));};exports['show']=function(_0x54d497,_0x1dbce7){var _0x5f29de={'raw':![],'where':{'id':_0x54d497[_0x712d('0x37')]['id']}},_0x4dfe1b={};_0x4dfe1b[_0x712d('0x25')]=_[_0x712d('0x23')](db[_0x712d('0x1e')][_0x712d('0x38')]);_0x4dfe1b['query']=_['keys'](_0x54d497[_0x712d('0x22')]);_0x4dfe1b[_0x712d('0x24')]=_[_0x712d('0x26')](_0x4dfe1b[_0x712d('0x25')],_0x4dfe1b['query']);_0x5f29de[_0x712d('0x28')]=_['intersection'](_0x4dfe1b[_0x712d('0x25')],qs[_0x712d('0x27')](_0x54d497['query'][_0x712d('0x27')]));_0x5f29de['attributes']=_0x5f29de[_0x712d('0x28')][_0x712d('0x29')]?_0x5f29de['attributes']:_0x4dfe1b[_0x712d('0x25')];if(_0x54d497['query']['includeAll']){_0x5f29de[_0x712d('0x33')]=[{'all':!![]}];}_0x5f29de=_[_0x712d('0x2f')]({},_0x5f29de,_0x54d497[_0x712d('0x39')]);return db[_0x712d('0x1e')][_0x712d('0x3a')](_0x5f29de)['then'](handleEntityNotFound(_0x1dbce7,null))[_0x712d('0x12')](respondWithResult(_0x1dbce7,null))[_0x712d('0x36')](handleError(_0x1dbce7,null));};exports[_0x712d('0x3b')]=function(_0x298efe,_0x1fca83){return db[_0x712d('0x1e')][_0x712d('0x3b')](_0x298efe['body'],{})[_0x712d('0x12')](function(_0x17bfb2){var _0x415ea2=_0x298efe[_0x712d('0x3c')]['get']({'plain':!![]});if(!_0x415ea2)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x415ea2['role']===_0x712d('0x3c')){var _0x44dcf8=_0x17bfb2[_0x712d('0x14')]({'plain':!![]});var _0x1645f7=_0x712d('0x15');return db['UserProfileSection'][_0x712d('0x3a')]({'where':{'name':_0x1645f7,'userProfileId':_0x415ea2['userProfileId']},'raw':!![]})[_0x712d('0x12')](function(_0x122e1c){if(_0x122e1c&&_0x122e1c[_0x712d('0x3d')]===0x0){return db['UserProfileResource'][_0x712d('0x3b')]({'name':_0x44dcf8[_0x712d('0x1a')],'resourceId':_0x44dcf8['id'],'type':_0x122e1c[_0x712d('0x1a')],'sectionId':_0x122e1c['id']},{})[_0x712d('0x12')](function(){return _0x17bfb2;});}else{return _0x17bfb2;}})[_0x712d('0x36')](function(_0x599574){logger[_0x712d('0x3e')](_0x712d('0x3f'),_0x599574);throw _0x599574;});}return _0x17bfb2;})[_0x712d('0x12')](respondWithResult(_0x1fca83,0xc9))[_0x712d('0x36')](handleError(_0x1fca83,null));};exports[_0x712d('0x11')]=function(_0x581ee2,_0x545240){if(_0x581ee2['body']['id']){delete _0x581ee2[_0x712d('0x40')]['id'];}return db[_0x712d('0x1e')]['find']({'where':{'id':_0x581ee2[_0x712d('0x37')]['id']}})[_0x712d('0x12')](handleEntityNotFound(_0x545240,null))['then'](saveUpdates(_0x581ee2['body'],null))[_0x712d('0x12')](function(_0x346770){if(!_0x346770)return;var _0x14f8d9=_0x346770[_0x712d('0x14')]({'plain':!![]});return _0x14f8d9;})[_0x712d('0x12')](function(_0xf41218){var _0x1e9887=new Redis(config[_0x712d('0x8')]);_0x1e9887[_0x712d('0x16')](_0x712d('0x17'),JSON[_0x712d('0x18')](_[_0x712d('0x2d')](_0xf41218,['id','name',_0x712d('0x41')])));return _0xf41218;})[_0x712d('0x12')](respondWithResult(_0x545240,null))['catch'](handleError(_0x545240,null));};exports[_0x712d('0x13')]=function(_0xa87279,_0x57cbc8){return db[_0x712d('0x1e')][_0x712d('0x3a')]({'where':{'id':_0xa87279[_0x712d('0x37')]['id']}})[_0x712d('0x12')](handleEntityNotFound(_0x57cbc8,null))[_0x712d('0x12')](removeEntity(_0x57cbc8,null))[_0x712d('0x36')](handleError(_0x57cbc8,null));};