Skip to content
Open
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
90 changes: 72 additions & 18 deletions src/api/comment/content-types/comment/lifecycles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
beforeCreate(event) {
// Set author to the user sending the request
const ctx = strapi.requestContext.get();
event.params.data.user = ctx.state.user;
event.params.data.author = ctx.state.user.username;
Expand All @@ -9,47 +8,102 @@ module.exports = {
async afterCreate(event) {
const { id: commentId, text: commentText } = event.result;
const comment = await strapi.entityService.findOne(
"api::comment.comment",
commentId,
{
populate: ["idea_card", "user"],
}
"api::comment.comment",
commentId,
{ populate: ["idea_card", "user"] }
);

const idea = comment.idea_card;
const user = comment.user;
const currentUserId = user.id;

// Get idea owner
const ideaWithOwner = await strapi.entityService.findOne(
"api::idea-card.idea-card",
idea.id,
{ populate: ["ideaOwner"] }
);
const ideaOwnerId = ideaWithOwner?.ideaOwner?.id;

/** verify that user isn't already subscribed to ideaCard */
// Get all EXISTING active subscribers for this idea,
// excluding the commenter AND the idea owner (added separately below)
const existingSubscriptions = await strapi.entityService.findMany(
"api::subscription.subscription",
{
filters: { user: user, entityId: idea.id },
limit: 1,
"api::subscription.subscription",
{
filters: {
entityId: idea.id,
active: true,
$and: [
{ user: { id: { $ne: currentUserId } } },
{ user: { id: { $ne: ideaOwnerId } } },
],
},
populate: ["user"],
}
);

// Build unique set of user IDs to notify
const usersToNotify = new Set();

// Add idea owner if they are not the commenter
if (ideaOwnerId && ideaOwnerId !== currentUserId) {
usersToNotify.add(ideaOwnerId);
}

// Add existing subscribers (already excludes commenter and owner)
for (const sub of existingSubscriptions) {
if (sub.user?.id) {
usersToNotify.add(sub.user.id);
}
}


// Subscribe the commenter if not already subscribed
const commenterSubscription = await strapi.entityService.findMany(
"api::subscription.subscription",
{
filters: { user: { id: currentUserId }, entityId: idea.id },
limit: 1,
}
);

if (!existingSubscriptions.length)
if (!commenterSubscription.length) {
await strapi.entityService.create("api::subscription.subscription", {
data: {
entityType: "Comment",
entityType: "IdeaCard", // Fixed: subscription is on the idea, not a comment
entityId: idea.id,
createdDateTime: new Date(),
active: true,
user: user,
user: currentUserId,
},
});
}

await strapi.entityService.create("api::event.event", {
// Create the event record
const newEvent = await strapi.entityService.create("api::event.event", {
data: {
action: "Commented",
entityName: idea.ideaName,
entityName: ideaWithOwner.ideaName,
content: commentText,
entityType: "Comment",
entityId: idea.id,
originatedEntityId: commentId,
eventUser: user?.id,
eventUser: currentUserId,
createdDateTime: new Date(),
},
});

// Create one notification per user to notify
for (const userId of usersToNotify) {
await strapi.entityService.create("api::notification.notification", {
data: {
user: userId,
event: newEvent.id,
createdDateTime: new Date(),
readDateTime: null,
},
});
}

},
};
};
50 changes: 29 additions & 21 deletions src/api/event/content-types/event/lifecycles.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
module.exports = {
async afterCreate(event) {

// Extract the ID of the created event
const { id, entityId } = event.result;
const { id, entityId, action, entityType } = event.result;

// Comment notifications are fully handled in comment/lifecycles.js.
// Bail out here to prevent any possibility of duplicate notifications.
if (action === "Commented" || entityType === "Comment") {
console.log(`Skipping automatic notifications for comment event ${id}`);
return;
}

try {
// Fetch all users with active subscriptions for the given entityId
const subscriptions = await strapi.entityService.findMany(
"api::subscription.subscription",
{
filters: {
entityId: entityId,
status: "active",
},
populate: ["user"],
}
"api::subscription.subscription",
{
filters: {
entityId: entityId,
active: true, // Changed from status: "active" to active: true
},
populate: ["user"],
}
);

// Generate notifications for each subscribed user
await Promise.all(
subscriptions.map((subscription) => {
return strapi.entityService.create("api::notification.notification", {
data: {
createdDateTime: new Date().toISOString(),
readDateTime: null,
event: id,
user: subscription.user.id,
},
});
})
subscriptions.map((subscription) => {
return strapi.entityService.create("api::notification.notification", {
data: {
createdDateTime: new Date().toISOString(),
readDateTime: null,
event: id,
user: subscription.user.id,
},
});
})
);

console.log(
`Notifications created for event ${id} and ${subscriptions.length} subscribed users.`
`Notifications created for event ${id} and ${subscriptions.length} subscribed users.`
);
} catch (error) {
console.error("Error creating notifications:", error);
}
},
};
};
Loading