- 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;
});

View File

@@ -189,6 +189,7 @@ const SiteSchema = new Schema({
},
ecomm: {
enablePreOrders: { type: Boolean, default: false },
NoteExtraOnCart: { type: String, default: '' },
}
});

View File

@@ -56,6 +56,9 @@ const UserSchema = new mongoose.Schema({
message: '{VALUE} is not a valid email'
}*/
},
hash: {
type: String,
},
idapp: {
type: String,
required: true,
@@ -164,6 +167,12 @@ const UserSchema = new mongoose.Schema({
news_on: {
type: Boolean,
},
email_errata: {
type: Boolean,
},
lastid_newstosent: {
type: String
},
aportador_solidario: { // da cancellare
type: String,
},
@@ -1146,6 +1155,70 @@ UserSchema.statics.setaportador_solidario = async function (
return !!myrec;
};
UserSchema.statics.setNewsletter = async function (
idapp, username, newsletter_on) {
const User = this;
if (username === undefined)
return false;
const myquery = {
'idapp': idapp,
'username': username,
};
const myrec = await User.findOneAndUpdate(myquery,
{ $set: { 'news_on': newsletter_on } }, { new: false });
return !!myrec;
};
UserSchema.statics.setEmailErrata = async function (
idapp, username, email_errata) {
const User = this;
if (username === undefined)
return false;
const myquery = {
'idapp': idapp,
'username': username,
};
const myrec = await User.findOneAndUpdate(myquery,
{ $set: { email_errata } }, { new: false });
return !!myrec;
};
UserSchema.statics.isEmailErrata = async function (idapp, username) {
const User = this;
return await User.findOne({
idapp, username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}).then((rec) => {
return ((rec) ? rec.email_errata : false);
}).catch((e) => {
return false;
});
};
UserSchema.statics.isNewsletterOn = async function (idapp, username) {
const User = this;
return await User.findOne({
idapp, username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}).then((rec) => {
return ((rec) ? rec.news_on : false);
}).catch((e) => {
console.error('isNewsletterOn', e);
return false;
});
};
UserSchema.statics.setVerifiedByAportadorToALL = async function () {
return await User.updateMany({}, { $set: { 'verified_by_aportador': true } },
@@ -1549,6 +1622,19 @@ UserSchema.statics.getUsernameById = async function (idapp, id) {
});
};
UserSchema.statics.getUsernameByEmail = async function (idapp, email) {
const User = this;
return await User.findOne({
idapp, email,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}, { username: 1 }).then((myuser) => {
return ((myuser) ? myuser.username : '');
}).catch((e) => {
});
};
UserSchema.statics.getUserById = function (idapp, id) {
const User = this;