a7b9a8d6645481e52376c8c92994ae164c5f5ad9
[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 _0xf105=['find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','error','update','params','addUsers','ids','omit','removeUsers','emit','userSquareProject:remove','getUsers','findOne','User','options','eml-format','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','redis','localhost','./squareProject.socket','register','status','end','json','undefined','count','offset','Content-Range','apply','reject','save','then','destroy','get','SquareProjects','UserProfileResource','stack','name','index','map','SquareProject','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','VIRTUAL','filter','merge','findAll','rows','catch','includeAll'];(function(_0x20e440,_0x14735c){var _0x595ca3=function(_0x138030){while(--_0x138030){_0x20e440['push'](_0x20e440['shift']());}};_0x595ca3(++_0x14735c);}(_0xf105,0x135));var _0x5f10=function(_0x557cac,_0x29882d){_0x557cac=_0x557cac-0x0;var _0x1e78e3=_0xf105[_0x557cac];return _0x1e78e3;};'use strict';var emlformat=require(_0x5f10('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x5f10('0x1'));var jsonpatch=require(_0x5f10('0x2'));var rp=require(_0x5f10('0x3'));var moment=require('moment');var BPromise=require(_0x5f10('0x4'));var Mustache=require(_0x5f10('0x5'));var util=require(_0x5f10('0x6'));var path=require(_0x5f10('0x7'));var sox=require('sox');var csv=require(_0x5f10('0x8'));var ejs=require(_0x5f10('0x9'));var fs=require('fs');var fs_extra=require(_0x5f10('0xa'));var _=require(_0x5f10('0xb'));var squel=require(_0x5f10('0xc'));var crypto=require(_0x5f10('0xd'));var jsforce=require(_0x5f10('0xe'));var deskjs=require('desk.js');var toCsv=require(_0x5f10('0x8'));var querystring=require('querystring');var Papa=require(_0x5f10('0xf'));var Redis=require(_0x5f10('0x10'));var authService=require(_0x5f10('0x11'));var qs=require(_0x5f10('0x12'));var as=require(_0x5f10('0x13'));var hardwareService=require(_0x5f10('0x14'));var logger=require(_0x5f10('0x15'))(_0x5f10('0x16'));var utils=require('../../config/utils');var config=require(_0x5f10('0x17'));var licenseUtil=require(_0x5f10('0x18'));var db=require('../../mysqldb')['db'];config['redis']=_['defaults'](config[_0x5f10('0x19')],{'host':_0x5f10('0x1a'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x5f10('0x19')]));require(_0x5f10('0x1b'))[_0x5f10('0x1c')](socket);function respondWithStatusCode(_0x525239,_0x2e3cf0){_0x2e3cf0=_0x2e3cf0||0xcc;return function(_0x4e93d3){if(_0x4e93d3){return _0x525239['sendStatus'](_0x2e3cf0);}return _0x525239[_0x5f10('0x1d')](_0x2e3cf0)[_0x5f10('0x1e')]();};}function respondWithResult(_0x52573d,_0x2020b9){_0x2020b9=_0x2020b9||0xc8;return function(_0x5252ff){if(_0x5252ff){return _0x52573d['status'](_0x2020b9)[_0x5f10('0x1f')](_0x5252ff);}};}function respondWithFilteredResult(_0x5ea779,_0x17cf51){return function(_0x1c2c69){if(_0x1c2c69){var _0x5316cd=typeof _0x17cf51['offset']===_0x5f10('0x20')&&typeof _0x17cf51['limit']===_0x5f10('0x20');var _0x5f456e=_0x1c2c69[_0x5f10('0x21')];var _0x3e57fd=_0x5316cd?0x0:_0x17cf51['offset'];var _0x3f711d=_0x5316cd?_0x1c2c69[_0x5f10('0x21')]:_0x17cf51[_0x5f10('0x22')]+_0x17cf51['limit'];var _0x3da70a;if(_0x3f711d>=_0x5f456e){_0x3f711d=_0x5f456e;_0x3da70a=0xc8;}else{_0x3da70a=0xce;}_0x5ea779['status'](_0x3da70a);return _0x5ea779['set'](_0x5f10('0x23'),_0x3e57fd+'-'+_0x3f711d+'/'+_0x5f456e)[_0x5f10('0x1f')](_0x1c2c69);}return null;};}function patchUpdates(_0x38fb94){return function(_0x34e103){try{jsonpatch[_0x5f10('0x24')](_0x34e103,_0x38fb94,!![]);}catch(_0x3e2bce){return BPromise[_0x5f10('0x25')](_0x3e2bce);}return _0x34e103[_0x5f10('0x26')]();};}function saveUpdates(_0x554345,_0x2a5ec1){return function(_0x2e4413){if(_0x2e4413){return _0x2e4413['update'](_0x554345)[_0x5f10('0x27')](function(_0x478425){return _0x478425;});}return null;};}function removeEntity(_0x29a8e8,_0x3d6998){return function(_0x26e6d7){if(_0x26e6d7){return _0x26e6d7[_0x5f10('0x28')]()[_0x5f10('0x27')](function(){var _0xc91d9d=_0x26e6d7[_0x5f10('0x29')]({'plain':!![]});var _0x3e0463=_0x5f10('0x2a');return db[_0x5f10('0x2b')][_0x5f10('0x28')]({'where':{'type':_0x3e0463,'resourceId':_0xc91d9d['id']}})['then'](function(){return _0x26e6d7;});})['then'](function(){_0x29a8e8[_0x5f10('0x1d')](0xcc)[_0x5f10('0x1e')]();});}};}function handleEntityNotFound(_0x574ff2,_0x349432){return function(_0x3b8a51){if(!_0x3b8a51){_0x574ff2['sendStatus'](0x194);}return _0x3b8a51;};}function handleError(_0x3129a3,_0x22b269){_0x22b269=_0x22b269||0x1f4;return function(_0x3b5c86){logger['error'](_0x3b5c86[_0x5f10('0x2c')]);if(_0x3b5c86[_0x5f10('0x2d')]){delete _0x3b5c86[_0x5f10('0x2d')];}_0x3129a3[_0x5f10('0x1d')](_0x22b269)['send'](_0x3b5c86);};}exports[_0x5f10('0x2e')]=function(_0x49790f,_0x356f00){var _0x154062={},_0x3fea04={},_0x11779c={'count':0x0,'rows':[]};var _0x7bb02a=_[_0x5f10('0x2f')](db[_0x5f10('0x30')][_0x5f10('0x31')],function(_0x525669){return{'name':_0x525669[_0x5f10('0x32')],'type':_0x525669[_0x5f10('0x33')][_0x5f10('0x34')]};});_0x3fea04[_0x5f10('0x35')]=_[_0x5f10('0x2f')](_0x7bb02a,_0x5f10('0x2d'));_0x3fea04[_0x5f10('0x36')]=_[_0x5f10('0x37')](_0x49790f['query']);_0x3fea04[_0x5f10('0x38')]=_[_0x5f10('0x39')](_0x3fea04[_0x5f10('0x35')],_0x3fea04['query']);_0x154062[_0x5f10('0x3a')]=_[_0x5f10('0x39')](_0x3fea04['model'],qs[_0x5f10('0x3b')](_0x49790f[_0x5f10('0x36')][_0x5f10('0x3b')]));_0x154062[_0x5f10('0x3a')]=_0x154062['attributes'][_0x5f10('0x3c')]?_0x154062[_0x5f10('0x3a')]:_0x3fea04[_0x5f10('0x35')];if(!_0x49790f[_0x5f10('0x36')][_0x5f10('0x3d')](_0x5f10('0x3e'))){_0x154062['limit']=qs[_0x5f10('0x3f')](_0x49790f['query']['limit']);_0x154062[_0x5f10('0x22')]=qs[_0x5f10('0x22')](_0x49790f['query'][_0x5f10('0x22')]);}_0x154062[_0x5f10('0x40')]=qs[_0x5f10('0x41')](_0x49790f[_0x5f10('0x36')][_0x5f10('0x41')]);_0x154062[_0x5f10('0x42')]=qs[_0x5f10('0x38')](_[_0x5f10('0x43')](_0x49790f[_0x5f10('0x36')],_0x3fea04['filters']),_0x7bb02a);if(_0x49790f[_0x5f10('0x36')]['filter']){_0x154062[_0x5f10('0x42')]=_['merge'](_0x154062['where'],{'$or':_[_0x5f10('0x2f')](_0x7bb02a,function(_0x38fc3b){if(_0x38fc3b[_0x5f10('0x33')]!==_0x5f10('0x44')){var _0x57b037={};_0x57b037[_0x38fc3b[_0x5f10('0x2d')]]={'$like':'%'+_0x49790f['query'][_0x5f10('0x45')]+'%'};return _0x57b037;}})});}_0x154062=_[_0x5f10('0x46')]({},_0x154062,_0x49790f['options']);var _0x3a48a4={'where':_0x154062[_0x5f10('0x42')]};return db['SquareProject'][_0x5f10('0x21')](_0x3a48a4)[_0x5f10('0x27')](function(_0x4f1219){_0x11779c[_0x5f10('0x21')]=_0x4f1219;if(_0x49790f['query']['includeAll']){_0x154062['include']=[{'all':!![]}];}return db[_0x5f10('0x30')][_0x5f10('0x47')](_0x154062);})[_0x5f10('0x27')](function(_0x1ead47){_0x11779c[_0x5f10('0x48')]=_0x1ead47;return _0x11779c;})[_0x5f10('0x27')](respondWithFilteredResult(_0x356f00,_0x154062))[_0x5f10('0x49')](handleError(_0x356f00,null));};exports['show']=function(_0x392d88,_0x3df81a){var _0xd9561={'raw':![],'where':{'id':_0x392d88['params']['id']}},_0x4fa576={};_0x4fa576[_0x5f10('0x35')]=_['keys'](db[_0x5f10('0x30')]['rawAttributes']);_0x4fa576[_0x5f10('0x36')]=_[_0x5f10('0x37')](_0x392d88['query']);_0x4fa576[_0x5f10('0x38')]=_[_0x5f10('0x39')](_0x4fa576['model'],_0x4fa576[_0x5f10('0x36')]);_0xd9561[_0x5f10('0x3a')]=_[_0x5f10('0x39')](_0x4fa576['model'],qs['fields'](_0x392d88['query']['fields']));_0xd9561['attributes']=_0xd9561['attributes'][_0x5f10('0x3c')]?_0xd9561[_0x5f10('0x3a')]:_0x4fa576[_0x5f10('0x35')];if(_0x392d88[_0x5f10('0x36')][_0x5f10('0x4a')]){_0xd9561['include']=[{'all':!![]}];}_0xd9561=_['merge']({},_0xd9561,_0x392d88['options']);return db[_0x5f10('0x30')][_0x5f10('0x4b')](_0xd9561)['then'](handleEntityNotFound(_0x3df81a,null))['then'](respondWithResult(_0x3df81a,null))[_0x5f10('0x49')](handleError(_0x3df81a,null));};exports[_0x5f10('0x4c')]=function(_0x1d1420,_0x40ef89){return db[_0x5f10('0x30')][_0x5f10('0x4c')](_0x1d1420[_0x5f10('0x4d')],{})[_0x5f10('0x27')](function(_0x259845){var _0x42f050=_0x1d1420[_0x5f10('0x4e')][_0x5f10('0x29')]({'plain':!![]});if(!_0x42f050)throw new Error(_0x5f10('0x4f'));if(_0x42f050[_0x5f10('0x50')]==='user'){var _0x2ef5c9=_0x259845[_0x5f10('0x29')]({'plain':!![]});var _0x3421cd='SquareProjects';return db[_0x5f10('0x51')]['find']({'where':{'name':_0x3421cd,'userProfileId':_0x42f050['userProfileId']},'raw':!![]})[_0x5f10('0x27')](function(_0x559f95){if(_0x559f95&&_0x559f95[_0x5f10('0x52')]===0x0){return db[_0x5f10('0x2b')][_0x5f10('0x4c')]({'name':_0x2ef5c9[_0x5f10('0x2d')],'resourceId':_0x2ef5c9['id'],'type':_0x559f95[_0x5f10('0x2d')],'sectionId':_0x559f95['id']},{})['then'](function(){return _0x259845;});}else{return _0x259845;}})[_0x5f10('0x49')](function(_0xb3292f){logger[_0x5f10('0x53')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0xb3292f);throw _0xb3292f;});}return _0x259845;})['then'](respondWithResult(_0x40ef89,0xc9))[_0x5f10('0x49')](handleError(_0x40ef89,null));};exports[_0x5f10('0x54')]=function(_0x3c499d,_0xaea7e0){if(_0x3c499d[_0x5f10('0x4d')]['id']){delete _0x3c499d[_0x5f10('0x4d')]['id'];}return db['SquareProject'][_0x5f10('0x4b')]({'where':{'id':_0x3c499d[_0x5f10('0x55')]['id']}})[_0x5f10('0x27')](handleEntityNotFound(_0xaea7e0,null))['then'](saveUpdates(_0x3c499d[_0x5f10('0x4d')],null))['then'](respondWithResult(_0xaea7e0,null))['catch'](handleError(_0xaea7e0,null));};exports[_0x5f10('0x28')]=function(_0x54e9f1,_0x1ee8fb){return db[_0x5f10('0x30')][_0x5f10('0x4b')]({'where':{'id':_0x54e9f1[_0x5f10('0x55')]['id']}})[_0x5f10('0x27')](handleEntityNotFound(_0x1ee8fb,null))[_0x5f10('0x27')](removeEntity(_0x1ee8fb,null))[_0x5f10('0x49')](handleError(_0x1ee8fb,null));};exports[_0x5f10('0x56')]=function(_0x509ed2,_0x1f96c5,_0x4724cc){return db[_0x5f10('0x30')][_0x5f10('0x4b')]({'where':{'id':_0x509ed2[_0x5f10('0x55')]['id']}})[_0x5f10('0x27')](handleEntityNotFound(_0x1f96c5,null))[_0x5f10('0x27')](function(_0xf8d22e){if(_0xf8d22e){return _0xf8d22e[_0x5f10('0x56')](_0x509ed2[_0x5f10('0x4d')][_0x5f10('0x57')],_[_0x5f10('0x58')](_0x509ed2[_0x5f10('0x4d')],[_0x5f10('0x57'),'id'])||{});}})[_0x5f10('0x27')](respondWithResult(_0x1f96c5,null))['catch'](handleError(_0x1f96c5,null));};exports['removeUsers']=function(_0x29dca9,_0x9e8e31,_0xec02a8){return db[_0x5f10('0x30')][_0x5f10('0x4b')]({'where':{'id':_0x29dca9[_0x5f10('0x55')]['id']}})['then'](handleEntityNotFound(_0x9e8e31,null))[_0x5f10('0x27')](function(_0x39ff5b){if(_0x39ff5b){return _0x39ff5b[_0x5f10('0x59')](_0x29dca9[_0x5f10('0x36')]['ids'])['then'](function(){if(_['isArray'](_0x29dca9['query'][_0x5f10('0x57')])){for(var _0x742caf=0x0;_0x742caf<_0x29dca9[_0x5f10('0x36')][_0x5f10('0x57')]['length'];_0x742caf+=0x1){socket[_0x5f10('0x5a')]('userSquareProject:remove',{'UserId':Number(_0x29dca9[_0x5f10('0x36')]['ids'][_0x742caf]),'SquareProjectId':Number(_0x29dca9[_0x5f10('0x55')]['id'])});}}else{socket[_0x5f10('0x5a')](_0x5f10('0x5b'),{'UserId':Number(_0x29dca9['query'][_0x5f10('0x57')]),'SquareProjectId':Number(_0x29dca9['params']['id'])});}});}})['then'](respondWithStatusCode(_0x9e8e31,null))[_0x5f10('0x49')](handleError(_0x9e8e31,null));};exports[_0x5f10('0x5c')]=function(_0x33c718,_0x5c9c9c,_0x1682f6){var _0xf72a2f={};var _0x46c76f={};var _0x337fb4;var _0x1e80e4;return db[_0x5f10('0x30')][_0x5f10('0x5d')]({'where':{'id':_0x33c718[_0x5f10('0x55')]['id']}})[_0x5f10('0x27')](handleEntityNotFound(_0x5c9c9c,null))[_0x5f10('0x27')](function(_0x2a85c2){if(_0x2a85c2){_0x337fb4=_0x2a85c2;_0x46c76f[_0x5f10('0x35')]=_[_0x5f10('0x37')](db[_0x5f10('0x5e')]['rawAttributes']);_0x46c76f[_0x5f10('0x36')]=_['keys'](_0x33c718[_0x5f10('0x36')]);_0x46c76f[_0x5f10('0x38')]=_[_0x5f10('0x39')](_0x46c76f['model'],_0x46c76f[_0x5f10('0x36')]);_0xf72a2f[_0x5f10('0x3a')]=_[_0x5f10('0x39')](_0x46c76f[_0x5f10('0x35')],qs[_0x5f10('0x3b')](_0x33c718[_0x5f10('0x36')][_0x5f10('0x3b')]));_0xf72a2f[_0x5f10('0x3a')]=_0xf72a2f[_0x5f10('0x3a')][_0x5f10('0x3c')]?_0xf72a2f[_0x5f10('0x3a')]:_0x46c76f[_0x5f10('0x35')];_0xf72a2f['order']=qs['sort'](_0x33c718[_0x5f10('0x36')][_0x5f10('0x41')]);_0xf72a2f[_0x5f10('0x42')]=qs['filters'](_[_0x5f10('0x43')](_0x33c718[_0x5f10('0x36')],_0x46c76f[_0x5f10('0x38')]));if(_0x33c718[_0x5f10('0x36')][_0x5f10('0x45')]){_0xf72a2f[_0x5f10('0x42')]=_[_0x5f10('0x46')](_0xf72a2f[_0x5f10('0x42')],{'$or':_[_0x5f10('0x2f')](_0xf72a2f[_0x5f10('0x3a')],function(_0x4dcb44){var _0x47b838={};_0x47b838[_0x4dcb44]={'$like':'%'+_0x33c718['query'][_0x5f10('0x45')]+'%'};return _0x47b838;})});}_0xf72a2f=_[_0x5f10('0x46')]({},_0xf72a2f,_0x33c718[_0x5f10('0x5f')]);return _0x337fb4[_0x5f10('0x5c')](_0xf72a2f);}})[_0x5f10('0x27')](function(_0x32be4d){if(_0x32be4d){_0x1e80e4=_0x32be4d[_0x5f10('0x3c')];if(!_0x33c718[_0x5f10('0x36')][_0x5f10('0x3d')](_0x5f10('0x3e'))){_0xf72a2f['limit']=qs['limit'](_0x33c718[_0x5f10('0x36')][_0x5f10('0x3f')]);_0xf72a2f[_0x5f10('0x22')]=qs[_0x5f10('0x22')](_0x33c718[_0x5f10('0x36')]['offset']);}return _0x337fb4[_0x5f10('0x5c')](_0xf72a2f);}})[_0x5f10('0x27')](function(_0x4cdadc){if(_0x4cdadc){return _0x4cdadc?{'count':_0x1e80e4,'rows':_0x4cdadc}:null;}})['then'](respondWithResult(_0x5c9c9c,null))[_0x5f10('0x49')](handleError(_0x5c9c9c,null));};