fix(frontend): project create-user HTTP status onto a 'created' flag

createSystemUser now returns 'created' derived from $httpStatus
(201 create / 200 idempotent retry).
main
MidoriKurage 2026-09-07 14:34:46 +08:00 committed by lyingbug
parent 9e8673816e
commit 384fbcf7ed
3 changed files with 24 additions and 29 deletions

View File

@ -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<CreateSystemUserResponse> {
const response = await post('/api/v1/system/admin/users/create', req)
return response as unknown as CreateSystemUserResponse
export async function createSystemUser(req: CreateSystemUserRequest): Promise<CreateSystemUserResult> {
const response = await post<CreateSystemUserResponse>('/api/v1/system/admin/users/create', req)
return { ...response, created: response.$httpStatus === 201 }
}
// ---- System Settings (P1) ----

View File

@ -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' },
)
})

View File

@ -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<CreateSystemUserResult, 'user'>,
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) {