152 lines
4.0 KiB
JavaScript
Executable File
152 lines
4.0 KiB
JavaScript
Executable File
const express = require('express');
|
|
const router = express.Router();
|
|
const mongoose = require('mongoose').set('debug', false);
|
|
const Subscription = mongoose.model('subscribers');
|
|
// const q = require('q');
|
|
const webpush = require('web-push');
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
const {authenticate} = require('../middleware/authenticate');
|
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
const server_constants = require('../tools/server_constants');
|
|
|
|
const {User} = require('../models/user');
|
|
|
|
const globalTables = require('../tools/globalTables');
|
|
|
|
router.post('/', (req, res) => {
|
|
const payload = {
|
|
title: req.body.title,
|
|
message: req.body.message,
|
|
url: req.body.url,
|
|
ttl: req.body.ttl,
|
|
icon: req.body.icon,
|
|
image: req.body.image,
|
|
badge: req.body.badge,
|
|
tag: req.body.tag,
|
|
};
|
|
|
|
Subscription.find({}, (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.VAPI_KEY_SUBJECT,
|
|
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',
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
|
|
async function SendMsgTo(idapp, username, params) {
|
|
|
|
return User.find({idapp, username}).then((arrusers) => {
|
|
if (arrusers !== null) {
|
|
for (const user of arrusers) {
|
|
tools.sendNotificationToUser(user._id, params.title, params.content,
|
|
params.openUrl, params.openUrl2, params.tag, params.actions).
|
|
then(ris => {
|
|
if (ris) {
|
|
|
|
} else {
|
|
// already sent the error on calling sendNotificationToUser
|
|
}
|
|
}).
|
|
catch(e => {
|
|
console.error(e.message);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
router.post('/send', authenticate, async (req, res) => {
|
|
const idapp = req.body.idapp;
|
|
const params = req.body.params;
|
|
|
|
// console.log('params', params);
|
|
|
|
let nummsg = 0;
|
|
|
|
if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) &&
|
|
!User.isTutor(req.user.perm))) {
|
|
// If without permissions, exit
|
|
return res.status(404).
|
|
send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''});
|
|
}
|
|
|
|
try {
|
|
if (params.typesend === 0)
|
|
params.typesend = shared_consts.TypeSend.PUSH_NOTIFICATION;
|
|
params.sendreally = true;
|
|
const ris = await globalTables.SendMsgToParam(idapp, params);
|
|
|
|
return res.send({
|
|
code: server_constants.RIS_CODE_OK,
|
|
msg: ris.nummsgsent + ' Msg Inviati su ' + ris.numrec + ' !',
|
|
nummsg: ris.nummsgsent,
|
|
});
|
|
} catch (e) {
|
|
return res.send(
|
|
{code: server_constants.RIS_CODE_ERR, msg: nummsg + ' Msg Inviati !'});
|
|
}
|
|
|
|
});
|
|
|
|
router.get('/', (req, res) => {
|
|
res.json({
|
|
data: 'Invalid Request Bad',
|
|
});
|
|
});
|
|
module.exports = router;
|