-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add Idempotency header to PartnerProgramSummary #2970
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughA partner program summary cron route now includes an Idempotency-Key header in each email batch payload sent via Resend. The header uses the format Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
apps/web/app/(ee)/api/cron/partner-program-summary/route.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
🔇 Additional comments (1)
apps/web/app/(ee)/api/cron/partner-program-summary/route.ts (1)
334-336: ThesendBatchEmailfunction accepts theheadersparameter as part of the TypeScript signature.The Resend API supports a "headers" parameter for custom email headers in batch email payloads, allowing you to add headers per message. However, note that Idempotency-Key is documented as an HTTP request header for deduplicating an entire batch (not per-message). The current implementation adds a unique Idempotency-Key to each individual email item in the batch, which may not align with the documented pattern. Verify that this per-message approach is intentional and produces the desired deduplication behavior.
| headers: { | ||
| "Idempotency-Key": `${program.id}-${partner.id}`, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include reporting period in the idempotency key.
The current idempotency key format (${program.id}-${partner.id}) lacks a temporal component, meaning the same key is reused every month. While email providers typically expire idempotency keys after 24 hours, this creates two concerns:
- If the cron job needs to be re-run within the idempotency TTL window (e.g., to fix a bug or update data), the duplicate key will prevent emails from being resent to partners.
- The intent isn't explicit—each monthly report is a distinct email and should have a unique key.
Including the reporting period makes the key unique per report cycle and more robust.
Apply this diff to include the reporting period:
+ const reportingMonth = format(currentMonth, "MMM yyyy");
+
await sendBatchEmail(
summary.map(({ partner, ...rest }) => ({
subject: `Your ${reportingMonth} performance report for ${program.name} program`,
to: partner.email!,
react: PartnerProgramSummary({
program,
partner,
...rest,
reportingPeriod: {
month: reportingMonth,
start: currentMonth.toISOString(),
end: endOfMonth(currentMonth).toISOString(),
},
}),
variant: "notifications",
headers: {
- "Idempotency-Key": `${program.id}-${partner.id}`,
+ "Idempotency-Key": `${program.id}-${partner.id}-${format(currentMonth, "yyyy-MM")}`,
},
})),
);Note: Move the reportingMonth declaration above the sendBatchEmail call as shown, since it's currently declared at line 317 but needs to be accessible here. Alternatively, use format(currentMonth, "yyyy-MM") directly in the idempotency key as suggested above.
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In apps/web/app/(ee)/api/cron/partner-program-summary/route.ts around lines 334
to 336, the idempotency key currently uses `${program.id}-${partner.id}` and
must include the reporting period so each monthly report is unique; move the
reportingMonth declaration (currently at ~line 317) above the sendBatchEmail
call and change the header to include it (e.g.
`${program.id}-${partner.id}-${reportingMonth}`) or inline the formatted month
with format(currentMonth, "yyyy-MM") in the Idempotency-Key; ensure the
reportingMonth/format expression is defined before the headers and preserve
existing string interpolation and header structure.
Summary by CodeRabbit