Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Système de notification pour actualités #160

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions back/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ DO_SPACE_BUCKET=
DO_SPACE_DIRECTORY=
BUGSNAG_API_KEY=
MAPBOX_TOKEN=
EMAIL_TEST=
HASH_EMAIL_NOTIFICATION=
16 changes: 16 additions & 0 deletions back/api/actuality/config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/notifications/update",
"handler": "actuality.updateNotifications",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/notifications/info",
"handler": "actuality.infoNotifications",
"config": {
"policies": []
}
}
]
}
28 changes: 28 additions & 0 deletions back/api/actuality/controllers/actuality.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,32 @@ module.exports = {
const entity = await strapi.services.actuality.findOne({ slug: id });
return sanitizeEntity(entity, { model: strapi.models.actuality });
},
async infoNotifications(ctx) {
const { email } = ctx.query;
const user = await strapi.query('user', 'users-permissions').findOne({ email: decodeURIComponent(email) });

if (!user) {
return {
success: false,
message: 'User not found',
};
}

return {
hasSubscribeActualityEmail: user.hasSubscribeActualityEmail,
};
},
async updateNotifications(ctx) {
const { email, hasSubscribeActualityEmail } = ctx.request.body;

const user = await strapi.query('user', 'users-permissions').update(
{ email },
{ hasSubscribeActualityEmail: hasSubscribeActualityEmail }
);

return {
success: true,
hasSubscribeActualityEmail: user.hasSubscribeActualityEmail,
};
},
};
162 changes: 162 additions & 0 deletions back/api/actuality/documentation/1.0.0/actuality.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,134 @@
}
]
}
},
"/notifications/update": {
"post": {
"deprecated": false,
"description": "Create a new record",
"responses": {
"200": {
"description": "response",
"content": {
"application/json": {
"schema": {
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "Not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"summary": "",
"tags": [
"Actuality"
],
"requestBody": {
"description": "",
"required": true,
"content": {
"application/json": {
"schema": {
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
}
}
},
"/notifications/info": {
"get": {
"deprecated": false,
"description": "",
"responses": {
"200": {
"description": "response",
"content": {
"application/json": {
"schema": {
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "Not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"summary": "",
"tags": [
"Actuality"
],
"parameters": []
}
}
},
"components": {
Expand Down Expand Up @@ -601,6 +729,23 @@
"slug": {
"type": "string"
},
"notification_email_subject": {
"type": "string"
},
"notification_email_message": {
"type": "string"
},
"notification_email_sent_at": {
"type": "string",
"format": "date-time"
},
"notification_email_broadcast_date": {
"type": "string",
"format": "date-time"
},
"notification_email_test": {
"type": "string"
},
"published_at": {
"type": "string",
"format": "date-time"
Expand All @@ -622,6 +767,23 @@
"slug": {
"type": "string"
},
"notification_email_subject": {
"type": "string"
},
"notification_email_message": {
"type": "string"
},
"notification_email_sent_at": {
"type": "string",
"format": "date-time"
},
"notification_email_broadcast_date": {
"type": "string",
"format": "date-time"
},
"notification_email_test": {
"type": "string"
},
"published_at": {
"type": "string",
"format": "date-time"
Expand Down
11 changes: 11 additions & 0 deletions back/api/actuality/models/actuality.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
const createSlug = require("url-slug");

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
* to customize this model
Expand All @@ -16,6 +17,16 @@ module.exports = {
if (data.title) {
data.slug = createSlug(data.title);
}

if (data.notification_email_test) {

try {
await strapi.services.actuality.sendActualityEmails(data, [data.notification_email_test]);
data.notification_email_test = null;
} catch (error) {
console.error('Failed to send test email:', error);
}
}
},
},
};
18 changes: 17 additions & 1 deletion back/api/actuality/models/actuality.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@
"images"
],
"plugin": "upload",
"required": true
"required": true,
"pluginOptions": {}
},
"slug": {
"type": "string",
"required": false,
"unique": true
},
"notification_email_subject": {
"type": "string"
},
"notification_email_message": {
"type": "richtext"
},
"notification_email_sent_at": {
"type": "datetime"
},
"notification_email_broadcast_date": {
"type": "datetime"
},
"notification_email_test": {
"type": "email"
}
}
}
47 changes: 46 additions & 1 deletion back/api/actuality/services/actuality.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
'use strict';
const removeMd = require('remove-markdown')
const showdown = require('showdown');
const CryptoJS = require('crypto-js');

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services)
* to customize this service
*/

module.exports = {};
const validateEmail = (email) => {
return String(email)
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
};

module.exports = {
async sendActualityEmails(actuality, emails) {
const converter = new showdown.Converter();
const date = new Date().toLocaleDateString('fr-FR', { year: 'numeric', month: 'long', day: 'numeric' });
const image = await strapi.query('file', 'upload').findOne({ id: actuality.image });

const token = CryptoJS.AES.encrypt(
actuality.notification_email_test,
process.env.HASH_EMAIL_NOTIFICATION,
).toString()

for (const email of emails) {
if (validateEmail(email)) {
await strapi.plugins['email'].services.email.sendEmail(
{
to: email,
},
{
templateId: 'actuality-notification',
subject: actuality.notification_email_subject,
},
{
actuality_title: actuality.title,
actuality_date: date,
actuality_image: image.formats.medium.url,
actuality_description: removeMd(actuality.content).split(' ').slice(0, 35).join(' ') + '...',
actuality_link: `${process.env.FRONT_URL}/actualites/${actuality.slug}`,
notification_link: `${process.env.FRONT_URL}/notifications?token=${token}`,
email_message: actuality.notification_email_message ? converter.makeHtml(actuality.notification_email_message) : null,
}
)
}
}
},
};
Loading
Loading