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

125 lines
3.0 KiB
JavaScript
Executable File

const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const MailingListSchema = new Schema({
idapp: {
type: String,
},
email: {
type: String,
trim: true,
},
hash: {
type: String,
},
name: {
type: String,
trim: true,
},
surname: {
type: String,
trim: true,
},
statesub: {
type: Boolean,
default: true
},
wrongerr: {
type: Boolean,
default: false
},
lastid_newstosent: {
type: String
}
});
MailingListSchema.statics.getFieldsForSearch = function () {
return [{ field: 'name', type: tools.FieldType.string },
{ field: 'surname', type: tools.FieldType.string },
{ field: 'email', type: tools.FieldType.string }]
};
MailingListSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
MailingListSchema.statics.findAllIdAppSubscribed = function (idapp) {
const MailingList = this;
const myfind = { idapp, statesub: true, wrongerr: { $ne: true } };
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return MailingList.find(myfind, (err, arrrec) => {
return arrrec
});
};
MailingListSchema.statics.getnumSent = async function (idapp, idmailinglist) {
const MailingList = this;
const myfind = { idapp, statesub: true, lastid_newstosent: idmailinglist };
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return await MailingList.countDocuments(myfind);
};
MailingListSchema.statics.isOk = async function (idapp, iduser, idmailinglist) {
const MailingList = this;
const myfind = { idapp, _id: iduser, statesub: true, lastid_newstosent: { $ne: idmailinglist } };
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return await MailingList.countDocuments(myfind) > 0;
};
MailingListSchema.statics.findAllIdApp = async function (idapp) {
const MailingList = this;
const myfind = { idapp };
return await MailingList.find(myfind, (err, arrrec) => {
return arrrec
});
};
MailingListSchema.statics.isUnsubscribed = async function (idapp, hash) {
const MailingList = this;
let myperson = await MailingList.findOne({ idapp, hash });
console.log('myperson', myperson);
if (!!myperson) {
return (!myperson.statesub)
}
};
MailingListSchema.statics.findByHash = function (idapp, hash) {
const MailingList = this;
const myfind = { idapp, hash };
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return MailingList.findOne(myfind, (err, rec) => {
return rec
});
};
const MailingList = mongoose.model('MailingList', MailingListSchema);
module.exports = { MailingList };