refactor: rename Recipient to Person, remove username, add userId FK
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import type { Dictionary } from "@/i18n/dictionaries"
|
||||
|
||||
export type PersonSchemaCopy = Dictionary["inventory"]["people"]["schema"]
|
||||
|
||||
const defaultPersonSchemaCopy: PersonSchemaCopy = {
|
||||
firstNameRequired: "First name is required",
|
||||
lastNameRequired: "Last name is required",
|
||||
departmentRequired: "Department is required",
|
||||
emailInvalid: "Email format is invalid",
|
||||
idRequired: "ID is required",
|
||||
}
|
||||
|
||||
const personDepartments = [
|
||||
"IT",
|
||||
"ENGINEERING",
|
||||
"TRAFFIC",
|
||||
"DRIVER",
|
||||
"LOGISTICS",
|
||||
"ADMINISTRATION",
|
||||
"SALES",
|
||||
"OTHER",
|
||||
] as const
|
||||
|
||||
function buildPersonBaseSchema(copy: PersonSchemaCopy) {
|
||||
return z.object({
|
||||
id: z.string().optional(),
|
||||
firstName: z.string().min(1, {
|
||||
error: copy.firstNameRequired,
|
||||
}),
|
||||
lastName: z.string().min(1, {
|
||||
error: copy.lastNameRequired,
|
||||
}),
|
||||
department: z.enum(personDepartments, {
|
||||
error: copy.departmentRequired,
|
||||
}),
|
||||
email: z.string().optional().nullable(),
|
||||
phone: z.string().optional().nullable(),
|
||||
userId: z.string().uuid().optional().nullable(),
|
||||
})
|
||||
}
|
||||
|
||||
export const personSchema = buildPersonBaseSchema(defaultPersonSchemaCopy)
|
||||
|
||||
export function buildCreatePersonSchema(copy: PersonSchemaCopy) {
|
||||
return buildPersonBaseSchema(copy).superRefine((data, ctx) => {
|
||||
if (data.email && !z.string().email().safeParse(data.email).success) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: copy.emailInvalid,
|
||||
path: ["email"],
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const createPersonSchema = buildCreatePersonSchema(
|
||||
defaultPersonSchemaCopy,
|
||||
)
|
||||
|
||||
export type CreatePersonFormType = z.infer<typeof createPersonSchema>
|
||||
|
||||
export function buildUpdatePersonSchema(copy: PersonSchemaCopy) {
|
||||
return buildPersonBaseSchema(copy).extend({
|
||||
id: z.string().nonempty(copy.idRequired),
|
||||
})
|
||||
}
|
||||
|
||||
export const updatePersonSchema = buildUpdatePersonSchema(
|
||||
defaultPersonSchemaCopy,
|
||||
)
|
||||
|
||||
export type UpdatePersonFormType = z.infer<typeof updatePersonSchema>
|
||||
@@ -1,80 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import type { Dictionary } from "@/i18n/dictionaries"
|
||||
|
||||
export type RecipientSchemaCopy =
|
||||
Dictionary["inventory"]["recipients"]["schema"]
|
||||
|
||||
const defaultRecipientSchemaCopy: RecipientSchemaCopy = {
|
||||
usernameRequired: "Username is required",
|
||||
firstNameRequired: "First name is required",
|
||||
lastNameRequired: "Last name is required",
|
||||
departmentRequired: "Department is required",
|
||||
emailInvalid: "Email format is invalid",
|
||||
idRequired: "ID is required",
|
||||
}
|
||||
|
||||
const recipientDepartments = [
|
||||
"IT",
|
||||
"ENGINEERING",
|
||||
"TRAFFIC",
|
||||
"DRIVER",
|
||||
"LOGISTICS",
|
||||
"ADMINISTRATION",
|
||||
"SALES",
|
||||
"OTHER",
|
||||
] as const
|
||||
|
||||
function buildRecipientBaseSchema(copy: RecipientSchemaCopy) {
|
||||
return z.object({
|
||||
id: z.string().optional(),
|
||||
username: z.string().min(1, {
|
||||
error: copy.usernameRequired,
|
||||
}),
|
||||
firstName: z.string().min(1, {
|
||||
error: copy.firstNameRequired,
|
||||
}),
|
||||
lastName: z.string().min(1, {
|
||||
error: copy.lastNameRequired,
|
||||
}),
|
||||
department: z.enum(recipientDepartments, {
|
||||
error: copy.departmentRequired,
|
||||
}),
|
||||
email: z.string().optional().nullable(),
|
||||
phone: z.string().optional().nullable(),
|
||||
})
|
||||
}
|
||||
|
||||
export const recipientSchema = buildRecipientBaseSchema(
|
||||
defaultRecipientSchemaCopy,
|
||||
)
|
||||
|
||||
export function buildCreateRecipientSchema(copy: RecipientSchemaCopy) {
|
||||
return buildRecipientBaseSchema(copy).superRefine((data, ctx) => {
|
||||
if (data.email && !z.string().email().safeParse(data.email).success) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: copy.emailInvalid,
|
||||
path: ["email"],
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const createRecipientSchema = buildCreateRecipientSchema(
|
||||
defaultRecipientSchemaCopy,
|
||||
)
|
||||
|
||||
export type CreateRecipientFormType = z.infer<typeof createRecipientSchema>
|
||||
|
||||
export function buildUpdateRecipientSchema(copy: RecipientSchemaCopy) {
|
||||
return buildRecipientBaseSchema(copy).extend({
|
||||
id: z.string().nonempty(copy.idRequired),
|
||||
})
|
||||
}
|
||||
|
||||
export const updateRecipientSchema = buildUpdateRecipientSchema(
|
||||
defaultRecipientSchemaCopy,
|
||||
)
|
||||
|
||||
export type UpdateRecipientFormType = z.infer<typeof updateRecipientSchema>
|
||||
Reference in New Issue
Block a user