feat: add unified Person+User creation backend
This commit is contained in:
@@ -13,7 +13,7 @@ const defaultPersonSchemaCopy: PersonSchemaCopy = {
|
||||
userIdInvalid: "User ID must be a valid UUID",
|
||||
}
|
||||
|
||||
const personDepartments = [
|
||||
export const personDepartments = [
|
||||
"IT",
|
||||
"ENGINEERING",
|
||||
"TRAFFIC",
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import type { Dictionary } from "@/i18n/dictionaries"
|
||||
import { personDepartments } from "@/schemas/person.schema"
|
||||
|
||||
export type UserSchemaCopy = Dictionary["admin"]["users"]["schema"]
|
||||
|
||||
export type UnifiedSchemaCopy = Dictionary["admin"]["users"]["schema"] &
|
||||
Dictionary["inventory"]["people"]["schema"]
|
||||
|
||||
export const defaultUserSchemaCopy: UserSchemaCopy = {
|
||||
nameRequired: "Name is required",
|
||||
emailInvalid: "Invalid email",
|
||||
@@ -67,3 +71,43 @@ export type CreateUserFormType = z.infer<typeof createUserSchema>
|
||||
export type UpdateUserFormType = z.infer<typeof updateUserSchema>
|
||||
export type SetUserActiveFormType = z.infer<typeof setUserActiveSchema>
|
||||
export type ResetUserPasswordFormType = z.infer<typeof resetUserPasswordSchema>
|
||||
|
||||
export const unifiedFormRoleSchema = z.enum([
|
||||
"ADMIN",
|
||||
"MANAGER",
|
||||
"STAFF",
|
||||
"VIEWER",
|
||||
"NO_USER",
|
||||
])
|
||||
|
||||
export function buildUnifiedCreateSchema(copy: UnifiedSchemaCopy) {
|
||||
return z
|
||||
.object({
|
||||
firstName: z.string().trim().min(1, { error: copy.firstNameRequired }),
|
||||
lastName: z.string().trim().min(1, { error: copy.lastNameRequired }),
|
||||
department: z.enum(personDepartments, {
|
||||
error: copy.departmentRequired,
|
||||
}),
|
||||
email: z.email({ error: copy.emailInvalid }),
|
||||
phone: z.string().optional().nullable(),
|
||||
role: unifiedFormRoleSchema,
|
||||
password: z.string().optional(),
|
||||
isActive: z.boolean(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (
|
||||
data.role !== "NO_USER" &&
|
||||
(!data.password || data.password.length < 8)
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: copy.passwordMinLength,
|
||||
path: ["password"],
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export type UnifiedCreateFormType = z.infer<
|
||||
ReturnType<typeof buildUnifiedCreateSchema>
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user