From 12b7266e5c776b0cf80141faa6e23d31838ad6bf Mon Sep 17 00:00:00 2001 From: Mukul Jain Date: Mon, 9 Jan 2017 03:22:38 +0530 Subject: [PATCH] moongose-csv --- models/astronomy.js | 3 +- models/contact.js | 2 + models/cybros.js | 2 + models/ecell.js | 3 +- models/literary.js | 3 +- models/payment.js | 3 +- models/paymentMUN.js | 4 +- models/paymentSIF.js | 4 +- models/quiz.js | 4 +- models/robotics.js | 4 +- models/sif.js | 4 +- models/user.js | 6 +-- models/workshop.js | 3 +- package.json | 1 + public/partials/users.ejs | 4 +- routes/cryptex.js | 2 +- routes/form.js | 86 +++++++++++++++++++++++++++++++++++++++ 17 files changed, 118 insertions(+), 20 deletions(-) diff --git a/models/astronomy.js b/models/astronomy.js index b8e0f71..50fe2e9 100755 --- a/models/astronomy.js +++ b/models/astronomy.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var astronomyRegistrationSchema = mongoose.Schema({ team : [ @@ -25,5 +25,6 @@ var astronomyRegistrationSchema = mongoose.Schema({ }, }); +astronomyRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Astronomy', astronomyRegistrationSchema); diff --git a/models/contact.js b/models/contact.js index c1d7f80..b1cdab3 100644 --- a/models/contact.js +++ b/models/contact.js @@ -1,4 +1,5 @@ var mongoose = require('mongoose'); +var mongoose_csv = require('mongoose-csv'); var contactSchema = mongoose.Schema({ name : String, @@ -6,5 +7,6 @@ var contactSchema = mongoose.Schema({ query : String, }); +contactSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Contact', contactSchema); diff --git a/models/cybros.js b/models/cybros.js index 603d06e..a19aba6 100755 --- a/models/cybros.js +++ b/models/cybros.js @@ -1,4 +1,5 @@ var mongoose = require('mongoose'); +var mongoose_csv = require('mongoose-csv'); var cybrosRegistrationSchema = mongoose.Schema({ @@ -25,5 +26,6 @@ var cybrosRegistrationSchema = mongoose.Schema({ }, }); +cybrosRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Cybros', cybrosRegistrationSchema); diff --git a/models/ecell.js b/models/ecell.js index e0999b1..8a9377a 100755 --- a/models/ecell.js +++ b/models/ecell.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var ecellRegistrationSchema = mongoose.Schema({ team : [ @@ -27,5 +27,6 @@ var ecellRegistrationSchema = mongoose.Schema({ }, }); +ecellRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Ecell', ecellRegistrationSchema); diff --git a/models/literary.js b/models/literary.js index 4e1049f..37e6163 100755 --- a/models/literary.js +++ b/models/literary.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var literaryRegistrationSchema = mongoose.Schema({ team : [ @@ -25,5 +25,6 @@ var literaryRegistrationSchema = mongoose.Schema({ }, }); +literaryRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Literary', literaryRegistrationSchema); diff --git a/models/payment.js b/models/payment.js index 94958cf..efbbe85 100644 --- a/models/payment.js +++ b/models/payment.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var paymentSchema = mongoose.Schema({ id : String, @@ -8,5 +8,6 @@ var paymentSchema = mongoose.Schema({ order_id : String }); +paymentSchema.plugin(mongoose_csv); // create the model for payments and expose it to our app module.exports = mongoose.model('PaymentDB', paymentSchema); diff --git a/models/paymentMUN.js b/models/paymentMUN.js index a2a9cb4..bf63127 100644 --- a/models/paymentMUN.js +++ b/models/paymentMUN.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var paymentMUNSchema = mongoose.Schema({ order_id : String, @@ -16,6 +16,6 @@ var paymentMUNSchema = mongoose.Schema({ timestamps: { createdAt: 'created_at' } } ); - +paymentMUNSchema.plugin(mongoose_csv); // create the model for payments and expose it to our app module.exports = mongoose.model('PaymentMUN', paymentMUNSchema); diff --git a/models/paymentSIF.js b/models/paymentSIF.js index cc90f8f..42fefca 100644 --- a/models/paymentSIF.js +++ b/models/paymentSIF.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var paymentSIFSchema = mongoose.Schema({ order_id : String, @@ -13,6 +13,6 @@ var paymentSIFSchema = mongoose.Schema({ timestamps: { createdAt: 'created_at' } } ); - +paymentSIFSchema.plugin(mongoose_csv); // create the model for payments and expose it to our app module.exports = mongoose.model('PaymentSIF', paymentSIFSchema); diff --git a/models/quiz.js b/models/quiz.js index a92e035..dea372e 100755 --- a/models/quiz.js +++ b/models/quiz.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var quizRegistrationSchema = mongoose.Schema({ team : [ @@ -24,6 +24,6 @@ var quizRegistrationSchema = mongoose.Schema({ amount : String, }, }); - +quizRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Quiz', quizRegistrationSchema); diff --git a/models/robotics.js b/models/robotics.js index d656166..29adbde 100755 --- a/models/robotics.js +++ b/models/robotics.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var roboticsRegistrationSchema = mongoose.Schema({ team : [ @@ -24,6 +24,6 @@ var roboticsRegistrationSchema = mongoose.Schema({ amount : String, }, }); - +roboticsRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Robotics', roboticsRegistrationSchema); diff --git a/models/sif.js b/models/sif.js index 0759021..4bfcbab 100644 --- a/models/sif.js +++ b/models/sif.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var sifSchema = mongoose.Schema({ detail: @@ -24,6 +24,6 @@ var sifSchema = mongoose.Schema({ amount : Number, }, }); - +sifSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('SIF', sifSchema); diff --git a/models/user.js b/models/user.js index edaf66f..8c7e7f8 100755 --- a/models/user.js +++ b/models/user.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'); - +var mongoose_csv = require('mongoose-csv'); var userSchema = mongoose.Schema({ googletoken : String, @@ -9,7 +9,7 @@ var userSchema = mongoose.Schema({ email : String, name : String, phoneNumber : Number, - cryptexLevel : Number, + cryptexLevel : Number, cryptexTime : Number, //the time when cryptexLevel got updated; college : String, year : Number, @@ -20,6 +20,6 @@ var userSchema = mongoose.Schema({ events : Array, paidEvents : Array, }); - +userSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('User', userSchema); diff --git a/models/workshop.js b/models/workshop.js index bd749f1..d9f832d 100644 --- a/models/workshop.js +++ b/models/workshop.js @@ -1,4 +1,5 @@ var mongoose = require('mongoose'); +var mongoose_csv = require('mongoose-csv'); var workshopRegistrationSchema = mongoose.Schema({ team : [ @@ -23,6 +24,6 @@ var workshopRegistrationSchema = mongoose.Schema({ amount : String, }, }); - +workshopRegistrationSchema.plugin(mongoose_csv); // create the model for users and expose it to our app module.exports = mongoose.model('Workshop', workshopRegistrationSchema); diff --git a/package.json b/package.json index 306f962..a8f4071 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "jade": "~1.11.0", "jsonwebtoken": "^7.1.9", "mongoose": "^4.6.3", + "mongoose-csv": "^1.0.0", "mongoose-currency": "^0.2.0", "morgan": "~1.7.0", "njwt": "^0.3.2", diff --git a/public/partials/users.ejs b/public/partials/users.ejs index 9b351e8..d5b768c 100644 --- a/public/partials/users.ejs +++ b/public/partials/users.ejs @@ -20,7 +20,9 @@ padding: 10px 5px; } - +
+ Export to csv +
<% for(var i=0; i
diff --git a/routes/cryptex.js b/routes/cryptex.js index 678fcc5..5675805 100644 --- a/routes/cryptex.js +++ b/routes/cryptex.js @@ -95,7 +95,7 @@ router.get('/leaderboard', Verify.verifyOrdinaryUser ,function(req, res) { }; if(req.decoded.sub === ""){ isLoggedIn = false; - User.find({'cryptexLevel' : { $exists : true}}, null, {sort : {'cryptexLevel' : -1, 'cryptexTime' : 1 }}, function(err,results){ + User.find({'cryptexLevel' : { $exists : true}}, usersProjection, {sort : {'cryptexLevel' : -1, 'cryptexTime' : 1 }}, function(err,results){ res.render('cryptex_leaderboard', { "isLoggedIn" : isLoggedIn, results : results diff --git a/routes/form.js b/routes/form.js index 610cd6e..e89a96c 100644 --- a/routes/form.js +++ b/routes/form.js @@ -17,6 +17,8 @@ var PaymentDB = require('../models/payment'); var PaymentMUN = require('../models/paymentMUN'); var PaymentSIF = require('../models/paymentSIF'); var EventURL = require('../config/eventURL'); +var mongoose_csv = require('mongoose-csv'); + router.get('/sif/startup', Verify.verifyOrdinaryUser ,function(req, res) { @@ -280,5 +282,89 @@ router.get('/user/registered', Verify.verifyOrdinaryUser ,function(req, res) { }); }); +router.get('/user/registered/csv', Verify.verifyOrdinaryUser ,function(req, res) { + var poc = authUser.poc; + if(req.decoded.sub === "" || (poc.indexOf(req.decoded.sub) === -1)){ + res.end("You are not authorized. Login and try"); + return; + } + res.writeHead(200, { + 'Content-Type': 'text/csv', + 'Content-Disposition': 'attachment; filename=registereduser.csv' + }); + // pipe file using mongoose-csv + User.find().csv(res); +}); + + +router.get('/csv/*', Verify.verifyOrdinaryUser ,function(req, res) { + var poc = authUser.poc; + var allowedUser = ['jainmukul1996@gmail.com']; + var paymentUser = poc.slice(0); + + if(EventURL[req.query.event] === undefined){ + res.end('Please Check the link once again there may some typo in event name'); + return; + } + + switch(req.params['0']) { + case "astronomy": + eventx = Astronomy; + allowedUser = authUser.astronomy; + paymentUser = paymentUser.concat(allowedUser[0]); + break; + case "coding": + eventx = Cybros; + allowedUser = authUser.cybros; + paymentUser = paymentUser.concat(allowedUser[0]); + break; + case "literature": + eventx = Literary; + break; + case "robotics": + eventx = Robotics; + allowedUser = authUser.robotics; + paymentUser = paymentUser.concat(allowedUser[0]); + break; + case "management": + eventx = Ecell; + allowedUser = authUser.ecell; + paymentUser = paymentUser.concat(allowedUser[0]); + break; + case "quizzing": + eventx = Quiz; + allowedUser = authUser.quiz; + paymentUser = paymentUser.concat(allowedUser[0]); + break; + case "workshop": + eventx = Workshop; + allowedUser = authUser.admin; + break; + default: + res.end('Please Check the link once again there may some typo in club name'); + return; + break; + } + if(req.decoded.sub === "" || (poc.indexOf(req.decoded.sub) === -1 && allowedUser.indexOf(req.decoded.sub) === -1)){ + res.end("You are not authorized. Login and try"); + return; + } + + User.findOne({'email' : req.decoded.sub }, function(err, user) { + // if there are any errors, return the error + if (err){ + return done(err); + } + // check to see if theres already a user with that email + if (user){ + res.writeHead(200, { + 'Content-Type': 'text/csv', + 'Content-Disposition': 'attachment; filename=' + req.query.event + '.csv' + }); + eventx.find({'eventName' : req.query.event}).csv(res); + } + }); +}); + module.exports = router;