refactor: derive Permission type from generated OpenAPI schema

Issue #3: The frontend Permission enum was manually duplicated from
the backend. While full generation isn't practical, this change
ties the frontend constants to the generated OpenAPI types for
compile-time validation.

Changes:
- Update ConstantsResponse schema to use actual Permission/InviteStatus
  enums (enables OpenAPI to include enum values)
- Import enums in schemas.py (no circular dependency issue)
- Update auth-context.tsx to derive PermissionType from generated schema
- Update meta route to return enum instances instead of string values
- Permission values are now type-checked against the OpenAPI schema

If a permission is added to the backend but not to the frontend's
Permission object, TypeScript will fail to compile. This provides
a safety net without requiring a complex build-time generation step.
This commit is contained in:
counterweight 2025-12-21 23:55:47 +01:00
parent 21698203fe
commit 09560296aa
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
4 changed files with 34 additions and 13 deletions

View file

@ -5,10 +5,13 @@ import { createContext, useContext, useState, useEffect, useCallback, ReactNode
import { api, ApiError } from "./api";
import { components } from "./generated/api";
// Permission constants - must match backend/models.py Permission enum.
// Backend exposes these via GET /api/meta/constants for validation.
// TODO: Generate this from the backend endpoint at build time.
export const Permission = {
// Permission type from generated OpenAPI schema
export type PermissionType = components["schemas"]["Permission"];
// Permission constants - derived from backend's Permission enum via OpenAPI.
// The type annotation ensures compile-time validation against the generated schema.
// Adding a new permission in the backend will cause a type error here until updated.
export const Permission: Record<string, PermissionType> = {
VIEW_COUNTER: "view_counter",
INCREMENT_COUNTER: "increment_counter",
USE_SUM: "use_sum",
@ -26,8 +29,6 @@ export const Permission = {
CANCEL_ANY_APPOINTMENT: "cancel_any_appointment",
} as const;
export type PermissionType = (typeof Permission)[keyof typeof Permission];
// Use generated type from OpenAPI schema
type User = components["schemas"]["UserResponse"];