fix: WebPush Notification

This commit is contained in:
Paolo Arena
2019-02-13 18:47:58 +01:00
parent df31a9ea9b
commit 0b4ac6391d
8 changed files with 115 additions and 88 deletions

View File

@@ -25,7 +25,6 @@ const _ = require('lodash');
const { ObjectID } = require('mongodb');
router.post('/', authenticate, (req, res) => {
var body = _.pick(req.body, tools.allfieldTodoWithId());
@@ -36,7 +35,7 @@ router.post('/', authenticate, (req, res) => {
tools.mylog('ID :', todo._id, todo.descr, todo.userId, req.user._id);
if (!('descr' in req.body)){
if (!('descr' in req.body)) {
return res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
}
@@ -58,14 +57,15 @@ router.post('/', authenticate, (req, res) => {
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) {
}
sendNotificationToUser(todo.userId, 'Todo: ' + record.descr, record.descr, '/todo/' + todo.category, 'todo')
.then(ris => {
if (ris) {
res.send({ record });
} else {
// already sent the error on calling sendNotificationToUser
}
})
})
}).catch((e) => {
console.log('ERRORE in TODO POST', e.message);
@@ -81,19 +81,20 @@ function sendNotificationToUser(userId, title, content, openUrl, tag) {
message: content,
url: openUrl,
tag,
// ttl: req.body.ttl,
// ttl: req.body.ttl,
// icon: req.body.icon,
// image: req.body.image,
// badge: req.body.badge,
// tag: req.body.tag
};
Subscription.find({ userId }, (err, subscriptions) => {
return Subscription.find({ userId }, (err, subscriptions) => {
if (err) {
console.error(`Error occurred while getting subscriptions`);
res.status(500).json({
error: 'Technical error occurred'
});
return false;
} else {
let parallelSubscriptionCalls = subscriptions.map((subscription) => {
return new Promise((resolve, reject) => {
@@ -147,6 +148,7 @@ function sendNotificationToUser(userId, title, content, openUrl, tag) {
// res.json({
// data: 'Push triggered'
// });
return true;
}
});
@@ -164,7 +166,7 @@ router.patch('/:id', authenticate, (req, res) => {
}
Todo.findByIdAndUpdate(id, {$set: body}, {new: true}).then((todo) => {
Todo.findByIdAndUpdate(id, { $set: body }, { new: true }).then((todo) => {
tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at);
if (!todo) {
return res.status(404).send();
@@ -179,14 +181,14 @@ router.patch('/:id', authenticate, (req, res) => {
tools.mylog('PATCH ', todo.descr, todo._id);
res.send({todo});
res.send({ todo });
}).catch((e) => {
tools.mylogserr('Error patch TODO: ', e);
res.status(400).send();
})
});
router.get('/:userId', authenticate, (req, res) => {
var userId = req.params.userId;
@@ -228,7 +230,7 @@ router.delete('/:id', authenticate, (req, res) => {
tools.mylog('DELETED ', todo.descr, todo._id);
res.send({todo});
res.send({ todo });
}).catch((e) => {
res.status(400).send();
});