feat: add unified Person+User creation backend
This commit is contained in:
@@ -9,12 +9,20 @@ import {
|
||||
type CreatePersonFormType,
|
||||
type UpdatePersonFormType,
|
||||
} from "@/schemas/person.schema"
|
||||
import {
|
||||
buildUnifiedCreateSchema,
|
||||
type UnifiedCreateFormType,
|
||||
} from "@/schemas/user.schema"
|
||||
import {
|
||||
createPersonUseCase,
|
||||
createPersonUserUseCase,
|
||||
updatePersonUseCase,
|
||||
} from "@/use-cases/person.use-cases"
|
||||
|
||||
import { localizePersonFieldErrors } from "./person.messages"
|
||||
import { localizeUnifiedCreateFieldErrors } from "./user.messages"
|
||||
|
||||
const PERSON_USER_PATH = "/admin/users"
|
||||
|
||||
export async function createNewPerson(formData: CreatePersonFormType) {
|
||||
const { dictionary } = await getI18n()
|
||||
@@ -56,6 +64,50 @@ export async function createNewPerson(formData: CreatePersonFormType) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function createPersonUserAction(
|
||||
formData: UnifiedCreateFormType,
|
||||
) {
|
||||
const { dictionary } = await getI18n()
|
||||
const userCopy = dictionary.admin.users
|
||||
const schemaCopy = {
|
||||
...userCopy.schema,
|
||||
...dictionary.inventory.people.schema,
|
||||
}
|
||||
const validatedFields = buildUnifiedCreateSchema(schemaCopy).safeParse(
|
||||
formData,
|
||||
)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
success: false,
|
||||
errors: flattenError(validatedFields.error).fieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await createPersonUserUseCase(validatedFields.data)
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
...result,
|
||||
errors: localizeUnifiedCreateFieldErrors(
|
||||
result.errors,
|
||||
userCopy.actions,
|
||||
schemaCopy,
|
||||
),
|
||||
message: userCopy.actions.createFailure,
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath(PERSON_USER_PATH)
|
||||
|
||||
return { success: true, message: userCopy.actions.createSuccess }
|
||||
} catch (error) {
|
||||
console.error("Database error:", error)
|
||||
return { success: false, message: userCopy.actions.createFailure }
|
||||
}
|
||||
}
|
||||
|
||||
export async function updatePerson(formData: UpdatePersonFormType) {
|
||||
const { dictionary } = await getI18n()
|
||||
const copy = dictionary.inventory.people
|
||||
|
||||
Reference in New Issue
Block a user