Skip to content

Commit

Permalink
feat: add actuality error mail
Browse files Browse the repository at this point in the history
  • Loading branch information
baptadn committed Jan 20, 2025
1 parent 8845188 commit 5cac5cb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
20 changes: 20 additions & 0 deletions back/api/actuality/services/actuality.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ const validateEmail = (email) => {
};

module.exports = {
async sendActualityErrorEmail(actuality, email, error) {
let errorMessage = `Une erreur est survenue lors de la publication de l'actualité "${actuality.title}". Merci de vérifier les informations de l'actualité et réessayer.`

if (error === 'not_published') {
errorMessage = `Les notificiations concernant l'actualité "${actuality.title}" n'ont pas été envoyées car elle est encore en brouillon et ce malgré le fait que la date de publication soit passée. Merci de la publier pour pouvoir envoyer les notifications.`
}

await strapi.plugins['email'].services.email.sendEmail(
{
to: email,
},
{
templateId: 'actuality-error',
},
{
actuality_error: errorMessage,
actuality_url: `${process.env.BASE_URL}/admin/plugins/content-manager/collectionType/application::actuality.actuality/${actuality.id}`,
}
)
},
async sendActualityEmails(actuality, emails) {
const date = new Date().toLocaleDateString('fr-FR', { year: 'numeric', month: 'long', day: 'numeric' });
const image = await strapi.query('file', 'upload').findOne({ id: actuality.image });
Expand Down
31 changes: 22 additions & 9 deletions back/config/functions/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ module.exports = {
notification_email_broadcast_date_null: false,
notification_email_broadcast_date_lte: new Date(),
notification_email_sent_at_null: true,
published_at_null: false,
});

const users = await knex.raw(`
Expand All @@ -80,21 +79,35 @@ module.exports = {
(type = 'place' AND place.id IS NOT NULL)
)
`);

for (const actuality of actualities) {
try {
const emails = [...new Set(users.rows.map(user => user.email))]
if (!actuality.published_at) {
throw new Error('not_published');
}

const emails = [...new Set(users.rows.map(user => user.email))]
console.log(`Sending actuality ${actuality.id} email to ${emails.length} users`)

await strapi.services.actuality.sendActualityEmails({ ...actuality, image: actuality.image.id }, emails);
await strapi.query("actuality").update(
{ id: actuality.id }, {
notification_email_sent_at: new Date(),
});
} catch (error) {
console.log(`Error sending actuality ${actuality.id} email`, error);
}
let errorType = "unknown"

if (error.message === 'not_published') {
errorType = 'not_published'
}

await strapi.query("actuality").update(
{ id: actuality.id }, {
notification_email_sent_at: new Date(),
},);
await strapi.services.actuality.sendActualityErrorEmail(actuality, process.env.EMAIL_RECIPIENT, errorType);
await strapi.query("actuality").update(
{ id: actuality.id }, {
notification_email_broadcast_date: null,
notification_email_sent_at: null,
})
}
}
},
}
};

0 comments on commit 5cac5cb

Please sign in to comment.