-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(webapp): admin back-office editors for org max projects and batch rate limit #3475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
isshaddad
wants to merge
7
commits into
main
Choose a base branch
from
feat/admin-back-office-org-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3c82e4
refactor(webapp): split admin back-office org page into per-section c…
isshaddad 90cf9d0
feat(webapp): admin editor for org maximum project count
isshaddad 3cc7248
refactor(webapp): generalize admin rate-limit section for reuse
isshaddad 147195b
feat(webapp): admin editor for org batch rate limit
isshaddad 9926521
coderabbit fixes
isshaddad a325bd5
more coderabbit fixes
isshaddad 8c01a06
refactor(webapp): tighten admin rate-limit duration types
isshaddad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: feature | ||
| --- | ||
|
|
||
| Admin back office: edit an organization's batch rate limit (`batchRateLimitConfig`) from the org page, alongside the existing API rate limit editor. The rate-limit form UI is now shared between the API and batch sections. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: feature | ||
| --- | ||
|
|
||
| Admin back office: edit an organization's `maximumProjectCount` from the org page, beneath the API rate limit editor. |
55 changes: 55 additions & 0 deletions
55
apps/webapp/app/components/admin/backOffice/ApiRateLimitSection.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { prisma } from "~/db.server"; | ||
| import { env } from "~/env.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { type Duration } from "~/services/rateLimiter.server"; | ||
| import { API_RATE_LIMIT_INTENT } from "./ApiRateLimitSection"; | ||
| import { | ||
| handleRateLimitAction, | ||
| resolveEffectiveRateLimit, | ||
| type RateLimitActionResult, | ||
| type RateLimitDomain, | ||
| } from "./RateLimitSection.server"; | ||
| import type { EffectiveRateLimit } from "./RateLimitSection"; | ||
|
|
||
| export const apiRateLimitDomain: RateLimitDomain = { | ||
| intent: API_RATE_LIMIT_INTENT, | ||
| systemDefault: () => ({ | ||
| type: "tokenBucket", | ||
| refillRate: env.API_RATE_LIMIT_REFILL_RATE, | ||
| interval: env.API_RATE_LIMIT_REFILL_INTERVAL as Duration, | ||
| maxTokens: env.API_RATE_LIMIT_MAX, | ||
| }), | ||
| apply: async (orgId, next, adminUserId) => { | ||
| const existing = await prisma.organization.findFirst({ | ||
| where: { id: orgId }, | ||
| select: { apiRateLimiterConfig: true }, | ||
| }); | ||
| if (!existing) { | ||
| throw new Response(null, { status: 404 }); | ||
| } | ||
| await prisma.organization.update({ | ||
| where: { id: orgId }, | ||
| data: { apiRateLimiterConfig: next as any }, | ||
| }); | ||
| logger.info("admin.backOffice.apiRateLimit", { | ||
| adminUserId, | ||
| orgId, | ||
| previous: existing.apiRateLimiterConfig, | ||
| next, | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| export function resolveEffectiveApiRateLimit( | ||
| override: unknown | ||
| ): EffectiveRateLimit { | ||
| return resolveEffectiveRateLimit(override, apiRateLimitDomain); | ||
| } | ||
|
|
||
| export function handleApiRateLimitAction( | ||
| formData: FormData, | ||
| orgId: string, | ||
| adminUserId: string | ||
| ): Promise<RateLimitActionResult> { | ||
| return handleRateLimitAction(formData, orgId, adminUserId, apiRateLimitDomain); | ||
| } |
17 changes: 17 additions & 0 deletions
17
apps/webapp/app/components/admin/backOffice/ApiRateLimitSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { | ||
| RateLimitSection, | ||
| type RateLimitWrapperProps, | ||
| } from "./RateLimitSection"; | ||
|
|
||
| export const API_RATE_LIMIT_INTENT = "set-rate-limit"; | ||
| export const API_RATE_LIMIT_SAVED_VALUE = "rate-limit"; | ||
|
|
||
| export function ApiRateLimitSection(props: RateLimitWrapperProps) { | ||
| return ( | ||
| <RateLimitSection | ||
| title="API rate limit" | ||
| intent={API_RATE_LIMIT_INTENT} | ||
| {...props} | ||
| /> | ||
| ); | ||
| } |
55 changes: 55 additions & 0 deletions
55
apps/webapp/app/components/admin/backOffice/BatchRateLimitSection.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { prisma } from "~/db.server"; | ||
| import { env } from "~/env.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { type Duration } from "~/services/rateLimiter.server"; | ||
| import { BATCH_RATE_LIMIT_INTENT } from "./BatchRateLimitSection"; | ||
| import { | ||
| handleRateLimitAction, | ||
| resolveEffectiveRateLimit, | ||
| type RateLimitActionResult, | ||
| type RateLimitDomain, | ||
| } from "./RateLimitSection.server"; | ||
| import type { EffectiveRateLimit } from "./RateLimitSection"; | ||
|
|
||
| export const batchRateLimitDomain: RateLimitDomain = { | ||
| intent: BATCH_RATE_LIMIT_INTENT, | ||
| systemDefault: () => ({ | ||
| type: "tokenBucket", | ||
| refillRate: env.BATCH_RATE_LIMIT_REFILL_RATE, | ||
| interval: env.BATCH_RATE_LIMIT_REFILL_INTERVAL as Duration, | ||
| maxTokens: env.BATCH_RATE_LIMIT_MAX, | ||
| }), | ||
| apply: async (orgId, next, adminUserId) => { | ||
| const existing = await prisma.organization.findFirst({ | ||
| where: { id: orgId }, | ||
| select: { batchRateLimitConfig: true }, | ||
| }); | ||
| if (!existing) { | ||
| throw new Response(null, { status: 404 }); | ||
| } | ||
| await prisma.organization.update({ | ||
| where: { id: orgId }, | ||
| data: { batchRateLimitConfig: next as any }, | ||
| }); | ||
| logger.info("admin.backOffice.batchRateLimit", { | ||
| adminUserId, | ||
| orgId, | ||
| previous: existing.batchRateLimitConfig, | ||
| next, | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| export function resolveEffectiveBatchRateLimit( | ||
| override: unknown | ||
| ): EffectiveRateLimit { | ||
| return resolveEffectiveRateLimit(override, batchRateLimitDomain); | ||
| } | ||
|
|
||
| export function handleBatchRateLimitAction( | ||
| formData: FormData, | ||
| orgId: string, | ||
| adminUserId: string | ||
| ): Promise<RateLimitActionResult> { | ||
| return handleRateLimitAction(formData, orgId, adminUserId, batchRateLimitDomain); | ||
| } |
17 changes: 17 additions & 0 deletions
17
apps/webapp/app/components/admin/backOffice/BatchRateLimitSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { | ||
| RateLimitSection, | ||
| type RateLimitWrapperProps, | ||
| } from "./RateLimitSection"; | ||
|
|
||
| export const BATCH_RATE_LIMIT_INTENT = "set-batch-rate-limit"; | ||
| export const BATCH_RATE_LIMIT_SAVED_VALUE = "batch-rate-limit"; | ||
|
|
||
| export function BatchRateLimitSection(props: RateLimitWrapperProps) { | ||
| return ( | ||
| <RateLimitSection | ||
| title="Batch rate limit" | ||
| intent={BATCH_RATE_LIMIT_INTENT} | ||
| {...props} | ||
| /> | ||
| ); | ||
| } |
48 changes: 48 additions & 0 deletions
48
apps/webapp/app/components/admin/backOffice/MaxProjectsSection.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| 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 }; | ||
| } | ||
115 changes: 115 additions & 0 deletions
115
apps/webapp/app/components/admin/backOffice/MaxProjectsSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { Form } from "@remix-run/react"; | ||
| import { useEffect, useState } from "react"; | ||
| import { Button } from "~/components/primitives/Buttons"; | ||
| import { FormError } from "~/components/primitives/FormError"; | ||
| import { Header2 } from "~/components/primitives/Headers"; | ||
| import { Input } from "~/components/primitives/Input"; | ||
| import { Label } from "~/components/primitives/Label"; | ||
| import { Paragraph } from "~/components/primitives/Paragraph"; | ||
| import * as Property from "~/components/primitives/PropertyTable"; | ||
|
|
||
| export const MAX_PROJECTS_INTENT = "set-max-projects"; | ||
| export const MAX_PROJECTS_SAVED_VALUE = "max-projects"; | ||
|
|
||
| type FieldErrors = Record<string, string[] | undefined> | null; | ||
|
|
||
| type Props = { | ||
| maximumProjectCount: number; | ||
| errors: FieldErrors; | ||
| savedJustNow: boolean; | ||
| isSubmitting: boolean; | ||
| }; | ||
|
|
||
| export function MaxProjectsSection({ | ||
| maximumProjectCount, | ||
| errors, | ||
| savedJustNow, | ||
| isSubmitting, | ||
| }: Props) { | ||
| const hasFieldErrors = !!errors && Object.keys(errors).length > 0; | ||
| const fieldError = (field: string) => | ||
| errors && field in errors ? errors[field]?.[0] : undefined; | ||
|
|
||
| const [isEditing, setIsEditing] = useState(hasFieldErrors); | ||
| const [value, setValue] = useState(String(maximumProjectCount)); | ||
|
|
||
| useEffect(() => { | ||
| if (hasFieldErrors) setIsEditing(true); | ||
| }, [hasFieldErrors]); | ||
|
|
||
| useEffect(() => { | ||
| if (savedJustNow && !hasFieldErrors) setIsEditing(false); | ||
| }, [savedJustNow, hasFieldErrors]); | ||
|
|
||
| return ( | ||
| <section className="flex flex-col gap-3 rounded-md border border-charcoal-700 bg-charcoal-800 p-4"> | ||
| <div className="flex items-center justify-between"> | ||
| <Header2>Maximum projects</Header2> | ||
| {!isEditing && ( | ||
| <Button | ||
| variant="tertiary/small" | ||
| onClick={() => setIsEditing(true)} | ||
| disabled={isSubmitting} | ||
| > | ||
| Edit | ||
| </Button> | ||
| )} | ||
| </div> | ||
|
|
||
| {savedJustNow && ( | ||
| <div className="rounded-md border border-green-600/40 bg-green-600/10 px-3 py-2"> | ||
| <Paragraph variant="small" className="text-green-500"> | ||
| Saved. | ||
| </Paragraph> | ||
| </div> | ||
| )} | ||
|
|
||
| {!isEditing ? ( | ||
| <Property.Table> | ||
| <Property.Item> | ||
| <Property.Label>Limit</Property.Label> | ||
| <Property.Value> | ||
| {maximumProjectCount.toLocaleString()} | ||
| </Property.Value> | ||
| </Property.Item> | ||
| </Property.Table> | ||
| ) : ( | ||
| <Form method="post" className="flex flex-col gap-3 pt-2"> | ||
| <input type="hidden" name="intent" value={MAX_PROJECTS_INTENT} /> | ||
| <div className="flex flex-col gap-1"> | ||
| <Label>Maximum projects</Label> | ||
| <Input | ||
| name="maximumProjectCount" | ||
| type="number" | ||
| min={1} | ||
| value={value} | ||
| onChange={(e) => setValue(e.target.value)} | ||
| required | ||
| /> | ||
| <FormError>{fieldError("maximumProjectCount")}</FormError> | ||
| </div> | ||
| <div className="flex items-center gap-2"> | ||
| <Button | ||
| type="submit" | ||
| variant="primary/medium" | ||
| disabled={isSubmitting || !value.trim()} | ||
| > | ||
| Save | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| variant="tertiary/medium" | ||
| onClick={() => { | ||
| setValue(String(maximumProjectCount)); | ||
| setIsEditing(false); | ||
| }} | ||
| disabled={isSubmitting} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| </div> | ||
| </Form> | ||
| )} | ||
| </section> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.