- Created first Test with Mocha
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
var {User} = require('../models/user');
|
||||
|
||||
router.get('/:email', (req, res) => {
|
||||
var email = req.params.email;
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
var {User} = require('../models/user');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
|
||||
router.post(process.env.LINKVERIF_REG, (req, res) => {
|
||||
var body = _.pick(req.body, ['idapp', 'idlink']);
|
||||
var idapp = body.idapp;
|
||||
@@ -50,7 +57,7 @@ router.post(process.env.LINK_REQUEST_NEWPASSWORD, (req, res) => {
|
||||
user.tokenforgot = jwt.sign(user._id.toHexString(), process.env.SIGNCODE).toString();
|
||||
user.date_tokenforgot = new Date();
|
||||
user.save().then(() => {
|
||||
sendemail.sendEmail_RequestNewPassword(getlang(res), user.email, user.idapp, user.tokenforgot);
|
||||
sendemail.sendEmail_RequestNewPassword(res.locale, user.email, user.idapp, user.tokenforgot);
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,20 +5,23 @@ const Subscription = mongoose.model('subscribers');
|
||||
const webpush = require('web-push');
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
const subscriptionModel = new Subscription(req.body.subs);
|
||||
subscriptionModel.save((err, subscription) => {
|
||||
if (err) {
|
||||
console.error(`Error occurred while saving subscription. Err: ${err}`);
|
||||
res.status(500).json({
|
||||
error: 'Technical error occurred'
|
||||
});
|
||||
} else {
|
||||
// Send 201 - resource created
|
||||
res.status(201).json({ data: 'Subscription saved.' });
|
||||
console.log('req.body.others', req.body.others);
|
||||
let subscriptionModel = new Subscription(req.body.subs);
|
||||
subscriptionModel.userId = req.body.others.userId
|
||||
|
||||
sendBackNotif(subscription, req.body.options)
|
||||
}
|
||||
});
|
||||
subscriptionModel.save((err, subscription) => {
|
||||
if (err) {
|
||||
console.error(`Error occurred while saving subscription. Err: ${err}`);
|
||||
res.status(500).json({
|
||||
error: 'Technical error occurred'
|
||||
});
|
||||
} else {
|
||||
// Send 201 - resource created
|
||||
res.status(201).json({ data: 'Subscription saved.' });
|
||||
|
||||
sendBackNotif(subscription, req.body.options)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function sendBackNotif(subscription, payload) {
|
||||
@@ -30,11 +33,10 @@ function sendBackNotif(subscription, payload) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.json({
|
||||
data: 'Invalid Request Bad'
|
||||
});
|
||||
res.json({
|
||||
data: 'Invalid Request Bad'
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -4,6 +4,9 @@ const webpush = require('web-push');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
|
||||
var { Todo } = require('../models/todo');
|
||||
|
||||
const _ = require('lodash');
|
||||
@@ -28,7 +31,7 @@ router.post('/:id', authenticate, (req, res) => {
|
||||
console.log('RECORD NON VALIDO !?', req.body)
|
||||
}
|
||||
|
||||
sendNotificationToUser('New Post', 'New Post added!', '/' + todo.category);
|
||||
sendNotificationToUser(todo.userId, 'New Todo', 'New Todo added!', '/' + todo.category, 'todo');
|
||||
|
||||
todo.save().then((doc) => {
|
||||
res.send(doc);
|
||||
@@ -38,6 +41,78 @@ router.post('/:id', authenticate, (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function sendNotificationToUser(userId, title, content, openUrl, tag) {
|
||||
|
||||
const payload = {
|
||||
title: title,
|
||||
message: content,
|
||||
url: openUrl,
|
||||
tag,
|
||||
// ttl: req.body.ttl,
|
||||
// icon: req.body.icon,
|
||||
// image: req.body.image,
|
||||
// badge: req.body.badge,
|
||||
// tag: req.body.tag
|
||||
};
|
||||
|
||||
Subscription.find({userId: userId}, (err, subscriptions) => {
|
||||
if (err) {
|
||||
console.error(`Error occurred while getting subscriptions`);
|
||||
res.status(500).json({
|
||||
error: 'Technical error occurred'
|
||||
});
|
||||
} else {
|
||||
let parallelSubscriptionCalls = subscriptions.map((subscription) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const pushSubscription = {
|
||||
endpoint: subscription.endpoint,
|
||||
keys: {
|
||||
p256dh: subscription.keys.p256dh,
|
||||
auth: subscription.keys.auth
|
||||
}
|
||||
};
|
||||
|
||||
const pushPayload = JSON.stringify(payload);
|
||||
const pushOptions = {
|
||||
vapidDetails: {
|
||||
subject: process.env.URLBASE_APP1,
|
||||
privateKey: process.env.PRIVATE_VAPI_KEY,
|
||||
publicKey: process.env.PUBLIC_VAPI_KEY,
|
||||
},
|
||||
TTL: payload.ttl,
|
||||
headers: {}
|
||||
};
|
||||
webpush.sendNotification(
|
||||
pushSubscription,
|
||||
pushPayload,
|
||||
pushOptions
|
||||
).then((value) => {
|
||||
resolve({
|
||||
status: true,
|
||||
endpoint: subscription.endpoint,
|
||||
data: value
|
||||
});
|
||||
}).catch((err) => {
|
||||
reject({
|
||||
status: false,
|
||||
endpoint: subscription.endpoint,
|
||||
data: err
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
// q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
|
||||
// console.info(pushResults);
|
||||
// });
|
||||
// res.json({
|
||||
// data: 'Push triggered'
|
||||
// });
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
var body = _.pick(req.body, allfieldTodo);
|
||||
@@ -52,7 +127,7 @@ router.patch('/:id', authenticate, (req, res) => {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
todo.modified = false
|
||||
todo.modified = false;
|
||||
|
||||
res.send({todo});
|
||||
}).catch((e) => {
|
||||
@@ -60,25 +135,6 @@ router.patch('/:id', authenticate, (req, res) => {
|
||||
})
|
||||
});
|
||||
|
||||
function sendNotificationToUser(title, content, openUrl) {
|
||||
|
||||
// Create payload
|
||||
const payload = JSON.stringify(
|
||||
{
|
||||
title,
|
||||
content,
|
||||
openUrl
|
||||
}
|
||||
);
|
||||
|
||||
subscriptioncfg = {};
|
||||
|
||||
// Pass object into sendNotification
|
||||
|
||||
webpush.sendNotification(subscriptioncfg, payload).catch(err => console.error(err));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
router.get('/:userId', authenticate, (req, res) => {
|
||||
|
||||
@@ -3,49 +3,65 @@ const router = express.Router();
|
||||
|
||||
var { User } = require('../models/user');
|
||||
|
||||
var sendemail = require('../sendemail');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
var reg = require('../reg/registration');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
function testing() {
|
||||
return (process.env.TESTING_ON === '1')
|
||||
}
|
||||
|
||||
function mylog(...args) {
|
||||
if (!testing())
|
||||
console.log(args)
|
||||
}
|
||||
|
||||
function mylogshow(...args) {
|
||||
console.log(args)
|
||||
}
|
||||
|
||||
// POST /users
|
||||
router.post('/', (req, res) => {
|
||||
console.log("POST /users");
|
||||
mylog("POST /users");
|
||||
var body = _.pick(req.body, ['email', 'password', 'username', 'idapp', 'keyappid', 'lang']);
|
||||
var user = new User(body);
|
||||
|
||||
// console.log("LANG PASSATO = " + user.lang);
|
||||
// console.log("IDAPP = " + user.idapp);
|
||||
// mylog("LANG PASSATO = " + user.lang, "IDAPP", user.idapp);
|
||||
|
||||
user.linkreg = reg.getlinkregByEmail(body.email, body.username);
|
||||
user.verified_email = false;
|
||||
if (testing()) {
|
||||
user.verified_email = true;
|
||||
}
|
||||
|
||||
user.save().then(() => {
|
||||
User.findByUsername(user.username)
|
||||
.then((usertrovato) => {
|
||||
//console.log("USERNAME : " + user.username);
|
||||
//console.log("TROVATO USERNAME ? " + usertrovato);
|
||||
|
||||
mylog("TROVATO USERNAME ? ", user.username, usertrovato);
|
||||
if (usertrovato !== null) {
|
||||
//console.log("Non esiste ancora");
|
||||
// Non esiste ancora, allora genero il TOKEN !
|
||||
return user.generateAuthToken();
|
||||
} else {
|
||||
//console.log("Esiste già! Quindi non creo lo user.");
|
||||
// Esiste già! Quindi non creo lo user.
|
||||
res.status(11100).send();
|
||||
return 0;
|
||||
}
|
||||
}).then((token) => {
|
||||
// passo il token in x-auth
|
||||
//console.log("USER");
|
||||
//console.log(user);
|
||||
console.log("TOKEN: ");
|
||||
console.log(token);
|
||||
// mylog("passo il TOKEN: ", token);
|
||||
res.header('x-auth', token).send(user);
|
||||
|
||||
console.log("LINKREG = " + user.linkreg);
|
||||
// mylog("LINKREG = " + user.linkreg);
|
||||
// Invia un'email all'utente
|
||||
sendemail.sendEmail_Registration(getlang(res), user.email, user.username, user.idapp, user.linkreg);
|
||||
// mylog('process.env.TESTING_ON', process.env.TESTING_ON);
|
||||
if (!testing()) {
|
||||
sendemail.sendEmail_Registration(res.locale, user.email, user.username, user.idapp, user.linkreg);
|
||||
}
|
||||
|
||||
});
|
||||
}).catch((e) => {
|
||||
res.status(400).send(e);
|
||||
@@ -70,18 +86,19 @@ router.post('/login', (req, res) => {
|
||||
var body = _.pick(req.body, ['username', 'password', 'idapp', 'keyappid', 'lang']);
|
||||
var user = new User(body);
|
||||
|
||||
console.log("user: " + user.username + " pwd = " + user.password);
|
||||
mylogshow("username: " + user.username + " pwd = " + user.password);
|
||||
|
||||
mylogshow("user REC:", user);
|
||||
|
||||
if (body.keyappid !== process.env.KEY_APP_ID)
|
||||
// Se non faccio la richesa con la IDAPP giusta, allora esco!
|
||||
return res.status(400).send();
|
||||
|
||||
|
||||
User.findByCredentials(user.username, user.password)
|
||||
.then((user) => {
|
||||
console.log("CREDENZIALI ! ");
|
||||
mylogshow("CREDENZIALI ! ");
|
||||
if (!user) {
|
||||
console.log("NOT FOUND !");
|
||||
mylogshow("NOT FOUND !");
|
||||
res.status(404).send({ code: server_constants.RIS_CODE_LOGIN_ERR });
|
||||
} else {
|
||||
return user.generateAuthToken().then((token) => {
|
||||
@@ -91,23 +108,23 @@ router.post('/login', (req, res) => {
|
||||
usertosend.userId = user._id.toHexString();
|
||||
usertosend.verified_email = user.verified_email;
|
||||
|
||||
// console.log("user.verified_email:" + user.verified_email);
|
||||
// console.log("usertosend.userId", usertosend.userId);
|
||||
// mylog("user.verified_email:" + user.verified_email);
|
||||
// mylog("usertosend.userId", usertosend.userId);
|
||||
|
||||
// console.log("usertosend:");
|
||||
// console.log(usertosend);
|
||||
// mylog("usertosend:");
|
||||
// mylog(usertosend);
|
||||
res.header('x-auth', token).send(usertosend);
|
||||
// console.log("TROVATOOO!");
|
||||
// mylog("TROVATOOO!");
|
||||
});
|
||||
}
|
||||
}).catch((e) => {
|
||||
console.log("ERR: " + e);
|
||||
mylog("ERR: " + e);
|
||||
res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
|
||||
});
|
||||
});
|
||||
|
||||
router.delete('/me/token', authenticate, (req, res) => {
|
||||
console.log("TOKENREM = " + req.token);
|
||||
mylog("TOKENREM = " + req.token);
|
||||
req.user.removeToken(req.token).then(() => {
|
||||
res.status(200).send();
|
||||
}, () => {
|
||||
|
||||
Reference in New Issue
Block a user