Add email sent/failed structured logging
All checks were successful
Deploy / deploy (push) Successful in 1m38s
All checks were successful
Deploy / deploy (push) Successful in 1m38s
Implement email logging per observability spec: - Add structured logging for email sent (info level) and failed (error level) - Include userId, type, and recipient fields in log events - Add userId parameter to email functions (sendDailyEmail, sendPeriodConfirmationEmail, sendTokenExpirationWarning) - Update cron routes (notifications, garmin-sync) to pass userId 6 new tests added to email.test.ts (now 30 tests total) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
// ABOUTME: Sends daily training notifications and period confirmation emails.
|
||||
import { Resend } from "resend";
|
||||
|
||||
import { logger } from "@/lib/logger";
|
||||
import { emailSentTotal } from "@/lib/metrics";
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
||||
@@ -28,7 +29,10 @@ export interface DailyEmailData {
|
||||
ketoGuidance: string;
|
||||
}
|
||||
|
||||
export async function sendDailyEmail(data: DailyEmailData): Promise<void> {
|
||||
export async function sendDailyEmail(
|
||||
data: DailyEmailData,
|
||||
userId?: string,
|
||||
): Promise<void> {
|
||||
const subject = `Today's Training: ${data.decision.icon} ${data.decision.status}`;
|
||||
|
||||
const body = `Good morning!
|
||||
@@ -53,20 +57,27 @@ ${data.decision.icon} ${data.decision.reason}
|
||||
---
|
||||
Auto-generated by PhaseFlow`;
|
||||
|
||||
await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to: data.to,
|
||||
subject,
|
||||
text: body,
|
||||
});
|
||||
try {
|
||||
await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to: data.to,
|
||||
subject,
|
||||
text: body,
|
||||
});
|
||||
|
||||
emailSentTotal.inc({ type: "daily" });
|
||||
logger.info({ userId, type: "daily", recipient: data.to }, "Email sent");
|
||||
emailSentTotal.inc({ type: "daily" });
|
||||
} catch (err) {
|
||||
logger.error({ userId, type: "daily", err }, "Email failed");
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendPeriodConfirmationEmail(
|
||||
to: string,
|
||||
lastPeriodDate: Date,
|
||||
cycleLength: number,
|
||||
userId?: string,
|
||||
): Promise<void> {
|
||||
const subject = "🔵 Period Tracking Updated";
|
||||
|
||||
@@ -78,17 +89,28 @@ Your calendar will update automatically within 24 hours.
|
||||
---
|
||||
Auto-generated by PhaseFlow`;
|
||||
|
||||
await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to,
|
||||
subject,
|
||||
text: body,
|
||||
});
|
||||
try {
|
||||
await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to,
|
||||
subject,
|
||||
text: body,
|
||||
});
|
||||
|
||||
logger.info(
|
||||
{ userId, type: "period_confirmation", recipient: to },
|
||||
"Email sent",
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error({ userId, type: "period_confirmation", err }, "Email failed");
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendTokenExpirationWarning(
|
||||
to: string,
|
||||
daysUntilExpiry: number,
|
||||
userId?: string,
|
||||
): Promise<void> {
|
||||
const isUrgent = daysUntilExpiry <= 7;
|
||||
|
||||
@@ -112,12 +134,18 @@ This will ensure your training recommendations continue to use fresh Garmin data
|
||||
---
|
||||
Auto-generated by PhaseFlow`;
|
||||
|
||||
await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to,
|
||||
subject,
|
||||
text: body,
|
||||
});
|
||||
try {
|
||||
await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to,
|
||||
subject,
|
||||
text: body,
|
||||
});
|
||||
|
||||
emailSentTotal.inc({ type: "warning" });
|
||||
logger.info({ userId, type: "warning", recipient: to }, "Email sent");
|
||||
emailSentTotal.inc({ type: "warning" });
|
||||
} catch (err) {
|
||||
logger.error({ userId, type: "warning", err }, "Email failed");
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user