- cleaned some code.

- routing offline
- pushNotification
This commit is contained in:
Paolo Arena
2019-02-12 12:06:25 +01:00
parent d24b232a2d
commit 71132b9a57
8 changed files with 219 additions and 92 deletions

View File

@@ -1,7 +1,14 @@
const express = require('express');
const router = express.Router();
// SETTINGS WebPush Configuration
const webpush = require('web-push');
const publicVapidKey = process.env.PUBLIC_VAPI_KEY;
const privateVapidKey = process.env.PRIVATE_VAPI_KEY;
webpush.setVapidDetails('mailto:' + process.env.EMAIL_FROM, publicVapidKey, privateVapidKey);
const tools = require('../tools/general');
var server_constants = require('../tools/server_constants');
@@ -27,11 +34,7 @@ router.post('/', authenticate, (req, res) => {
// todo.expiring_at = new Date(todo.expiring_at);
tools.mylogshow('TODO', todo);
tools.mylog('ID :', todo._id);
tools.mylog('userid', todo.userId, req.user._id);
tools.mylog('ID :', todo._id, todo.descr, todo.userId, req.user._id);
if (!('descr' in req.body)){
return res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
@@ -50,14 +53,19 @@ router.post('/', authenticate, (req, res) => {
console.log('RECORD NON VALIDO !?', req.body)
}
sendNotificationToUser(todo.userId, 'New Todo', 'New Todo added!', '/' + todo.category, 'todo');
todo.save().then((writeresult) => {
let idobj = writeresult._id;
Todo.findById(idobj)
.then(record => {
tools.mylog('REC SAVED :', record.descr);
res.send({record});
try {
sendNotificationToUser(todo.userId, 'Todo: ' + record.descr, record.descr, '/' + todo.category, 'todo');
} catch (e) {
}
})
}).catch((e) => {
console.log('ERRORE in TODO POST', e.message);
@@ -80,7 +88,7 @@ function sendNotificationToUser(userId, title, content, openUrl, tag) {
// tag: req.body.tag
};
Subscription.find({userId: userId}, (err, subscriptions) => {
Subscription.find({ userId }, (err, subscriptions) => {
if (err) {
console.error(`Error occurred while getting subscriptions`);
res.status(500).json({
@@ -108,6 +116,10 @@ function sendNotificationToUser(userId, title, content, openUrl, tag) {
headers: {}
};
// console.log('pushSubscription', pushSubscription);
// console.log('pushOptions', pushOptions);
// console.log('pushPayload', pushPayload);
webpush.sendNotification(
pushSubscription,
pushPayload,
@@ -126,8 +138,7 @@ function sendNotificationToUser(userId, title, content, openUrl, tag) {
});
});
}).catch(error => {
console.log('ERROR: sendNotificationToUser', error
)
console.log('ERROR: sendNotificationToUser', error.data)
});
});
// q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
@@ -154,7 +165,7 @@ router.patch('/:id', authenticate, (req, res) => {
Todo.findByIdAndUpdate(id, {$set: body}, {new: true}).then((todo) => {
tools.mylogshow(' TODO TO MODIFY: ', todo)
tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at);
if (!todo) {
return res.status(404).send();
}
@@ -193,7 +204,7 @@ router.get('/:userId', authenticate, (req, res) => {
// Extract all the todos of the userId only
Todo.findAllByUserId(userId).then((todos) => {
tools.mylog('todos', todos);
tools.mylog('todos', todos.length);
res.send({ todos: todos });
}).catch((e) => {