- Create Newsletter Page: MailingList (without the class style, using Gulp tasks)#94
This commit is contained in:
112
src/server/models/sendmsg.js
Normal file
112
src/server/models/sendmsg.js
Normal file
@@ -0,0 +1,112 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user