Files
Iss 2b845455b0 feat(webapp): admin back-office editors for org max projects and batch rate limit (#3475)
## Summary

- Adds admin-only editors on the back-office org page for
`Organization.maximumProjectCount` and
`Organization.batchRateLimitConfig`, alongside the existing API rate
limit editor.
- Splits the back-office org page into per-section components
(`ApiRateLimitSection`, `BatchRateLimitSection`, `MaxProjectsSection`)
so each tool is self-contained — adding new sections later doesn't bloat
the route.
- Generalizes the rate-limit form into a reusable `RateLimitSection`
component + `RateLimitDomain` server config so API and batch share the
same UI, validation, and action handler. Each domain only owns its env
defaults, DB column, and logger key.
- "Saved." banner and validation errors are scoped to the section that
submitted, not the page.

Heads-up: the API rate-limit log key was renamed
`admin.backOffice.rateLimit` → `admin.backOffice.apiRateLimit` for
symmetry with the new `admin.backOffice.batchRateLimit`.

## Test plan

- [ ] As an admin, visit `/admin/back-office/orgs/:orgId` and confirm
all three sections render with the org's current values (or system
defaults).
- [ ] Edit and save each section; confirm only that section shows the
"Saved." banner.
- [ ] Submit invalid input (e.g. `0` tokens, malformed interval);
confirm errors render in the offending form only and the other sections
stay closed.
- [ ] Confirm a non-admin user is redirected away from the route.
- [ ] After saving a rate-limit override, hit the org with traffic and
confirm the new limit is enforced (API rate limit + batch rate limit
code paths read the column at request time).
2026-05-11 10:19:41 -04:00

49 lines
1.4 KiB
TypeScript

import { z } from "zod";
import { prisma } from "~/db.server";
import { logger } from "~/services/logger.server";
import { MAX_PROJECTS_INTENT } from "./MaxProjectsSection";
const SetMaxProjectsSchema = z.object({
intent: z.literal(MAX_PROJECTS_INTENT),
// Capped at PostgreSQL INTEGER max (Prisma Int) so oversized input fails
// validation cleanly instead of crashing the update.
maximumProjectCount: z.coerce.number().int().min(1).max(2_147_483_647),
});
export type MaxProjectsActionResult =
| { ok: true }
| { ok: false; errors: Record<string, string[] | undefined> };
export async function handleMaxProjectsAction(
formData: FormData,
orgId: string,
adminUserId: string
): Promise<MaxProjectsActionResult> {
const submission = SetMaxProjectsSchema.safeParse(Object.fromEntries(formData));
if (!submission.success) {
return { ok: false, errors: submission.error.flatten().fieldErrors };
}
const existing = await prisma.organization.findFirst({
where: { id: orgId },
select: { maximumProjectCount: true },
});
if (!existing) {
throw new Response(null, { status: 404 });
}
await prisma.organization.update({
where: { id: orgId },
data: { maximumProjectCount: submission.data.maximumProjectCount },
});
logger.info("admin.backOffice.maxProjects", {
adminUserId,
orgId,
previous: existing.maximumProjectCount,
next: submission.data.maximumProjectCount,
});
return { ok: true };
}