- cleaned some code.
- routing offline - pushNotification
This commit is contained in:
@@ -4,33 +4,81 @@ const mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
const webpush = require('web-push');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
const isValidSaveRequest = (req, res) => {
|
||||
try {
|
||||
if (!req.body || !req.body.subs.endpoint) {
|
||||
res.status(400);
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(JSON.stringify({
|
||||
error: {
|
||||
id: 'no-endpoint',
|
||||
message: 'Subscription must have an endpoint'
|
||||
}
|
||||
}));
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
};
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
console.log('req.body.others', req.body.others);
|
||||
|
||||
if (!isValidSaveRequest(req, res)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let subscriptionModel = new Subscription(req.body.subs);
|
||||
subscriptionModel.userId = req.body.others.userId
|
||||
subscriptionModel.userId = req.body.others.userId;
|
||||
subscriptionModel.access = req.body.others.access;
|
||||
|
||||
subscriptionModel.save((err, subscription) => {
|
||||
if (err) {
|
||||
console.error(`Error occurred while saving subscription. Err: ${err}`);
|
||||
res.status(500).json({
|
||||
error: 'Technical error occurred'
|
||||
// Find if already exist
|
||||
Subscription.findOne( {userId: subscriptionModel.userId, access: subscriptionModel.access})
|
||||
.then(itemsub => {
|
||||
return itemsub
|
||||
})
|
||||
.catch(err => {
|
||||
// Not found
|
||||
return null
|
||||
})
|
||||
.then(myitem => {
|
||||
|
||||
if (myitem === null)
|
||||
myitem = subscriptionModel;
|
||||
|
||||
myitem.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.' });
|
||||
res.send({ data: 'Subscription saved.' });
|
||||
|
||||
tools.sendBackNotif(subscription, req.body.options)
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Send 201 - resource created
|
||||
res.status(201).json({ data: 'Subscription saved.' });
|
||||
|
||||
sendBackNotif(subscription, req.body.options)
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
router.delete('/del', authenticate, (req, res) => {
|
||||
// tools.mylog("TOKENREM = " + req.token);
|
||||
Subscription.findOneAndRemove( { userId: req.user._id, access: req.access } ).then(() => {
|
||||
res.status(200).send();
|
||||
}, () => {
|
||||
res.status(400).send();
|
||||
});
|
||||
});
|
||||
|
||||
function sendBackNotif(subscription, payload) {
|
||||
|
||||
console.log('payload');
|
||||
// Pass object into sendNotification
|
||||
webpush.sendNotification(subscription, JSON.stringify(payload)).catch(err => console.error(err));
|
||||
|
||||
}
|
||||
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user