diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 5b503a1f..7cc0c6e9 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -398,22 +398,25 @@ export interface CreateSystemUserResponse { * be fetched again. */ generated_password?: string +} + +export interface CreateSystemUserResult extends CreateSystemUserResponse { /** - * True on the 200 retry when the identity already existed. The shared - * axios interceptor drops HTTP status, so callers must read this flag - * instead of the status code. + * True only when this call created the account (HTTP 201), false on the + * idempotent 200 retry (identity already existed). */ - idempotent?: boolean + created: boolean } /** * Provision a new local user account (SystemAdmin only). - * Backend returns the unwrapped CreateSystemUserResponse body. - * 201 on create, 200 with `idempotent: true` when the identity existed. + * The backend answers 201 on create and 200 on the idempotent retry with + * the same CreateSystemUserResponse body. The status is projected onto + * `created`. */ -export async function createSystemUser(req: CreateSystemUserRequest): Promise { - const response = await post('/api/v1/system/admin/users/create', req) - return response as unknown as CreateSystemUserResponse +export async function createSystemUser(req: CreateSystemUserRequest): Promise { + const response = await post('/api/v1/system/admin/users/create', req) + return { ...response, created: response.$httpStatus === 201 } } // ---- System Settings (P1) ---- diff --git a/frontend/src/views/system/createUserDialogState.test.ts b/frontend/src/views/system/createUserDialogState.test.ts index a32a9751..43595cf6 100644 --- a/frontend/src/views/system/createUserDialogState.test.ts +++ b/frontend/src/views/system/createUserDialogState.test.ts @@ -14,7 +14,7 @@ const identity = { username: 'alice', email: 'alice@example.com' } test('reveal wins whenever the server minted a one-time password', () => { const view = resolveCreateUserView( - { generated_password: 'OnceOnly9', idempotent: false }, + { generated_password: 'OnceOnly9', created: true }, identity, true, ) @@ -26,27 +26,27 @@ test('reveal wins whenever the server minted a one-time password', () => { }) }) -test('idempotent flag is the 200 retry signal when no password was minted', () => { +test('created:false is the 200 retry signal when no password was minted', () => { assert.deepEqual( - resolveCreateUserView({ idempotent: true }, identity, true), + resolveCreateUserView({ created: false }, identity, true), { kind: 'idempotent' }, ) assert.deepEqual( - resolveCreateUserView({ idempotent: true }, identity, false), + resolveCreateUserView({ created: false }, identity, false), { kind: 'idempotent' }, ) }) -test('supplied-password create without the idempotent flag is a real create', () => { +test('created:true with a supplied password is a real create', () => { assert.deepEqual( - resolveCreateUserView({}, identity, false), + resolveCreateUserView({ created: true }, identity, false), { kind: 'created' }, ) }) -test('auto-generate create missing generated_password is not treated as idempotent', () => { +test('auto-generate create that failed to return generated_password surfaces missingPassword', () => { assert.deepEqual( - resolveCreateUserView({}, identity, true), + resolveCreateUserView({ created: true }, identity, true), { kind: 'missingPassword' }, ) }) diff --git a/frontend/src/views/system/createUserDialogState.ts b/frontend/src/views/system/createUserDialogState.ts index d4ab5191..6c9e4fb8 100644 --- a/frontend/src/views/system/createUserDialogState.ts +++ b/frontend/src/views/system/createUserDialogState.ts @@ -1,22 +1,14 @@ /** * Pure helpers for the SystemAdmin create-user dialog. - * - * The axios interceptor discards HTTP status, so 201 (created) and 200 - * (idempotent retry) used to look identical. The backend now sets - * `idempotent` on the body; generated_password is still the signal for - * the one-time reveal view. */ +import type { CreateSystemUserResult } from "@/api/system" + export type CreateUserIdentity = { username: string email: string } -export type CreateUserApiResponse = { - generated_password?: string - idempotent?: boolean -} - export type CreateUserReveal = CreateUserIdentity & { generatedPassword: string } @@ -30,7 +22,7 @@ export type CreateUserView = export type CreateUserNotice = 'reveal' | 'created' | 'idempotent' | 'missingPassword' export function resolveCreateUserView( - response: CreateUserApiResponse, + response: Omit, identity: CreateUserIdentity, autoGenerate: boolean, ): CreateUserView { @@ -43,7 +35,7 @@ export function resolveCreateUserView( generatedPassword: generated, } } - if (response.idempotent) { + if (!response.created) { return { kind: 'idempotent' } } if (autoGenerate) {