Commit a2207644 authored by Janik Münzenberger's avatar Janik Münzenberger
Browse files

Edited routes

parent 02fd7e88
......@@ -46,7 +46,7 @@ function analysis(req, res) {
success: true,
data: jobId
});
completeJob(jobId, data.body);
clearInterval(interval);
}
......@@ -142,16 +142,16 @@ function addJob(imageName, plantJob, resultIdJob, user = null) {
function completeJob(id, result) {
Job.findByIdAndUpdate(id, {
$set: {
result: result,
finish: true
}
}, {
safe: true,
upsert: true
}).catch(err => {
winston.error('Could not complete Job', err);
});
$set: {
result: result,
finish: true
}
}, {
safe: true,
upsert: true
}).catch(err => {
winston.error('Could not complete Job', err);
});
}
function addToUser(userId, jobId) {
......@@ -174,14 +174,35 @@ function addToUser(userId, jobId) {
function getJob(req, res) {
let id = req.params.id;
Job.findById(id).populate('result.disease_id', ['name', 'symptoms']).populate('plant',['name', 'image_url'])
Job.findById(id).populate('result.disease_id', ['name', 'symptoms']).populate('plant', ['name', 'image_url'])
.then(job => {
res.status(200);
res.send({
success: true,
data: job
});
if (job.finish === true) {
res.send({
success: true,
data: job
});
} else {
getResults(job.resultId)
.then(response => {
if (response.statusCode === 204) throw new errors.TimeoutError('Results are stil not available');
else {
job.result = response.body;
job.finish = true;
job.save().then(job => {
Job.findById(job._id).populate('result.disease_id', ['name', 'symptoms']).populate('plant', ['name', 'image_url'])
.then(job => {
res.send({
success: true,
data: job
})
})
});
}
});
}
}).catch(err => {
console.log("Test");
winston.error(err);
errors.sendError(res, err);
});
......
const nodemailer = require('nodemailer');
const logger = require('winston');
const errors = require('../lib/errors');
function email(req, res) {
var sender = req.body.sender;
var receiver = req.body.receiver;
var subject = req.body.subject;
var msg = req.body.message;
let sender = req.body.sender;
let receiver = req.body.receiver;
let subject = req.body.subject;
let msg = req.body.message;
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {
nodemailer.createTestAccount().then(account => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
......@@ -32,12 +33,14 @@ function email(req, res) {
};
// send the E-mail with sendMail using the defined transporter
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
logger.error(err);
}
res.send(info);
});
transporter.sendMail(mailOptions)
.then(info => res.send({
success: true,
data: info
}));
}).catch(err => {
winston.error(err);
errors.sendError(err);
});
}
......
......@@ -27,7 +27,7 @@ function postNotification (req, res) {
}
function receiveFromApi(req, res){
console.req(req.params);
console.log(req.params);
console.log(req.query);
console.log(req.body);
res.send();
......
......@@ -24,7 +24,8 @@ router.get('/gardeners', gardenerController.getGardeners);
router.get('/gardeners/:id', gardenerController.getGardener);
router.post('/gardeners', gardenerController.postGardener);
router.post('/notificaions/result', notificationController.receiveFromApi);
router.post('/notifications/result', notificationController.receiveFromApi);
router.post('/notifications/result/:id', notificationController.receiveFromApi);
router.post('/notifications', notificationController.postNotification);
router.post('/email', middleware.checkBody(["sender", "receiver", "subject", "message"]), emailController.email);
......@@ -40,10 +41,14 @@ router.get('/result/:id',
router.get('/history', middleware.verifyJWT_MW(), historyController.history);
// User function: registration, login, deleting and updating
// Authentification function: registration, login
router.post('/register', middleware.checkBody(['name', 'email', 'password'], true), authController.register);
router.post('/login', middleware.verifyLoginData, authController.login);
// User functions - Verification by AuthToken
router.get('/users',
middleware.verifyJWT_MW(),
userController.getUser);
router.post('/users/delete',
middleware.verifyJWT_MW(),
......
......@@ -3,7 +3,22 @@ const winston = require('winston');
const User = require('../model/user');
const errors = require('../lib/errors');
// ToDo: Implement profile update
function getUser(req, res){
User.findById(req.tokenData._id)
.then(user => {
user = user.toObject();
delete user.password;
res.send({
success: true,
data: user
})
}).catch(err => {
winston.error(err);
errors.sendError(err);
});
}
function update(req, res) {
let update = req.body;
delete update.email;
......@@ -41,6 +56,7 @@ function remove(req, res) {
}
module.exports = {
getUser,
update,
remove
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment