- Sending Newsletter, others settings.
This commit is contained in:
@@ -1 +1 @@
|
|||||||
=`Newsletter `
|
=`${dataemail.templ.subject}`
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ const tools = require('../tools/general');
|
|||||||
|
|
||||||
const { ObjectID } = require('mongodb');
|
const { ObjectID } = require('mongodb');
|
||||||
|
|
||||||
|
const { Settings } = require('./settings');
|
||||||
|
|
||||||
// Resolving error Unknown modifier: $pushAll
|
// Resolving error Unknown modifier: $pushAll
|
||||||
mongoose.plugin(schema => {
|
mongoose.plugin(schema => {
|
||||||
schema.options.usePushEach = true
|
schema.options.usePushEach = true
|
||||||
@@ -113,13 +115,13 @@ MyEventSchema.statics.findAllIdApp = function (idapp) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
MyEventSchema.statics.getLastEvents = function (idapp) {
|
MyEventSchema.statics.getLastEvents = async function (idapp) {
|
||||||
const Event = this;
|
const Event = this;
|
||||||
|
|
||||||
const lastn = 3;
|
const lastn = await Settings.getValDbSettings(idapp, 'SHOW_LAST_N_EV', 1);
|
||||||
|
|
||||||
const query = [
|
const query = [
|
||||||
{ $match: { idapp } },
|
{ $match: { idapp, dateTimeStart: { $gte: tools.IncDateNow(- 1000 * 60 * 60 * 24) } } },
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: 'operators',
|
from: 'operators',
|
||||||
@@ -152,7 +154,11 @@ MyEventSchema.statics.getLastEvents = function (idapp) {
|
|||||||
.aggregate(query)
|
.aggregate(query)
|
||||||
.then((arrrec) => {
|
.then((arrrec) => {
|
||||||
// console.table(arrrec);
|
// console.table(arrrec);
|
||||||
return arrrec.slice(-lastn)
|
if (lastn > 0) {
|
||||||
|
return arrrec.slice(0, lastn);
|
||||||
|
} else {
|
||||||
|
return arrrec
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ SettingsSchema.statics.executeQueryTable = function (idapp, params) {
|
|||||||
return tools.executeQueryTable(this, idapp, params);
|
return tools.executeQueryTable(this, idapp, params);
|
||||||
};
|
};
|
||||||
|
|
||||||
SettingsSchema.statics.getValDbSettings = function (idapp, key) {
|
SettingsSchema.statics.getValDbSettings = function (idapp, key, def) {
|
||||||
return Settings.findOne({ idapp, key })
|
return Settings.findOne({ idapp, key })
|
||||||
.then((myrec) => {
|
.then((myrec) => {
|
||||||
// console.log('getValDbSettings', myrec, 'idapp', idapp);
|
// console.log('getValDbSettings', myrec, 'idapp', idapp);
|
||||||
@@ -63,12 +63,12 @@ SettingsSchema.statics.getValDbSettings = function (idapp, key) {
|
|||||||
else
|
else
|
||||||
return myrec.value_str;
|
return myrec.value_str;
|
||||||
} else {
|
} else {
|
||||||
return ''
|
return def
|
||||||
}
|
}
|
||||||
|
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error('getValDbSettings', err);
|
console.error('getValDbSettings', err);
|
||||||
return null;
|
return def;
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ module.exports = {
|
|||||||
|
|
||||||
// console.log("check EMAIL :" + checkifSendEmail());
|
// console.log("check EMAIL :" + checkifSendEmail());
|
||||||
|
|
||||||
|
if (replyTo === '')
|
||||||
|
replyTo = mylocalsconf.dataemail.email_reply;
|
||||||
|
|
||||||
const paramemail = {
|
const paramemail = {
|
||||||
message: {
|
message: {
|
||||||
from: mylocalsconf.dataemail.from, // sender address
|
from: mylocalsconf.dataemail.from, // sender address
|
||||||
@@ -66,7 +69,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('paramemail', paramemail);
|
// console.log('paramemail', paramemail);
|
||||||
|
|
||||||
const email = new Email(paramemail);
|
const email = new Email(paramemail);
|
||||||
|
|
||||||
@@ -79,6 +82,7 @@ module.exports = {
|
|||||||
locals: mylocalsconf,
|
locals: mylocalsconf,
|
||||||
})
|
})
|
||||||
.then((ris) => {
|
.then((ris) => {
|
||||||
|
console.log('ris EMAIL', ris);
|
||||||
return !!ris
|
return !!ris
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@@ -408,6 +412,7 @@ module.exports = {
|
|||||||
disc_title: await Settings.getValDbSettings(idapp, 'DISC_TITLE'),
|
disc_title: await Settings.getValDbSettings(idapp, 'DISC_TITLE'),
|
||||||
height_logo: await Settings.getValDbSettings(idapp, 'HEIGHT_LOGO'),
|
height_logo: await Settings.getValDbSettings(idapp, 'HEIGHT_LOGO'),
|
||||||
from: await Settings.getValDbSettings(idapp, 'EMAIL_FROM'),
|
from: await Settings.getValDbSettings(idapp, 'EMAIL_FROM'),
|
||||||
|
email_reply: await Settings.getValDbSettings(idapp, 'EMAIL_REPLY', ''),
|
||||||
pwd_from: await Settings.getValDbSettings(idapp, 'PWD_FROM'),
|
pwd_from: await Settings.getValDbSettings(idapp, 'PWD_FROM'),
|
||||||
email_service: await Settings.getValDbSettings(idapp, 'EMAIL_SERVICE_SEND'),
|
email_service: await Settings.getValDbSettings(idapp, 'EMAIL_SERVICE_SEND'),
|
||||||
email_port: await Settings.getValDbSettings(idapp, 'EMAIL_PORT'),
|
email_port: await Settings.getValDbSettings(idapp, 'EMAIL_PORT'),
|
||||||
@@ -481,11 +486,15 @@ module.exports = {
|
|||||||
|
|
||||||
const smtpTransport = this.getTransport();
|
const smtpTransport = this.getTransport();
|
||||||
|
|
||||||
|
let secpause = await Settings.getValDbSettings(idapp, 'MSEC_PAUSE_SEND', process.env.DELAY_SENDEMAIL);
|
||||||
|
if (secpause > process.env.DELAY_SENDEMAIL)
|
||||||
|
secpause = process.env.DELAY_SENDEMAIL;
|
||||||
|
|
||||||
// LOOP USERS EMAIL
|
// LOOP USERS EMAIL
|
||||||
for (const user of userstosend) {
|
for (const user of userstosend) {
|
||||||
|
|
||||||
if (process.env.DEBUG)
|
if (process.env.DEBUG)
|
||||||
await tools.snooze(10000);
|
await tools.snooze(30000);
|
||||||
|
|
||||||
activate = await Newstosent.isActivated(id_newstosent);
|
activate = await Newstosent.isActivated(id_newstosent);
|
||||||
|
|
||||||
@@ -508,7 +517,7 @@ module.exports = {
|
|||||||
if (user.lastid_newstosent !== id_newstosent.toString()) {
|
if (user.lastid_newstosent !== id_newstosent.toString()) {
|
||||||
// Send Email to the User
|
// Send Email to the User
|
||||||
console.log('-> Invio Email (', mynewsrec.numemail_sent, '/', mynewsrec.numemail_tot, ')');
|
console.log('-> Invio Email (', mynewsrec.numemail_sent, '/', mynewsrec.numemail_tot, ')');
|
||||||
const esito = this.sendEmail_base('newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '', smtpTransport);
|
const esito = this.sendEmail_base('newsletter/' + lang, mylocalsconf.emailto, mylocalsconf, mylocalsconf.dataemail.email_reply, smtpTransport);
|
||||||
|
|
||||||
//Put the result in the database, to check if is sent or not.
|
//Put the result in the database, to check if is sent or not.
|
||||||
const updateml = await MailingList.findOneAndUpdate({
|
const updateml = await MailingList.findOneAndUpdate({
|
||||||
@@ -518,7 +527,7 @@ module.exports = {
|
|||||||
|
|
||||||
|
|
||||||
//Delay for send email...
|
//Delay for send email...
|
||||||
await tools.snooze(process.env.DELAY_SENDEMAIL);
|
await tools.snooze(secpause);
|
||||||
|
|
||||||
|
|
||||||
mynewsrec.lastemailsent_Job = new Date();
|
mynewsrec.lastemailsent_Job = new Date();
|
||||||
@@ -597,6 +606,9 @@ module.exports = {
|
|||||||
hashemail: tools.getHash(myemail),
|
hashemail: tools.getHash(myemail),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log('myarrevents');
|
||||||
|
console.table(mylocalsconf.arrevents);
|
||||||
|
|
||||||
mylocalsconf.dataemail = await this.getdataemail(idapp);
|
mylocalsconf.dataemail = await this.getdataemail(idapp);
|
||||||
|
|
||||||
this.replacefields(mylocalsconf);
|
this.replacefields(mylocalsconf);
|
||||||
@@ -604,7 +616,7 @@ module.exports = {
|
|||||||
const smtpTransport = this.getTransport();
|
const smtpTransport = this.getTransport();
|
||||||
|
|
||||||
console.log('-> Invio Email TEST a', mylocalsconf.emailto, 'previewonly', previewonly);
|
console.log('-> Invio Email TEST a', mylocalsconf.emailto, 'previewonly', previewonly);
|
||||||
return this.sendEmail_base('newsletter/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '', smtpTransport, previewonly);
|
return this.sendEmail_base('newsletter/' + lang, mylocalsconf.emailto, mylocalsconf, '', smtpTransport, previewonly);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -230,8 +230,8 @@ function testmsgwebpush() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cron every 5 minutes
|
// Cron every X minutes
|
||||||
cron.schedule('*/5 * * * *', () => {
|
cron.schedule('*/2 * * * *', () => {
|
||||||
console.log('Running Cron Job');
|
console.log('Running Cron Job');
|
||||||
mycron();
|
mycron();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user