Files
freeplanet_serverside/server/models/sendmsg.js
Paolo Arena 9205468065 - Message notify when 'Ask Info' and user is not logged
- Ask Info and Book show message if not logged
- TableField fixed and added some features
2019-11-04 20:30:09 +01:00

113 lines
2.2 KiB
JavaScript

const mongoose = require('mongoose');
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 sendmsgSchema = new Schema({
idapp: {
type: String,
},
source: {
page: { type: String },
event_id: { type: String }
},
origin: {
username: { type: String },
},
dest: {
idapp: { type: String, },
username: { type: String },
},
message: {
type: String,
},
datemsg: {
type: Date,
},
status: {
type: Number,
},
options: {
type: Number,
},
read: {
type: Boolean,
default: false
},
deleted: {
type: Boolean,
default: false
},
});
sendmsgSchema.statics.findAllMsgByUsernameIdAndIdApp = function (username, lastdataread, idapp) {
const SendMsg = this;
return SendMsg.find({
$and: [
{ $or: [ { 'dest.username': username }, { 'origin.username': username },] },
{ 'datemsg': {$gt: new Date(lastdataread)} },
{ idapp }
]
}).then((arrmsg) => {
console.log('arrmsg', arrmsg.length);
return arrmsg
}).catch((err) => {
console.error('err', err);
});
};
sendmsgSchema.statics.findLastGroupByUserIdAndIdApp = function (userId, username, idapp) {
const SendMsg = this;
return SendMsg.aggregate([
{
$match: {
$or: [{ 'origin.username': username }, { 'dest.username': username }, { idapp }],
$and: [{ idapp }]
}
},
{
$group:
{
_id: "$dest.username",
message: { $last: "$message" },
datemsg: { $last: "$datemsg" },
dest: { $last: "$dest" },
origin: { $last: "$origin" },
read: { $last: "$read" }
}
},
{
$sort: { datemsg: -1 }
},
])
.then((arrmsg) => {
// Remove duplicate
// Exclude my chat
const myarr = arrmsg.filter((ris) => ris._id !== username);
// console.table(myarr);
return myarr
}).catch((err) => {
console.error(err);
});
};
const SendMsg = mongoose.model('SendMsg', sendmsgSchema);
module.exports = { SendMsg };