Invite several teammates at once
Node · Node · intermediate · modification
Adds the backend for the new "Invite teammates" dialog, so an admin can invite a batch of people in one go instead of one at a time. `POST /api/invites/bulk` takes the dialog's email and role columns; `buildInviteRows` in the invites service validates them, skips the rows without an email and reports a bad row by its number in the dialog, and `inviteMany` writes the result in one transaction through the existing `upsertInvite` (which now accepts a transaction client). The single invite route is untouched. `test/invites.test.js` adds unit tests for `buildInviteRows`.
Node 22, Express 4.21 (CommonJS), Prisma 5.22 on PostgreSQL 16. Existing, unchanged: `src/db.js` exports the shared `prisma` client; `src/app.js` applies `express.json({ limit: '100kb' })`, mounts `app.use('/api/invites', requireAuth, invitesRouter)`, and ends with an error handler that answers body-parser errors with their own status (400, 413) and logs anything else and answers `500 { error: 'internal' }`; `requireAuth` answers 401 without a session and otherwise sets `req.user = { id, workspaceId, role }` (positive integer ids, `role` is `member` or `admin`); `requireAdmin` in `src/middleware/auth.js` answers `403 { error: 'forbidden' }` unless `req.user.role` is `admin`. Model: `Invite { id Int @id @default(autoincrement()), workspaceId Int, email String, role String, token String @unique, invitedById Int, emailedAt DateTime?, createdAt DateTime @default(now()), @@unique([workspaceId, email]) }`. The mailer job (unchanged) emails each invite whose `emailedAt` is null; accepting an invite (unchanged) gives the account the invite's `role` in the workspace, and an `admin` can manage members, billing and invites. Two admins inviting the same new address at the same instant can hit the unique constraint, which goes to the error handler like any database error (same as the single invite). The dialog (web app) ships separately and always sends both columns with one entry per row; its role cell is a select that defaults to `member`.
Requirements
- `POST /api/invites/bulk` (workspace admins only, through the existing `requireAdmin`) backs the new invite dialog, a table with an email column and a role column. The body is `{ "emails": [...], "roles": [...] }`: the two columns in the dialog's row order, so `emails[k]` and `roles[k]` belong to the same row. Each row invites the email on that row to the caller's workspace with the role on that same row.
- The dialog also sends its rows without an email: it keeps an empty row at the bottom for the next entry, and an admin can clear the email of any row, so such rows can appear anywhere in the list. A row whose email is empty or only whitespace (as `String.prototype.trim` defines it) is skipped, whatever its role cell holds.
- Every other row is checked like the single invite: the email goes through the existing `normalizeEmail` (`trim()` then `toLowerCase()` on the whole address, as JavaScript defines them, non-ASCII characters included; this is the product's rule for both routes, so the same input always maps to the same stored invite) and must then match the existing `EMAIL_RE`, which admits non-ASCII addresses; the role must be `member` or `admin`. If any such row fails, or its normalized email repeats an earlier row's, the response is `400 { error: 'invalid_row', row }`, where `row` is that row's 1-based number in the dialog (counting the rows without an email), and no invite is created or changed.
- `emails` and `roles` must both be arrays of strings, of the same length, with 1 to 50 entries; otherwise the response is `400 { error: 'invalid_body' }`. On success the rows are written in one transaction and the response is `200 { invited: n }`, the number of rows that were applied (0 when every row was empty).
- An email that already has an invite in the workspace keeps that invite (and its token) and takes the role from its row, as the single invite does. Unchanged: `POST /api/invites` (the single invite) behaves as before; database errors go to the app's error handler (`next(err)`).
Files touched
- src/routes/invites.js
- src/services/invites.js
- test/invites.test.js