Notifiche - Tutti - non letti
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
const mongoose = require('mongoose').set('debug', false)
|
const mongoose = require('mongoose').set('debug', false);
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
mongoose.Promise = global.Promise;
|
mongoose.Promise = global.Promise;
|
||||||
mongoose.level = "F";
|
mongoose.level = 'F';
|
||||||
|
|
||||||
const { ObjectID } = require('mongodb');
|
const {ObjectID} = require('mongodb');
|
||||||
|
|
||||||
// Resolving error Unknown modifier: $pushAll
|
// Resolving error Unknown modifier: $pushAll
|
||||||
mongoose.plugin(schema => {
|
mongoose.plugin(schema => {
|
||||||
schema.options.usePushEach = true
|
schema.options.usePushEach = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
const sendNotifSchema = new Schema({
|
const sendNotifSchema = new Schema({
|
||||||
@@ -27,6 +27,9 @@ const sendNotifSchema = new Schema({
|
|||||||
descr: {
|
descr: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
link: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
datenotif: {
|
datenotif: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
@@ -35,36 +38,62 @@ const sendNotifSchema = new Schema({
|
|||||||
},
|
},
|
||||||
read: {
|
read: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false
|
default: false,
|
||||||
},
|
},
|
||||||
deleted: {
|
deleted: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false
|
default: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
sendNotifSchema.statics.setNotifAsRead = function(idapp, username, idnotif) {
|
||||||
|
const SendNotif = this;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
|
||||||
sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function (username, lastdataread, idapp) {
|
if (idnotif) {
|
||||||
|
return SendNotif.findOneAndUpdate({
|
||||||
|
$and: [
|
||||||
|
{idapp},
|
||||||
|
{dest: username},
|
||||||
|
{'_id': idnotif},
|
||||||
|
],
|
||||||
|
$or: [
|
||||||
|
{deleted: {$exists: false}},
|
||||||
|
{deleted: {$exists: true, $eq: false}}],
|
||||||
|
|
||||||
|
}, {$set: {read: true}}, {new: false}).then((ret) => {
|
||||||
|
return !!ret;
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error('err', err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, lastdataread, idapp) {
|
||||||
const SendNotif = this;
|
const SendNotif = this;
|
||||||
|
|
||||||
return SendNotif.find({
|
return SendNotif.find({
|
||||||
$and: [
|
$and: [
|
||||||
{ idapp },
|
{idapp},
|
||||||
{ 'dest': username },
|
{'dest': username},
|
||||||
{ 'datenotif': {$gt: new Date(lastdataread)} },
|
{'datenotif': {$gt: new Date(lastdataread)}},
|
||||||
]
|
],
|
||||||
}).then((arrnotif) => {
|
}).lean().sort({datenotif: -1}).then((arrnotif) => {
|
||||||
console.log('arrnotif', arrnotif.length);
|
console.log('arrnotif', arrnotif.length);
|
||||||
return arrnotif
|
return arrnotif;
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error('err', err);
|
console.error('err', err);
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function (username, idapp) {
|
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp) {
|
||||||
const SendNotif = this;
|
const SendNotif = this;
|
||||||
|
|
||||||
return SendNotif.aggregate([
|
return SendNotif.aggregate([
|
||||||
@@ -72,28 +101,27 @@ sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function (username, idap
|
|||||||
$match: {
|
$match: {
|
||||||
idapp,
|
idapp,
|
||||||
dest: username,
|
dest: username,
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
$group:
|
$group:
|
||||||
{
|
{
|
||||||
_id: "$dest",
|
_id: '$dest',
|
||||||
descr: { $last: "$message" },
|
descr: {$last: '$message'},
|
||||||
datenotif: { $last: "$datenotif" },
|
datenotif: {$last: '$datenotif'},
|
||||||
read: { $last: "$read" }
|
read: {$last: '$read'},
|
||||||
}
|
},
|
||||||
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
$sort: { datenotif: -1 }
|
$sort: {datenotif: -1},
|
||||||
},
|
},
|
||||||
])
|
]).then((arrnotif) => {
|
||||||
.then((arrnotif) => {
|
|
||||||
// Remove duplicate
|
// Remove duplicate
|
||||||
// Exclude my chat
|
// Exclude my chat
|
||||||
const myarr = arrnotif.filter((ris) => ris._id !== username);
|
const myarr = arrnotif.filter((ris) => ris._id !== username);
|
||||||
// console.table(myarr);
|
// console.table(myarr);
|
||||||
return myarr
|
return myarr;
|
||||||
|
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@@ -101,7 +129,6 @@ sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function (username, idap
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
|
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
|
||||||
|
|
||||||
module.exports = { SendNotif: SendNotif };
|
module.exports = {SendNotif: SendNotif};
|
||||||
|
|||||||
@@ -358,10 +358,13 @@ const UserSchema = new mongoose.Schema({
|
|||||||
date: {type: Date},
|
date: {type: Date},
|
||||||
}], // username
|
}], // username
|
||||||
|
|
||||||
notifs: {
|
notifs: [
|
||||||
type: Number,
|
{
|
||||||
default: 1,
|
_id: false,
|
||||||
|
dir: {type: Number},
|
||||||
|
value: {type: Number},
|
||||||
},
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
;
|
;
|
||||||
@@ -1350,7 +1353,7 @@ UserSchema.statics.getUserProfileByUsername = async function(
|
|||||||
verified_email: 1,
|
verified_email: 1,
|
||||||
verified_by_aportador: 1,
|
verified_by_aportador: 1,
|
||||||
'profile.nationality': 1,
|
'profile.nationality': 1,
|
||||||
"profile.mygroups": 1,
|
'profile.mygroups': 1,
|
||||||
'profile.qualifica': 1,
|
'profile.qualifica': 1,
|
||||||
'profile.biografia': 1,
|
'profile.biografia': 1,
|
||||||
'profile.teleg_id': 1,
|
'profile.teleg_id': 1,
|
||||||
@@ -1384,7 +1387,7 @@ UserSchema.statics.getUserProfileByUsername = async function(
|
|||||||
verified_by_aportador: 1,
|
verified_by_aportador: 1,
|
||||||
notask_verif: 1,
|
notask_verif: 1,
|
||||||
'profile.nationality': 1,
|
'profile.nationality': 1,
|
||||||
"profile.mygroups": 1,
|
'profile.mygroups': 1,
|
||||||
'profile.qualifica': 1,
|
'profile.qualifica': 1,
|
||||||
'profile.biografia': 1,
|
'profile.biografia': 1,
|
||||||
'profile.teleg_id': 1,
|
'profile.teleg_id': 1,
|
||||||
@@ -1774,7 +1777,6 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
|||||||
ris = await MyGroup.deleteGroup(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
ris = await MyGroup.deleteGroup(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
||||||
console.log('ris', ris);
|
console.log('ris', ris);
|
||||||
|
|
||||||
|
|
||||||
} else if (cmd === shared_consts.GROUPSCMD.CANCEL_REQ_GROUP) {
|
} else if (cmd === shared_consts.GROUPSCMD.CANCEL_REQ_GROUP) {
|
||||||
|
|
||||||
ris = await MyGroup.removeReqGroup(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
ris = await MyGroup.removeReqGroup(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
||||||
@@ -1814,7 +1816,7 @@ function getWhatToShow(idapp, username) {
|
|||||||
verified_by_aportador: 1,
|
verified_by_aportador: 1,
|
||||||
notask_verif: 1,
|
notask_verif: 1,
|
||||||
'profile.nationality': 1,
|
'profile.nationality': 1,
|
||||||
"profile.mygroups": 1,
|
'profile.mygroups': 1,
|
||||||
'profile.qualifica': 1,
|
'profile.qualifica': 1,
|
||||||
'profile.biografia': 1,
|
'profile.biografia': 1,
|
||||||
'profile.username_telegram': 1,
|
'profile.username_telegram': 1,
|
||||||
@@ -2706,8 +2708,8 @@ UserSchema.statics.getUsersAutorizzare = async function(idapp) {
|
|||||||
const myfind = {
|
const myfind = {
|
||||||
idapp,
|
idapp,
|
||||||
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
|
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
|
||||||
'profile.teleg_id': { $gt: 0 },
|
'profile.teleg_id': {$gt: 0},
|
||||||
verified_by_aportador: { $exists: false },
|
verified_by_aportador: {$exists: false},
|
||||||
};
|
};
|
||||||
|
|
||||||
return User.count(myfind);
|
return User.count(myfind);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const sendemail = require('../sendemail');
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const sendNotif = async (res, idapp, user, recbooking) => {
|
const sendNotifBooking = async (res, idapp, user, recbooking) => {
|
||||||
//++Todo: tools.sendNotificationToUser
|
//++Todo: tools.sendNotificationToUser
|
||||||
|
|
||||||
// Send Email
|
// Send Email
|
||||||
@@ -50,7 +50,7 @@ router.post('/', authenticate, (req, res) => {
|
|||||||
}).then((recbooking) => {
|
}).then((recbooking) => {
|
||||||
// tools.mylog('booking:', booking);
|
// tools.mylog('booking:', booking);
|
||||||
// tools.mylog('already exist');
|
// tools.mylog('already exist');
|
||||||
sendNotif(res, myrec.idapp, req.user, myrec);
|
sendNotifBooking(res, myrec.idapp, req.user, myrec);
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id });
|
return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id });
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -62,7 +62,7 @@ router.post('/', authenticate, (req, res) => {
|
|||||||
|
|
||||||
Booking.findById(idobj)
|
Booking.findById(idobj)
|
||||||
.then((recbooking) => {
|
.then((recbooking) => {
|
||||||
sendNotif(res, myrec.idapp, req.user, writeresult);
|
sendNotifBooking(res, myrec.idapp, req.user, writeresult);
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: writeresult._id });
|
return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: writeresult._id });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -86,7 +86,7 @@ router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
|
|||||||
|
|
||||||
if (notify === '1') {
|
if (notify === '1') {
|
||||||
recbooking.booked = false;
|
recbooking.booked = false;
|
||||||
sendNotif(res, idapp, req.user, recbooking);
|
sendNotifBooking(res, idapp, req.user, recbooking);
|
||||||
}
|
}
|
||||||
|
|
||||||
tools.mylog('DELETED ', recbooking._id);
|
tools.mylog('DELETED ', recbooking._id);
|
||||||
|
|||||||
@@ -4,14 +4,16 @@ const router = express.Router();
|
|||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
const server_constants = require('../tools/server_constants');
|
const server_constants = require('../tools/server_constants');
|
||||||
|
|
||||||
const { authenticate } = require('../middleware/authenticate');
|
const {authenticate} = require('../middleware/authenticate');
|
||||||
|
|
||||||
const { MyEvent } = require('../models/myevent');
|
const {MyEvent} = require('../models/myevent');
|
||||||
|
|
||||||
const { ObjectID } = require('mongodb');
|
const {ObjectID} = require('mongodb');
|
||||||
|
|
||||||
const sendemail = require('../sendemail');
|
const sendemail = require('../sendemail');
|
||||||
|
|
||||||
|
const globalTables = require('../tools/globalTables');
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
router.post('/', authenticate, (req, res) => {
|
router.post('/', authenticate, (req, res) => {
|
||||||
@@ -29,20 +31,19 @@ router.post('/', authenticate, (req, res) => {
|
|||||||
if (check.exit) return check.ret;
|
if (check.exit) return check.ret;
|
||||||
|
|
||||||
// Modify:
|
// Modify:
|
||||||
return MyEvent.findOne({ id })
|
return MyEvent.findOne({id}).then((trovato) => {
|
||||||
.then((trovato) => {
|
|
||||||
// console.log('trovato', trovato);
|
// console.log('trovato', trovato);
|
||||||
if (trovato) {
|
if (trovato) {
|
||||||
return myevent.findOneAndUpdate({ id }, { $set: fieldtochange }, {
|
return myevent.findOneAndUpdate({id}, {$set: fieldtochange}, {
|
||||||
new: false,
|
new: false,
|
||||||
upsert: true
|
upsert: true,
|
||||||
}).then((recmyevent) => {
|
}).then((recmyevent) => {
|
||||||
// tools.mylog('myevent:', myevent);
|
// tools.mylog('myevent:', myevent);
|
||||||
// tools.mylog('already exist');
|
// tools.mylog('already exist');
|
||||||
sendNotif(res, myrec.idapp, req.user, recmyevent);
|
globalTables.sendNotif(res, myrec.idapp, req.user, recmyevent);
|
||||||
return res
|
return res;
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id });
|
res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// save to database a new record
|
// save to database a new record
|
||||||
@@ -51,14 +52,13 @@ router.post('/', authenticate, (req, res) => {
|
|||||||
return myevent.save().then((writeresult) => {
|
return myevent.save().then((writeresult) => {
|
||||||
let idobj = writeresult._id;
|
let idobj = writeresult._id;
|
||||||
|
|
||||||
myevent.findById(idobj)
|
myevent.findById(idobj).then((recmyevent) => {
|
||||||
.then((recmyevent) => {
|
globalTables.sendNotif(res, myrec.idapp, req.user, recmyevent);
|
||||||
sendNotif(res, myrec.idapp, req.user, recmyevent);
|
res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id});
|
||||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
|
router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
|
||||||
@@ -73,18 +73,17 @@ router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (notify === '1')
|
if (notify === '1')
|
||||||
sendNotif(res, idapp, req.user, recmyevent);
|
globalTables.sendNotif(res, idapp, req.user, recmyevent);
|
||||||
|
|
||||||
tools.mylog('DELETED ', recmyevent.descr, recmyevent._id);
|
tools.mylog('DELETED ', recmyevent.descr, recmyevent._id);
|
||||||
|
|
||||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id });
|
res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id});
|
||||||
|
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
res.status(400).send();
|
res.status(400).send();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
|
router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
|
||||||
const userId = req.params.userId;
|
const userId = req.params.userId;
|
||||||
const idapp = req.params.idapp;
|
const idapp = req.params.idapp;
|
||||||
@@ -99,12 +98,12 @@ router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
|
|||||||
|
|
||||||
if (userId !== String(req.user._id)) {
|
if (userId !== String(req.user._id)) {
|
||||||
// I'm trying to write something not mine!
|
// I'm trying to write something not mine!
|
||||||
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
|
return res.status(404).send({code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract all the todos of the userId only
|
// Extract all the todos of the userId only
|
||||||
return MyEvent.findAllByUserIdAndIdApp(userId, idapp, sall).then((recevent) => {
|
return MyEvent.findAllByUserIdAndIdApp(userId, idapp, sall).then((recevent) => {
|
||||||
res.send({ recevent });
|
res.send({recevent});
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.log(e.message);
|
console.log(e.message);
|
||||||
res.status(400).send(e);
|
res.status(400).send(e);
|
||||||
@@ -112,5 +111,4 @@ router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -131,16 +131,12 @@ router.post('/send', authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (params.typesend === 0)
|
|
||||||
params.typesend = shared_consts.TypeSend.PUSH_NOTIFICATION;
|
|
||||||
|
|
||||||
if (params.typemsg === shared_consts.TypeMsg.SEND_TO_MYSELF) {
|
if (params.typemsg === shared_consts.TypeMsg.SEND_TO_MYSELF) {
|
||||||
params.usernameDest = req.user.username;
|
params.usernameDest = req.user.username;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ris = await globalTables.SendMsgToParam(idapp, params);
|
const ris = await globalTables.SendMsgToParam(idapp, params);
|
||||||
|
|
||||||
|
|
||||||
return res.send({
|
return res.send({
|
||||||
code: server_constants.RIS_CODE_OK,
|
code: server_constants.RIS_CODE_OK,
|
||||||
msg: ris.nummsgsent + ` Msg ${msgcosa} su ` + ris.numrec + ' !',
|
msg: ris.nummsgsent + ` Msg ${msgcosa} su ` + ris.numrec + ' !',
|
||||||
|
|||||||
@@ -16,36 +16,10 @@ const sendemail = require('../sendemail');
|
|||||||
|
|
||||||
const shared_consts = require('../tools/shared_nodejs');
|
const shared_consts = require('../tools/shared_nodejs');
|
||||||
|
|
||||||
|
const globalTables = require('../tools/globalTables');
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
function checkifSendPushNotification() {
|
|
||||||
return process.env.ENABLE_PUSHNOTIFICATION === "1";
|
|
||||||
//return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async function sendNotif(res, idapp, user, recmsg) {
|
|
||||||
if (tools.isBitActive(recmsg.options, shared_consts.MessageOptions.Notify_ByPushNotification)) {
|
|
||||||
if (this.checkifSendPushNotification) {
|
|
||||||
console.log('SEND PUSH NOTIFICATION ')
|
|
||||||
//++Todo: tools.sendNotificationToUser
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read from the operator table first
|
|
||||||
let emaildest = await Operator.getEmailByUsername(recmsg.dest.idapp, recmsg.dest.username);
|
|
||||||
if (!emaildest)
|
|
||||||
emaildest = await User.getEmailByUsername(recmsg.dest.idapp, recmsg.dest.username);
|
|
||||||
|
|
||||||
console.log('emaildest', emaildest);
|
|
||||||
|
|
||||||
// Send Msg by EMAIL
|
|
||||||
if (emaildest && tools.isBitActive(recmsg.options, shared_consts.MessageOptions.Notify_ByEmail))
|
|
||||||
await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recmsg);
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
router.post('/', authenticate, (req, res) => {
|
router.post('/', authenticate, (req, res) => {
|
||||||
tools.mylog('INIZIO - SendMsg');
|
tools.mylog('INIZIO - SendMsg');
|
||||||
@@ -72,7 +46,7 @@ router.post('/', authenticate, (req, res) => {
|
|||||||
// Add this field because I don't want to add into the database
|
// Add this field because I don't want to add into the database
|
||||||
recmsg.source.infoevent = body.source.infoevent;
|
recmsg.source.infoevent = body.source.infoevent;
|
||||||
|
|
||||||
return await sendNotif(res, body.idapp, req.user, recmsg).then((ris) => {
|
return await globalTables.sendNotif(res, body.idapp, req.user, recmsg).then((ris) => {
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id });
|
return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id });
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ const router = express.Router();
|
|||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
const server_constants = require('../tools/server_constants');
|
const server_constants = require('../tools/server_constants');
|
||||||
|
|
||||||
const { authenticate } = require('../middleware/authenticate');
|
const {authenticate} = require('../middleware/authenticate');
|
||||||
|
|
||||||
const { User } = require('../models/user');
|
const {User} = require('../models/user');
|
||||||
const { Operator } = require('../models/operator');
|
const {Operator} = require('../models/operator');
|
||||||
const { SendNotif } = require('../models/sendnotif');
|
const {SendNotif} = require('../models/sendnotif');
|
||||||
|
|
||||||
const { ObjectID } = require('mongodb');
|
const {ObjectID} = require('mongodb');
|
||||||
|
|
||||||
const sendemail = require('../sendemail');
|
const sendemail = require('../sendemail');
|
||||||
|
|
||||||
@@ -18,33 +18,6 @@ const shared_consts = require('../tools/shared_nodejs');
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
|
||||||
async function sendNotif(res, idapp, user, recnotif) {
|
|
||||||
// Controlla nelle impostazioni che tipo di Notifica visualizzare
|
|
||||||
|
|
||||||
|
|
||||||
if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByPushNotification)) {
|
|
||||||
if (this.checkifSendPushNotification) {
|
|
||||||
console.log('SEND PUSH NOTIFICATION ')
|
|
||||||
//++Todo: tools.sendNotificationToUser
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read from the operator table first
|
|
||||||
let emaildest = await Operator.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username);
|
|
||||||
if (!emaildest)
|
|
||||||
emaildest = await User.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username);
|
|
||||||
|
|
||||||
console.log('emaildest', emaildest);
|
|
||||||
|
|
||||||
// Send Msg by EMAIL
|
|
||||||
if (emaildest && tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByEmail))
|
|
||||||
await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recnotif);
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
router.post('/', authenticate, (req, res) => {
|
router.post('/', authenticate, (req, res) => {
|
||||||
tools.mylog('INIZIO - SendNotif');
|
tools.mylog('INIZIO - SendNotif');
|
||||||
// tools.mylog('req.body', req.body);
|
// tools.mylog('req.body', req.body);
|
||||||
@@ -59,25 +32,45 @@ router.post('/', authenticate, (req, res) => {
|
|||||||
// console.log('fieldtochange', fieldtochange);
|
// console.log('fieldtochange', fieldtochange);
|
||||||
|
|
||||||
myrecnotif._id = new ObjectID();
|
myrecnotif._id = new ObjectID();
|
||||||
return myrecnotif.save()
|
return myrecnotif.save().then((writeresult) => {
|
||||||
.then((writeresult) => {
|
|
||||||
let idobj = writeresult._id;
|
let idobj = writeresult._id;
|
||||||
|
|
||||||
myrecnotif._id = idobj;
|
myrecnotif._id = idobj;
|
||||||
|
|
||||||
return SendNotif.findById(idobj)
|
return SendNotif.findById(idobj).then(async (recnotif) => {
|
||||||
.then(async (recnotif) => {
|
|
||||||
// Add this field because I don't want to add into the database
|
// Add this field because I don't want to add into the database
|
||||||
|
|
||||||
return await sendNotif(res, body.idapp, req.user, recnotif).then((ris) => {
|
return await globalTables.sendNotif(res, body.idapp, req.user, recnotif).then((ris) => {
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, notif: '', id: recnotif._id });
|
return res.send({code: server_constants.RIS_CODE_OK, notif: '', id: recnotif._id});
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.log(e.message);
|
console.log(e.message);
|
||||||
// res.status(400).send(e);
|
// res.status(400).send(e);
|
||||||
return res.send({ code: server_constants.RIS_CODE_ERR, notif: '' });
|
return res.send({code: server_constants.RIS_CODE_ERR, notif: ''});
|
||||||
})
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/setall/:username/:idapp', authenticate, async (req, res) => {
|
||||||
|
|
||||||
|
const idapp = req.params.idapp;
|
||||||
|
const username = req.params.username;
|
||||||
|
const username_call = req.user.username;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (username === username_call) {
|
||||||
|
const arrNotifs = await SendNotif.find({idapp, dest: username, read: false}).lean();
|
||||||
|
if (arrNotifs) {
|
||||||
|
for (const rec of arrNotifs) {
|
||||||
|
await SendNotif.setNotifAsRead(idapp, username_call, rec._id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.send(true);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
res.status(400).send(e);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -90,15 +83,14 @@ router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => {
|
|||||||
|
|
||||||
if (req.user.idapp !== idapp) {
|
if (req.user.idapp !== idapp) {
|
||||||
// I'm trying to get something not mine!
|
// I'm trying to get something not mine!
|
||||||
return res.status(404).send({ code: server_constants.RIS_CODE_NOT_MY_USERNAME });
|
return res.status(404).send({code: server_constants.RIS_CODE_NOT_MY_USERNAME});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Extract all the todos of the userId only
|
// Extract all the todos of the userId only
|
||||||
return SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp).then((arrnotif) => {
|
return SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp).then((arrnotif) => {
|
||||||
// const wait = new Promise((resolve, reject) => {
|
// const wait = new Promise((resolve, reject) => {
|
||||||
// setTimeout(() => {
|
// setTimeout(() => {
|
||||||
res.send({ arrnotif });
|
res.send({arrnotif});
|
||||||
// }, 2000);
|
// }, 2000);
|
||||||
// });
|
// });
|
||||||
|
|
||||||
@@ -109,5 +101,4 @@ router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ const sendemail = require('../sendemail');
|
|||||||
|
|
||||||
const {Settings} = require('../models/settings');
|
const {Settings} = require('../models/settings');
|
||||||
|
|
||||||
|
const {SendNotif} = require('../models/sendnotif');
|
||||||
|
|
||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
const shared_consts = require('../tools/shared_nodejs');
|
const shared_consts = require('../tools/shared_nodejs');
|
||||||
|
|
||||||
@@ -377,11 +379,15 @@ router.patch('/:id', authenticate, (req, res) => {
|
|||||||
|
|
||||||
router.post('/profile', authenticate, (req, res) => {
|
router.post('/profile', authenticate, (req, res) => {
|
||||||
const username = req.body['username'];
|
const username = req.body['username'];
|
||||||
idapp = req.body.idapp;
|
const idapp = req.body.idapp;
|
||||||
locale = req.body.locale;
|
const locale = req.body.locale;
|
||||||
|
const idnotif = req.body['idnotif'] ? req.body['idnotif'] : '';
|
||||||
|
|
||||||
//++Todo: controlla che tipo di dati ha il permesso di leggere
|
//++Todo: controlla che tipo di dati ha il permesso di leggere
|
||||||
|
|
||||||
|
// Check if ìs a Notif to read
|
||||||
|
SendNotif.setNotifAsRead(idapp, username, idnotif);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return User.getUserProfileByUsername(idapp, username, req.user.username,
|
return User.getUserProfileByUsername(idapp, username, req.user.username,
|
||||||
false, req.user.perm).
|
false, req.user.perm).
|
||||||
@@ -447,7 +453,7 @@ router.post('/notifs', authenticate, async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (!!myuser) {
|
if (!!myuser) {
|
||||||
if (notifs) {
|
if (tools.isArray(notifs) && notifs.length >= 0) {
|
||||||
myuser.profile.notifs = notifs;
|
myuser.profile.notifs = notifs;
|
||||||
myuser.save();
|
myuser.save();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -576,6 +576,7 @@ module.exports = {
|
|||||||
'sender',
|
'sender',
|
||||||
'dest',
|
'dest',
|
||||||
'descr',
|
'descr',
|
||||||
|
'link',
|
||||||
'datenotif',
|
'datenotif',
|
||||||
'read',
|
'read',
|
||||||
'deleted',
|
'deleted',
|
||||||
@@ -2934,4 +2935,22 @@ module.exports = {
|
|||||||
return mystr;
|
return mystr;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
updateQueryStringParameter(uri, key, value) {
|
||||||
|
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
|
||||||
|
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
|
||||||
|
if (uri.match(re)) {
|
||||||
|
return uri.replace(re, '$1' + key + "=" + value + '$2');
|
||||||
|
} else {
|
||||||
|
return uri + separator + key + "=" + value;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
isArray(val) {
|
||||||
|
return Array.isArray(val)
|
||||||
|
},
|
||||||
|
|
||||||
|
getContentByTypeMsg (typemsg) {
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ const tools = require('./general');
|
|||||||
|
|
||||||
const shared_consts = require('./shared_nodejs');
|
const shared_consts = require('./shared_nodejs');
|
||||||
|
|
||||||
|
const sendemail = require('../sendemail');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
getTableByTableName(tablename) {
|
getTableByTableName(tablename) {
|
||||||
@@ -211,11 +213,55 @@ module.exports = {
|
|||||||
return mytable;
|
return mytable;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
checkifSendPushNotification() {
|
||||||
|
return process.env.ENABLE_PUSHNOTIFICATION === '1';
|
||||||
|
},
|
||||||
|
|
||||||
|
async sendNotif(res, idapp, user, recnotif, cmd) {
|
||||||
|
// Controlla nelle impostazioni che tipo di Notifica visualizzare
|
||||||
|
|
||||||
|
if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByPushNotification)) {
|
||||||
|
if (this.checkifSendPushNotification) {
|
||||||
|
console.log('SEND PUSH NOTIFICATION ');
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
sendreally: true,
|
||||||
|
typesend: shared_consts.TypeSend.PUSH_NOTIFICATION,
|
||||||
|
title: this.getNomeAppByIdApp(idapp),
|
||||||
|
content: recnotif.descr ? recnotif.descr : tools.getContentByTypeMsg(recnotif.typemsg),
|
||||||
|
openUrl: tools.updateQueryStringParameter(recnotif.link, 'idnotif', recnotif._id),
|
||||||
|
typemsg: recnotif.typemsg ? recnotif.typemsg : shared_consts.TypeMsg.SEND_TO_USER,
|
||||||
|
}
|
||||||
|
|
||||||
|
ris = await this.SendMsgToParam(idapp, params);
|
||||||
|
|
||||||
|
recnotif.link = mylink;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send Msg by EMAIL
|
||||||
|
if (emaildest && tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByEmail)) {
|
||||||
|
// Read from the operator table first
|
||||||
|
let emaildest = await Operator.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username);
|
||||||
|
if (!emaildest)
|
||||||
|
emaildest = await User.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username);
|
||||||
|
|
||||||
|
console.log('emaildest', emaildest);
|
||||||
|
|
||||||
|
await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recnotif);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
SendMsgToParam: async function(idapp, params) {
|
SendMsgToParam: async function(idapp, params) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const telegrambot = require('../telegram/telegrambot');
|
const telegrambot = require('../telegram/telegrambot');
|
||||||
|
|
||||||
|
if (params.typesend === 0)
|
||||||
|
params.typesend = shared_consts.TypeSend.PUSH_NOTIFICATION;
|
||||||
|
|
||||||
let query = {idapp};
|
let query = {idapp};
|
||||||
let addquery = {};
|
let addquery = {};
|
||||||
|
|
||||||
@@ -249,8 +295,8 @@ module.exports = {
|
|||||||
addquery = {
|
addquery = {
|
||||||
'profile.teleg_id': {$gt: 1},
|
'profile.teleg_id': {$gt: 1},
|
||||||
'profile.username_telegram': {$exists: true},
|
'profile.username_telegram': {$exists: true},
|
||||||
$expr: { $gt: [ { $strLenCP: "$profile.username_telegram" }, 3 ] },
|
$expr: {$gt: [{$strLenCP: '$profile.username_telegram'}, 3]},
|
||||||
'verified_by_aportador': {$exists: false}
|
'verified_by_aportador': {$exists: false},
|
||||||
};
|
};
|
||||||
} else if (params.typemsg === shared_consts.TypeMsg.SEND_TO_GROUP) {
|
} else if (params.typemsg === shared_consts.TypeMsg.SEND_TO_GROUP) {
|
||||||
if (params.groupnameDest) {
|
if (params.groupnameDest) {
|
||||||
@@ -287,8 +333,7 @@ module.exports = {
|
|||||||
let risult = null;
|
let risult = null;
|
||||||
|
|
||||||
if (params.sendreally) {
|
if (params.sendreally) {
|
||||||
if (tools.isBitActive(params.typesend,
|
if (tools.isBitActive(params.typesend, shared_consts.TypeSend.PUSH_NOTIFICATION)) {
|
||||||
shared_consts.TypeSend.PUSH_NOTIFICATION)) {
|
|
||||||
risult = tools.sendNotificationToUser(user._id, mytitle, mycontent, params.openUrl, params.openUrl2, params.tag,
|
risult = tools.sendNotificationToUser(user._id, mytitle, mycontent, params.openUrl, params.openUrl2, params.tag,
|
||||||
params.actions).
|
params.actions).
|
||||||
then(ris => {
|
then(ris => {
|
||||||
|
|||||||
Reference in New Issue
Block a user