forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-form.ts
More file actions
126 lines (108 loc) · 4.11 KB
/
Copy pathuser-form.ts
File metadata and controls
126 lines (108 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { z } from 'zod'
import {
type PermissionCatalog,
type AdminPermissionMatrix,
normalizeAdminPermissions,
} from '@/lib/admin-permissions'
import { quotaUnitsToDollars } from '@/lib/format'
import { ROLE } from '@/lib/roles'
import { DEFAULT_GROUP } from '../constants'
import { type UserFormData, type User } from '../types'
// ============================================================================
// Form Schema
// ============================================================================
export const userFormSchema = z.object({
username: z.string().min(1, 'Username is required'),
display_name: z.string().optional(),
password: z.string().optional(),
role: z.number().optional(),
quota_dollars: z.number().min(0).optional(),
group: z.string().optional(),
remark: z.string().optional(),
admin_permissions: z
.record(z.string(), z.record(z.string(), z.boolean()))
.optional(),
})
export type UserFormValues = z.infer<typeof userFormSchema>
// ============================================================================
// Form Defaults
// ============================================================================
export const USER_FORM_DEFAULT_VALUES: UserFormValues = {
username: '',
display_name: '',
password: '',
role: 1, // Default to common user
quota_dollars: 0,
group: DEFAULT_GROUP,
remark: '',
// Filled against the backend catalog at render time; see UsersMutateDrawer.
admin_permissions: {},
}
// ============================================================================
// Form Data Transformation
// ============================================================================
/**
* Transform form data to API payload
*/
export function transformFormDataToPayload(
data: UserFormValues,
userId?: number,
catalog?: PermissionCatalog
): UserFormData & { id?: number } {
const payload: UserFormData & { id?: number } = {
username: data.username,
display_name: data.display_name || data.username,
password: data.password || undefined,
}
const role = userId === undefined ? data.role || 1 : (data.role ?? 0)
// Only send the permission matrix when the target is an admin and the catalog
// is available; without the catalog we cannot build a full matrix, so we omit
// the field (the backend then leaves existing permissions untouched).
if (role >= ROLE.ADMIN && catalog) {
payload.admin_permissions = normalizeAdminPermissions(
data.admin_permissions as AdminPermissionMatrix | undefined,
catalog
)
}
// For create: only send required fields
if (userId === undefined) {
payload.role = role
} else {
// For update: quota is adjusted atomically via /api/user/manage, not sent here
payload.group = data.group
payload.remark = data.remark || undefined
payload.id = userId
}
return payload
}
/**
* Transform user data to form defaults. The admin permission matrix is passed
* through as-is (the backend already returns a full matrix); it is filled against
* the catalog at render time in UsersMutateDrawer.
*/
export function transformUserToFormDefaults(user: User): UserFormValues {
return {
username: user.username,
display_name: user.display_name,
password: '',
role: user.role,
quota_dollars: quotaUnitsToDollars(user.quota),
group: user.group || DEFAULT_GROUP,
remark: user.remark || '',
admin_permissions: user.admin_permissions ?? {},
}
}