- aggiornato sistema per inviare le newsletter !

This commit is contained in:
Surya Paolo
2024-02-08 01:34:30 +01:00
parent 2151502d30
commit dacfc5a844
8 changed files with 223 additions and 85 deletions

View File

@@ -6,6 +6,7 @@ const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
const { User } = require('./user');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
@@ -13,115 +14,84 @@ mongoose.plugin(schema => {
});
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();
params.fieldsearch = User.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
MailingListSchema.statics.findAllIdAppSubscribed = function (idapp) {
const MailingList = this;
const myfind = { idapp, statesub: true, wrongerr: { $ne: true } };
const myfind = {
idapp,
news_on: true,
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
$or: [
{ email_errata: { $exists: false } },
{ email_errata: { $exists: true, $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 User.find(myfind, (err, arrrec) => {
return arrrec
});
};
MailingListSchema.statics.getnumSent = async function (idapp, idmailinglist) {
const MailingList = this;
MailingListSchema.statics.getnumSent = async function (idapp, idUser) {
const myfind = { idapp, statesub: true, lastid_newstosent: idmailinglist };
const myfind = { idapp, news_on: true, lastid_newstosent: idUser };
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return await MailingList.countDocuments(myfind);
return await User.countDocuments(myfind);
};
MailingListSchema.statics.isOk = async function (idapp, iduser, idmailinglist) {
const MailingList = this;
MailingListSchema.statics.isOk = async function (idapp, iduser, idUser) {
const myfind = { idapp, _id: iduser, statesub: true, lastid_newstosent: { $ne: idmailinglist } };
const myfind = { idapp, _id: iduser, news_on: true, lastid_newstosent: { $ne: idUser } };
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return await MailingList.countDocuments(myfind) > 0;
return await User.countDocuments(myfind) > 0;
};
MailingListSchema.statics.findAllIdApp = async function (idapp) {
const MailingList = this;
const myfind = { idapp };
return await MailingList.find(myfind, (err, arrrec) => {
return await User.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);
let myperson = await User.findOne({ idapp, hash });
if (!!myperson) {
return (!myperson.statesub)
return (!myperson.news_on)
}
};
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 User.findOne(myfind, (err, rec) => {
return rec
});
};
const MailingList = mongoose.model('MailingList', MailingListSchema);
MailingList.createIndexes((err) => {
User.createIndexes((err) => {
if (err) throw err;
});