From 5fda8708992e8a32be93ca9487c63744b42b5f0c Mon Sep 17 00:00:00 2001 From: Vytor Calixto <vytorcalixto@gmail.com> Date: Tue, 10 Oct 2017 10:51:51 -0300 Subject: [PATCH] Add download middleware to teacher route --- src/libs/routes/teacher.js | 42 ++++---------------------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/src/libs/routes/teacher.js b/src/libs/routes/teacher.js index bf89a8e4..99aea83e 100644 --- a/src/libs/routes/teacher.js +++ b/src/libs/routes/teacher.js @@ -18,7 +18,9 @@ const id2str = require(`${libs}/middlewares/id2str`); const config = require(`${libs}/config`); -const request = require(`request`); +const passport = require('passport'); + +const download = require(`${libs}/middlewares/downloadDatabase`); const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; @@ -391,42 +393,6 @@ teacherApp.get('/', rqf.parse(), (req, res, next) => { next(); }, id2str.transform(), response('teacher')); -teacherApp.get('/download', (req, res, next) => { - // first, query the mapping - req.sql.from('mapping_docente') - .field('target_name') - .field('name'); - next(); -}, query, (req, res, next) => { - req.resetSql(); - next(); -}, rqf.parse(), rqf.build(), (req, res, next) => { - let username = req.query.user; - let email = req.query.email; - - req.sql.from('docente') - .field('*'); - let header = ''; - req.result.forEach((result) => { - if(header === '') header += result.name; - else header = header + ';' + result.name; - }); - - let form = { - query: req.sql.toString(), - table: req.sql.tableFrom, - name: req.sql.tableFrom, - username, - email, - header - }; - request.post(config.cdn.url + '/api/v1/file', {form}, (err, response, body) => { - if(err) { - log.error(err); - return res.json({error: err}); - } - res.json({msg: 'Wait for download email'}); - }); -}); +teacherApp.get('/download', passport.authenticate('bearer', { session: false }), rqf.parse(), rqf.build(), download('docente', 'mapping_docente')); module.exports = teacherApp; -- GitLab