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,8 +2,10 @@
|
||||
// ABOUTME: Tests email composition, subject lines, and Resend integration.
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { mockSend } = vi.hoisted(() => ({
|
||||
const { mockSend, mockLoggerInfo, mockLoggerError } = vi.hoisted(() => ({
|
||||
mockSend: vi.fn().mockResolvedValue({ id: "mock-email-id" }),
|
||||
mockLoggerInfo: vi.fn(),
|
||||
mockLoggerError: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock the resend module before importing email utilities
|
||||
@@ -13,6 +15,14 @@ vi.mock("resend", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock the logger
|
||||
vi.mock("@/lib/logger", () => ({
|
||||
logger: {
|
||||
info: mockLoggerInfo,
|
||||
error: mockLoggerError,
|
||||
},
|
||||
}));
|
||||
|
||||
import type { DailyEmailData } from "./email";
|
||||
import {
|
||||
sendDailyEmail,
|
||||
@@ -277,3 +287,135 @@ describe("sendTokenExpirationWarning", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("email structured logging", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
const sampleDailyEmailData: DailyEmailData = {
|
||||
to: "user@example.com",
|
||||
cycleDay: 15,
|
||||
phase: "OVULATION",
|
||||
decision: {
|
||||
status: "TRAIN",
|
||||
reason: "Body battery high",
|
||||
icon: "💪",
|
||||
},
|
||||
bodyBatteryCurrent: 85,
|
||||
bodyBatteryYesterdayLow: 45,
|
||||
hrvStatus: "Balanced",
|
||||
weekIntensity: 60,
|
||||
phaseLimit: 80,
|
||||
remainingMinutes: 20,
|
||||
seeds: "Sesame",
|
||||
carbRange: "100-150g",
|
||||
ketoGuidance: "No",
|
||||
};
|
||||
|
||||
describe("sendDailyEmail logging", () => {
|
||||
it("logs email sent with info level on success", async () => {
|
||||
await sendDailyEmail(sampleDailyEmailData, "user-123");
|
||||
expect(mockLoggerInfo).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: "user-123",
|
||||
type: "daily",
|
||||
recipient: "user@example.com",
|
||||
}),
|
||||
"Email sent",
|
||||
);
|
||||
});
|
||||
|
||||
it("logs email failed with error level on failure", async () => {
|
||||
const error = new Error("Resend API failed");
|
||||
mockSend.mockRejectedValueOnce(error);
|
||||
|
||||
await expect(
|
||||
sendDailyEmail(sampleDailyEmailData, "user-123"),
|
||||
).rejects.toThrow("Resend API failed");
|
||||
|
||||
expect(mockLoggerError).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: "user-123",
|
||||
type: "daily",
|
||||
err: error,
|
||||
}),
|
||||
"Email failed",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendPeriodConfirmationEmail logging", () => {
|
||||
it("logs email sent with info level on success", async () => {
|
||||
await sendPeriodConfirmationEmail(
|
||||
"user@example.com",
|
||||
new Date("2025-01-15"),
|
||||
31,
|
||||
"user-456",
|
||||
);
|
||||
expect(mockLoggerInfo).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: "user-456",
|
||||
type: "period_confirmation",
|
||||
recipient: "user@example.com",
|
||||
}),
|
||||
"Email sent",
|
||||
);
|
||||
});
|
||||
|
||||
it("logs email failed with error level on failure", async () => {
|
||||
const error = new Error("Resend API failed");
|
||||
mockSend.mockRejectedValueOnce(error);
|
||||
|
||||
await expect(
|
||||
sendPeriodConfirmationEmail(
|
||||
"user@example.com",
|
||||
new Date("2025-01-15"),
|
||||
31,
|
||||
"user-456",
|
||||
),
|
||||
).rejects.toThrow("Resend API failed");
|
||||
|
||||
expect(mockLoggerError).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: "user-456",
|
||||
type: "period_confirmation",
|
||||
err: error,
|
||||
}),
|
||||
"Email failed",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendTokenExpirationWarning logging", () => {
|
||||
it("logs email sent with info level on success", async () => {
|
||||
await sendTokenExpirationWarning("user@example.com", 14, "user-789");
|
||||
expect(mockLoggerInfo).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: "user-789",
|
||||
type: "warning",
|
||||
recipient: "user@example.com",
|
||||
}),
|
||||
"Email sent",
|
||||
);
|
||||
});
|
||||
|
||||
it("logs email failed with error level on failure", async () => {
|
||||
const error = new Error("Resend API failed");
|
||||
mockSend.mockRejectedValueOnce(error);
|
||||
|
||||
await expect(
|
||||
sendTokenExpirationWarning("user@example.com", 14, "user-789"),
|
||||
).rejects.toThrow("Resend API failed");
|
||||
|
||||
expect(mockLoggerError).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: "user-789",
|
||||
type: "warning",
|
||||
err: error,
|
||||
}),
|
||||
"Email failed",
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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