Built motion from commit 25580385.|2.6.2
[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 _0x126f=['../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','redis','localhost','socket.io-emitter','./squareProject.socket','register','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','update','then','destroy','get','SquareProjects','UserProfileResource','error','stack','name','send','index','map','SquareProject','rawAttributes','fieldName','key','model','filters','intersection','query','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','filter','merge','type','options','includeAll','findAll','rows','catch','show','keys','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','addUsers','omit','ids','removeUsers','isArray','userSquareProject:remove','findOne','User','getUsers','eml-format','rimraf','zip-dir','fast-json-patch','bluebird','mustache','util','path','to-csv','ejs','fs-extra','lodash','squel','jsforce','querystring','ioredis','../../components/auth/service','../../components/parsers/qs'];(function(_0x2a60a6,_0x3968ff){var _0x574216=function(_0x39eddd){while(--_0x39eddd){_0x2a60a6['push'](_0x2a60a6['shift']());}};_0x574216(++_0x3968ff);}(_0x126f,0x1d8));var _0xf126=function(_0x35dfe6,_0x4244fa){_0x35dfe6=_0x35dfe6-0x0;var _0x59651c=_0x126f[_0x35dfe6];return _0x59651c;};'use strict';var emlformat=require(_0xf126('0x0'));var rimraf=require(_0xf126('0x1'));var zipdir=require(_0xf126('0x2'));var jsonpatch=require(_0xf126('0x3'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xf126('0x4'));var Mustache=require(_0xf126('0x5'));var util=require(_0xf126('0x6'));var path=require(_0xf126('0x7'));var sox=require('sox');var csv=require(_0xf126('0x8'));var ejs=require(_0xf126('0x9'));var fs=require('fs');var fs_extra=require(_0xf126('0xa'));var _=require(_0xf126('0xb'));var squel=require(_0xf126('0xc'));var crypto=require('crypto');var jsforce=require(_0xf126('0xd'));var deskjs=require('desk.js');var toCsv=require(_0xf126('0x8'));var querystring=require(_0xf126('0xe'));var Papa=require('papaparse');var Redis=require(_0xf126('0xf'));var authService=require(_0xf126('0x10'));var qs=require(_0xf126('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf126('0x12'));var logger=require(_0xf126('0x13'))(_0xf126('0x14'));var utils=require(_0xf126('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0xf126('0x16'));var db=require(_0xf126('0x17'))['db'];config[_0xf126('0x18')]=_['defaults'](config['redis'],{'host':_0xf126('0x19'),'port':0x18eb});var socket=require(_0xf126('0x1a'))(new Redis(config['redis']));require(_0xf126('0x1b'))[_0xf126('0x1c')](socket);function respondWithStatusCode(_0x54ae44,_0x5c4210){_0x5c4210=_0x5c4210||0xcc;return function(_0x110c64){if(_0x110c64){return _0x54ae44[_0xf126('0x1d')](_0x5c4210);}return _0x54ae44[_0xf126('0x1e')](_0x5c4210)[_0xf126('0x1f')]();};}function respondWithResult(_0x51fc45,_0x3428cf){_0x3428cf=_0x3428cf||0xc8;return function(_0x309c69){if(_0x309c69){return _0x51fc45[_0xf126('0x1e')](_0x3428cf)[_0xf126('0x20')](_0x309c69);}};}function respondWithFilteredResult(_0x1d9e25,_0xb3c56e){return function(_0xc1f9ec){if(_0xc1f9ec){var _0x3c8b83=typeof _0xb3c56e[_0xf126('0x21')]===_0xf126('0x22')&&typeof _0xb3c56e[_0xf126('0x23')]===_0xf126('0x22');var _0x2fa7e6=_0xc1f9ec[_0xf126('0x24')];var _0x32c6b9=_0x3c8b83?0x0:_0xb3c56e[_0xf126('0x21')];var _0x451211=_0x3c8b83?_0xc1f9ec[_0xf126('0x24')]:_0xb3c56e[_0xf126('0x21')]+_0xb3c56e[_0xf126('0x23')];var _0xe82625;if(_0x451211>=_0x2fa7e6){_0x451211=_0x2fa7e6;_0xe82625=0xc8;}else{_0xe82625=0xce;}_0x1d9e25[_0xf126('0x1e')](_0xe82625);return _0x1d9e25[_0xf126('0x25')](_0xf126('0x26'),_0x32c6b9+'-'+_0x451211+'/'+_0x2fa7e6)['json'](_0xc1f9ec);}return null;};}function patchUpdates(_0x39bbed){return function(_0x569fa9){try{jsonpatch[_0xf126('0x27')](_0x569fa9,_0x39bbed,!![]);}catch(_0x26eb26){return BPromise[_0xf126('0x28')](_0x26eb26);}return _0x569fa9['save']();};}function saveUpdates(_0x29d018,_0x726083){return function(_0x5044b4){if(_0x5044b4){return _0x5044b4[_0xf126('0x29')](_0x29d018)[_0xf126('0x2a')](function(_0x17b12a){return _0x17b12a;});}return null;};}function removeEntity(_0xc76220,_0x42d1b9){return function(_0xf4d040){if(_0xf4d040){return _0xf4d040[_0xf126('0x2b')]()[_0xf126('0x2a')](function(){var _0x1f0fb3=_0xf4d040[_0xf126('0x2c')]({'plain':!![]});var _0x3eef2c=_0xf126('0x2d');return db[_0xf126('0x2e')]['destroy']({'where':{'type':_0x3eef2c,'resourceId':_0x1f0fb3['id']}})[_0xf126('0x2a')](function(){return _0xf4d040;});})[_0xf126('0x2a')](function(){_0xc76220['status'](0xcc)[_0xf126('0x1f')]();});}};}function handleEntityNotFound(_0x1e38ca,_0xfea39d){return function(_0x4af0e8){if(!_0x4af0e8){_0x1e38ca['sendStatus'](0x194);}return _0x4af0e8;};}function handleError(_0x20719a,_0x2db676){_0x2db676=_0x2db676||0x1f4;return function(_0x16cf4e){logger[_0xf126('0x2f')](_0x16cf4e[_0xf126('0x30')]);if(_0x16cf4e[_0xf126('0x31')]){delete _0x16cf4e['name'];}_0x20719a['status'](_0x2db676)[_0xf126('0x32')](_0x16cf4e);};}exports[_0xf126('0x33')]=function(_0x520dd6,_0x2f2e3b){var _0x18b756={},_0x3da34b={},_0x7a7d20={'count':0x0,'rows':[]};var _0x4e0a43=_[_0xf126('0x34')](db[_0xf126('0x35')][_0xf126('0x36')],function(_0x8712f3){return{'name':_0x8712f3[_0xf126('0x37')],'type':_0x8712f3['type'][_0xf126('0x38')]};});_0x3da34b[_0xf126('0x39')]=_[_0xf126('0x34')](_0x4e0a43,_0xf126('0x31'));_0x3da34b['query']=_['keys'](_0x520dd6['query']);_0x3da34b[_0xf126('0x3a')]=_[_0xf126('0x3b')](_0x3da34b[_0xf126('0x39')],_0x3da34b[_0xf126('0x3c')]);_0x18b756[_0xf126('0x3d')]=_[_0xf126('0x3b')](_0x3da34b[_0xf126('0x39')],qs[_0xf126('0x3e')](_0x520dd6[_0xf126('0x3c')][_0xf126('0x3e')]));_0x18b756['attributes']=_0x18b756['attributes'][_0xf126('0x3f')]?_0x18b756['attributes']:_0x3da34b[_0xf126('0x39')];if(!_0x520dd6['query'][_0xf126('0x40')](_0xf126('0x41'))){_0x18b756['limit']=qs['limit'](_0x520dd6[_0xf126('0x3c')][_0xf126('0x23')]);_0x18b756[_0xf126('0x21')]=qs[_0xf126('0x21')](_0x520dd6[_0xf126('0x3c')]['offset']);}_0x18b756['order']=qs[_0xf126('0x42')](_0x520dd6[_0xf126('0x3c')]['sort']);_0x18b756[_0xf126('0x43')]=qs[_0xf126('0x3a')](_[_0xf126('0x44')](_0x520dd6[_0xf126('0x3c')],_0x3da34b['filters']),_0x4e0a43);if(_0x520dd6[_0xf126('0x3c')][_0xf126('0x45')]){_0x18b756[_0xf126('0x43')]=_[_0xf126('0x46')](_0x18b756['where'],{'$or':_[_0xf126('0x34')](_0x4e0a43,function(_0x589b77){if(_0x589b77[_0xf126('0x47')]!=='VIRTUAL'){var _0x330cbe={};_0x330cbe[_0x589b77[_0xf126('0x31')]]={'$like':'%'+_0x520dd6['query'][_0xf126('0x45')]+'%'};return _0x330cbe;}})});}_0x18b756=_[_0xf126('0x46')]({},_0x18b756,_0x520dd6[_0xf126('0x48')]);var _0x49471d={'where':_0x18b756[_0xf126('0x43')]};return db[_0xf126('0x35')][_0xf126('0x24')](_0x49471d)['then'](function(_0x1bbbf1){_0x7a7d20[_0xf126('0x24')]=_0x1bbbf1;if(_0x520dd6['query'][_0xf126('0x49')]){_0x18b756['include']=[{'all':!![]}];}return db[_0xf126('0x35')][_0xf126('0x4a')](_0x18b756);})[_0xf126('0x2a')](function(_0x58d137){_0x7a7d20[_0xf126('0x4b')]=_0x58d137;return _0x7a7d20;})[_0xf126('0x2a')](respondWithFilteredResult(_0x2f2e3b,_0x18b756))[_0xf126('0x4c')](handleError(_0x2f2e3b,null));};exports[_0xf126('0x4d')]=function(_0xa56354,_0x5b6cb3){var _0x57820c={'raw':![],'where':{'id':_0xa56354['params']['id']}},_0x43b876={};_0x43b876[_0xf126('0x39')]=_['keys'](db[_0xf126('0x35')][_0xf126('0x36')]);_0x43b876[_0xf126('0x3c')]=_[_0xf126('0x4e')](_0xa56354['query']);_0x43b876[_0xf126('0x3a')]=_[_0xf126('0x3b')](_0x43b876[_0xf126('0x39')],_0x43b876[_0xf126('0x3c')]);_0x57820c[_0xf126('0x3d')]=_[_0xf126('0x3b')](_0x43b876[_0xf126('0x39')],qs[_0xf126('0x3e')](_0xa56354[_0xf126('0x3c')][_0xf126('0x3e')]));_0x57820c['attributes']=_0x57820c['attributes'][_0xf126('0x3f')]?_0x57820c[_0xf126('0x3d')]:_0x43b876[_0xf126('0x39')];if(_0xa56354['query'][_0xf126('0x49')]){_0x57820c['include']=[{'all':!![]}];}_0x57820c=_['merge']({},_0x57820c,_0xa56354['options']);return db[_0xf126('0x35')][_0xf126('0x4f')](_0x57820c)[_0xf126('0x2a')](handleEntityNotFound(_0x5b6cb3,null))['then'](respondWithResult(_0x5b6cb3,null))['catch'](handleError(_0x5b6cb3,null));};exports[_0xf126('0x50')]=function(_0x4c1b8b,_0x2a9b3b){return db['SquareProject'][_0xf126('0x50')](_0x4c1b8b[_0xf126('0x51')],{})['then'](function(_0x2b0db8){var _0x89bef=_0x4c1b8b[_0xf126('0x52')][_0xf126('0x2c')]({'plain':!![]});if(!_0x89bef)throw new Error(_0xf126('0x53'));if(_0x89bef[_0xf126('0x54')]==='user'){var _0x4b9c7d=_0x2b0db8['get']({'plain':!![]});var _0x41b18e='SquareProjects';return db['UserProfileSection'][_0xf126('0x4f')]({'where':{'name':_0x41b18e,'userProfileId':_0x89bef[_0xf126('0x55')]},'raw':!![]})[_0xf126('0x2a')](function(_0x319cb0){if(_0x319cb0&&_0x319cb0[_0xf126('0x56')]===0x0){return db[_0xf126('0x2e')][_0xf126('0x50')]({'name':_0x4b9c7d['name'],'resourceId':_0x4b9c7d['id'],'type':_0x319cb0[_0xf126('0x31')],'sectionId':_0x319cb0['id']},{})['then'](function(){return _0x2b0db8;});}else{return _0x2b0db8;}})[_0xf126('0x4c')](function(_0x3f8b05){logger['error'](_0xf126('0x57'),_0x3f8b05);throw _0x3f8b05;});}return _0x2b0db8;})[_0xf126('0x2a')](respondWithResult(_0x2a9b3b,0xc9))[_0xf126('0x4c')](handleError(_0x2a9b3b,null));};exports[_0xf126('0x29')]=function(_0x2819f5,_0x545628){if(_0x2819f5[_0xf126('0x51')]['id']){delete _0x2819f5[_0xf126('0x51')]['id'];}return db[_0xf126('0x35')]['find']({'where':{'id':_0x2819f5['params']['id']}})['then'](handleEntityNotFound(_0x545628,null))[_0xf126('0x2a')](saveUpdates(_0x2819f5[_0xf126('0x51')],null))['then'](respondWithResult(_0x545628,null))[_0xf126('0x4c')](handleError(_0x545628,null));};exports[_0xf126('0x2b')]=function(_0x469b65,_0xf7a6cc){return db['SquareProject'][_0xf126('0x4f')]({'where':{'id':_0x469b65['params']['id']}})[_0xf126('0x2a')](handleEntityNotFound(_0xf7a6cc,null))[_0xf126('0x2a')](removeEntity(_0xf7a6cc,null))[_0xf126('0x4c')](handleError(_0xf7a6cc,null));};exports['addUsers']=function(_0x46ff26,_0x1077c7,_0x40c1f6){return db[_0xf126('0x35')]['find']({'where':{'id':_0x46ff26[_0xf126('0x58')]['id']}})[_0xf126('0x2a')](handleEntityNotFound(_0x1077c7,null))[_0xf126('0x2a')](function(_0x3780e0){if(_0x3780e0){return _0x3780e0[_0xf126('0x59')](_0x46ff26[_0xf126('0x51')]['ids'],_[_0xf126('0x5a')](_0x46ff26[_0xf126('0x51')],[_0xf126('0x5b'),'id'])||{});}})[_0xf126('0x2a')](respondWithResult(_0x1077c7,null))['catch'](handleError(_0x1077c7,null));};exports[_0xf126('0x5c')]=function(_0xcf00b,_0x4831fd,_0x5d988d){return db[_0xf126('0x35')]['find']({'where':{'id':_0xcf00b[_0xf126('0x58')]['id']}})[_0xf126('0x2a')](handleEntityNotFound(_0x4831fd,null))[_0xf126('0x2a')](function(_0x18090f){if(_0x18090f){return _0x18090f['removeUsers'](_0xcf00b['query'][_0xf126('0x5b')])['then'](function(){if(_[_0xf126('0x5d')](_0xcf00b[_0xf126('0x3c')][_0xf126('0x5b')])){for(var _0x5484e9=0x0;_0x5484e9<_0xcf00b[_0xf126('0x3c')][_0xf126('0x5b')][_0xf126('0x3f')];_0x5484e9+=0x1){socket['emit']('userSquareProject:remove',{'UserId':Number(_0xcf00b[_0xf126('0x3c')][_0xf126('0x5b')][_0x5484e9]),'SquareProjectId':Number(_0xcf00b[_0xf126('0x58')]['id'])});}}else{socket['emit'](_0xf126('0x5e'),{'UserId':Number(_0xcf00b[_0xf126('0x3c')][_0xf126('0x5b')]),'SquareProjectId':Number(_0xcf00b[_0xf126('0x58')]['id'])});}});}})[_0xf126('0x2a')](respondWithStatusCode(_0x4831fd,null))[_0xf126('0x4c')](handleError(_0x4831fd,null));};exports['getUsers']=function(_0x3df4fe,_0x73c4cc,_0x392c1c){var _0x5fcf95={};var _0x2fc398={};var _0x127f2b;var _0x2ef15a;return db[_0xf126('0x35')][_0xf126('0x5f')]({'where':{'id':_0x3df4fe[_0xf126('0x58')]['id']}})[_0xf126('0x2a')](handleEntityNotFound(_0x73c4cc,null))['then'](function(_0x661280){if(_0x661280){_0x127f2b=_0x661280;_0x2fc398[_0xf126('0x39')]=_['keys'](db[_0xf126('0x60')][_0xf126('0x36')]);_0x2fc398['query']=_[_0xf126('0x4e')](_0x3df4fe[_0xf126('0x3c')]);_0x2fc398[_0xf126('0x3a')]=_[_0xf126('0x3b')](_0x2fc398[_0xf126('0x39')],_0x2fc398[_0xf126('0x3c')]);_0x5fcf95[_0xf126('0x3d')]=_[_0xf126('0x3b')](_0x2fc398[_0xf126('0x39')],qs[_0xf126('0x3e')](_0x3df4fe[_0xf126('0x3c')][_0xf126('0x3e')]));_0x5fcf95[_0xf126('0x3d')]=_0x5fcf95[_0xf126('0x3d')][_0xf126('0x3f')]?_0x5fcf95[_0xf126('0x3d')]:_0x2fc398['model'];_0x5fcf95['order']=qs['sort'](_0x3df4fe['query'][_0xf126('0x42')]);_0x5fcf95[_0xf126('0x43')]=qs['filters'](_[_0xf126('0x44')](_0x3df4fe[_0xf126('0x3c')],_0x2fc398[_0xf126('0x3a')]));if(_0x3df4fe[_0xf126('0x3c')][_0xf126('0x45')]){_0x5fcf95[_0xf126('0x43')]=_[_0xf126('0x46')](_0x5fcf95[_0xf126('0x43')],{'$or':_[_0xf126('0x34')](_0x5fcf95[_0xf126('0x3d')],function(_0x3e3af2){var _0x371ca8={};_0x371ca8[_0x3e3af2]={'$like':'%'+_0x3df4fe[_0xf126('0x3c')][_0xf126('0x45')]+'%'};return _0x371ca8;})});}_0x5fcf95=_['merge']({},_0x5fcf95,_0x3df4fe['options']);return _0x127f2b[_0xf126('0x61')](_0x5fcf95);}})[_0xf126('0x2a')](function(_0x1ec082){if(_0x1ec082){_0x2ef15a=_0x1ec082[_0xf126('0x3f')];if(!_0x3df4fe[_0xf126('0x3c')]['hasOwnProperty'](_0xf126('0x41'))){_0x5fcf95[_0xf126('0x23')]=qs[_0xf126('0x23')](_0x3df4fe['query']['limit']);_0x5fcf95[_0xf126('0x21')]=qs[_0xf126('0x21')](_0x3df4fe[_0xf126('0x3c')][_0xf126('0x21')]);}return _0x127f2b[_0xf126('0x61')](_0x5fcf95);}})['then'](function(_0x27a73f){if(_0x27a73f){return _0x27a73f?{'count':_0x2ef15a,'rows':_0x27a73f}:null;}})[_0xf126('0x2a')](respondWithResult(_0x73c4cc,null))[_0xf126('0x4c')](handleError(_0x73c4cc,null));};