Fix: Todo Multi refresh ...
fix some promises problem
This commit is contained in:
@@ -2,7 +2,6 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
const webpush = require('web-push');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
@@ -27,7 +26,7 @@ const isValidSaveRequest = (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
router.post('/', authenticate, (req, res) => {
|
||||
console.log('req.body.others', req.body.others);
|
||||
|
||||
if (!isValidSaveRequest(req, res)) {
|
||||
@@ -37,9 +36,10 @@ router.post('/', (req, res) => {
|
||||
let subscriptionModel = new Subscription(req.body.subs);
|
||||
subscriptionModel.userId = req.body.others.userId;
|
||||
subscriptionModel.access = req.body.others.access;
|
||||
subscriptionModel.browser = req.get('User-Agent');
|
||||
|
||||
// Find if already exist
|
||||
Subscription.findOne( {userId: subscriptionModel.userId, access: subscriptionModel.access})
|
||||
Subscription.findOne( {userId: subscriptionModel.userId, access: subscriptionModel.access, browser: subscriptionModel.browser})
|
||||
.then(itemsub => {
|
||||
return itemsub
|
||||
})
|
||||
@@ -67,7 +67,9 @@ router.post('/', (req, res) => {
|
||||
// res.status(201).json({ data: 'Subscription saved.' });
|
||||
res.send({ data: 'Subscription saved.' });
|
||||
|
||||
tools.sendBackNotif(subscription, req.body.options)
|
||||
if (req.body.options !== null) {
|
||||
tools.sendBackNotif(subscription, req.body.options)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -78,7 +80,8 @@ router.post('/', (req, res) => {
|
||||
|
||||
router.delete('/del', authenticate, (req, res) => {
|
||||
// tools.mylog("TOKENREM = " + req.token);
|
||||
Subscription.findOneAndRemove( { userId: req.user._id, access: req.access } ).then(() => {
|
||||
const browser = req.get('User-Agent');
|
||||
Subscription.findOneAndRemove( { userId: req.user._id, access: req.access, browser } ).then(() => {
|
||||
res.status(200).send();
|
||||
}, () => {
|
||||
res.status(400).send();
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
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');
|
||||
@@ -58,7 +50,7 @@ router.post('/', authenticate, (req, res) => {
|
||||
.then(record => {
|
||||
tools.mylog('REC SAVED :', record.descr);
|
||||
|
||||
sendNotificationToUser(todo.userId, 'Todo: ' + record.descr, record.descr, '/todo/' + todo.category, 'todo')
|
||||
tools.sendNotificationToUser(todo.userId, 'Todo: ' + record.descr, record.descr, '/todo/' + todo.category, 'todo')
|
||||
.then(ris => {
|
||||
if (ris) {
|
||||
res.send({ record });
|
||||
@@ -74,85 +66,6 @@ router.post('/', 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
|
||||
};
|
||||
|
||||
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) => {
|
||||
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: {}
|
||||
};
|
||||
|
||||
// console.log('pushSubscription', pushSubscription);
|
||||
// console.log('pushOptions', pushOptions);
|
||||
// console.log('pushPayload', pushPayload);
|
||||
|
||||
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
|
||||
});
|
||||
});
|
||||
}).catch(error => {
|
||||
console.log('ERROR: sendNotificationToUser', error.data)
|
||||
});
|
||||
});
|
||||
// q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
|
||||
// console.info(pushResults);
|
||||
// });
|
||||
// res.json({
|
||||
// data: 'Push triggered'
|
||||
// });
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
|
||||
@@ -18,8 +18,8 @@ var { authenticate } = require('../middleware/authenticate');
|
||||
var mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
|
||||
function existSubScribe(userId, access) {
|
||||
return Subscription.findOne({ userId, access })
|
||||
function existSubScribe(userId, access, browser) {
|
||||
return Subscription.findOne({ userId, access, browser })
|
||||
.then(itemsub => {
|
||||
return itemsub
|
||||
})
|
||||
@@ -126,11 +126,11 @@ router.post('/login', (req, res) => {
|
||||
|
||||
})
|
||||
.then((myris) => {
|
||||
const useragent = req.get('User-Agent');
|
||||
const access = 'auth ' + useragent;
|
||||
const access = 'auth';
|
||||
const browser = req.get('User-Agent');
|
||||
|
||||
// Check if already exist Subscribe
|
||||
return existSubScribe(myris.usertosend.userId, access).then(subscribe => {
|
||||
return existSubScribe(myris.usertosend.userId, access, browser).then(subscribe => {
|
||||
return (subscribe !== null)
|
||||
}).then(subsExistonDb => {
|
||||
return { usertosend: myris.usertosend, token: myris.token, subsExistonDb }
|
||||
|
||||
Reference in New Issue
Block a user