ba8b8c0f7ef2261c49dfad085d340fd92bfa21ec
[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 _0xb9a5=['role','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','addUsers','ids','isArray','emit','userSquareProject:remove','findOne','rawAttributes','getUsers','nolimit','rimraf','zip-dir','fast-json-patch','bluebird','mustache','util','path','to-csv','ejs','fs-extra','squel','crypto','jsforce','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','sendStatus','status','end','json','undefined','offset','limit','set','Content-Range','apply','reject','update','then','destroy','get','SquareProjects','error','name','send','index','SquareProject','fieldName','type','key','model','map','query','keys','filters','intersection','attributes','fields','order','sort','where','merge','VIRTUAL','filter','options','count','includeAll','findAll','catch','show','params','length','include','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user'];(function(_0x153ae1,_0x58e421){var _0x18550c=function(_0x31ca18){while(--_0x31ca18){_0x153ae1['push'](_0x153ae1['shift']());}};_0x18550c(++_0x58e421);}(_0xb9a5,0xc8));var _0x5b9a=function(_0x37c006,_0x1f647a){_0x37c006=_0x37c006-0x0;var _0x3f306e=_0xb9a5[_0x37c006];return _0x3f306e;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x5b9a('0x0'));var zipdir=require(_0x5b9a('0x1'));var jsonpatch=require(_0x5b9a('0x2'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x5b9a('0x3'));var Mustache=require(_0x5b9a('0x4'));var util=require(_0x5b9a('0x5'));var path=require(_0x5b9a('0x6'));var sox=require('sox');var csv=require(_0x5b9a('0x7'));var ejs=require(_0x5b9a('0x8'));var fs=require('fs');var fs_extra=require(_0x5b9a('0x9'));var _=require('lodash');var squel=require(_0x5b9a('0xa'));var crypto=require(_0x5b9a('0xb'));var jsforce=require(_0x5b9a('0xc'));var deskjs=require('desk.js');var toCsv=require(_0x5b9a('0x7'));var querystring=require('querystring');var Papa=require(_0x5b9a('0xd'));var Redis=require(_0x5b9a('0xe'));var authService=require(_0x5b9a('0xf'));var qs=require(_0x5b9a('0x10'));var as=require(_0x5b9a('0x11'));var hardwareService=require(_0x5b9a('0x12'));var logger=require(_0x5b9a('0x13'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x5b9a('0x14'));var db=require(_0x5b9a('0x15'))['db'];config[_0x5b9a('0x16')]=_[_0x5b9a('0x17')](config['redis'],{'host':_0x5b9a('0x18'),'port':0x18eb});var socket=require(_0x5b9a('0x19'))(new Redis(config['redis']));require('./squareProject.socket')[_0x5b9a('0x1a')](socket);function respondWithStatusCode(_0x1a1e1e,_0x595eeb){_0x595eeb=_0x595eeb||0xcc;return function(_0x11f85a){if(_0x11f85a){return _0x1a1e1e[_0x5b9a('0x1b')](_0x595eeb);}return _0x1a1e1e[_0x5b9a('0x1c')](_0x595eeb)[_0x5b9a('0x1d')]();};}function respondWithResult(_0x495aa1,_0x2718da){_0x2718da=_0x2718da||0xc8;return function(_0x5891e1){if(_0x5891e1){return _0x495aa1[_0x5b9a('0x1c')](_0x2718da)[_0x5b9a('0x1e')](_0x5891e1);}};}function respondWithFilteredResult(_0x48541b,_0x48c817){return function(_0x307940){if(_0x307940){var _0x22a05b=typeof _0x48c817['offset']==='undefined'&&typeof _0x48c817['limit']===_0x5b9a('0x1f');var _0x16d551=_0x307940['count'];var _0x40474d=_0x22a05b?0x0:_0x48c817[_0x5b9a('0x20')];var _0x47a186=_0x22a05b?_0x307940['count']:_0x48c817[_0x5b9a('0x20')]+_0x48c817[_0x5b9a('0x21')];var _0x13a80d;if(_0x47a186>=_0x16d551){_0x47a186=_0x16d551;_0x13a80d=0xc8;}else{_0x13a80d=0xce;}_0x48541b[_0x5b9a('0x1c')](_0x13a80d);return _0x48541b[_0x5b9a('0x22')](_0x5b9a('0x23'),_0x40474d+'-'+_0x47a186+'/'+_0x16d551)[_0x5b9a('0x1e')](_0x307940);}return null;};}function patchUpdates(_0x5e6038){return function(_0x2fad17){try{jsonpatch[_0x5b9a('0x24')](_0x2fad17,_0x5e6038,!![]);}catch(_0xfe7387){return BPromise[_0x5b9a('0x25')](_0xfe7387);}return _0x2fad17['save']();};}function saveUpdates(_0x57bba6,_0x4c92d5){return function(_0xe086ba){if(_0xe086ba){return _0xe086ba[_0x5b9a('0x26')](_0x57bba6)[_0x5b9a('0x27')](function(_0x1c8124){return _0x1c8124;});}return null;};}function removeEntity(_0x52263a,_0x576d50){return function(_0x1c5256){if(_0x1c5256){return _0x1c5256[_0x5b9a('0x28')]()[_0x5b9a('0x27')](function(){var _0x7240ba=_0x1c5256[_0x5b9a('0x29')]({'plain':!![]});var _0x463dde=_0x5b9a('0x2a');return db['UserProfileResource']['destroy']({'where':{'type':_0x463dde,'resourceId':_0x7240ba['id']}})[_0x5b9a('0x27')](function(){return _0x1c5256;});})[_0x5b9a('0x27')](function(){_0x52263a[_0x5b9a('0x1c')](0xcc)[_0x5b9a('0x1d')]();});}};}function handleEntityNotFound(_0x1bccc8,_0x15280b){return function(_0x3e9833){if(!_0x3e9833){_0x1bccc8[_0x5b9a('0x1b')](0x194);}return _0x3e9833;};}function handleError(_0x64b0a,_0x5833d7){_0x5833d7=_0x5833d7||0x1f4;return function(_0x9ab86c){logger[_0x5b9a('0x2b')](_0x9ab86c['stack']);if(_0x9ab86c['name']){delete _0x9ab86c[_0x5b9a('0x2c')];}_0x64b0a[_0x5b9a('0x1c')](_0x5833d7)[_0x5b9a('0x2d')](_0x9ab86c);};}exports[_0x5b9a('0x2e')]=function(_0x4bd963,_0x3bf926){var _0x520edd={},_0x6fcbc7={},_0xfbc798={'count':0x0,'rows':[]};var _0x4519da=_['map'](db[_0x5b9a('0x2f')]['rawAttributes'],function(_0x2f1e69){return{'name':_0x2f1e69[_0x5b9a('0x30')],'type':_0x2f1e69[_0x5b9a('0x31')][_0x5b9a('0x32')]};});_0x6fcbc7[_0x5b9a('0x33')]=_[_0x5b9a('0x34')](_0x4519da,'name');_0x6fcbc7[_0x5b9a('0x35')]=_[_0x5b9a('0x36')](_0x4bd963[_0x5b9a('0x35')]);_0x6fcbc7[_0x5b9a('0x37')]=_[_0x5b9a('0x38')](_0x6fcbc7[_0x5b9a('0x33')],_0x6fcbc7[_0x5b9a('0x35')]);_0x520edd[_0x5b9a('0x39')]=_[_0x5b9a('0x38')](_0x6fcbc7[_0x5b9a('0x33')],qs[_0x5b9a('0x3a')](_0x4bd963['query'][_0x5b9a('0x3a')]));_0x520edd[_0x5b9a('0x39')]=_0x520edd['attributes']['length']?_0x520edd[_0x5b9a('0x39')]:_0x6fcbc7[_0x5b9a('0x33')];if(!_0x4bd963['query']['hasOwnProperty']('nolimit')){_0x520edd[_0x5b9a('0x21')]=qs['limit'](_0x4bd963['query'][_0x5b9a('0x21')]);_0x520edd[_0x5b9a('0x20')]=qs['offset'](_0x4bd963[_0x5b9a('0x35')][_0x5b9a('0x20')]);}_0x520edd[_0x5b9a('0x3b')]=qs['sort'](_0x4bd963['query'][_0x5b9a('0x3c')]);_0x520edd[_0x5b9a('0x3d')]=qs[_0x5b9a('0x37')](_['pick'](_0x4bd963['query'],_0x6fcbc7[_0x5b9a('0x37')]),_0x4519da);if(_0x4bd963[_0x5b9a('0x35')]['filter']){_0x520edd[_0x5b9a('0x3d')]=_[_0x5b9a('0x3e')](_0x520edd[_0x5b9a('0x3d')],{'$or':_[_0x5b9a('0x34')](_0x4519da,function(_0x4ef105){if(_0x4ef105['type']!==_0x5b9a('0x3f')){var _0xdaeb3d={};_0xdaeb3d[_0x4ef105[_0x5b9a('0x2c')]]={'$like':'%'+_0x4bd963[_0x5b9a('0x35')][_0x5b9a('0x40')]+'%'};return _0xdaeb3d;}})});}_0x520edd=_['merge']({},_0x520edd,_0x4bd963[_0x5b9a('0x41')]);var _0x1ae901={'where':_0x520edd[_0x5b9a('0x3d')]};return db[_0x5b9a('0x2f')][_0x5b9a('0x42')](_0x1ae901)[_0x5b9a('0x27')](function(_0x3e9488){_0xfbc798[_0x5b9a('0x42')]=_0x3e9488;if(_0x4bd963['query'][_0x5b9a('0x43')]){_0x520edd['include']=[{'all':!![]}];}return db[_0x5b9a('0x2f')][_0x5b9a('0x44')](_0x520edd);})[_0x5b9a('0x27')](function(_0x40492e){_0xfbc798['rows']=_0x40492e;return _0xfbc798;})['then'](respondWithFilteredResult(_0x3bf926,_0x520edd))[_0x5b9a('0x45')](handleError(_0x3bf926,null));};exports[_0x5b9a('0x46')]=function(_0x411461,_0x2ba675){var _0x1029cb={'raw':![],'where':{'id':_0x411461[_0x5b9a('0x47')]['id']}},_0x2db837={};_0x2db837[_0x5b9a('0x33')]=_['keys'](db['SquareProject']['rawAttributes']);_0x2db837['query']=_['keys'](_0x411461['query']);_0x2db837[_0x5b9a('0x37')]=_[_0x5b9a('0x38')](_0x2db837[_0x5b9a('0x33')],_0x2db837[_0x5b9a('0x35')]);_0x1029cb[_0x5b9a('0x39')]=_[_0x5b9a('0x38')](_0x2db837[_0x5b9a('0x33')],qs[_0x5b9a('0x3a')](_0x411461[_0x5b9a('0x35')][_0x5b9a('0x3a')]));_0x1029cb['attributes']=_0x1029cb[_0x5b9a('0x39')][_0x5b9a('0x48')]?_0x1029cb[_0x5b9a('0x39')]:_0x2db837[_0x5b9a('0x33')];if(_0x411461['query'][_0x5b9a('0x43')]){_0x1029cb[_0x5b9a('0x49')]=[{'all':!![]}];}_0x1029cb=_[_0x5b9a('0x3e')]({},_0x1029cb,_0x411461[_0x5b9a('0x41')]);return db[_0x5b9a('0x2f')][_0x5b9a('0x4a')](_0x1029cb)[_0x5b9a('0x27')](handleEntityNotFound(_0x2ba675,null))['then'](respondWithResult(_0x2ba675,null))[_0x5b9a('0x45')](handleError(_0x2ba675,null));};exports[_0x5b9a('0x4b')]=function(_0x360139,_0x4ee1f1){return db['SquareProject'][_0x5b9a('0x4b')](_0x360139[_0x5b9a('0x4c')],{})['then'](function(_0x1f4b89){var _0x5c2f72=_0x360139[_0x5b9a('0x4d')][_0x5b9a('0x29')]({'plain':!![]});if(!_0x5c2f72)throw new Error(_0x5b9a('0x4e'));if(_0x5c2f72[_0x5b9a('0x4f')]===_0x5b9a('0x4d')){var _0x1b7270=_0x1f4b89[_0x5b9a('0x29')]({'plain':!![]});var _0x465ee4='SquareProjects';return db['UserProfileSection'][_0x5b9a('0x4a')]({'where':{'name':_0x465ee4,'userProfileId':_0x5c2f72[_0x5b9a('0x50')]},'raw':!![]})[_0x5b9a('0x27')](function(_0x7e437b){if(_0x7e437b&&_0x7e437b[_0x5b9a('0x51')]===0x0){return db[_0x5b9a('0x52')][_0x5b9a('0x4b')]({'name':_0x1b7270[_0x5b9a('0x2c')],'resourceId':_0x1b7270['id'],'type':_0x7e437b[_0x5b9a('0x2c')],'sectionId':_0x7e437b['id']},{})[_0x5b9a('0x27')](function(){return _0x1f4b89;});}else{return _0x1f4b89;}})['catch'](function(_0x269fc8){logger[_0x5b9a('0x2b')](_0x5b9a('0x53'),_0x269fc8);throw _0x269fc8;});}return _0x1f4b89;})['then'](respondWithResult(_0x4ee1f1,0xc9))['catch'](handleError(_0x4ee1f1,null));};exports[_0x5b9a('0x26')]=function(_0x974c3e,_0x206540){if(_0x974c3e['body']['id']){delete _0x974c3e[_0x5b9a('0x4c')]['id'];}return db[_0x5b9a('0x2f')]['find']({'where':{'id':_0x974c3e[_0x5b9a('0x47')]['id']}})[_0x5b9a('0x27')](handleEntityNotFound(_0x206540,null))['then'](saveUpdates(_0x974c3e[_0x5b9a('0x4c')],null))[_0x5b9a('0x27')](respondWithResult(_0x206540,null))[_0x5b9a('0x45')](handleError(_0x206540,null));};exports[_0x5b9a('0x28')]=function(_0x403870,_0x169f55){return db[_0x5b9a('0x2f')][_0x5b9a('0x4a')]({'where':{'id':_0x403870[_0x5b9a('0x47')]['id']}})['then'](handleEntityNotFound(_0x169f55,null))[_0x5b9a('0x27')](removeEntity(_0x169f55,null))['catch'](handleError(_0x169f55,null));};exports[_0x5b9a('0x54')]=function(_0x11572d,_0x142ca2,_0xb58354){return db['SquareProject']['find']({'where':{'id':_0x11572d[_0x5b9a('0x47')]['id']}})[_0x5b9a('0x27')](handleEntityNotFound(_0x142ca2,null))['then'](function(_0x1b0598){if(_0x1b0598){return _0x1b0598[_0x5b9a('0x54')](_0x11572d[_0x5b9a('0x4c')][_0x5b9a('0x55')],_['omit'](_0x11572d['body'],[_0x5b9a('0x55'),'id'])||{});}})[_0x5b9a('0x27')](respondWithResult(_0x142ca2,null))[_0x5b9a('0x45')](handleError(_0x142ca2,null));};exports['removeUsers']=function(_0x5bb77d,_0x9d54cc,_0x39dae5){return db[_0x5b9a('0x2f')][_0x5b9a('0x4a')]({'where':{'id':_0x5bb77d['params']['id']}})['then'](handleEntityNotFound(_0x9d54cc,null))['then'](function(_0x2a16af){if(_0x2a16af){return _0x2a16af['removeUsers'](_0x5bb77d['query'][_0x5b9a('0x55')])[_0x5b9a('0x27')](function(){if(_[_0x5b9a('0x56')](_0x5bb77d[_0x5b9a('0x35')][_0x5b9a('0x55')])){for(var _0x3172c3=0x0;_0x3172c3<_0x5bb77d[_0x5b9a('0x35')]['ids'][_0x5b9a('0x48')];_0x3172c3+=0x1){socket[_0x5b9a('0x57')](_0x5b9a('0x58'),{'UserId':Number(_0x5bb77d['query'][_0x5b9a('0x55')][_0x3172c3]),'SquareProjectId':Number(_0x5bb77d[_0x5b9a('0x47')]['id'])});}}else{socket[_0x5b9a('0x57')](_0x5b9a('0x58'),{'UserId':Number(_0x5bb77d[_0x5b9a('0x35')]['ids']),'SquareProjectId':Number(_0x5bb77d[_0x5b9a('0x47')]['id'])});}});}})[_0x5b9a('0x27')](respondWithStatusCode(_0x9d54cc,null))[_0x5b9a('0x45')](handleError(_0x9d54cc,null));};exports['getUsers']=function(_0x566a7b,_0xd8bfc3,_0x361b8e){var _0x4ec2cc={};var _0x5804ff={};var _0x404fd2;var _0x2717a6;return db[_0x5b9a('0x2f')][_0x5b9a('0x59')]({'where':{'id':_0x566a7b[_0x5b9a('0x47')]['id']}})[_0x5b9a('0x27')](handleEntityNotFound(_0xd8bfc3,null))['then'](function(_0x520f1a){if(_0x520f1a){_0x404fd2=_0x520f1a;_0x5804ff[_0x5b9a('0x33')]=_[_0x5b9a('0x36')](db['User'][_0x5b9a('0x5a')]);_0x5804ff[_0x5b9a('0x35')]=_[_0x5b9a('0x36')](_0x566a7b['query']);_0x5804ff[_0x5b9a('0x37')]=_[_0x5b9a('0x38')](_0x5804ff[_0x5b9a('0x33')],_0x5804ff[_0x5b9a('0x35')]);_0x4ec2cc[_0x5b9a('0x39')]=_[_0x5b9a('0x38')](_0x5804ff[_0x5b9a('0x33')],qs[_0x5b9a('0x3a')](_0x566a7b[_0x5b9a('0x35')]['fields']));_0x4ec2cc[_0x5b9a('0x39')]=_0x4ec2cc[_0x5b9a('0x39')]['length']?_0x4ec2cc[_0x5b9a('0x39')]:_0x5804ff[_0x5b9a('0x33')];_0x4ec2cc[_0x5b9a('0x3b')]=qs[_0x5b9a('0x3c')](_0x566a7b['query'][_0x5b9a('0x3c')]);_0x4ec2cc[_0x5b9a('0x3d')]=qs[_0x5b9a('0x37')](_['pick'](_0x566a7b[_0x5b9a('0x35')],_0x5804ff[_0x5b9a('0x37')]));if(_0x566a7b[_0x5b9a('0x35')][_0x5b9a('0x40')]){_0x4ec2cc['where']=_[_0x5b9a('0x3e')](_0x4ec2cc[_0x5b9a('0x3d')],{'$or':_[_0x5b9a('0x34')](_0x4ec2cc[_0x5b9a('0x39')],function(_0x6c06e8){var _0x2e6a29={};_0x2e6a29[_0x6c06e8]={'$like':'%'+_0x566a7b['query'][_0x5b9a('0x40')]+'%'};return _0x2e6a29;})});}_0x4ec2cc=_[_0x5b9a('0x3e')]({},_0x4ec2cc,_0x566a7b['options']);return _0x404fd2[_0x5b9a('0x5b')](_0x4ec2cc);}})[_0x5b9a('0x27')](function(_0x35137b){if(_0x35137b){_0x2717a6=_0x35137b[_0x5b9a('0x48')];if(!_0x566a7b[_0x5b9a('0x35')]['hasOwnProperty'](_0x5b9a('0x5c'))){_0x4ec2cc[_0x5b9a('0x21')]=qs[_0x5b9a('0x21')](_0x566a7b[_0x5b9a('0x35')]['limit']);_0x4ec2cc['offset']=qs[_0x5b9a('0x20')](_0x566a7b[_0x5b9a('0x35')][_0x5b9a('0x20')]);}return _0x404fd2[_0x5b9a('0x5b')](_0x4ec2cc);}})[_0x5b9a('0x27')](function(_0x9bc6ff){if(_0x9bc6ff){return _0x9bc6ff?{'count':_0x2717a6,'rows':_0x9bc6ff}:null;}})['then'](respondWithResult(_0xd8bfc3,null))[_0x5b9a('0x45')](handleError(_0xd8bfc3,null));};