135 lines
2.5 KiB
JavaScript
Executable File
135 lines
2.5 KiB
JavaScript
Executable File
const mongoose = require('mongoose').set('debug', false);
|
|
const Schema = mongoose.Schema;
|
|
|
|
mongoose.Promise = global.Promise;
|
|
mongoose.level = 'F';
|
|
|
|
const {ObjectID} = require('mongodb');
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
mongoose.plugin(schema => {
|
|
schema.options.usePushEach = true;
|
|
});
|
|
|
|
const sendNotifSchema = new Schema({
|
|
idapp: {
|
|
type: String,
|
|
},
|
|
type: {
|
|
type: Number,
|
|
},
|
|
sender: { // mittente
|
|
type: String,
|
|
},
|
|
dest: {
|
|
type: String,
|
|
},
|
|
descr: {
|
|
type: String,
|
|
},
|
|
link: {
|
|
type: String,
|
|
},
|
|
datenotif: {
|
|
type: Date,
|
|
},
|
|
status: {
|
|
type: Number,
|
|
},
|
|
read: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
deleted: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
|
|
});
|
|
|
|
sendNotifSchema.statics.setNotifAsRead = function(idapp, username, idnotif) {
|
|
const SendNotif = this;
|
|
|
|
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;
|
|
}
|
|
};
|
|
|
|
sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, lastdataread, idapp) {
|
|
const SendNotif = this;
|
|
|
|
return SendNotif.find({
|
|
$and: [
|
|
{idapp},
|
|
{'dest': username},
|
|
{'datenotif': {$gt: new Date(lastdataread)}},
|
|
],
|
|
}).lean().sort({datenotif: -1}).then((arrnotif) => {
|
|
console.log('arrnotif', arrnotif.length);
|
|
return arrnotif;
|
|
}).catch((err) => {
|
|
console.error('err', err);
|
|
});
|
|
|
|
};
|
|
|
|
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp) {
|
|
const SendNotif = this;
|
|
|
|
return SendNotif.aggregate([
|
|
{
|
|
$match: {
|
|
idapp,
|
|
dest: username,
|
|
},
|
|
},
|
|
{
|
|
$group:
|
|
{
|
|
_id: '$dest',
|
|
descr: {$last: '$message'},
|
|
datenotif: {$last: '$datenotif'},
|
|
read: {$last: '$read'},
|
|
},
|
|
|
|
},
|
|
{
|
|
$sort: {datenotif: -1},
|
|
},
|
|
]).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);
|
|
});
|
|
|
|
};
|
|
|
|
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
|
|
|
|
module.exports = {SendNotif: SendNotif};
|