Skip to content

Use saved template overrides for outgoing invitation emails - #305

Open
joei4cm wants to merge 3 commits into
CopilotKit:mainfrom
joei4cm:fix/send-invite-template-override
Open

joei4cm wants to merge 3 commits into
CopilotKit:mainfrom
joei4cm:fix/send-invite-template-override

Conversation

@joei4cm

@joei4cm joei4cm commented Dec 1, 2026

Copy link
Copy Markdown

Refs #279.

Saved template overrides now reach outgoing invitation email. POST /api/team/invite and POST /api/team/invite/resend pass the stored-override lookup into sendEmail, so a saved invite override controls the subject and body, and removing it falls back to the packaged template.

The lookup is shared with the template GET and preview routes through apps/web/src/lib/template-override.ts, so the editor, preview, and delivery path read the same row. The templates screen now distinguishes the invite template, which is used for outgoing invitations, from the other preview-only templates.

Invite previews now use the same context shape the invite routes send, so variables that invitation emails cannot fill render empty in preview instead of showing unrelated sample data. Template subject saves also reject control characters because the subject becomes an email header.

Regression coverage exercises both invite paths with a saved override and after the override is removed, using the real sendEmail renderer with a fake transport. Preview coverage checks the invite context and the new subject validation.

Validation:

  • cd apps/web && NODE_OPTIONS='--max-old-space-size=4096' pnpm --config.concurrency=2 exec vitest run src/__tests__/team-api.test.ts src/__tests__/templates-page.test.tsx src/__tests__/templates-api.test.ts --maxWorkers=2 — 3 files, 86 tests passed.
  • Full suite and typecheck are left to CI.

Note: #280 covers the remaining copy and dead-issue-reference cleanup from #279; this PR is the send-path fix. Happy to rebase if #280 lands first.

🤖 Generated with Claude Code

Co-Authored-By: Claude Code <noreply@anthropic.com>
@NathanTarbert

Copy link
Copy Markdown
Collaborator

Thanks @joei4cm, this is a great fix. Pulling the lookup into lib/template-override.ts means the editor, the preview and the send path all read the same row, which is exactly what #279 needed. The regression test is really solid too. It sends through the real sendEmail on both the invite and resend routes, with the override saved and then removed, so it would catch this breaking again.

There are two things I'd like to ask for before we merge.

The first is the format check, which is what's failing CI. It checks every file a PR touches, and api/team/invite/route.ts and api/team/invite/resend/route.ts weren't Prettier-formatted to begin with. Could you run prettier --write on those two files? Typecheck and tests run after the format step, so they'll get their turn on GitHub once it passes.

The second is new now that saved templates reach real inboxes. An admin can save an invitation template that no longer contains {{invite.url}}, for example by deleting the Accept Invitation line while rewording the email. Every invite after that arrives with no way to accept it. Nothing fails, and the invite routes still return 200, so nobody would know until invitees start asking how to join. Could you have the PUT in api/templates/[slug]/route.ts reject an invite body that's missing {{invite.url}}, with a 400 that says the accept link is required, plus a test for it?

Thanks as well for offering to rebase around #280. Whichever one lands second will need it, since both touch the same strings in page.tsx.

joei4cm and others added 2 commits December 2, 2026 07:08
Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
@joei4cm

joei4cm commented Dec 2, 2026

Copy link
Copy Markdown
Author

Both requests are addressed: 44e6595 formats the invite routes, and 06e1ecd makes PUT /api/templates/invite reject bodies missing {{invite.url}} with a 400 and adds regression coverage. The focused template API suite passed 49 tests on the validated branch. Thanks for the review.

@NathanTarbert

Copy link
Copy Markdown
Collaborator

Thanks @joei4cm, both of those look great. The accept-link check is exactly what I was after, and it's nice to see the invite routes formatted too.

Reading through it again, there are three things I'd like to ask for before we merge.

The first is invite templates that were saved before this change. The new check runs on save, so an invite override already in the database never went through it. Once this merges, those rows start going out in real emails. If one is missing {{invite.url}}, the invitee gets an email they can't accept, and the route still returns 200. Could you also check for the link on the send side, so an invite override without it falls back to the on-disk template and logs a warning?

The second is the check itself. interpolate trims whitespace inside the braces, so {{ invite.url }} renders a working link. The includes('{{invite.url}}') check in api/templates/[slug]/route.ts rejects it with a 400, though. Could you match it with something like /\{\{\s*invite\.url\s*\}\}/ instead, and add a test that the spaced form saves? Using the same pattern on the send side keeps the two in agreement.

The third is in team-api.test.ts. The saved override in the new test has no {{invite.url}}, so the test expects an invite without an accept link to be delivered. Could you add the link to that fixture and assert that the delivered email contains the accept URL? On the loop's second pass, toHaveBeenCalledWith is still satisfied by the call from the first pass, so it doesn't show the lookup ran again. Clearing the mock between passes, or checking the call count, would fix that.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants