af519bb7fa07bc4c196bd557b20a98fb4c7e76f9
[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 _0xccc1=['../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','defaults','socket.io-emitter','redis','./squareProject.socket','register','sendStatus','status','end','json','count','offset','limit','set','Content-Range','apply','save','then','get','SquareProjects','UserProfileResource','destroy','error','stack','send','index','map','rawAttributes','fieldName','type','key','model','name','query','filters','intersection','attributes','fields','hasOwnProperty','order','sort','pick','where','VIRTUAL','filter','merge','options','SquareProject','includeAll','include','findAll','catch','show','params','keys','find','create','body','user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','addUsers','ids','removeUsers','isArray','length','userSquareProject:remove','emit','getUsers','findOne','nolimit','html-pdf','eml-format','zip-dir','fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','crypto','jsforce','desk.js','querystring','papaparse','../../components/parsers/qs'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0xccc1,0x119));var _0x1ccc=function(_0xa3ff70,_0x4a8609){_0xa3ff70=_0xa3ff70-0x0;var _0x538dbb=_0xccc1[_0xa3ff70];return _0x538dbb;};'use strict';var pdf=require(_0x1ccc('0x0'));var emlformat=require(_0x1ccc('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x1ccc('0x2'));var jsonpatch=require(_0x1ccc('0x3'));var rp=require(_0x1ccc('0x4'));var moment=require(_0x1ccc('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x1ccc('0x6'));var util=require(_0x1ccc('0x7'));var path=require(_0x1ccc('0x8'));var sox=require(_0x1ccc('0x9'));var csv=require(_0x1ccc('0xa'));var ejs=require(_0x1ccc('0xb'));var fs=require('fs');var _=require(_0x1ccc('0xc'));var squel=require('squel');var crypto=require(_0x1ccc('0xd'));var jsforce=require(_0x1ccc('0xe'));var deskjs=require(_0x1ccc('0xf'));var toCsv=require('to-csv');var querystring=require(_0x1ccc('0x10'));var Papa=require(_0x1ccc('0x11'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x1ccc('0x12'));var as=require(_0x1ccc('0x13'));var hardwareService=require(_0x1ccc('0x14'));var logger=require(_0x1ccc('0x15'))(_0x1ccc('0x16'));var utils=require('../../config/utils');var config=require(_0x1ccc('0x17'));var licenseUtil=require(_0x1ccc('0x18'));var db=require(_0x1ccc('0x19'))['db'];config['redis']=_[_0x1ccc('0x1a')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0x1ccc('0x1b'))(new Redis(config[_0x1ccc('0x1c')]));require(_0x1ccc('0x1d'))[_0x1ccc('0x1e')](socket);function respondWithStatusCode(_0x1e0240,_0x3daf9a){_0x3daf9a=_0x3daf9a||0xcc;return function(_0x498a1c){if(_0x498a1c){return _0x1e0240[_0x1ccc('0x1f')](_0x3daf9a);}return _0x1e0240[_0x1ccc('0x20')](_0x3daf9a)[_0x1ccc('0x21')]();};}function respondWithResult(_0x1c786e,_0x5a4479){_0x5a4479=_0x5a4479||0xc8;return function(_0x8c29ec){if(_0x8c29ec){return _0x1c786e[_0x1ccc('0x20')](_0x5a4479)[_0x1ccc('0x22')](_0x8c29ec);}};}function respondWithFilteredResult(_0x1d4329,_0x1a07f5){return function(_0x11127c){if(_0x11127c){var _0xe2605b=_0x11127c[_0x1ccc('0x23')],_0xabe01a=_0x1a07f5[_0x1ccc('0x24')],_0x529ce6=_0x1a07f5[_0x1ccc('0x24')]+_0x1a07f5[_0x1ccc('0x25')],_0x3a8c0b;if(_0x529ce6>=_0xe2605b){_0x529ce6=_0xe2605b;_0x3a8c0b=0xc8;}else{_0x3a8c0b=0xce;}_0x1d4329['status'](_0x3a8c0b);return _0x1d4329[_0x1ccc('0x26')](_0x1ccc('0x27'),_0xabe01a+'-'+_0x529ce6+'/'+_0xe2605b)[_0x1ccc('0x22')](_0x11127c);}return null;};}function patchUpdates(_0x841647){return function(_0x52493c){try{jsonpatch[_0x1ccc('0x28')](_0x52493c,_0x841647,!![]);}catch(_0x1221da){return BPromise['reject'](_0x1221da);}return _0x52493c[_0x1ccc('0x29')]();};}function saveUpdates(_0x46e161,_0x8d354){return function(_0x1e8cfd){if(_0x1e8cfd){return _0x1e8cfd['update'](_0x46e161)[_0x1ccc('0x2a')](function(_0x110b13){return _0x110b13;});}return null;};}function removeEntity(_0x57d064,_0x276491){return function(_0x141dbf){if(_0x141dbf){return _0x141dbf['destroy']()['then'](function(){var _0x3f7b3c=_0x141dbf[_0x1ccc('0x2b')]({'plain':!![]});var _0x5a1df6=_0x1ccc('0x2c');return db[_0x1ccc('0x2d')][_0x1ccc('0x2e')]({'where':{'type':_0x5a1df6,'resourceId':_0x3f7b3c['id']}})['then'](function(){return _0x141dbf;});})['then'](function(){_0x57d064[_0x1ccc('0x20')](0xcc)[_0x1ccc('0x21')]();});}};}function handleEntityNotFound(_0x4b45f6,_0x254c90){return function(_0x2c10be){if(!_0x2c10be){_0x4b45f6['sendStatus'](0x194);}return _0x2c10be;};}function handleError(_0x150196,_0x1d3c04){_0x1d3c04=_0x1d3c04||0x1f4;return function(_0x26bc57){logger[_0x1ccc('0x2f')](_0x26bc57[_0x1ccc('0x30')]);if(_0x26bc57['name']){delete _0x26bc57['name'];}_0x150196[_0x1ccc('0x20')](_0x1d3c04)[_0x1ccc('0x31')](_0x26bc57);};}exports[_0x1ccc('0x32')]=function(_0xe9e7f0,_0x502b15){var _0x2167b8={},_0x1be0c9={},_0x3d3c4b={'count':0x0,'rows':[]};var _0xac3262=_[_0x1ccc('0x33')](db['SquareProject'][_0x1ccc('0x34')],function(_0x217016){return{'name':_0x217016[_0x1ccc('0x35')],'type':_0x217016[_0x1ccc('0x36')][_0x1ccc('0x37')]};});_0x1be0c9[_0x1ccc('0x38')]=_['map'](_0xac3262,_0x1ccc('0x39'));_0x1be0c9[_0x1ccc('0x3a')]=_['keys'](_0xe9e7f0[_0x1ccc('0x3a')]);_0x1be0c9[_0x1ccc('0x3b')]=_[_0x1ccc('0x3c')](_0x1be0c9[_0x1ccc('0x38')],_0x1be0c9[_0x1ccc('0x3a')]);_0x2167b8[_0x1ccc('0x3d')]=_[_0x1ccc('0x3c')](_0x1be0c9['model'],qs[_0x1ccc('0x3e')](_0xe9e7f0[_0x1ccc('0x3a')][_0x1ccc('0x3e')]));_0x2167b8[_0x1ccc('0x3d')]=_0x2167b8['attributes']['length']?_0x2167b8[_0x1ccc('0x3d')]:_0x1be0c9[_0x1ccc('0x38')];if(!_0xe9e7f0[_0x1ccc('0x3a')][_0x1ccc('0x3f')]('nolimit')){_0x2167b8[_0x1ccc('0x25')]=qs['limit'](_0xe9e7f0[_0x1ccc('0x3a')][_0x1ccc('0x25')]);_0x2167b8['offset']=qs[_0x1ccc('0x24')](_0xe9e7f0['query'][_0x1ccc('0x24')]);}_0x2167b8[_0x1ccc('0x40')]=qs[_0x1ccc('0x41')](_0xe9e7f0[_0x1ccc('0x3a')][_0x1ccc('0x41')]);_0x2167b8['where']=qs['filters'](_[_0x1ccc('0x42')](_0xe9e7f0[_0x1ccc('0x3a')],_0x1be0c9[_0x1ccc('0x3b')]),_0xac3262);if(_0xe9e7f0['query']['filter']){_0x2167b8['where']=_['merge'](_0x2167b8[_0x1ccc('0x43')],{'$or':_[_0x1ccc('0x33')](_0xac3262,function(_0x46ddd6){if(_0x46ddd6['type']!==_0x1ccc('0x44')){var _0x2e386f={};_0x2e386f[_0x46ddd6['name']]={'$like':'%'+_0xe9e7f0[_0x1ccc('0x3a')][_0x1ccc('0x45')]+'%'};return _0x2e386f;}})});}_0x2167b8=_[_0x1ccc('0x46')]({},_0x2167b8,_0xe9e7f0[_0x1ccc('0x47')]);var _0x5724b5={'where':_0x2167b8[_0x1ccc('0x43')]};return db[_0x1ccc('0x48')]['count'](_0x5724b5)[_0x1ccc('0x2a')](function(_0x95d7ed){_0x3d3c4b[_0x1ccc('0x23')]=_0x95d7ed;if(_0xe9e7f0[_0x1ccc('0x3a')][_0x1ccc('0x49')]){_0x2167b8[_0x1ccc('0x4a')]=[{'all':!![]}];}return db[_0x1ccc('0x48')][_0x1ccc('0x4b')](_0x2167b8);})[_0x1ccc('0x2a')](function(_0x5273bb){_0x3d3c4b['rows']=_0x5273bb;return _0x3d3c4b;})[_0x1ccc('0x2a')](respondWithFilteredResult(_0x502b15,_0x2167b8))[_0x1ccc('0x4c')](handleError(_0x502b15,null));};exports[_0x1ccc('0x4d')]=function(_0x39f5b1,_0x534042){var _0x20271e={'raw':![],'where':{'id':_0x39f5b1[_0x1ccc('0x4e')]['id']}},_0x34e0b8={};_0x34e0b8['model']=_[_0x1ccc('0x4f')](db['SquareProject'][_0x1ccc('0x34')]);_0x34e0b8[_0x1ccc('0x3a')]=_[_0x1ccc('0x4f')](_0x39f5b1[_0x1ccc('0x3a')]);_0x34e0b8[_0x1ccc('0x3b')]=_[_0x1ccc('0x3c')](_0x34e0b8[_0x1ccc('0x38')],_0x34e0b8[_0x1ccc('0x3a')]);_0x20271e[_0x1ccc('0x3d')]=_[_0x1ccc('0x3c')](_0x34e0b8[_0x1ccc('0x38')],qs['fields'](_0x39f5b1['query']['fields']));_0x20271e[_0x1ccc('0x3d')]=_0x20271e['attributes']['length']?_0x20271e[_0x1ccc('0x3d')]:_0x34e0b8[_0x1ccc('0x38')];if(_0x39f5b1['query'][_0x1ccc('0x49')]){_0x20271e[_0x1ccc('0x4a')]=[{'all':!![]}];}_0x20271e=_[_0x1ccc('0x46')]({},_0x20271e,_0x39f5b1['options']);return db[_0x1ccc('0x48')][_0x1ccc('0x50')](_0x20271e)[_0x1ccc('0x2a')](handleEntityNotFound(_0x534042,null))[_0x1ccc('0x2a')](respondWithResult(_0x534042,null))['catch'](handleError(_0x534042,null));};exports[_0x1ccc('0x51')]=function(_0x5b2394,_0x22ef07){return db['SquareProject'][_0x1ccc('0x51')](_0x5b2394[_0x1ccc('0x52')],{})[_0x1ccc('0x2a')](function(_0x4d616d){var _0x1fd772=_0x5b2394[_0x1ccc('0x53')][_0x1ccc('0x2b')]({'plain':!![]});if(!_0x1fd772)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x1fd772[_0x1ccc('0x54')]===_0x1ccc('0x53')){var _0x5570b1=_0x4d616d[_0x1ccc('0x2b')]({'plain':!![]});var _0x633af9=_0x1ccc('0x2c');return db[_0x1ccc('0x55')]['find']({'where':{'name':_0x633af9,'userProfileId':_0x1fd772[_0x1ccc('0x56')]},'raw':!![]})[_0x1ccc('0x2a')](function(_0x12c6d4){if(_0x12c6d4&&_0x12c6d4[_0x1ccc('0x57')]===0x0){return db['UserProfileResource']['create']({'name':_0x5570b1[_0x1ccc('0x39')],'resourceId':_0x5570b1['id'],'type':_0x12c6d4[_0x1ccc('0x39')],'sectionId':_0x12c6d4['id']},{})[_0x1ccc('0x2a')](function(){return _0x4d616d;});}else{return _0x4d616d;}})['catch'](function(_0x563b4e){logger[_0x1ccc('0x2f')](_0x1ccc('0x58'),_0x563b4e);throw _0x563b4e;});}return _0x4d616d;})[_0x1ccc('0x2a')](respondWithResult(_0x22ef07,0xc9))[_0x1ccc('0x4c')](handleError(_0x22ef07,null));};exports[_0x1ccc('0x59')]=function(_0x237e97,_0x36d13c){if(_0x237e97[_0x1ccc('0x52')]['id']){delete _0x237e97[_0x1ccc('0x52')]['id'];}return db[_0x1ccc('0x48')]['find']({'where':{'id':_0x237e97['params']['id']}})[_0x1ccc('0x2a')](handleEntityNotFound(_0x36d13c,null))[_0x1ccc('0x2a')](saveUpdates(_0x237e97['body'],null))[_0x1ccc('0x2a')](respondWithResult(_0x36d13c,null))[_0x1ccc('0x4c')](handleError(_0x36d13c,null));};exports[_0x1ccc('0x2e')]=function(_0x1a63c9,_0x263730){return db[_0x1ccc('0x48')][_0x1ccc('0x50')]({'where':{'id':_0x1a63c9[_0x1ccc('0x4e')]['id']}})[_0x1ccc('0x2a')](handleEntityNotFound(_0x263730,null))[_0x1ccc('0x2a')](removeEntity(_0x263730,null))[_0x1ccc('0x4c')](handleError(_0x263730,null));};exports[_0x1ccc('0x5a')]=function(_0x1224c8,_0x3a6197,_0xf0fe88){return db[_0x1ccc('0x48')][_0x1ccc('0x50')]({'where':{'id':_0x1224c8['params']['id']}})[_0x1ccc('0x2a')](handleEntityNotFound(_0x3a6197,null))[_0x1ccc('0x2a')](function(_0x3c0be2){if(_0x3c0be2){return _0x3c0be2[_0x1ccc('0x5a')](_0x1224c8['body'][_0x1ccc('0x5b')],_['omit'](_0x1224c8[_0x1ccc('0x52')],[_0x1ccc('0x5b'),'id'])||{});}})[_0x1ccc('0x2a')](respondWithResult(_0x3a6197,null))[_0x1ccc('0x4c')](handleError(_0x3a6197,null));};exports[_0x1ccc('0x5c')]=function(_0x4ebf68,_0x402902,_0x179d89){return db[_0x1ccc('0x48')][_0x1ccc('0x50')]({'where':{'id':_0x4ebf68[_0x1ccc('0x4e')]['id']}})[_0x1ccc('0x2a')](handleEntityNotFound(_0x402902,null))[_0x1ccc('0x2a')](function(_0x34f3eb){if(_0x34f3eb){return _0x34f3eb[_0x1ccc('0x5c')](_0x4ebf68[_0x1ccc('0x3a')][_0x1ccc('0x5b')])[_0x1ccc('0x2a')](function(){if(_[_0x1ccc('0x5d')](_0x4ebf68[_0x1ccc('0x3a')][_0x1ccc('0x5b')])){for(var _0x3ec7ee=0x0;_0x3ec7ee<_0x4ebf68[_0x1ccc('0x3a')][_0x1ccc('0x5b')][_0x1ccc('0x5e')];_0x3ec7ee+=0x1){socket['emit'](_0x1ccc('0x5f'),{'UserId':Number(_0x4ebf68[_0x1ccc('0x3a')][_0x1ccc('0x5b')][_0x3ec7ee]),'SquareProjectId':Number(_0x4ebf68[_0x1ccc('0x4e')]['id'])});}}else{socket[_0x1ccc('0x60')](_0x1ccc('0x5f'),{'UserId':Number(_0x4ebf68[_0x1ccc('0x3a')]['ids']),'SquareProjectId':Number(_0x4ebf68[_0x1ccc('0x4e')]['id'])});}});}})['then'](respondWithStatusCode(_0x402902,null))[_0x1ccc('0x4c')](handleError(_0x402902,null));};exports[_0x1ccc('0x61')]=function(_0x191802,_0x5c3053,_0x553d69){var _0x1cf16a={};var _0x2d8e56={};var _0x32d6e2;var _0x137c10;return db['SquareProject'][_0x1ccc('0x62')]({'where':{'id':_0x191802[_0x1ccc('0x4e')]['id']}})[_0x1ccc('0x2a')](handleEntityNotFound(_0x5c3053,null))[_0x1ccc('0x2a')](function(_0x393739){if(_0x393739){_0x32d6e2=_0x393739;_0x2d8e56['model']=_[_0x1ccc('0x4f')](db['User'][_0x1ccc('0x34')]);_0x2d8e56['query']=_[_0x1ccc('0x4f')](_0x191802[_0x1ccc('0x3a')]);_0x2d8e56[_0x1ccc('0x3b')]=_[_0x1ccc('0x3c')](_0x2d8e56[_0x1ccc('0x38')],_0x2d8e56['query']);_0x1cf16a[_0x1ccc('0x3d')]=_[_0x1ccc('0x3c')](_0x2d8e56[_0x1ccc('0x38')],qs['fields'](_0x191802[_0x1ccc('0x3a')]['fields']));_0x1cf16a[_0x1ccc('0x3d')]=_0x1cf16a['attributes']['length']?_0x1cf16a[_0x1ccc('0x3d')]:_0x2d8e56[_0x1ccc('0x38')];_0x1cf16a[_0x1ccc('0x40')]=qs['sort'](_0x191802[_0x1ccc('0x3a')][_0x1ccc('0x41')]);_0x1cf16a[_0x1ccc('0x43')]=qs[_0x1ccc('0x3b')](_[_0x1ccc('0x42')](_0x191802['query'],_0x2d8e56['filters']));if(_0x191802['query'][_0x1ccc('0x45')]){_0x1cf16a['where']=_[_0x1ccc('0x46')](_0x1cf16a[_0x1ccc('0x43')],{'$or':_[_0x1ccc('0x33')](_0x1cf16a[_0x1ccc('0x3d')],function(_0x5f3356){var _0x201bc9={};_0x201bc9[_0x5f3356]={'$like':'%'+_0x191802[_0x1ccc('0x3a')][_0x1ccc('0x45')]+'%'};return _0x201bc9;})});}_0x1cf16a=_[_0x1ccc('0x46')]({},_0x1cf16a,_0x191802['options']);return _0x32d6e2[_0x1ccc('0x61')](_0x1cf16a);}})[_0x1ccc('0x2a')](function(_0x4a468b){if(_0x4a468b){_0x137c10=_0x4a468b[_0x1ccc('0x5e')];if(!_0x191802[_0x1ccc('0x3a')][_0x1ccc('0x3f')](_0x1ccc('0x63'))){_0x1cf16a[_0x1ccc('0x25')]=qs[_0x1ccc('0x25')](_0x191802[_0x1ccc('0x3a')][_0x1ccc('0x25')]);_0x1cf16a[_0x1ccc('0x24')]=qs[_0x1ccc('0x24')](_0x191802[_0x1ccc('0x3a')]['offset']);}return _0x32d6e2[_0x1ccc('0x61')](_0x1cf16a);}})['then'](function(_0x39c65e){if(_0x39c65e){return _0x39c65e?{'count':_0x137c10,'rows':_0x39c65e}:null;}})['then'](respondWithResult(_0x5c3053,null))[_0x1ccc('0x4c')](handleError(_0x5c3053,null));};