Skip to content

feat: Implement recruiting email templates (#19) - #25

Merged
VidurShah merged 3 commits into
mainfrom
aysel/email-templates
Nov 18, 2026
Merged

VidurShah merged 3 commits into
mainfrom
aysel/email-templates

Conversation

@ayselomer

Copy link
Copy Markdown
Collaborator

Summary

Implements the recruiting email templates functionality for Issue #19.

Changes

  • Added shared email template rendering with HTML escaping and placeholder validation.
  • Defined allowed placeholders for each email kind.
  • Added the email_templates database schema and migration.
  • Created a seed script for default email templates that preserves existing edits.
  • Implemented GET and PUT API endpoints for retrieving and updating templates.
  • Added an email templates management page with editing and live previews.
  • Added validation feedback, loading states, and error handling.
  • Added unit tests for template rendering and API endpoints.

Testing

  • 61 tests passing (53 API + 8 shared).
  • All lint checks passing.
  • TypeScript checks passing across all packages.
  • Manually tested frontend rendering and placeholder validation.

Notes

  • Authentication restrictions are marked with TODO(auth) until the authentication system is implemented.
  • Email sending is outside the scope of this ticket.

Closes #19

@VidurShah VidurShah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Aysel. The placeholder validation and the live preview are nice, and the shared rendering helpers are a good idea. A few things need changing before this can go in; details inline.

Comment thread apps/api/tests/email-templates.test.ts Outdated
});

describe("Email template endpoints", () => {
it("GET returns the email templates", async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails on a fresh database (it fails in CI): it expects at least one template, but nothing seeds them. Could the test insert what it needs first? Also, the PUT tests change real rows and never clean up. Main's DB tests wrap each test in a transaction that rolls back (see semesters/members tests); could these follow that pattern?

import { validate } from "../lib/validation";
import { errorResponse } from "../lib/errors";

export const emailTemplateRoutes = new Hono()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per CONTRIBUTING, business rules belong in apps/api/src/services. Could the kind check, placeholder validation and upsert move into a services/email-templates.ts, with this route just calling it? For the :kind param, parsing with emailKindSchema.safeParse(...) would also replace the manual EMAIL_KINDS.some(...) check and the as EmailKind cast.


body: text("body").notNull(),

// TODO(auth): Add foreign key to members once the members table exists.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The members table exists on main now, so this can become a real foreign key (.references(() => members.id, { onDelete: "set null" })). Also, updatedBy is never set anywhere. Fine to leave as TODO(auth) until sessions map to members, but the FK can go in now. Small one: pgEnum("email_kind", EMAIL_KINDS) would reuse the shared list instead of repeating it.

Comment thread apps/api/src/db/seed-email-templates.ts Outdated
},
];

async function seedEmailTemplates() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a separate script that nothing runs, so bun run db:seed won't create the templates. Could you fold it into src/db/seed.ts (same onConflictDoNothing pattern as positions)? Also, it never closes the DB connection, so the script may hang. seed.ts uses finally { await db.$client.end() }.

return match;
}

return escapeHtml(vars[key] ?? "");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This HTML-escapes every value, including in the subject, which is plain text, so a subject would show & instead of &. In the preview, React escapes again, so a value like O'Brien shows as O'Brien. Maybe add an escape option (on for the HTML body when sending, off for subjects and the preview)?

Migration regenerated as 0008 on top of main's 0001-0007. email_kind is now
built from EMAIL_KINDS (moved to shared enums with the other enum lists), and
updated_by gets its foreign key to members now that the table exists
(TDD 3.1).
- routes/email-templates.ts imported src/db at the top, so the whole API
  failed to start without DATABASE_URL; load it on first use
- PUT records email_template.update in the audit log (TDD 2.5) in the same
  transaction as the edit, only when something changed
- seedEmailTemplates is exported and called from db/seed.ts
- The invite placeholder is {{inviteUrl}}, a link, instead of the raw token;
  the default invite template now includes it
- Tests seed the defaults first and restore the invite template afterward,
  so they pass on an empty database and don't leave edits behind; new tests
  for the audit entry, the no-change case, seed idempotence, and that every
  default uses only allowed placeholders

@VidurShah VidurShah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and test-merged with the rest of this round; full CI passes.

@VidurShah
VidurShah merged commit 0f74c92 into main Nov 18, 2026
1 check passed
@VidurShah
VidurShah deleted the aysel/email-templates branch November 30, 2026 21:50
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.

Email templates

2 participants