116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
import { z } from "zod";
|
|
import i18next from "i18next";
|
|
import type { CreateCardPinPayload } from "../api/cardPinApi";
|
|
import { formatToApiPhone } from "@/lib/utils";
|
|
|
|
const optional = () => z.string().default("");
|
|
|
|
export const cardPinFormSchema = z.object({
|
|
uniqueId: z.string().min(1, "validation.required"),
|
|
status: z.string().min(1, "validation.required"),
|
|
customerName: z.string().min(1, "validation.required"),
|
|
customerSurname: z.string().min(1, "validation.required"),
|
|
customerPatronicName: optional(),
|
|
bornAt: optional(),
|
|
region: z.string().min(1, "validation.required"),
|
|
branchId: z.string().min(1, "validation.required"),
|
|
cardTypeId: z.string().min(1, "validation.required"),
|
|
cardNumber: z.string().min(1, "validation.required"),
|
|
phone: z.string().min(1, "validation.required"),
|
|
passportSerie: z.string().min(1, "validation.required"),
|
|
passportId: z.string().min(1, "validation.required"),
|
|
passportOne: optional(),
|
|
passportTwo: optional(),
|
|
passportThree: optional(),
|
|
passportFour: optional(),
|
|
userId: z.string().min(1, "validation.required"),
|
|
notes: optional(),
|
|
});
|
|
|
|
export type CardPinFormData = z.infer<typeof cardPinFormSchema>;
|
|
|
|
export const DEFAULT_FORM_VALUES: CardPinFormData = {
|
|
uniqueId: "",
|
|
status: "",
|
|
customerName: "",
|
|
customerSurname: "",
|
|
customerPatronicName: "",
|
|
bornAt: "",
|
|
region: "",
|
|
branchId: "",
|
|
cardTypeId: "",
|
|
cardNumber: "",
|
|
phone: "",
|
|
passportSerie: "",
|
|
passportId: "",
|
|
passportOne: "",
|
|
passportTwo: "",
|
|
passportThree: "",
|
|
passportFour: "",
|
|
userId: "",
|
|
notes: "",
|
|
};
|
|
|
|
export const stepSchemas: Record<number, z.ZodType<Partial<CardPinFormData>>> = {
|
|
0: cardPinFormSchema.pick({
|
|
uniqueId: true,
|
|
status: true,
|
|
cardNumber: true,
|
|
cardTypeId: true,
|
|
region: true,
|
|
branchId: true,
|
|
}),
|
|
1: cardPinFormSchema.pick({
|
|
customerName: true,
|
|
customerSurname: true,
|
|
bornAt: true,
|
|
phone: true,
|
|
userId: true,
|
|
}),
|
|
2: cardPinFormSchema.pick({
|
|
passportSerie: true,
|
|
passportId: true,
|
|
}),
|
|
};
|
|
|
|
function translateMsg(msg: string): string {
|
|
return msg.startsWith("validation.") ? i18next.t(msg, msg) : msg;
|
|
}
|
|
|
|
export function validateStep(stepIndex: number, form: CardPinFormData): Partial<Record<keyof CardPinFormData, string>> {
|
|
const schema = stepSchemas[stepIndex];
|
|
const result = schema.safeParse(form);
|
|
if (result.success) return {};
|
|
|
|
const errors: Partial<Record<keyof CardPinFormData, string>> = {};
|
|
for (const issue of result.error.issues) {
|
|
const key = issue.path[0] as keyof CardPinFormData;
|
|
if (!errors[key]) errors[key] = translateMsg(issue.message);
|
|
}
|
|
return errors;
|
|
}
|
|
|
|
export function buildPayload(data: CardPinFormData): CreateCardPinPayload {
|
|
return {
|
|
uniqueId: data.uniqueId.trim(),
|
|
status: data.status.trim(),
|
|
customerName: data.customerName.trim(),
|
|
customerSurname: data.customerSurname.trim(),
|
|
customerPatronicName: data.customerPatronicName.trim(),
|
|
bornAt: data.bornAt.trim(),
|
|
region: data.region.trim(),
|
|
branchId: Number(data.branchId),
|
|
cardTypeId: Number(data.cardTypeId),
|
|
cardNumber: data.cardNumber.trim(),
|
|
phone: formatToApiPhone(data.phone),
|
|
passportSerie: data.passportSerie.trim(),
|
|
passportId: data.passportId.trim(),
|
|
passportOne: data.passportOne.trim(),
|
|
passportTwo: data.passportTwo.trim(),
|
|
passportThree: data.passportThree.trim(),
|
|
passportFour: data.passportFour.trim(),
|
|
userId: Number(data.userId),
|
|
notes: data.notes.trim(),
|
|
};
|
|
}
|