Skip to content

Commit

Permalink
Merge pull request techx#152 from techx/update
Browse files Browse the repository at this point in the history
Update dependencies and add CSV export button
  • Loading branch information
pshirlyn authored Apr 1, 2020
2 parents 13d83cd + 3b588de commit 86158f8
Show file tree
Hide file tree
Showing 6 changed files with 633 additions and 666 deletions.
21 changes: 21 additions & 0 deletions app/client/src/services/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ angular.module('reg')
// Admin Only
// -------------------------

getCSV: function(){
$http.get(base + 'exportcsv').then(function (data, status, headers) {
var linkElement = document.createElement('a');
try {
linkElement.setAttribute('href', data.data.path);
linkElement.setAttribute("download", data.data.filename);
var clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
} catch (ex) {
console.log(ex);
}

}, function (data) {
console.log(data);
});
},

getStats: function(){
return $http.get(base + 'stats');
},
Expand Down
4 changes: 4 additions & 0 deletions app/client/views/admin/users/adminUsersCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ angular.module('reg')
});
};

$scope.getCSV = function(){
UserService.getCSV();
};

$scope.toggleCheckIn = function($event, user, index) {
$event.stopPropagation();

Expand Down
4 changes: 4 additions & 0 deletions app/client/views/admin/users/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
{{page}}
</button>

<br>

<button type="button" style="margin-top: 20px;" ng-click="getCSV()" class="ui green button">Export to CSV</button>

</div>
<div id="table-container" class="thirteen wide column">
<div class="ui header">
Expand Down
76 changes: 76 additions & 0 deletions app/server/routes/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var UserController = require('../controllers/UserController');
var SettingsController = require('../controllers/SettingsController');
var User = require('../models/User');
var json2csv = require('json2csv').parse;
var path = require('path');

var request = require('request');

Expand Down Expand Up @@ -142,6 +145,79 @@ module.exports = function(router) {
UserController.getStats(defaultResponse(req, res));
});

router.get('/users/exportcsv', isAdmin, function(req, res, next){
function timeStamp() {
// Create a date object with the current time
var now = new Date();
// Create an array with the current month, day and time
var date = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ];

// Create an array with the current hour, minute and second
var time = [ now.getHours(), now.getMinutes(), now.getSeconds() ];

// Determine AM or PM suffix based on the hour
var suffix = ( time[0] < 12 ) ? "AM" : "PM";

// Convert hour from military time
time[0] = ( time[0] < 12 ) ? time[0] : time[0] - 12;

// If hour is 0, set it to 12
time[0] = time[0] || 12;

// If seconds and minutes are less than 10, add a zero
for ( var i = 1; i < 3; i++ ) {
if ( time[i] < 10 ) {
time[i] = "0" + time[i];
}
}

// Return the formatted string
return '_'+date.join("-") + "_" + time.join("-") + "_" + suffix;
}

var filename = "export_quill_users" + timeStamp() + ".csv";

var fields = ['_id','email','verified','timestamp','lastUpdated',
'profile.adult','profile.name','profile.school',
'profile.gender','profile.graduationYear',
'profile.description','profile.essay','status.name',
'status.completedProfile','status.admitted',
'status.confirmed','status.declined','status.checkedIn',
'status.reimbursementGiven',
];
var fs = require('fs');

User.find({}, function (err, users_data) {
if (err) {
return res.status(501).json({err});
}
else {
let csv;
try {
csv = json2csv(users_data, {fields});
} catch (err) {
console.log(err);
return res.status(502).json({err});
}
const filePath = path.join(__dirname, "../..","client","assets",filename)
fs.writeFile(filePath, csv, function (err) {
if (err) {
return res.json(err).status(503);
}
else {
setTimeout(function () {
fs.unlinkSync(filePath);
}, 30000)
return res.json({
path: "/assets/" + filename,
filename: filename
});
}
})
}
})
});

/**
* [OWNER/ADMIN]
*
Expand Down
Loading

0 comments on commit 86158f8

Please sign in to comment.