-
-
Notifications
You must be signed in to change notification settings - Fork 1
Mail when new Feedback Submitted #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3b6f9c1
fbe6869
3c41543
5fa640d
9e7c45f
ac5e609
d948cf1
30e4bd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| // This source file is part of the MyHeart Counts project | ||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||
| // SPDX-FileCopyrightText: 2026 Stanford University and the project authors (see CONTRIBUTORS.md) | ||||||||||||||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import { logger } from "firebase-functions"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { onDocumentCreated } from "firebase-functions/v2/firestore"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { createTransport } from "nodemailer"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import type SMTPTransport from "nodemailer/lib/smtp-transport"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||
| feedbackEmailSecretParams, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getFeedbackCoordinatorEmail, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getFeedbackSenderEmail, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getSmtpHost, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getSmtpPassword, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getSmtpPort, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getSmtpUsername, | ||||||||||||||||||||||||||||||||||||||||||||||||
| } from "../env.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { defaultServiceAccount } from "./helpers.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const formatFeedbackEmail = ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| feedbackId: string, | ||||||||||||||||||||||||||||||||||||||||||||||||
| data: Record<string, unknown>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ): { subject: string; text: string } => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const subject = `[MyHeartCounts] New Feedback Received (ID: ${feedbackId})`; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const { accountId, ...rest } = data; | ||||||||||||||||||||||||||||||||||||||||||||||||
| const lines: string[] = [ | ||||||||||||||||||||||||||||||||||||||||||||||||
| `New feedback has been submitted.`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ``, | ||||||||||||||||||||||||||||||||||||||||||||||||
| `Feedback ID: ${feedbackId}`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| `From user: ${typeof accountId === "string" ? accountId : "unknown"}`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ``, | ||||||||||||||||||||||||||||||||||||||||||||||||
| `--- Feedback Content ---`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| for (const [key, value] of Object.entries(rest)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const formatted = | ||||||||||||||||||||||||||||||||||||||||||||||||
| typeof value === "string" ? value : JSON.stringify(value, null, 2); | ||||||||||||||||||||||||||||||||||||||||||||||||
| lines.push(`${key}: ${formatted}`); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return { subject, text: lines.join("\n") }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export const onFeedbackCreated = onDocumentCreated( | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| document: "feedback/{feedbackId}", | ||||||||||||||||||||||||||||||||||||||||||||||||
| serviceAccount: defaultServiceAccount, | ||||||||||||||||||||||||||||||||||||||||||||||||
| secrets: feedbackEmailSecretParams, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| async (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const snapshot = event.data; | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!snapshot) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warn("onFeedbackCreated: No data in event"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const feedbackId = event.params.feedbackId; | ||||||||||||||||||||||||||||||||||||||||||||||||
| const data = snapshot.data(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { subject, text } = formatFeedbackEmail(feedbackId, data); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // requireTLS/forceAuth make the transport fail loudly if the server does | ||||||||||||||||||||||||||||||||||||||||||||||||
| // not offer STARTTLS or AUTH, instead of silently sending | ||||||||||||||||||||||||||||||||||||||||||||||||
| // unauthenticated. forceAuth is supported by nodemailer at runtime but | ||||||||||||||||||||||||||||||||||||||||||||||||
| // missing from @types/nodemailer, so the options object needs a cast. | ||||||||||||||||||||||||||||||||||||||||||||||||
| const port = Number(getSmtpPort()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const transporter = createTransport({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| host: getSmtpHost(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| port, | ||||||||||||||||||||||||||||||||||||||||||||||||
| secure: port === 465, | ||||||||||||||||||||||||||||||||||||||||||||||||
| requireTLS: true, | ||||||||||||||||||||||||||||||||||||||||||||||||
| forceAuth: true, | ||||||||||||||||||||||||||||||||||||||||||||||||
| logger: true, | ||||||||||||||||||||||||||||||||||||||||||||||||
| auth: { | ||||||||||||||||||||||||||||||||||||||||||||||||
| user: getSmtpUsername(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| pass: getSmtpPassword(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| } as SMTPTransport.Options); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| await transporter.sendMail({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| from: `"MyHeart Counts" <${getFeedbackSenderEmail()}>`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| to: getFeedbackCoordinatorEmail(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| subject, | ||||||||||||||||||||||||||||||||||||||||||||||||
| text, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||||||||||||||
| `Feedback notification sent for ${feedbackId} to ${getFeedbackCoordinatorEmail()}`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error( | ||||||||||||||||||||||||||||||||||||||||||||||||
| `Failed to send feedback notification for ${feedbackId}: ${String(error)}`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+92
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent failure may cause lost notifications. When email sending fails, the error is logged but not rethrown. This means Firebase considers the function successful and will not retry. Feedback notifications could be silently lost with only a log entry. Consider whether this is intentional:
Option A: Rethrow to enable retries } catch (error) {
logger.error(
`Failed to send feedback notification for ${feedbackId}: ${String(error)}`,
);
+ throw error; // Allow Firebase to retry
}Option B: Keep silent failure but document the decision } catch (error) {
+ // Intentionally not rethrowing: avoid duplicate emails on transient SMTP failures.
+ // Monitor these logs for persistent failures.
logger.error(
`Failed to send feedback notification for ${feedbackId}: ${String(error)}`,
);
}📝 Committable suggestion
Suggested change
Suggested change
🤖 Prompt for AI Agents
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a critcial component. |
||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.