From 922a946081f2ac1a54001b95586b5e95fefd607c Mon Sep 17 00:00:00 2001 From: Rafael B Date: Tue, 23 Jan 2018 18:08:26 +0000 Subject: [PATCH] Feedback and GHC Signups are now stored in a DB, not files. --- server/mongodb/accesses/access-feedback.js | 28 +++++++++++++++++ server/mongodb/accesses/access-signup.js | 30 ++++++++++++++++++ server/mongodb/accesses/mongo-access.js | 2 ++ server/mongodb/models/feedback.js | 32 +++++++++++++++++++ server/mongodb/models/signup.js | 36 ++++++++++++++++++++++ server/routes/general-routes.js | 27 ++++++++++++++-- server/routes/user-routes.js | 18 +++++++++-- 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 server/mongodb/accesses/access-feedback.js create mode 100644 server/mongodb/accesses/access-signup.js create mode 100644 server/mongodb/models/feedback.js create mode 100644 server/mongodb/models/signup.js diff --git a/server/mongodb/accesses/access-feedback.js b/server/mongodb/accesses/access-feedback.js new file mode 100644 index 0000000..3122a84 --- /dev/null +++ b/server/mongodb/accesses/access-feedback.js @@ -0,0 +1,28 @@ +const Feedback = require('../models/feedback'); +let TYPE = 'Feedback'; + +class AccessFeedback { + constructor() { + this.addFeedback = addFeedback; + } +} + +let access_signup = module.exports = exports = new AccessFeedback(); + + +/******************************** + * C.R.U.D. FUNCTIONS + *******************************/ +function addFeedback(name, entity, email, rating, message, intention, callback) { + let newFeedback = new Feedback({ + name: name, + entity: entity, + email: email, + rating: rating, + message: message, + intention: intention + }); + + newFeedback.save(callback); + +} diff --git a/server/mongodb/accesses/access-signup.js b/server/mongodb/accesses/access-signup.js new file mode 100644 index 0000000..db4ad4d --- /dev/null +++ b/server/mongodb/accesses/access-signup.js @@ -0,0 +1,30 @@ +const Signup = require('../models/signup'); +const Utils = require('./utils-accesses'); +let TYPE = 'Signup'; + +class AccessSignup { + constructor() { + this.addPreSignup = addPreSignup; + } +} + +let access_signup = module.exports = exports = new AccessSignup(); + + +/******************************** + * C.R.U.D. FUNCTIONS + *******************************/ +function addPreSignup(teamName, teamCaptain, teamContactEmail, teamContactPhone, participantsNumber, newsletter, EmailIST, callback) { + let newSignup = new Signup({ + teamName: teamName, + teamCaptain: teamCaptain, + teamContactEmail: teamContactEmail, + teamContactPhone: teamContactPhone, + participantsNumber: participantsNumber, + newsletter: newsletter, + EmailIST: EmailIST + }); + + newSignup.save(callback); + +} diff --git a/server/mongodb/accesses/mongo-access.js b/server/mongodb/accesses/mongo-access.js index 11eeef0..55939d4 100644 --- a/server/mongodb/accesses/mongo-access.js +++ b/server/mongodb/accesses/mongo-access.js @@ -7,8 +7,10 @@ function MongoAccess() { this.professors = require('./access-professor'); this.applications = require('./access-application'); this.admins = require('./access-admin'); + this.signup = require('./access-signup'); this.users = require('./access-user'); this.utils = require('./utils-accesses'); + this.feedback = require('./access-feedback'); } let mongo_access = module.exports = exports = new MongoAccess; \ No newline at end of file diff --git a/server/mongodb/models/feedback.js b/server/mongodb/models/feedback.js new file mode 100644 index 0000000..80d9ad9 --- /dev/null +++ b/server/mongodb/models/feedback.js @@ -0,0 +1,32 @@ +const mongoose = require('mongoose'); + +// User Schema - Abstract + +const FeedbackSchema = mongoose.Schema({ + entity: { + type: String, + required: true + }, + intention: { + type: String, + required: true + }, + rating: { + type: Number, + required: true + }, + name: { + type: String, + required: true + }, + email: { + type: String, + required: true + }, + message: { + type: String, + required: true, + } +}); + +const Feedback = module.exports = mongoose.model('Feedback', FeedbackSchema); diff --git a/server/mongodb/models/signup.js b/server/mongodb/models/signup.js new file mode 100644 index 0000000..16cfce5 --- /dev/null +++ b/server/mongodb/models/signup.js @@ -0,0 +1,36 @@ +const mongoose = require('mongoose'); + +// User Schema - Abstract + +const SignupSchema = mongoose.Schema({ + teamName: { + type: String, + required: true + }, + teamCaptain: { + type: String, + required: true + }, + teamContactEmail: { + type: String, + required: true + }, + teamContactPhone: { + type: String, + required: false + }, + EmailIST: { + type: String, + required: true + }, + participantsNumber: { + type: String, + required: true + }, + newsletter: { + type: Boolean, + required: true, + } +}); + +const Feedback = module.exports = mongoose.model('Signup', SignupSchema); diff --git a/server/routes/general-routes.js b/server/routes/general-routes.js index 16805e9..e32ced7 100644 --- a/server/routes/general-routes.js +++ b/server/routes/general-routes.js @@ -4,9 +4,14 @@ const UtilsRoutes = require('./utils-routes'); const multer = require('multer'); const ba_logger = require('../log/ba_logger'); const Utils = require('../mongodb/accesses/utils-accesses'); +const passport = require('passport'); +const DBAccess = require('../mongodb/accesses/mongo-access'); +const ERROR = "Não foi possível concluir o registo. Contacte a administração."; + +//If you wish to save signups as a file const fs = require('fs'); const path = require('path'); -const passport = require('passport'); + //Others, related with student router.post('/signupHashCode', passport.authenticate('jwt', {session: false}), (req, res, next) => { @@ -14,16 +19,29 @@ router.post('/signupHashCode', passport.authenticate('jwt', {session: false}), ( UtilsRoutes.replyFailure(res,"","Só os estudantes podem inscrever-se"); return; } - // return false se n + const teamName = req.body.signup.teamName; const teamCaptain = req.body.signup.teamCaptain; const teamContactEmail = req.body.signup.teamContactEmail; const teamContactPhone = req.body.signup.teamContactPhone; const newsletter = req.body.signup.newsletter; const participantsNumber = req.body.signup.participantsNumber; - const nomeAluno = req.user.name; + //const nomeAluno = req.user.name; const emailAluno = req.user.email; + DBAccess.signup.addPreSignup(teamName, teamCaptain,teamContactEmail, + teamContactPhone,participantsNumber,newsletter,emailAluno, (err) => { + if (err) { + console.log(err); + return UtilsRoutes.replyFailure(res,err,ERROR); + } else { + return UtilsRoutes.replySuccess(res,"",""); + } + }); + + + + /* Save as a file fileContent = "[EQUIPA]:" + teamName + "\n" + "[RESPONSÁVEL]:" + teamCaptain + "\n" + @@ -37,6 +55,8 @@ router.post('/signupHashCode', passport.authenticate('jwt', {session: false}), ( "[NOME]:" + nomeAluno + "\n" + "[EMAIL]:" + emailAluno; + + const filePath = '../files/GoogleHashCode/' + emailAluno + " [" + Date.now() + "].txt"; fs.writeFile(path.join(__dirname, filePath), fileContent, function (err) { @@ -47,6 +67,7 @@ router.post('/signupHashCode', passport.authenticate('jwt', {session: false}), ( ba_logger.ba("Nova Pré-Inscrição:" + teamCaptain + ":" + Utils.utc); UtilsRoutes.replySuccess(res, "", "Inscrição efetuado com sucesso"); }); + */ }); diff --git a/server/routes/user-routes.js b/server/routes/user-routes.js index 3af6baa..d35a004 100644 --- a/server/routes/user-routes.js +++ b/server/routes/user-routes.js @@ -16,6 +16,7 @@ const WRONG_PASSWORD_PART_2 = " tentativas restantes."; const WRONG_PASSWORD_INVALIDATE = "Errou todas as tentativas. A sua conta encontra-se invalidada. Por favor contacte a administração"; const USER_INVALID = "A sua conta está invalidada. Por favor contacte a administração"; const USER_UNCONFIRMED = "A sua conta está por confirmar. Por favor contacte a administração, para que se proceda à ativação da conta"; +const ERROR_F = "Não foi possível adicionar feedback. Por favor contacte a administração"; router.post('/login', (req, res, next) => { const email = req.body.username; @@ -109,8 +110,19 @@ router.post('/feedback', (req, res, next) => { const message = req.body.message; const rate = req.body.rate; const entity = req.body.entity; - const type = req.body.type; - + const intention = req.body.type; + + DBAccess.feedback.addFeedback(name, entity,email, + rate,message,intention, (err) => { + if (err) { + console.log(err); + return UtilsRoutes.replyFailure(res,err,ERROR_F); + } else { + return UtilsRoutes.replySuccess(res,"",""); + } + }); + + /*Save as a file fileContent = "[TYPE]:" + type + "\n" + "[RATE]:" + rate + "\n" + "[NOME]:" + name + "\n" + "[EMAIL]:" + email + "\n" + @@ -126,7 +138,7 @@ router.post('/feedback', (req, res, next) => { ba_logger.ba("Feedback:" + email + ":Gave Feedback:" + Utils.utc); UtilsRoutes.replySuccess(res, "", "Feedback foi enviado com sucesso"); }); - + */ }); module.exports = router; \ No newline at end of file