f801c2c14a7d15940e1f60f01702b507b60469ff
[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 _0xfafb=['production','params','lodash','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./squareProject.socket','register','json','offset','limit','count','status','update','then','get','SquareProjects','destroy','publish','end','sendStatus','stack','map','rawAttributes','fieldName','type','key','model','name','query','keys','filters','intersection','attributes','fields','length','order','sort','where','pick','filter','merge','VIRTUAL','options','SquareProject','includeAll','rows','show','find','catch','create','body','user','UserProfileSection','userProfileId','UserProfileResource','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','stringify'];(function(_0x5160a6,_0x5e9b19){var _0x5d1b57=function(_0x1b139f){while(--_0x1b139f){_0x5160a6['push'](_0x5160a6['shift']());}};_0x5d1b57(++_0x5e9b19);}(_0xfafb,0x1c2));var _0xbfaf=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xfafb[_0x3dd15e];return _0x231fd0;};'use strict';var _=require(_0xbfaf('0x0'));var Redis=require('ioredis');var qs=require('../../components/parsers/qs');var logger=require(_0xbfaf('0x1'))(_0xbfaf('0x2'));var config=require(_0xbfaf('0x3'));var db=require(_0xbfaf('0x4'))['db'];config[_0xbfaf('0x5')]=_[_0xbfaf('0x6')](config[_0xbfaf('0x5')],{'host':_0xbfaf('0x7'),'port':0x18eb});var socket=require(_0xbfaf('0x8'))(new Redis(config[_0xbfaf('0x5')]));require(_0xbfaf('0x9'))[_0xbfaf('0xa')](socket);function respondWithResult(_0x415953,_0x37b612){_0x37b612=_0x37b612||0xc8;return function(_0x1ca0d6){if(_0x1ca0d6){return _0x415953['status'](_0x37b612)[_0xbfaf('0xb')](_0x1ca0d6);}};}function respondWithFilteredResult(_0x4478ea,_0x3fe050){return function(_0x1b6e56){if(_0x1b6e56){var _0x5f31ca=typeof _0x3fe050[_0xbfaf('0xc')]==='undefined'&&typeof _0x3fe050[_0xbfaf('0xd')]==='undefined';var _0x36306=_0x1b6e56[_0xbfaf('0xe')];var _0x385b85=_0x5f31ca?0x0:_0x3fe050[_0xbfaf('0xc')];var _0x50c672=_0x5f31ca?_0x1b6e56[_0xbfaf('0xe')]:_0x3fe050['offset']+_0x3fe050[_0xbfaf('0xd')];var _0x5b34fa;if(_0x50c672>=_0x36306){_0x50c672=_0x36306;_0x5b34fa=0xc8;}else{_0x5b34fa=0xce;}_0x4478ea[_0xbfaf('0xf')](_0x5b34fa);return _0x4478ea['set']('Content-Range',_0x385b85+'-'+_0x50c672+'/'+_0x36306)['json'](_0x1b6e56);}return null;};}function saveUpdates(_0x1976d6){return function(_0x20c9f1){if(_0x20c9f1){return _0x20c9f1[_0xbfaf('0x10')](_0x1976d6)[_0xbfaf('0x11')](function(_0x2c44c8){return _0x2c44c8;});}return null;};}function removeEntity(_0x51efe9){return function(_0x5027d1){if(_0x5027d1){return _0x5027d1['destroy']()[_0xbfaf('0x11')](function(){var _0x1186d4=_0x5027d1[_0xbfaf('0x12')]({'plain':!![]});var _0x261c88=_0xbfaf('0x13');return db['UserProfileResource'][_0xbfaf('0x14')]({'where':{'type':_0x261c88,'resourceId':_0x1186d4['id']}})[_0xbfaf('0x11')](function(){return _0x5027d1;});})[_0xbfaf('0x11')](function(){var _0xf26518=new Redis(config['redis']);_0xf26518[_0xbfaf('0x15')]('agi',JSON['stringify']({'id':_0x5027d1['id'],'deleted':!![]}));})[_0xbfaf('0x11')](function(){_0x51efe9[_0xbfaf('0xf')](0xcc)[_0xbfaf('0x16')]();});}};}function handleEntityNotFound(_0x5dbf12){return function(_0x241315){if(!_0x241315){_0x5dbf12[_0xbfaf('0x17')](0x194);}return _0x241315;};}function handleError(_0xbcd8c3,_0x1165c4){_0x1165c4=_0x1165c4||0x1f4;return function(_0x3962ce){logger['error'](_0x3962ce[_0xbfaf('0x18')]);if(_0x3962ce['name']){delete _0x3962ce['name'];}_0xbcd8c3[_0xbfaf('0xf')](_0x1165c4)['send'](_0x3962ce);};}exports['index']=function(_0x169956,_0x2913d5){var _0x1f77af={},_0x90cd70={},_0x5b5645={'count':0x0,'rows':[]};var _0x2dde35=_[_0xbfaf('0x19')](db['SquareProject'][_0xbfaf('0x1a')],function(_0x59e3ee){return{'name':_0x59e3ee[_0xbfaf('0x1b')],'type':_0x59e3ee[_0xbfaf('0x1c')][_0xbfaf('0x1d')]};});_0x90cd70[_0xbfaf('0x1e')]=_['map'](_0x2dde35,_0xbfaf('0x1f'));_0x90cd70[_0xbfaf('0x20')]=_[_0xbfaf('0x21')](_0x169956[_0xbfaf('0x20')]);_0x90cd70[_0xbfaf('0x22')]=_[_0xbfaf('0x23')](_0x90cd70[_0xbfaf('0x1e')],_0x90cd70['query']);_0x1f77af[_0xbfaf('0x24')]=_['intersection'](_0x90cd70[_0xbfaf('0x1e')],qs[_0xbfaf('0x25')](_0x169956[_0xbfaf('0x20')]['fields']));_0x1f77af[_0xbfaf('0x24')]=_0x1f77af['attributes'][_0xbfaf('0x26')]?_0x1f77af['attributes']:_0x90cd70[_0xbfaf('0x1e')];if(!_0x169956[_0xbfaf('0x20')]['hasOwnProperty']('nolimit')){_0x1f77af[_0xbfaf('0xd')]=qs[_0xbfaf('0xd')](_0x169956[_0xbfaf('0x20')][_0xbfaf('0xd')]);_0x1f77af['offset']=qs[_0xbfaf('0xc')](_0x169956[_0xbfaf('0x20')][_0xbfaf('0xc')]);}_0x1f77af[_0xbfaf('0x27')]=qs[_0xbfaf('0x28')](_0x169956[_0xbfaf('0x20')][_0xbfaf('0x28')]);_0x1f77af[_0xbfaf('0x29')]=qs[_0xbfaf('0x22')](_[_0xbfaf('0x2a')](_0x169956[_0xbfaf('0x20')],_0x90cd70[_0xbfaf('0x22')]),_0x2dde35);if(_0x169956[_0xbfaf('0x20')][_0xbfaf('0x2b')]){_0x1f77af[_0xbfaf('0x29')]=_[_0xbfaf('0x2c')](_0x1f77af[_0xbfaf('0x29')],{'$or':_[_0xbfaf('0x19')](_0x2dde35,function(_0x5ccfb1){if(_0x5ccfb1[_0xbfaf('0x1c')]!==_0xbfaf('0x2d')){var _0x19d84f={};_0x19d84f[_0x5ccfb1[_0xbfaf('0x1f')]]={'$like':'%'+_0x169956[_0xbfaf('0x20')][_0xbfaf('0x2b')]+'%'};return _0x19d84f;}})});}_0x1f77af=_[_0xbfaf('0x2c')]({},_0x1f77af,_0x169956[_0xbfaf('0x2e')]);var _0x1b0952={'where':_0x1f77af['where']};return db[_0xbfaf('0x2f')][_0xbfaf('0xe')](_0x1b0952)['then'](function(_0x150c54){_0x5b5645['count']=_0x150c54;if(_0x169956['query'][_0xbfaf('0x30')]){_0x1f77af['include']=[{'all':!![]}];}return db[_0xbfaf('0x2f')]['findAll'](_0x1f77af);})[_0xbfaf('0x11')](function(_0x1adeb7){_0x5b5645[_0xbfaf('0x31')]=_0x1adeb7;return _0x5b5645;})[_0xbfaf('0x11')](respondWithFilteredResult(_0x2913d5,_0x1f77af))['catch'](handleError(_0x2913d5,null));};exports[_0xbfaf('0x32')]=function(_0x5e0f60,_0x56b34f){var _0x1b98f2={'raw':![],'where':{'id':_0x5e0f60['params']['id']}},_0x5b7630={};_0x5b7630[_0xbfaf('0x1e')]=_[_0xbfaf('0x21')](db[_0xbfaf('0x2f')][_0xbfaf('0x1a')]);_0x5b7630[_0xbfaf('0x20')]=_['keys'](_0x5e0f60[_0xbfaf('0x20')]);_0x5b7630[_0xbfaf('0x22')]=_[_0xbfaf('0x23')](_0x5b7630[_0xbfaf('0x1e')],_0x5b7630['query']);_0x1b98f2[_0xbfaf('0x24')]=_[_0xbfaf('0x23')](_0x5b7630[_0xbfaf('0x1e')],qs[_0xbfaf('0x25')](_0x5e0f60[_0xbfaf('0x20')][_0xbfaf('0x25')]));_0x1b98f2['attributes']=_0x1b98f2[_0xbfaf('0x24')]['length']?_0x1b98f2[_0xbfaf('0x24')]:_0x5b7630[_0xbfaf('0x1e')];if(_0x5e0f60[_0xbfaf('0x20')]['includeAll']){_0x1b98f2['include']=[{'all':!![]}];}_0x1b98f2=_['merge']({},_0x1b98f2,_0x5e0f60['options']);return db[_0xbfaf('0x2f')][_0xbfaf('0x33')](_0x1b98f2)[_0xbfaf('0x11')](handleEntityNotFound(_0x56b34f,null))[_0xbfaf('0x11')](respondWithResult(_0x56b34f,null))[_0xbfaf('0x34')](handleError(_0x56b34f,null));};exports[_0xbfaf('0x35')]=function(_0x4212bf,_0x35f984){return db[_0xbfaf('0x2f')]['create'](_0x4212bf[_0xbfaf('0x36')],{})['then'](function(_0x4c7fa0){var _0x1d7871=_0x4212bf[_0xbfaf('0x37')][_0xbfaf('0x12')]({'plain':!![]});if(!_0x1d7871)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x1d7871['role']===_0xbfaf('0x37')){var _0x5a8414=_0x4c7fa0[_0xbfaf('0x12')]({'plain':!![]});var _0x4abdb1=_0xbfaf('0x13');return db[_0xbfaf('0x38')][_0xbfaf('0x33')]({'where':{'name':_0x4abdb1,'userProfileId':_0x1d7871[_0xbfaf('0x39')]},'raw':!![]})[_0xbfaf('0x11')](function(_0x48557c){if(_0x48557c&&_0x48557c['autoAssociation']===0x0){return db[_0xbfaf('0x3a')]['create']({'name':_0x5a8414[_0xbfaf('0x1f')],'resourceId':_0x5a8414['id'],'type':_0x48557c['name'],'sectionId':_0x48557c['id']},{})['then'](function(){return _0x4c7fa0;});}else{return _0x4c7fa0;}})[_0xbfaf('0x34')](function(_0x490629){logger[_0xbfaf('0x3b')](_0xbfaf('0x3c'),_0x490629);throw _0x490629;});}return _0x4c7fa0;})['then'](respondWithResult(_0x35f984,0xc9))[_0xbfaf('0x34')](handleError(_0x35f984,null));};exports['update']=function(_0x4f4b0a,_0x366213){if(_0x4f4b0a[_0xbfaf('0x36')]['id']){delete _0x4f4b0a[_0xbfaf('0x36')]['id'];}return db[_0xbfaf('0x2f')][_0xbfaf('0x33')]({'where':{'id':_0x4f4b0a['params']['id']}})[_0xbfaf('0x11')](handleEntityNotFound(_0x366213,null))[_0xbfaf('0x11')](saveUpdates(_0x4f4b0a['body'],null))[_0xbfaf('0x11')](function(_0x53d6cd){if(!_0x53d6cd)return;var _0x17693b=_0x53d6cd[_0xbfaf('0x12')]({'plain':!![]});return _0x17693b;})['then'](function(_0x168009){var _0x2dcdd3=new Redis(config['redis']);_0x2dcdd3[_0xbfaf('0x15')]('agi',JSON[_0xbfaf('0x3d')](_[_0xbfaf('0x2a')](_0x168009,['id',_0xbfaf('0x1f'),_0xbfaf('0x3e')])));return _0x168009;})[_0xbfaf('0x11')](respondWithResult(_0x366213,null))[_0xbfaf('0x34')](handleError(_0x366213,null));};exports[_0xbfaf('0x14')]=function(_0xc83910,_0x53db7a){return db['SquareProject'][_0xbfaf('0x33')]({'where':{'id':_0xc83910[_0xbfaf('0x3f')]['id']}})[_0xbfaf('0x11')](handleEntityNotFound(_0x53db7a,null))[_0xbfaf('0x11')](removeEntity(_0x53db7a,null))['catch'](handleError(_0x53db7a,null));};