/* eslint-disable import/no-absolute-path */ import { Meteor } from 'meteor/meteor'; import i18n from 'i18next'; import sendEmail, { subjectTruncate } from '/imports/modules/server/send-email'; import getEmailOf from '/imports/modules/get-email-of-user'; import Comments from '/imports/api/Comments/Comments'; /* * When a comment is added to a fire, email the other users who commented on the * same fire (uniq, excluding the author). Ported from the old * startup/server/comments.js `onEvent` handler. */ export default function onCommentAdd(payload) { const { referenceId, userId } = payload; const query = { referenceId, userId: { $ne: userId } }; Comments.rawCollection().distinct('userId', query).then(async (users) => { const path = referenceId.replace(/fire-/, 'fire/archive/'); const fireUrl = Meteor.absoluteUrl(path); await Meteor.users.find({ _id: { $in: users } }).forEachAsync((user) => { const { firstName, emailAddress } = getEmailOf(user); if (emailAddress) { const emailOpts = { to: emailAddress, subject: subjectTruncate.apply(i18n.t('Hay más información sobre un fuego')), lang: user.lang, template: 'new-fire-comment', templateVars: { applicationName: i18n.t('AppName'), firstName, fireUrl } }; sendEmail(emailOpts).catch((error) => { console.warn(`comments new-fire-comment mail failed: ${error}`); }); } }); }).catch((error) => { console.warn(`comments distinct() failed: ${error}`); }); }