Skip to content

Use saved template overrides for outgoing invitation emails - #305

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

joei4cm wants to merge 4 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.

Rows saved before the PUT check existed never went through it, so an
invite override can still lack {{invite.url}} — and sending it mails the
invitee an email they cannot accept while the route reports success. The
send path now treats such an override as absent, falling back to the
on-disk template and logging a warning; the editor and preview still
read the stored row so an admin can fix it.

The required-link check also moves to a shared pattern: interpolate
trims whitespace inside the braces, so {{ invite.url }} renders a
working link and the save check must accept it. The spaced spelling now
saves, and the send-side fallback uses the same pattern.

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

joei4cm commented Dec 3, 2026

Copy link
Copy Markdown
Author

All three addressed in a516c69:

  • Send-side check: the invite routes now use findSendableTemplateOverride, which treats an invite override whose body lacks the accept link as absent — the loader falls back to the on-disk template and a warning is logged. The editor and preview still read the stored row via findTemplateOverride, so an admin can see and fix it.
  • Shared pattern: the check is now INVITE_URL_PLACEHOLDER = /\{\{\s*invite\.url\s*\}\}/ in lib/template-override.ts, used by both the PUT validation and the send-side fallback, matching what interpolate accepts. A spaced-form save test was added.
  • team-api.test.ts: the saved-override fixture now carries {{invite.url}}, the delivered email is asserted to contain /invite/accept?token=, and the mock is cleared each loop pass so the lookup assertion can't be satisfied by the previous pass. A new test covers the link-less override falling back to the on-disk template with a warning.

Validation: team-api + templates-api 77/77, full apps/web suite 1221 passed / 26 skipped, typecheck, next lint (0 errors), Prettier, git diff --check all pass.

@NathanTarbert

Copy link
Copy Markdown
Collaborator

Thanks @joei4cm, this is great. Falling back to the on-disk template when a saved invite has no accept link is exactly what I was hoping for. Keeping the editor and preview on the stored row is a nice touch too, since it means an admin can still see the problem and fix it.

Sharing one pattern between the save check and the send path means the two can't drift apart, and the spaced-spelling test pins that down. The test updates are really solid as well. Clearing the mock between passes and asserting the accept URL in the delivered email make that loop prove what it claims.

This is ready from my side. We'll merge once CI has run. Thanks again for sticking with it through a few rounds.

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