From 5e20b29aa04a69e3087c212c9d091ef2eef39c81 Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Fri, 6 Mar 2020 14:00:31 +0100 Subject: [PATCH] separate router for account and project --- app.js | 21 +-- routes/{routes.js => routes-account.js} | 173 +++--------------------- routes/routes-project.js | 118 ++++++++++++++++ 3 files changed, 152 insertions(+), 160 deletions(-) rename routes/{routes.js => routes-account.js} (79%) create mode 100644 routes/routes-project.js diff --git a/app.js b/app.js index bcbbfbfe..debd5c36 100644 --- a/app.js +++ b/app.js @@ -9,6 +9,10 @@ const session = require('express-session'); const errorhandler = require('errorhandler'); const flash = require('express-flash'); const i18n = require('i18n'); // internationalization +i18n.configure({ + locales:['de', 'en'], + directory: './locales' +}); var env = process.env.NODE_ENV || 'development'; const config = require('./config/config')[env]; @@ -24,6 +28,12 @@ app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false})); app.use(express.static(path.join(__dirname, 'public'))); +app.use(i18n.init); +app.use((req, res, next) => { + res.setLocale('de'); + next(); +}); + app.use(session( { resave: true, @@ -48,14 +58,9 @@ app.use(function(req, res, next) { next(); }); -// internationalization (i18n) -i18n.configure({ - locales:['de', 'en'], - directory: './locales' -}); -app.use(i18n.init); - -require('./routes/routes')(app, config, passport, i18n); +//require('./routes/routes')(app, config, passport, i18n); +require('./routes/routes-account')(app, config, passport, i18n); +require('./routes/routes-project')(app, config, passport); //require('./routes/dbconn')(app, config); require('./routes/api')(app, config, passport); diff --git a/routes/routes.js b/routes/routes-account.js similarity index 79% rename from routes/routes.js rename to routes/routes-account.js index 4a757553..47437253 100644 --- a/routes/routes.js +++ b/routes/routes-account.js @@ -9,7 +9,7 @@ const salt = 64; // salt length // forgot pwd const async = require('async') const crypto = require('crypto') -const nodemailer = require('nodemailer') +const mailer = require('./mailer') module.exports = function (app, config, passport, i18n) { @@ -74,28 +74,17 @@ module.exports = function (app, config, passport, i18n) { res.status(200).send(spMetadata); } ); - - // ======== NODEMAILER ==================== - var smtpTransport = nodemailer.createTransport({ - host: config.mailer.host, - secureConnection: config.mailer.secureConnection, - port: config.mailer.port, - auth: { - user: config.mailer.authUser, - pass: config.mailer.authPass - }, - tls: { - ciphers: config.mailer.tlsCiphers - } + + // ================ test i18n ================== + i18n.setLocale('de'); + app.get('/de', function(req, res) { + var greeting = i18n.__('Hello World') + res.send(greeting) }); - var mailOptions = { - to: "", - from: config.mailer.from, - subject: "", - text: "" - }; + var lang = 'DE' + // ======== APP ROUTES - ACCOUNT ==================== var updatePasswordMailSubject = "Ihr Passwort für das Transferportal wurde gespeichert." var mailSignature = "Mit den besten Grüßen,\ndas Transferportal-Team der HFT Stuttgart\n\n"+ "Transferportal der Hochschule für Technik Stuttgart\n"+ @@ -104,17 +93,7 @@ module.exports = function (app, config, passport, i18n) { "m4lab@hft-stuttgart.de\n"+ "https://transfer.hft-stuttgart.de" var updatePasswordMailContent = "Lieber Nutzer,\n\n"+"Ihr Passwort wurde erfolgreich geändert.\n\n"+mailSignature - - // ================ test i18n ================== - i18n.setLocale('de'); - app.get('/de', function(req, res) { - var greeting = i18n.__('Hello World') - res.send(greeting) - }); - - var lang = 'DE' - // ======== APP ROUTES - ACCOUNT ==================== app.get('/', function (req, res) { if (req.isAuthenticated()) { methods.getUserByEmail(req.user.email, function(data, err){ @@ -334,11 +313,11 @@ module.exports = function (app, config, passport, i18n) { else { //req.flash('success', "Pasword updated!") req.flash('success', "Passwort aktualisiert!") - mailOptions.to = req.user.email + mailer.options.to = req.user.email //mailOptions.subject = "Your M4_LAB Password has been updated." - mailOptions.subject = updatePasswordMailSubject - mailOptions.text = updatePasswordMailContent - smtpTransport.sendMail(mailOptions, function(err) { + mailer.options.subject = updatePasswordMailSubject + mailer.options.text = updatePasswordMailContent + mailer.transport.sendMail(mailer.options, function(err) { if (err) { console.log(err) } @@ -410,10 +389,10 @@ module.exports = function (app, config, passport, i18n) { }); // send email - mailOptions.to = emailAddress; - mailOptions.subject = emailSubject; - mailOptions.text = emailContent; - smtpTransport.sendMail(mailOptions, function(err) { + mailer.options.to = emailAddress; + mailer.options.subject = emailSubject; + mailer.options.text = emailContent; + mailer.transport.sendMail(mailer.options, function(err) { done(err, 'done'); }); } @@ -473,10 +452,10 @@ module.exports = function (app, config, passport, i18n) { //req.flash('success', "Your pasword has been updated.") req.flash('success', "Passwort aktualisiert!") // send notifiaction email - mailOptions.to = user.email - mailOptions.subject = updatePasswordMailSubject - mailOptions.text = updatePasswordMailContent - smtpTransport.sendMail(mailOptions, function(err) { + mailer.options.to = user.email + mailer.options.subject = updatePasswordMailSubject + mailer.options.text = updatePasswordMailContent + mailer.transport.sendMail(mailer.options, function(err) { if (err) { console.log(err) } @@ -555,115 +534,5 @@ module.exports = function (app, config, passport, i18n) { } }) }) - - // ======== APP ROUTES - PROJECT ==================== - app.get('/project', function (req, res) { - async.waterfall([ - // get all projects from projectdb - function(done) { - methods.getAllProjects(function(projectsOverview, err) { - if (!err) { - done(err, projectsOverview) - } - }) - }, - // create JSON object for front-end - function(projectsOverview, done) { - var activeProjects = [] - var nonActiveProjects = [] - - for (var i = 0; i < projectsOverview.length; i++) { - var project = { - id: projectsOverview[i].id, - logo: projectsOverview[i].logo, - akronym: projectsOverview[i].pname, - title: projectsOverview[i].title, - summary: projectsOverview[i].onelinesummary, - category: projectsOverview[i].category, - cp: projectsOverview[i].contact_email, - gitlab: projectsOverview[i].gitlab - } - if (projectsOverview[i].projectstatus == 0) { - nonActiveProjects.push(project) - } - else if (projectsOverview[i].projectstatus == 1) { - activeProjects.push(project) - } - } - - // render the page - if (req.isAuthenticated()) { - res.render(lang+'/project/projects', { - isUserAuthenticated: true, - nonActive: nonActiveProjects, - active: activeProjects - }); - } - else { - res.render(lang+'/project/projects', { - isUserAuthenticated: false, - nonActive: nonActiveProjects, - active: activeProjects - }); - } - } - ]) - }) - - app.get('/addprojectoverview', function (req, res) { - if (req.isAuthenticated()) { - res.render(lang+'/project/addProjectOverview') - } - else { - res.redirect('/login') - } - }) - - app.post('/addprojectoverview', function (req, res) { - if (req.isAuthenticated()) { - var wiki = 0 - if (req.body.wiki) - wiki = 1 - - var projectOverviewData = { - pname: req.body.pname, - title: req.body.title, - onelinesummary: req.body.summary, - category: req.body.category, - logo: req.body.logo, - gitlab: req.body.gitlabURL, - wiki: wiki, - overview: req.body.overview, - question: req.body.question, - approach: req.body.approach, - result: req.body.result, - keywords: req.body.keywords, - announcement: req.body.announcement, - term: req.body.term, - further_details: req.body.furtherDetails, - website: req.body.website, - src: req.body.src, - caption: req.body.caption, - contact_firstname: req.body.contactFirstname, - contact_lastname: req.body.contactLastname, - contact_email: req.body.contactEmail, - leader_firstname: req.body.leaderFirstname, - leader_lastname: req.body.leaderLastname, - leader_email: req.body.leaderEmail - } - - methods.addProjectOverview(projectOverviewData, function(err){ - if (err) { - //req.flash('error', "Failed") - req.flash('error', "Fehlgeschlagen") - res.redirect('/addProjectOverview'); - } - else { - req.flash('success', 'Your project has been created.') - res.redirect('/project'); - } - }) - } - }) }; \ No newline at end of file diff --git a/routes/routes-project.js b/routes/routes-project.js new file mode 100644 index 00000000..596dd0a3 --- /dev/null +++ b/routes/routes-project.js @@ -0,0 +1,118 @@ +const methods = require('./methods') +const async = require('async') + +module.exports = function (app) { + + // ======== APP ROUTES - PROJECT ==================== + var lang = 'DE' + + app.get('/project', function (req, res) { + async.waterfall([ + // get all projects from projectdb + function(done) { + methods.getAllProjects(function(projectsOverview, err) { + if (!err) { + done(err, projectsOverview) + } + }) + }, + // create JSON object for front-end + function(projectsOverview, done) { + var activeProjects = [] + var nonActiveProjects = [] + + for (var i = 0; i < projectsOverview.length; i++) { + var project = { + id: projectsOverview[i].id, + logo: projectsOverview[i].logo, + akronym: projectsOverview[i].pname, + title: projectsOverview[i].title, + summary: projectsOverview[i].onelinesummary, + category: projectsOverview[i].category, + cp: projectsOverview[i].contact_email, + gitlab: projectsOverview[i].gitlab + } + if (projectsOverview[i].projectstatus == 0) { + nonActiveProjects.push(project) + } + else if (projectsOverview[i].projectstatus == 1) { + activeProjects.push(project) + } + } + + // render the page + if (req.isAuthenticated()) { + res.render(lang+'/project/projects', { + isUserAuthenticated: true, + nonActive: nonActiveProjects, + active: activeProjects + }); + } + else { + res.render(lang+'/project/projects', { + isUserAuthenticated: false, + nonActive: nonActiveProjects, + active: activeProjects + }); + } + } + ]) + }) + + app.get('/addprojectoverview', function (req, res) { + if (req.isAuthenticated()) { + res.render(lang+'/project/addProjectOverview') + } + else { + res.redirect('/login') + } + }) + + app.post('/addprojectoverview', function (req, res) { + if (req.isAuthenticated()) { + var wiki = 0 + if (req.body.wiki) + wiki = 1 + + var projectOverviewData = { + pname: req.body.pname, + title: req.body.title, + onelinesummary: req.body.summary, + category: req.body.category, + logo: req.body.logo, + gitlab: req.body.gitlabURL, + wiki: wiki, + overview: req.body.overview, + question: req.body.question, + approach: req.body.approach, + result: req.body.result, + keywords: req.body.keywords, + announcement: req.body.announcement, + term: req.body.term, + further_details: req.body.furtherDetails, + website: req.body.website, + src: req.body.src, + caption: req.body.caption, + contact_firstname: req.body.contactFirstname, + contact_lastname: req.body.contactLastname, + contact_email: req.body.contactEmail, + leader_firstname: req.body.leaderFirstname, + leader_lastname: req.body.leaderLastname, + leader_email: req.body.leaderEmail + } + + methods.addProjectOverview(projectOverviewData, function(err){ + if (err) { + //req.flash('error', "Failed") + req.flash('error', "Fehlgeschlagen") + res.redirect('/addProjectOverview'); + } + else { + req.flash('success', 'Your project has been created.') + res.redirect('/project'); + } + }) + } + }) + +}; \ No newline at end of file -- GitLab