Files
arcadia-admin/app/lib/arcadia/users.ts
jules af2c8d6663 Phase 5: platform feature-flags CRUD, impersonation, billing catalogue
Three new platform screens on top of the Phase 1-4 work.

Feature flags (/feature-flags) — platform-wide flag registry. New route +
lib/arcadia/feature-flags.ts, capability platform.feature_flags, nav under
Automation. List/create/edit/delete with a per-row default toggle; pairs with
the Phase-4 per-tenant override tab.

Impersonation — "Impersonate" action on active users. Entirely client-side
token swap in session.ts (beginImpersonation parks the operator's session +
API token and swaps to the impersonation token; endImpersonation restores it),
with a sticky "Viewing as <email> — Stop" banner in the shell driven by the
JWT's impersonated_by claim. Stop is client-side because the impersonation
token carries the target's roles and can't reach the admin-gated /stop
endpoint; impersonation is stateless JWT so restoring the parked token is
sufficient.

Billing (/billing) — replaced the coming-soon stub with the real plan
catalogue from GET /billing/plans (lib/arcadia/billing.ts). Per-tenant plan
assignment stays on the tenant detail page; Entitlements + Apps remain honestly
marked "Soon".

Verified in-browser with real backend; typecheck adds zero errors (36→36).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 14:04:09 +10:00

150 lines
4.0 KiB
TypeScript

// Arcadia users API helpers.
//
// Backed by /api/v1/users (resources route). The OpenAPI spec doesn't yet
// describe these operations as typed paths, so we hand-roll types and use
// the generic verb methods on the client. Same pattern as tenants.ts.
import type { ArcadiaClient } from "@crema/arcadia-core-client"
export type UserStatus = "active" | "inactive" | "suspended"
export interface UserRoleSummary {
id: string
slug: string
name: string
permissions: string[]
}
export interface User {
id: string
email: string
first_name: string | null
last_name: string | null
full_name: string
status: UserStatus
email_verified: boolean
email_verified_at: string | null
last_sign_in_at: string | null
tenant_id: string
roles: UserRoleSummary[]
inserted_at: string
updated_at: string
}
export interface UserListParams {
status?: UserStatus
email_verified?: boolean
}
export interface UserInput {
email: string
first_name?: string | null
last_name?: string | null
status?: UserStatus
password?: string
role_ids?: string[]
}
export async function listUsers(
arcadia: ArcadiaClient,
params?: UserListParams,
): Promise<User[]> {
const queryParams = params
? {
status: params.status,
email_verified: params.email_verified == null ? undefined : String(params.email_verified),
}
: undefined
const res = await arcadia.GET<{ data: User[] }>("/api/v1/users", { params: queryParams })
return res.data
}
export async function getUser(arcadia: ArcadiaClient, id: string): Promise<User> {
const res = await arcadia.GET<{ data: User }>(`/api/v1/users/${id}`)
return res.data
}
export async function createUser(arcadia: ArcadiaClient, input: UserInput): Promise<User> {
const res = await arcadia.POST<{ data: User }>("/api/v1/users", { body: { user: input } })
return res.data
}
export async function updateUser(
arcadia: ArcadiaClient,
id: string,
input: Partial<UserInput>,
): Promise<User> {
const res = await arcadia.PATCH<{ data: User }>(`/api/v1/users/${id}`, {
body: { user: input },
})
return res.data
}
export async function deleteUser(arcadia: ArcadiaClient, id: string): Promise<void> {
await arcadia.DELETE(`/api/v1/users/${id}`)
}
export async function assignRole(
arcadia: ArcadiaClient,
userId: string,
roleId: string,
): Promise<User> {
const res = await arcadia.POST<{ data: User }>(`/api/v1/users/${userId}/roles/${roleId}`)
return res.data
}
export async function removeRole(
arcadia: ArcadiaClient,
userId: string,
roleId: string,
): Promise<User> {
const res = await arcadia.DELETE<{ data: User }>(`/api/v1/users/${userId}/roles/${roleId}`)
return res.data
}
export async function setUserStatus(
arcadia: ArcadiaClient,
id: string,
status: UserStatus,
): Promise<User> {
return updateUser(arcadia, id, { status })
}
// --- Impersonation ---
// The operator (platform/tenant admin) can act as another user for support.
// `startImpersonation` returns a token scoped to the target; the client swaps
// to it (see session.beginImpersonation). Stopping is a client-side restore of
// the operator's parked session — the /stop endpoint can't be reached with the
// impersonation token (it lacks admin), so we don't rely on it.
export interface CanImpersonate {
can_impersonate: boolean
user: Pick<User, "id" | "email" | "status"> & { first_name?: string; last_name?: string }
}
export interface ImpersonationToken {
access_token: string
impersonated_by: string
expires_in: number
}
export async function canImpersonate(
arcadia: ArcadiaClient,
userId: string,
): Promise<CanImpersonate> {
const res = await arcadia.GET<{ data: CanImpersonate }>(
`/api/v1/admin/impersonate/${userId}/can-impersonate`,
)
return res.data
}
export async function startImpersonation(
arcadia: ArcadiaClient,
userId: string,
): Promise<ImpersonationToken> {
const res = await arcadia.POST<{ data: ImpersonationToken }>(
`/api/v1/admin/impersonate/${userId}`,
)
return res.data
}