Item request notifications
This commit is contained in:
@@ -8,6 +8,7 @@ const TemplateManager = require("../core/TemplateManager");
|
||||
* - Sending reply notifications to comment authors
|
||||
* - Sending answer accepted notifications
|
||||
* - Sending thread activity notifications to participants
|
||||
* - Sending location-based item request notifications to nearby users
|
||||
*/
|
||||
class ForumEmailService {
|
||||
constructor() {
|
||||
@@ -384,6 +385,66 @@ class ForumEmailService {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send notification to nearby users about an item request
|
||||
* @param {Object} recipient - Recipient user object
|
||||
* @param {string} recipient.firstName - Recipient's first name
|
||||
* @param {string} recipient.email - Recipient's email
|
||||
* @param {Object} requester - User who posted the item request
|
||||
* @param {string} requester.firstName - Requester's first name
|
||||
* @param {string} requester.lastName - Requester's last name
|
||||
* @param {Object} post - Forum post object (item request)
|
||||
* @param {number} post.id - Post ID
|
||||
* @param {string} post.title - Item being requested
|
||||
* @param {string} post.content - Request description
|
||||
* @param {string|number} distance - Distance from recipient to request location (in miles)
|
||||
* @returns {Promise<{success: boolean, messageId?: string, error?: string}>}
|
||||
*/
|
||||
async sendItemRequestNotification(recipient, requester, post, distance) {
|
||||
if (!this.initialized) {
|
||||
await this.initialize();
|
||||
}
|
||||
|
||||
try {
|
||||
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:3000";
|
||||
const postUrl = `${frontendUrl}/forum/posts/${post.id}`;
|
||||
|
||||
const variables = {
|
||||
recipientName: recipient.firstName || "there",
|
||||
requesterName:
|
||||
`${requester.firstName} ${requester.lastName}`.trim() || "Someone",
|
||||
itemRequested: post.title,
|
||||
requestDescription: post.content,
|
||||
postUrl: postUrl,
|
||||
distance: distance,
|
||||
};
|
||||
|
||||
const htmlContent = await this.templateManager.renderTemplate(
|
||||
"forumItemRequestNotification",
|
||||
variables
|
||||
);
|
||||
|
||||
const subject = `Someone nearby is looking for: ${post.title}`;
|
||||
|
||||
const result = await this.emailClient.sendEmail(
|
||||
recipient.email,
|
||||
subject,
|
||||
htmlContent
|
||||
);
|
||||
|
||||
if (result.success) {
|
||||
console.log(
|
||||
`Item request notification email sent to ${recipient.email}`
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("Failed to send item request notification email:", error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ForumEmailService;
|
||||
|
||||
Reference in New Issue
Block a user