Circuits...
Circuits Fido e Max Qta Fixed error eslint: 7.0.0 is OK
This commit is contained in:
@@ -20,7 +20,7 @@ const {ObjectID} = require('mongodb');
|
||||
|
||||
const shared_consts = require('./shared_nodejs');
|
||||
|
||||
const mongoose = require('mongoose').set('debug', false);
|
||||
const mongoose = require('mongoose').set('debug', process.env.DEBUG);
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
|
||||
const server_constants = require('./server_constants');
|
||||
@@ -746,7 +746,7 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
sendNotificationToUser: function(userId, title, content, openUrl, openUrl2, tag, actions, id) {
|
||||
sendNotificationToUser: async function(userId, title, content, openUrl, openUrl2, tag, actions, id) {
|
||||
|
||||
content = this.convertHTMLtoText(content);
|
||||
|
||||
@@ -765,94 +765,99 @@ module.exports = {
|
||||
// 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'
|
||||
// });
|
||||
try {
|
||||
|
||||
//++ Rimuovi questo record !?
|
||||
return await 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 conta = 0;
|
||||
let parallelSubscriptionCalls = subscriptions.map((subscription) => {
|
||||
const trovati = subscriptions.length;
|
||||
return new Promise((resolve, reject) => {
|
||||
const pushSubscription = {
|
||||
endpoint: subscription.endpoint,
|
||||
keys: {
|
||||
p256dh: subscription.keys.p256dh,
|
||||
auth: subscription.keys.auth,
|
||||
},
|
||||
};
|
||||
//++ Rimuovi questo record !?
|
||||
|
||||
conta++;
|
||||
|
||||
const parse = require('url-parse');
|
||||
const parsedUrl = parse(subscription.endpoint);
|
||||
const audience = parsedUrl.protocol + '//' + parsedUrl.hostname;
|
||||
|
||||
const vapidHeaders = webpush.getVapidHeaders(
|
||||
audience,
|
||||
process.env.VAPI_KEY_SUBJECT,
|
||||
process.env.PUBLIC_VAPI_KEY,
|
||||
process.env.PRIVATE_VAPI_KEY,
|
||||
'aes128gcm',
|
||||
);
|
||||
|
||||
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: vapidHeaders,
|
||||
};
|
||||
|
||||
console.log('************ INVIO WEBPUSH.SENDNOTIFICATION N° ',
|
||||
conta, '/', trovati, 'A', subscription.browser);
|
||||
|
||||
const pushPayload = JSON.stringify(payload);
|
||||
|
||||
webpush.sendNotification(
|
||||
pushSubscription,
|
||||
pushPayload,
|
||||
pushOptions,
|
||||
).then((value) => {
|
||||
// console.log('Invio Push', value);
|
||||
resolve({
|
||||
status: true,
|
||||
return false;
|
||||
} else {
|
||||
let conta = 0;
|
||||
let parallelSubscriptionCalls = subscriptions.map((subscription) => {
|
||||
const trovati = subscriptions.length;
|
||||
return new Promise((resolve, reject) => {
|
||||
const pushSubscription = {
|
||||
endpoint: subscription.endpoint,
|
||||
data: value,
|
||||
});
|
||||
}).catch(async (err) => {
|
||||
console.error('err Push', err.body);
|
||||
keys: {
|
||||
p256dh: subscription.keys.p256dh,
|
||||
auth: subscription.keys.auth,
|
||||
},
|
||||
};
|
||||
|
||||
// Cancella dal DB la notifica Push, visto che da errore! (sarà scaduto)
|
||||
const ris = await Subscription.deleteOne({_id: subscription._id});
|
||||
conta++;
|
||||
|
||||
reject({
|
||||
status: false,
|
||||
endpoint: subscription.endpoint,
|
||||
data: err,
|
||||
const parse = require('url-parse');
|
||||
const parsedUrl = parse(subscription.endpoint);
|
||||
const audience = parsedUrl.protocol + '//' + parsedUrl.hostname;
|
||||
|
||||
const vapidHeaders = webpush.getVapidHeaders(
|
||||
audience,
|
||||
process.env.VAPI_KEY_SUBJECT,
|
||||
process.env.PUBLIC_VAPI_KEY,
|
||||
process.env.PRIVATE_VAPI_KEY,
|
||||
'aes128gcm',
|
||||
);
|
||||
|
||||
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: vapidHeaders,
|
||||
};
|
||||
|
||||
console.log('************ INVIO WEBPUSH.SENDNOTIFICATION N° ',
|
||||
conta, '/', trovati, 'A', subscription.browser);
|
||||
|
||||
const pushPayload = JSON.stringify(payload);
|
||||
|
||||
webpush.sendNotification(
|
||||
pushSubscription,
|
||||
pushPayload,
|
||||
pushOptions,
|
||||
).then((value) => {
|
||||
// console.log('Invio Push', value);
|
||||
resolve({
|
||||
status: true,
|
||||
endpoint: subscription.endpoint,
|
||||
data: value,
|
||||
});
|
||||
}).catch(async (err) => {
|
||||
console.error('err Push', err.body);
|
||||
|
||||
// Cancella dal DB la notifica Push, visto che da errore! (sarà scaduto)
|
||||
const ris = await Subscription.deleteOne({_id: subscription._id});
|
||||
|
||||
reject({
|
||||
status: false,
|
||||
endpoint: subscription.endpoint,
|
||||
data: err,
|
||||
});
|
||||
});
|
||||
}).catch(error => {
|
||||
console.log('ERROR: sendNotificationToUser', error.data.body ? error.data.body : error);
|
||||
});
|
||||
}).catch(error => {
|
||||
console.log('ERROR: sendNotificationToUser', error.data.body ? error.data.body : error);
|
||||
});
|
||||
});
|
||||
// q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
|
||||
// console.info(pushResults);
|
||||
// });
|
||||
// res.json({
|
||||
// data: 'Push triggered'
|
||||
// });
|
||||
return true;
|
||||
}
|
||||
});
|
||||
// q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
|
||||
// console.info(pushResults);
|
||||
// });
|
||||
// res.json({
|
||||
// data: 'Push triggered'
|
||||
// });
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}catch (e) {
|
||||
console.error('sendNotificationToUser', e.message);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
@@ -907,7 +912,7 @@ module.exports = {
|
||||
|
||||
if (userId) {
|
||||
// SEND PUSH NOTIFICATION
|
||||
this.sendNotificationToUser(userId, title, descr, openUrl, '', tag, actions);
|
||||
await this.sendNotificationToUser(userId, title, descr, openUrl, '', tag, actions);
|
||||
}
|
||||
|
||||
if (telegram && sendmynotif) {
|
||||
@@ -922,7 +927,7 @@ module.exports = {
|
||||
sendNotifAndMsgTelegram: async function(idapp, userId, objmsg, telegram, msgextrateleg) {
|
||||
|
||||
if (userId) {
|
||||
this.sendNotificationToUser(userId, objmsg.title, objmsg.descr, objmsg.openUrl, '', objmsg.tag, objmsg.actions);
|
||||
await this.sendNotificationToUser(userId, objmsg.title, objmsg.descr, objmsg.openUrl, '', objmsg.tag, objmsg.actions);
|
||||
}
|
||||
|
||||
if (telegram) {
|
||||
@@ -1065,13 +1070,17 @@ module.exports = {
|
||||
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_REMOVED_ADMIN;
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REQ) {
|
||||
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ;
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) {
|
||||
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_ACCEPTED;
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE) {
|
||||
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (sendnotif && typeid > 0) {
|
||||
// CREATE NOTIFICATION IN TABLE SENDNOTIF
|
||||
await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
return await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -1131,10 +1140,17 @@ module.exports = {
|
||||
try {
|
||||
// SEND TO THE ADMINS THE NOTIFICATIONS
|
||||
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REQ) {
|
||||
await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, extrarec.dest, username_action, circuitname, circuit.path, null,
|
||||
let ris = false;
|
||||
let inviato = false;
|
||||
|
||||
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REQ || cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT || cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE ) {
|
||||
ris = await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, extrarec.dest, username_action, circuitname, circuit.path, null,
|
||||
false, '', extrarec);
|
||||
|
||||
if (ris)
|
||||
inviato = true;
|
||||
// ++Todo: Inviare anche agli Admin ?!
|
||||
} else {
|
||||
|
||||
for (const singleadmin of circuit.admins) {
|
||||
@@ -1145,6 +1161,7 @@ module.exports = {
|
||||
|
||||
await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, singleadmin.username, username_action, circuitname,
|
||||
circuit.path, circuit, true, '', extrarec);
|
||||
inviato = true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('sendNotificationByCircuit', e);
|
||||
@@ -1153,15 +1170,18 @@ module.exports = {
|
||||
|
||||
if (!giainviato && cmd !== shared_consts.CIRCUITCMD.REQ) {
|
||||
// SEND TO THE USER DEST THE NOTIFICATION
|
||||
await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, usernameOrig, username_action, circuitname, circuit.path, null,
|
||||
ris = await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, usernameOrig, username_action, circuitname, circuit.path, null,
|
||||
false, '', extrarec);
|
||||
if (ris)
|
||||
inviato = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return inviato;
|
||||
|
||||
} catch (e) {
|
||||
console.error('sendNotificationByCircuit: ', e);
|
||||
return false;
|
||||
}
|
||||
|
||||
},
|
||||
@@ -1226,10 +1246,10 @@ module.exports = {
|
||||
// console.log('nomeapp 1: ' , this.getNomeAppByIdApp(1));
|
||||
// console.log('nomeapp 2: ' , this.getNomeAppByIdApp(2));
|
||||
|
||||
User.find({username: 'paoloar77', idapp: '1'}).then((arrusers) => {
|
||||
User.find({username: 'paoloar77', idapp: '1'}).then(async (arrusers) => {
|
||||
if (arrusers !== null) {
|
||||
for (const user of arrusers) {
|
||||
this.sendNotificationToUser(user._id, title, msg, '/', '', tag, []).
|
||||
await this.sendNotificationToUser(user._id, title, msg, '/', '', tag, []).
|
||||
then(ris => {
|
||||
if (ris) {
|
||||
|
||||
@@ -1591,7 +1611,7 @@ module.exports = {
|
||||
return myquery;
|
||||
},
|
||||
|
||||
getQueryTable: function(idapp, params, user) {
|
||||
getQueryTable: async function(idapp, params, user) {
|
||||
|
||||
if (typeof params.startRow !== 'number') {
|
||||
throw new Error('startRow must be number');
|
||||
@@ -1834,7 +1854,7 @@ module.exports = {
|
||||
query.push({
|
||||
$match: {
|
||||
$and: [
|
||||
{'user.idapp': idapp},
|
||||
{'user.idapp': params.idapp},
|
||||
],
|
||||
},
|
||||
});
|
||||
@@ -1871,7 +1891,7 @@ module.exports = {
|
||||
query.push({
|
||||
$match: {
|
||||
$and: [
|
||||
{'user.idapp': idapp},
|
||||
{'user.idapp': params.idapp},
|
||||
],
|
||||
},
|
||||
});
|
||||
@@ -1883,6 +1903,14 @@ module.exports = {
|
||||
},
|
||||
);
|
||||
|
||||
} else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS) {
|
||||
|
||||
const {Movement} = require('../models/movement');
|
||||
|
||||
const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, params.username, params.myid);
|
||||
|
||||
query.push(...myquery);
|
||||
|
||||
}
|
||||
|
||||
if (newvers) {
|
||||
@@ -2061,7 +2089,7 @@ module.exports = {
|
||||
},
|
||||
|
||||
async executeQueryTable(mythistable, idapp, params, user) {
|
||||
let query = this.getQueryTable(idapp, params, user);
|
||||
let query = await this.getQueryTable(idapp, params, user);
|
||||
|
||||
try {
|
||||
// console.log('query', query);
|
||||
@@ -2104,7 +2132,7 @@ module.exports = {
|
||||
// Before check if exist more than 1 record in the destination,
|
||||
// if Yes don't copy
|
||||
if (numrec <= 0) {
|
||||
return mythistable.findAllIdApp(idapporig).then(async (arrrec) => {
|
||||
return await mythistable.findAllIdApp(idapporig).then(async (arrrec) => {
|
||||
let num = 0;
|
||||
|
||||
for (let ind = 0; ind < arrrec.length; ind++) {
|
||||
|
||||
Reference in New Issue
Block a user