Add custom endpoint for contact and post
This commit is contained in:
@@ -46,5 +46,151 @@ export default factories.createCoreController(
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates or updates a contact.
|
||||
*
|
||||
* If a contact already exists with the same owner and user,
|
||||
* only the state field is updated. Otherwise, a new contact is created.
|
||||
*
|
||||
* @param {Object} ctx - The request context.
|
||||
* @returns {Object} The created or updated contact.
|
||||
*/
|
||||
async create(ctx) {
|
||||
// Log de la requête entrante
|
||||
console.log("POST /contacts - Requête reçue:", {
|
||||
body: ctx.request.body,
|
||||
user: ctx.state.user?.id || "anonymous",
|
||||
});
|
||||
|
||||
try {
|
||||
const { data } = ctx.request.body;
|
||||
const { owner, user, state } = data;
|
||||
|
||||
// Vérifier si un contact existe déjà avec le même owner et user
|
||||
const existingContact = await strapi
|
||||
.documents("api::contact.contact")
|
||||
.findFirst({
|
||||
filters: {
|
||||
owner: owner,
|
||||
user: user,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingContact) {
|
||||
// Mettre à jour uniquement le state
|
||||
console.log(
|
||||
"POST /contacts - Contact existant trouvé, mise à jour du state:",
|
||||
{
|
||||
documentId: existingContact.documentId,
|
||||
oldState: existingContact.state,
|
||||
newState: state,
|
||||
}
|
||||
);
|
||||
|
||||
const updatedContact = await strapi
|
||||
.documents("api::contact.contact")
|
||||
.update({
|
||||
documentId: existingContact.documentId,
|
||||
data: {
|
||||
state: state,
|
||||
respondedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return { data: updatedContact };
|
||||
}
|
||||
|
||||
// Sinon, créer un nouveau contact
|
||||
console.log("POST /contacts - Création d'un nouveau contact");
|
||||
const result = await super.create(ctx);
|
||||
|
||||
console.log("POST /contacts - Contact créé:", {
|
||||
id: result.data?.id,
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
// Log de l'erreur
|
||||
console.error("POST /contacts - Erreur:", {
|
||||
error: error.message,
|
||||
user: ctx.state.user?.id || "anonymous",
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes a contact by its ID.
|
||||
*
|
||||
* Logs the incoming request and attempts to find and delete the contact
|
||||
* document. If the contact has associated meta documents owned by the user,
|
||||
* those are deleted as well. If the contact is not found, a 404 response is returned.
|
||||
* Logs any errors encountered during the process.
|
||||
*
|
||||
* @param {Object} ctx - The request context.
|
||||
* @returns {Object} The response indicating the result of the delete operation.
|
||||
*/
|
||||
async delete(ctx) {
|
||||
// Log de la requête entrante
|
||||
console.log("DELETE /contacts/:id - Requête reçue:", {
|
||||
id: ctx.params.id,
|
||||
query: ctx.query,
|
||||
user: ctx.state.user?.id || "anonymous",
|
||||
});
|
||||
const { id } = ctx.params;
|
||||
|
||||
try {
|
||||
const result = await strapi
|
||||
.documents("api::contact.contact")
|
||||
.findFirst({
|
||||
filters: {
|
||||
$or: [
|
||||
{
|
||||
user: id,
|
||||
},
|
||||
{
|
||||
owner: id,
|
||||
},
|
||||
],
|
||||
},
|
||||
populate: {
|
||||
metas: {
|
||||
populate: ["owner"],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return ctx.notFound("Contact not found");
|
||||
}
|
||||
|
||||
if (result.metas) {
|
||||
const metaDocumentId = result.metas.filter(
|
||||
(s) => s.owner.id === ctx.state.user?.id
|
||||
)[0]?.documentId;
|
||||
if (metaDocumentId) {
|
||||
await strapi.documents("api::contact-meta.contact-meta").delete({
|
||||
documentId: metaDocumentId,
|
||||
});
|
||||
}
|
||||
}
|
||||
await strapi
|
||||
.documents("api::contact.contact")
|
||||
.delete({ documentId: result.documentId });
|
||||
|
||||
ctx.send({ message: "Contact deleted successfully" }, 200);
|
||||
} catch (error) {
|
||||
// Log de l'erreur
|
||||
console.error("DELETE /contacts/:id - Erreur:", {
|
||||
id: ctx.params.id,
|
||||
error: error.message,
|
||||
user: ctx.state.user?.id || "anonymous",
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
@@ -136,6 +136,18 @@ export default factories.createCoreController(
|
||||
});
|
||||
const followIds = followContacts.map((c) => c.user.id);
|
||||
|
||||
// Récupérer les contacts suivis (follow)
|
||||
const blockedContacts = await strapi.db
|
||||
.query("api::contact.contact")
|
||||
.findMany({
|
||||
where: {
|
||||
owner: { id: parseInt(userId) },
|
||||
state: "blocked",
|
||||
},
|
||||
populate: ["user"],
|
||||
});
|
||||
const blockedIds = blockedContacts.map((c) => c.user.id);
|
||||
|
||||
// 3️⃣ Récupérer les groupes où mes amis sont membres (en excluant mes propres groupes)
|
||||
const friendsGroupMemberships = await strapi.db
|
||||
.query("api::group-membership.group-membership")
|
||||
@@ -206,6 +218,9 @@ export default factories.createCoreController(
|
||||
// Vérifier si l'auteur est un contact suivi
|
||||
const isContactFollow = authorId ? followIds.includes(authorId) : false;
|
||||
|
||||
// Vérifier si l'auteur est bloqué
|
||||
const isBlocked = authorId ? blockedIds.includes(authorId) : false;
|
||||
|
||||
// Vérifier si je suis membre du groupe (seulement pour les posts de groupe)
|
||||
const isMember =
|
||||
contextType === "group" && contextId
|
||||
@@ -223,11 +238,14 @@ export default factories.createCoreController(
|
||||
friend: isFriend,
|
||||
contactFollow: isContactFollow,
|
||||
member: isMember,
|
||||
blocked: isBlocked,
|
||||
...(group && { group }), // Ajouter group seulement s'il existe
|
||||
};
|
||||
});
|
||||
|
||||
ctx.send(enrichedFeed);
|
||||
const filteredFeedBlocked = enrichedFeed.filter((post) => !post.blocked);
|
||||
|
||||
ctx.send(filteredFeedBlocked);
|
||||
},
|
||||
|
||||
async savePost(ctx) {
|
||||
|
||||
Reference in New Issue
Block a user