Files
freeplanet_serverside/src/server/models/sendnotif.js

135 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-07-21 00:21:03 +02:00
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
2022-07-21 00:21:03 +02:00
mongoose.level = 'F';
2022-07-21 00:21:03 +02:00
const {ObjectID} = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
2022-07-21 00:21:03 +02:00
schema.options.usePushEach = true;
});
const sendNotifSchema = new Schema({
idapp: {
type: String,
},
type: {
type: Number,
},
sender: { // mittente
type: String,
},
dest: {
type: String,
},
descr: {
type: String,
},
2022-07-21 00:21:03 +02:00
link: {
type: String,
},
datenotif: {
type: Date,
},
status: {
type: Number,
},
read: {
type: Boolean,
2022-07-21 00:21:03 +02:00
default: false,
},
deleted: {
type: Boolean,
2022-07-21 00:21:03 +02:00
default: false,
},
});
2022-07-21 00:21:03 +02:00
sendNotifSchema.statics.setNotifAsRead = function(idapp, username, idnotif) {
const SendNotif = this;
2022-07-21 00:21:03 +02:00
try {
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;
}
};
2022-07-21 00:21:03 +02:00
sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, lastdataread, idapp) {
const SendNotif = this;
return SendNotif.find({
$and: [
2022-07-21 00:21:03 +02:00
{idapp},
{'dest': username},
{'datenotif': {$gt: new Date(lastdataread)}},
],
}).lean().sort({datenotif: -1}).then((arrnotif) => {
console.log('arrnotif', arrnotif.length);
2022-07-21 00:21:03 +02:00
return arrnotif;
}).catch((err) => {
console.error('err', err);
});
};
2022-07-21 00:21:03 +02:00
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp) {
const SendNotif = this;
return SendNotif.aggregate([
{
$match: {
idapp,
dest: username,
2022-07-21 00:21:03 +02:00
},
},
{
$group:
2022-07-21 00:21:03 +02:00
{
_id: '$dest',
descr: {$last: '$message'},
datenotif: {$last: '$datenotif'},
read: {$last: '$read'},
},
},
{
2022-07-21 00:21:03 +02:00
$sort: {datenotif: -1},
},
2022-07-21 00:21:03 +02:00
]).then((arrnotif) => {
// Remove duplicate
// Exclude my chat
const myarr = arrnotif.filter((ris) => ris._id !== username);
// console.table(myarr);
return myarr;
2022-07-21 00:21:03 +02:00
}).catch((err) => {
console.error(err);
});
2022-07-21 00:21:03 +02:00
};
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
2022-07-21 00:21:03 +02:00
module.exports = {SendNotif: SendNotif};