- Created first Test with Mocha
This commit is contained in:
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user