Notifiche - Tutti - non letti

This commit is contained in:
Paolo Arena
2022-07-21 00:21:03 +02:00
parent cbd65ccdbe
commit e9ce597027
10 changed files with 235 additions and 177 deletions

View File

@@ -1,14 +1,14 @@
const mongoose = require('mongoose').set('debug', false)
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = "F";
mongoose.level = 'F';
const { ObjectID } = require('mongodb');
const {ObjectID} = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
schema.options.usePushEach = true;
});
const sendNotifSchema = new Schema({
@@ -27,6 +27,9 @@ const sendNotifSchema = new Schema({
descr: {
type: String,
},
link: {
type: String,
},
datenotif: {
type: Date,
},
@@ -35,36 +38,62 @@ const sendNotifSchema = new Schema({
},
read: {
type: Boolean,
default: false
default: false,
},
deleted: {
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;
return SendNotif.find({
$and: [
{ idapp },
{ 'dest': username },
{ 'datenotif': {$gt: new Date(lastdataread)} },
]
}).then((arrnotif) => {
{idapp},
{'dest': username},
{'datenotif': {$gt: new Date(lastdataread)}},
],
}).lean().sort({datenotif: -1}).then((arrnotif) => {
console.log('arrnotif', arrnotif.length);
return arrnotif
return arrnotif;
}).catch((err) => {
console.error('err', err);
});
};
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function (username, idapp) {
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp) {
const SendNotif = this;
return SendNotif.aggregate([
@@ -72,36 +101,34 @@ sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function (username, idap
$match: {
idapp,
dest: username,
}
},
},
{
$group:
{
_id: "$dest",
descr: { $last: "$message" },
datenotif: { $last: "$datenotif" },
read: { $last: "$read" }
}
{
_id: '$dest',
descr: {$last: '$message'},
datenotif: {$last: '$datenotif'},
read: {$last: '$read'},
},
},
{
$sort: { datenotif: -1 }
$sort: {datenotif: -1},
},
])
.then((arrnotif) => {
// Remove duplicate
// Exclude my chat
const myarr = arrnotif.filter((ris) => ris._id !== username);
// console.table(myarr);
return myarr
]).then((arrnotif) => {
// Remove duplicate
// Exclude my chat
const myarr = arrnotif.filter((ris) => ris._id !== username);
// console.table(myarr);
return myarr;
}).catch((err) => {
console.error(err);
});
}).catch((err) => {
console.error(err);
});
};
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
module.exports = { SendNotif: SendNotif };
module.exports = {SendNotif: SendNotif};