handling closing posts

This commit is contained in:
jackiettran
2025-11-17 17:53:41 -05:00
parent e260992ef2
commit 026e748bf8
9 changed files with 770 additions and 24 deletions

View File

@@ -313,6 +313,77 @@ class ForumEmailService {
return { success: false, error: error.message };
}
}
/**
* Send notification when a discussion is closed
* @param {Object} recipient - Recipient user object
* @param {string} recipient.firstName - Recipient's first name
* @param {string} recipient.email - Recipient's email
* @param {Object} closer - User who closed the discussion (can be admin or post author)
* @param {string} closer.firstName - Closer's first name
* @param {string} closer.lastName - Closer's last name
* @param {Object} post - Forum post object
* @param {number} post.id - Post ID
* @param {string} post.title - Post title
* @param {Date} closedAt - Timestamp when discussion was closed
* @returns {Promise<{success: boolean, messageId?: string, error?: string}>}
*/
async sendForumPostClosedNotification(
recipient,
closer,
post,
closedAt
) {
if (!this.initialized) {
await this.initialize();
}
try {
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:3000";
const postUrl = `${frontendUrl}/forum/posts/${post.id}`;
const timestamp = new Date(closedAt).toLocaleString("en-US", {
dateStyle: "medium",
timeStyle: "short",
});
const variables = {
recipientName: recipient.firstName || "there",
adminName:
`${closer.firstName} ${closer.lastName}`.trim() || "A user",
postTitle: post.title,
postUrl: postUrl,
timestamp: timestamp,
};
const htmlContent = await this.templateManager.renderTemplate(
"forumPostClosed",
variables
);
const subject = `Discussion closed: ${post.title}`;
const result = await this.emailClient.sendEmail(
recipient.email,
subject,
htmlContent
);
if (result.success) {
console.log(
`Forum post closed notification email sent to ${recipient.email}`
);
}
return result;
} catch (error) {
console.error(
"Failed to send forum post closed notification email:",
error
);
return { success: false, error: error.message };
}
}
}
module.exports = ForumEmailService;