-
Notifications
You must be signed in to change notification settings - Fork 10
feat(emails): optional idempotency_key makes a replayed send deliver once #807
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,12 @@ export interface ProcessAndSendEmailInput { | |||||
| html?: string; | ||||||
| headers?: Record<string, string>; | ||||||
| room_id?: string; | ||||||
| /** | ||||||
| * Makes the send exactly-once for a retried or replayed caller. Scheduled | ||||||
| * runs re-execute on crash or redeploy and would otherwise deliver the same | ||||||
| * report again (chat#1918). Forwarded to Resend, which dedupes on it. | ||||||
| */ | ||||||
| idempotencyKey?: string; | ||||||
| } | ||||||
|
|
||||||
| export interface ProcessAndSendEmailSuccess { | ||||||
|
|
@@ -39,7 +45,7 @@ export type ProcessAndSendEmailResult = ProcessAndSendEmailSuccess | ProcessAndS | |||||
| export async function processAndSendEmail( | ||||||
| input: ProcessAndSendEmailInput, | ||||||
| ): Promise<ProcessAndSendEmailResult> { | ||||||
| const { to, cc = [], subject, text, html = "", headers = {}, room_id } = input; | ||||||
| const { to, cc = [], subject, text, html = "", headers = {}, room_id, idempotencyKey } = input; | ||||||
|
|
||||||
| const roomData = room_id ? await selectRoomWithArtist(room_id) : null; | ||||||
| const footer = getEmailFooter(room_id, roomData?.artist_name || undefined); | ||||||
|
|
@@ -51,14 +57,17 @@ export async function processAndSendEmail( | |||||
| // card, DESIGN.md font stack, and the existing footer as the layout footer. | ||||||
| const htmlWithLayout = renderEmailLayout({ bodyHtml, footerHtml: footer }); | ||||||
|
|
||||||
| const result = await sendEmailWithResend({ | ||||||
| from: RECOUP_FROM_EMAIL, | ||||||
| to, | ||||||
| cc: cc.length > 0 ? cc : undefined, | ||||||
| subject, | ||||||
| html: htmlWithLayout, | ||||||
| headers, | ||||||
| }); | ||||||
| const result = await sendEmailWithResend( | ||||||
| { | ||||||
| from: RECOUP_FROM_EMAIL, | ||||||
| to, | ||||||
| cc: cc.length > 0 ? cc : undefined, | ||||||
| subject, | ||||||
| html: htmlWithLayout, | ||||||
| headers, | ||||||
| }, | ||||||
| idempotencyKey ? { idempotencyKey } : undefined, | ||||||
|
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. P2: An explicitly supplied empty Prompt for AI agents
Suggested change
|
||||||
| ); | ||||||
|
|
||||||
| if (result instanceof NextResponse) { | ||||||
| const data = await result.json(); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,10 @@ export const sendEmailBodySchema = z | |
| html: z.string().optional(), | ||
| headers: z.record(z.string(), z.string()).default({}).optional(), | ||
| chat_id: z.string().optional(), | ||
| idempotency_key: z | ||
|
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. P2: The new Prompt for AI agents |
||
| .string() | ||
| .max(256, "idempotency_key must be at most 256 characters") | ||
| .optional(), | ||
| account_id: z.string().uuid("account_id must be a valid UUID").optional(), | ||
| }) | ||
| // Guard: never send an empty/footer-only email. A malformed or empty body | ||
|
|
||
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.
P2: The new comment overstates the guarantee: a replay after Resend’s 24-hour retention window can deliver again; describe this as deduplication within Resend’s 24-hour window or add durable deduplication for longer-lived retries.
Prompt for AI agents