193 lines
4.7 KiB
TypeScript
193 lines
4.7 KiB
TypeScript
"use server"
|
|
|
|
import { revalidatePath } from "next/cache"
|
|
import { flattenError } from "zod"
|
|
import { getI18n } from "@/i18n/server"
|
|
import {
|
|
buildCreatePersonSchema,
|
|
buildUpdatePersonSchema,
|
|
type CreatePersonFormType,
|
|
type UpdatePersonFormType,
|
|
} from "@/schemas/person.schema"
|
|
import {
|
|
buildUnifiedCreateSchema,
|
|
buildUnifiedUpdateSchema,
|
|
type UnifiedCreateFormType,
|
|
type UnifiedSchemaCopy,
|
|
type UnifiedUpdateFormType,
|
|
} from "@/schemas/user.schema"
|
|
import {
|
|
createPersonUseCase,
|
|
createPersonUserUseCase,
|
|
updatePersonUseCase,
|
|
updatePersonUserUseCase,
|
|
} from "@/use-cases/person.use-cases"
|
|
|
|
import { localizePersonFieldErrors } from "./person.messages"
|
|
import { localizeUnifiedCreateFieldErrors } from "./user.messages"
|
|
|
|
const PERSON_USER_PATH = "/people"
|
|
|
|
export async function createNewPerson(formData: CreatePersonFormType) {
|
|
const { dictionary } = await getI18n()
|
|
const copy = dictionary.inventory.people
|
|
const validatedFields = buildCreatePersonSchema(copy.schema).safeParse(
|
|
formData,
|
|
)
|
|
|
|
if (!validatedFields.success) {
|
|
return {
|
|
success: false,
|
|
errors: flattenError(validatedFields.error).fieldErrors,
|
|
}
|
|
}
|
|
|
|
try {
|
|
const result = await createPersonUseCase(validatedFields.data)
|
|
|
|
if (!result.success) {
|
|
return {
|
|
...result,
|
|
errors: localizePersonFieldErrors(result.errors, copy.actions),
|
|
message: copy.actions.createFailure,
|
|
}
|
|
}
|
|
|
|
revalidatePath("/people")
|
|
|
|
return {
|
|
success: true,
|
|
message: copy.actions.createSuccess,
|
|
}
|
|
} catch (error) {
|
|
console.error("Database error:", error)
|
|
return {
|
|
success: false,
|
|
message: copy.actions.createFailure,
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
const validatedFields = buildUpdatePersonSchema(copy.schema).safeParse(
|
|
formData,
|
|
)
|
|
|
|
if (!validatedFields.success) {
|
|
return {
|
|
success: false,
|
|
errors: flattenError(validatedFields.error).fieldErrors,
|
|
}
|
|
}
|
|
|
|
try {
|
|
const result = await updatePersonUseCase(validatedFields.data)
|
|
|
|
if (!result.success) {
|
|
return {
|
|
...result,
|
|
errors: localizePersonFieldErrors(result.errors, copy.actions),
|
|
message: copy.actions.updateFailure,
|
|
}
|
|
}
|
|
|
|
revalidatePath("/people")
|
|
|
|
return {
|
|
success: true,
|
|
message: copy.actions.updateSuccess,
|
|
}
|
|
} catch (error) {
|
|
console.error("Database error:", error)
|
|
return {
|
|
success: false,
|
|
message: copy.actions.updateFailure,
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function updatePersonUserAction(formData: UnifiedUpdateFormType) {
|
|
const { dictionary } = await getI18n()
|
|
const userCopy = dictionary.admin.users
|
|
const personCopy = dictionary.inventory.people
|
|
const schemaCopy: UnifiedSchemaCopy = {
|
|
...userCopy.schema,
|
|
...personCopy.schema,
|
|
}
|
|
const validatedFields =
|
|
buildUnifiedUpdateSchema(schemaCopy).safeParse(formData)
|
|
|
|
if (!validatedFields.success) {
|
|
return {
|
|
success: false,
|
|
errors: flattenError(validatedFields.error).fieldErrors,
|
|
}
|
|
}
|
|
|
|
try {
|
|
const result = await updatePersonUserUseCase(validatedFields.data)
|
|
|
|
if (!result.success) {
|
|
return {
|
|
...result,
|
|
errors: localizeUnifiedCreateFieldErrors(
|
|
result.errors,
|
|
userCopy.actions,
|
|
schemaCopy,
|
|
),
|
|
message: personCopy.actions.updateFailure,
|
|
}
|
|
}
|
|
|
|
revalidatePath("/people")
|
|
|
|
return { success: true, message: personCopy.actions.updateSuccess }
|
|
} catch (error) {
|
|
console.error("Database error:", error)
|
|
return { success: false, message: personCopy.actions.updateFailure }
|
|
}
|
|
}
|