fix: WebPush Notification
This commit is contained in:
@@ -4,6 +4,8 @@ const mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
const webpush = require('web-push');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
const isValidSaveRequest = (req, res) => {
|
||||
@@ -47,8 +49,12 @@ router.post('/', (req, res) => {
|
||||
})
|
||||
.then(myitem => {
|
||||
|
||||
if (myitem === null)
|
||||
if (myitem === null) {
|
||||
myitem = subscriptionModel;
|
||||
tools.mylogshow('Subscription NOT EXISTED IN DB, so I use this created!', myitem)
|
||||
}else
|
||||
tools.mylogshow('Subscription already Existed!', myitem)
|
||||
|
||||
|
||||
myitem.save((err, subscription) => {
|
||||
if (err) {
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -88,7 +88,7 @@ router.get('/:username', (req, res) => {
|
||||
router.post('/login', (req, res) => {
|
||||
var body = _.pick(req.body, ['username', 'password', 'idapp', 'keyappid', 'lang']);
|
||||
var user = new User(body);
|
||||
const subs = _.pick(req.body, ['subs']);
|
||||
// const subs = _.pick(req.body, ['subs']);
|
||||
|
||||
tools.mylog("LOGIN: username: " + user.username + " pwd = " + user.password);
|
||||
|
||||
@@ -97,6 +97,7 @@ router.post('/login', (req, res) => {
|
||||
if (body.keyappid !== process.env.KEY_APP_ID)
|
||||
return res.status(400).send();
|
||||
|
||||
let resalreadysent = false;
|
||||
|
||||
User.findByCredentials(user.username, user.password)
|
||||
.then((user) => {
|
||||
@@ -108,70 +109,53 @@ router.post('/login', (req, res) => {
|
||||
return user
|
||||
})
|
||||
.then(user => {
|
||||
return user.generateAuthToken(req).then((token) => {
|
||||
var usertosend = User();
|
||||
usertosend.username = user.username;
|
||||
usertosend.email = user.email;
|
||||
usertosend.userId = user._id.toHexString();
|
||||
usertosend.verified_email = user.verified_email;
|
||||
if (user) {
|
||||
return user.generateAuthToken(req).then((token) => {
|
||||
var usertosend = User();
|
||||
usertosend.username = user.username;
|
||||
usertosend.email = user.email;
|
||||
usertosend.userId = user._id.toHexString();
|
||||
usertosend.verified_email = user.verified_email;
|
||||
|
||||
// tools.mylog("user.verified_email:" + user.verified_email);
|
||||
tools.mylog("usertosend.userId", usertosend.userId);
|
||||
// tools.mylog("user.verified_email:" + user.verified_email);
|
||||
tools.mylog("usertosend.userId", usertosend.userId);
|
||||
|
||||
// tools.mylog("usertosend:");
|
||||
// tools.mylog(usertosend);
|
||||
return { usertosend, token }
|
||||
})
|
||||
.then((myris) => {
|
||||
const useragent = req.get('User-Agent');
|
||||
const access = 'auth ' + useragent;
|
||||
// if (usertosend.tokens.length > 0)
|
||||
// access = usertosend.tokens[usertosend.tokens.length - 1].access;
|
||||
return existSubScribe(myris.usertosend.userId, access).then(subscribe => {
|
||||
let subscriptionModel = null;
|
||||
if (subscribe === null) {
|
||||
// Doesn't exist, so save sub passed in INPUT
|
||||
subscriptionModel = new Subscription(subs);
|
||||
subscriptionModel.userId = myris.usertosend.userId;
|
||||
subscriptionModel.access = access;
|
||||
// tools.mylog("usertosend:");
|
||||
// tools.mylog(usertosend);
|
||||
return { usertosend, token }
|
||||
|
||||
}
|
||||
return {usertosend: myris.usertosend, token: myris.token, subscriptionModel }
|
||||
}).then(myris => {
|
||||
if (myris.subscriptionModel !== null) {
|
||||
return myris.subscriptionModel.save().then((err, subscription) => {
|
||||
if (err) {
|
||||
console.error(`Error occurred while saving subscription. Err: ${err}`);
|
||||
res.status(500).json({
|
||||
error: 'Technical error occurred:' + err
|
||||
});
|
||||
} else {
|
||||
// Send 201 - resource created
|
||||
// res.status(201).json({ data: 'Subscription saved.' });
|
||||
// res.send({ data: 'Subscription saved.' });
|
||||
|
||||
tools.sendBackNotif(subscription, req.body.options)
|
||||
}
|
||||
return { usertosend: myris.usertosend, token: myris.token }
|
||||
});
|
||||
} else {
|
||||
return { usertosend: myris.usertosend, token: myris.token }
|
||||
}
|
||||
}).catch(e => {
|
||||
console.log('Err subscribing:', e);
|
||||
})
|
||||
.then((myris) => {
|
||||
const useragent = req.get('User-Agent');
|
||||
const access = 'auth ' + useragent;
|
||||
|
||||
// Check if already exist Subscribe
|
||||
return existSubScribe(myris.usertosend.userId, access).then(subscribe => {
|
||||
return (subscribe !== null)
|
||||
}).then(subsExistonDb => {
|
||||
return { usertosend: myris.usertosend, token: myris.token, subsExistonDb }
|
||||
}).catch(err => {
|
||||
return { usertosend: myris.usertosend, token: myris.token, subsExistonDb:false }
|
||||
})
|
||||
}).then(myris => {
|
||||
console.log('res', myris.token, myris.usertosend);
|
||||
|
||||
res.header('x-auth', myris.token).send({ usertosend: myris.usertosend, code: server_constants.RIS_CODE_OK });
|
||||
// SEND TOKEN AND CODE RESULT
|
||||
res.header('x-auth', myris.token).send({
|
||||
usertosend: myris.usertosend,
|
||||
code: server_constants.RIS_CODE_OK,
|
||||
subsExistonDb: myris.subsExistonDb
|
||||
});
|
||||
// tools.mylog("TROVATOOO!");
|
||||
|
||||
tools.mylog('FINE LOGIN')
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
tools.mylog("ERRORE IN LOGIN: " + e);
|
||||
res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
|
||||
if (!resalreadysent)
|
||||
res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user