0.12.14 : add notification on friend invite
All checks were successful
Build release Docker image / Build Docker Images (push) Successful in 7m0s
All checks were successful
Build release Docker image / Build Docker Images (push) Successful in 7m0s
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "harmony-back",
|
||||
"version": "0.12.13",
|
||||
"version": "0.12.14",
|
||||
"private": true,
|
||||
"description": "A Strapi application",
|
||||
"scripts": {
|
||||
|
||||
51
src/api/contact/content-types/contact/lifecycles.ts
Normal file
51
src/api/contact/content-types/contact/lifecycles.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
// src/api/contact/content-types/contact/lifecycles.js
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Se déclenche après qu'un contact a été créé.
|
||||
* @param {Object} result - L'entrée de contact qui vient d'être créée.
|
||||
*/
|
||||
async afterCreate(result) {
|
||||
// On vérifie si l'état du contact est bien 'pending'
|
||||
if (result.result.state === "pending") {
|
||||
const populatedContact = await strapi
|
||||
.documents("api::contact.contact")
|
||||
.findOne({
|
||||
documentId: result.result.documentId, // On utilise documentId au lieu de id
|
||||
populate: {
|
||||
owner: true, // On peuple la relation 'owner'
|
||||
user: true, // On peuple la relation 'user'
|
||||
},
|
||||
});
|
||||
const { user, owner } = populatedContact;
|
||||
|
||||
// On s'assure que le receiver et le sender existent bien
|
||||
if (user && owner) {
|
||||
try {
|
||||
// Appel du service de notification
|
||||
await strapi
|
||||
.service("api::notification.notification")
|
||||
?.createNotification({
|
||||
title: "Demande d'ami",
|
||||
message: `Tu as été invité par ${owner.username} à être son ami.`,
|
||||
type: "success",
|
||||
target_user: user.id,
|
||||
source: "system",
|
||||
payload: {},
|
||||
});
|
||||
|
||||
strapi.log.info(
|
||||
`Notification sent to user ${user.id} for new contact invitation from ${owner.id}.`
|
||||
);
|
||||
} catch (error) {
|
||||
// C'est une bonne pratique d'attraper les erreurs pour ne pas
|
||||
// bloquer la création du contact si l'envoi de la notification échoue.
|
||||
strapi.log.error(
|
||||
"Failed to send notification after contact creation:",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
58
src/api/contact/document-middleware.ts
Normal file
58
src/api/contact/document-middleware.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
// path: src/api/contact/document-middleware.ts
|
||||
|
||||
export default {
|
||||
/**
|
||||
* Déclenché à la création d’un document Contact
|
||||
*/
|
||||
async beforeCreate(event) {
|
||||
// Pas besoin ici, mais tu peux faire des validations avant création
|
||||
},
|
||||
|
||||
async afterCreate(event) {
|
||||
const { result } = event;
|
||||
|
||||
// On récupère les infos du contact
|
||||
const contact = result;
|
||||
|
||||
// On agit seulement sur les contacts en attente
|
||||
if (contact.state !== "pending") return;
|
||||
|
||||
const fromUserId = contact.fromUser?.id || contact.fromUser;
|
||||
const toUserId = contact.toUser?.id || contact.toUser;
|
||||
|
||||
if (!fromUserId || !toUserId) {
|
||||
strapi.log.warn("Notification non créée : fromUser ou toUser manquant.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
/*await strapi.service("api::notification.notification").create({
|
||||
data: {
|
||||
type: "contact-request",
|
||||
title: "Nouvelle demande de contact",
|
||||
message: `L'utilisateur #${fromUserId} vous a envoyé une demande de contact.`,
|
||||
user: toUserId,
|
||||
meta: {
|
||||
fromUserId,
|
||||
contactId: contact.id,
|
||||
},
|
||||
},
|
||||
});*/
|
||||
|
||||
strapi.log.info(`Notification envoyée à l’utilisateur ${toUserId}`);
|
||||
} catch (error) {
|
||||
strapi.log.error(
|
||||
"Erreur lors de la création de la notification :",
|
||||
error
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
async beforeUpdate(event) {
|
||||
// pour plus tard si tu veux : accepter/refuser…
|
||||
},
|
||||
|
||||
async afterUpdate(event) {
|
||||
// idem, pour envoyer une notif sur "accepted" ou "rejected"
|
||||
},
|
||||
};
|
||||
@@ -14,7 +14,7 @@
|
||||
"name": "Apache 2.0",
|
||||
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
|
||||
},
|
||||
"x-generation-date": "2025-12-09T22:24:41.239Z"
|
||||
"x-generation-date": "2025-12-11T14:30:17.431Z"
|
||||
},
|
||||
"x-strapi-config": {
|
||||
"plugins": [
|
||||
|
||||
Reference in New Issue
Block a user