From 089f8f3ee8cf5e768c3d47cdfba0c4977f3d859d Mon Sep 17 00:00:00 2001 From: julien vdb Date: Tue, 14 Oct 2025 22:21:53 +0200 Subject: [PATCH] V 0.11.5 : addComment and fields --- package.json | 2 +- .../group-membership/schema.json | 3 +- src/api/post/controllers/post.ts | 97 +++++++++++++++++++ src/api/post/routes/custom.ts | 5 + .../1.0.0/full_documentation.json | 77 ++++++++++----- types/generated/contentTypes.d.ts | 4 +- 6 files changed, 159 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 890e5c0..2c14889 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "harmony-back", - "version": "0.11.4", + "version": "0.11.5", "private": true, "description": "A Strapi application", "scripts": { diff --git a/src/api/group-membership/content-types/group-membership/schema.json b/src/api/group-membership/content-types/group-membership/schema.json index 76dadfc..3fdb4b5 100644 --- a/src/api/group-membership/content-types/group-membership/schema.json +++ b/src/api/group-membership/content-types/group-membership/schema.json @@ -30,7 +30,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] } } diff --git a/src/api/post/controllers/post.ts b/src/api/post/controllers/post.ts index 74b52e6..19f6030 100644 --- a/src/api/post/controllers/post.ts +++ b/src/api/post/controllers/post.ts @@ -623,5 +623,102 @@ export default factories.createCoreController( unhiddenPostId: postId, }); }, + + /** + * Add a comment to a post + * @param {Object} ctx - The Koa context object + * @returns {Promise} - Sends the created comment as the response + */ + async addComment(ctx) { + const user = ctx.state.user; + if (!user) { + return ctx.unauthorized("You must be logged in to add a comment"); + } + + const postId = parseInt(String(ctx.params.id)); + const { content } = ctx.request.body; + + // Validate inputs + if (isNaN(postId)) { + return ctx.badRequest("Invalid post ID"); + } + + if (!content || typeof content !== "string" || content.trim() === "") { + return ctx.badRequest("Comment content is required"); + } + + try { + // Step 1: Verify the post exists and get current comments + const currentPost = await strapi.db.query("api::post.post").findOne({ + where: { id: postId }, + populate: { + comments: { + fields: ["id"], + }, + }, + }); + + if (!currentPost) { + return ctx.notFound(`Post with id ${postId} not found`); + } + + console.log("=== Current Post Comments ==="); + console.log( + "Existing comment IDs:", + currentPost?.comments?.map((c: any) => c.id) + ); + + // Step 2: Create the new comment in the comments collection + const newComment = await strapi.db + .query("api::comment.comment") + .create({ + data: { + content: content.trim(), + owner: user.id, + likes: [], + replies: [], + }, + populate: { + owner: { + populate: { + avatar: true, + }, + }, + }, + }); + + console.log("=== Comment created successfully ==="); + console.log("New Comment:", JSON.stringify(newComment, null, 2)); + + // Step 3: Update the post to include the new comment ID + const existingCommentIds = + currentPost.comments?.map((c: any) => c.id) || []; + + await strapi.db.query("api::post.post").update({ + where: { id: postId }, + data: { + comments: [...existingCommentIds, newComment.id], + }, + }); + + console.log("=== Post updated with new comment ==="); + + return ctx.send({ + message: "Comment added successfully", + comment: newComment, + }); + } catch (error: any) { + // Log detailed error if creation fails + console.error("=== STRAPI ERROR DETAILS ==="); + console.error("Full error:", JSON.stringify(error, null, 2)); + if (error.error?.details?.errors) { + console.error( + "Validation errors:", + JSON.stringify(error.error.details.errors, null, 2) + ); + } + return ctx.internalServerError("Failed to add comment"); + } + }, }) ); diff --git a/src/api/post/routes/custom.ts b/src/api/post/routes/custom.ts index 9a0a58e..f23f62d 100644 --- a/src/api/post/routes/custom.ts +++ b/src/api/post/routes/custom.ts @@ -34,5 +34,10 @@ export default { path: "/posts/:id/like", handler: "post.likePost", }, + { + method: "POST", + path: "/posts/:id/comment", + handler: "post.addComment", + }, ], }; diff --git a/src/extensions/documentation/documentation/1.0.0/full_documentation.json b/src/extensions/documentation/documentation/1.0.0/full_documentation.json index 5f46950..f26df26 100644 --- a/src/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/src/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,7 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, - "x-generation-date": "2025-10-05T19:46:56.298Z" + "x-generation-date": "2025-10-14T20:21:29.388Z" }, "x-strapi-config": { "plugins": [ @@ -16550,7 +16550,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -19998,7 +19999,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -23888,7 +23890,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -27337,7 +27340,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -31234,7 +31238,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -35382,7 +35387,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -39936,7 +39942,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -43823,7 +43830,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -47612,7 +47620,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -51388,7 +51397,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -55200,7 +55210,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -59012,7 +59023,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -62785,7 +62797,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -66739,7 +66752,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -70182,7 +70196,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -75947,7 +75962,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -76120,7 +76136,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "locale": { @@ -78958,7 +78975,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -79796,7 +79814,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -82749,7 +82768,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -86647,7 +86667,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -89925,7 +89946,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -94395,7 +94417,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -98232,7 +98255,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { @@ -102029,7 +102053,8 @@ "member", "admin", "owner", - "follow" + "follow", + "pending" ] }, "createdAt": { diff --git a/types/generated/contentTypes.d.ts b/types/generated/contentTypes.d.ts index 226060d..1f20b1f 100644 --- a/types/generated/contentTypes.d.ts +++ b/types/generated/contentTypes.d.ts @@ -1019,7 +1019,9 @@ export interface ApiGroupMembershipGroupMembership > & Schema.Attribute.Private; publishedAt: Schema.Attribute.DateTime; - role: Schema.Attribute.Enumeration<['member', 'admin', 'owner', 'follow']>; + role: Schema.Attribute.Enumeration< + ['member', 'admin', 'owner', 'follow', 'pending'] + >; updatedAt: Schema.Attribute.DateTime; updatedBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> & Schema.Attribute.Private;