mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 21:41:57 +00:00
chore(FE-198): adjust Expense Request Form validation
This commit is contained in:
@@ -3,27 +3,32 @@ import { Expense } from '@/types/api/expense';
|
||||
import { formatDate } from '@/lib/helper';
|
||||
|
||||
type ExpenseFormSchemaType = {
|
||||
category?: {
|
||||
value: 'BOP' | 'NON-BOP';
|
||||
label: 'BOP' | 'NON-BOP';
|
||||
};
|
||||
location?: {
|
||||
value: number;
|
||||
label: string;
|
||||
};
|
||||
transaction_date?: string;
|
||||
kandangs?: { id: number; name: string }[];
|
||||
vendor?: {
|
||||
supplier?: {
|
||||
value: number;
|
||||
label: string;
|
||||
};
|
||||
existing_documents?: { name: string; url: string }[];
|
||||
request_documents?: File[];
|
||||
kandangExpenses: {
|
||||
kandangId: number;
|
||||
expenses: {
|
||||
existing_documents?: { id: number; name: string; url: string }[];
|
||||
deleted_documents?: number[];
|
||||
documents?: File[];
|
||||
cost_per_kandangs: {
|
||||
kandang_id: number;
|
||||
cost_items: {
|
||||
nonstock?: {
|
||||
value: number;
|
||||
label: string;
|
||||
};
|
||||
totalQuantity?: number;
|
||||
totalExpense?: number;
|
||||
quantity?: number;
|
||||
total_cost?: number;
|
||||
notes?: string;
|
||||
}[];
|
||||
}[];
|
||||
@@ -31,6 +36,11 @@ type ExpenseFormSchemaType = {
|
||||
|
||||
export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
||||
Yup.object({
|
||||
category: Yup.object({
|
||||
value: Yup.string().oneOf(['BOP', 'NON-BOP']).required(),
|
||||
label: Yup.string().oneOf(['BOP', 'NON-BOP']).required(),
|
||||
}).required('Kategori wajib diisi!'),
|
||||
|
||||
location: Yup.object({
|
||||
value: Yup.number().min(1).required(),
|
||||
label: Yup.string().required(),
|
||||
@@ -47,35 +57,36 @@ export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
||||
.min(1, 'Kandang wajib dipilih!')
|
||||
.required('Kandang wajib dipilih!'),
|
||||
|
||||
vendor: Yup.object({
|
||||
supplier: Yup.object({
|
||||
value: Yup.number().min(1).required(),
|
||||
label: Yup.string().required(),
|
||||
}).required('Vendor wajib diisi!'),
|
||||
|
||||
existing_documents: Yup.array().of(
|
||||
Yup.object({
|
||||
id: Yup.number().required(),
|
||||
name: Yup.string().required(),
|
||||
url: Yup.string().required(),
|
||||
})
|
||||
),
|
||||
|
||||
request_documents: Yup.array().of(Yup.mixed<File>().required()).optional(),
|
||||
deleted_documents: Yup.array().of(Yup.number().required()).optional(),
|
||||
|
||||
kandangExpenses: Yup.array()
|
||||
documents: Yup.array().of(Yup.mixed<File>().required()).optional(),
|
||||
|
||||
cost_per_kandangs: Yup.array()
|
||||
.of(
|
||||
Yup.object({
|
||||
kandangId: Yup.number().min(1, 'Wajib memilih kandang!').required(),
|
||||
expenses: Yup.array()
|
||||
kandang_id: Yup.number().min(1, 'Wajib memilih kandang!').required(),
|
||||
cost_items: Yup.array()
|
||||
.of(
|
||||
Yup.object({
|
||||
nonstock: Yup.object({
|
||||
value: Yup.number().min(1).required(),
|
||||
label: Yup.string().required(),
|
||||
}).required('Nonstock wajib diisi!'),
|
||||
totalQuantity: Yup.number().required(
|
||||
'Total kuantitas wajib diisi!'
|
||||
),
|
||||
totalExpense: Yup.number().required('Total biaya wajib diisi!'),
|
||||
quantity: Yup.number().required('Total kuantitas wajib diisi!'),
|
||||
total_cost: Yup.number().required('Total biaya wajib diisi!'),
|
||||
notes: Yup.string(),
|
||||
})
|
||||
)
|
||||
@@ -90,7 +101,7 @@ export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
||||
export const UpdateExpenseRequestFormSchema = ExpenseRequestFormSchema;
|
||||
|
||||
export const UploadRequestDocumentsFormSchema = Yup.object({
|
||||
request_documents: Yup.array().of(Yup.mixed<File>().required()).required(),
|
||||
documents: Yup.array().of(Yup.mixed<File>().required()).required(),
|
||||
});
|
||||
|
||||
export type ExpenseRequestFormValues = Yup.InferType<
|
||||
@@ -105,39 +116,52 @@ export const getExpenseFormInitialValues = (
|
||||
initialValues?: Expense
|
||||
): ExpenseRequestFormValues => {
|
||||
return {
|
||||
category: initialValues?.category
|
||||
? {
|
||||
value: initialValues.category,
|
||||
label: initialValues.category,
|
||||
}
|
||||
: undefined,
|
||||
location: initialValues?.location
|
||||
? {
|
||||
value: initialValues.location.id,
|
||||
label: initialValues.location.name,
|
||||
}
|
||||
: undefined,
|
||||
transaction_date: initialValues?.transaction_date
|
||||
? formatDate(initialValues.transaction_date, 'YYYY-MM-DD')
|
||||
transaction_date: initialValues?.expense_date
|
||||
? formatDate(initialValues.expense_date, 'YYYY-MM-DD')
|
||||
: undefined,
|
||||
kandangs: initialValues?.kandangs.map((kandang) => ({
|
||||
id: kandang.id,
|
||||
id: kandang.kandang_id,
|
||||
name: kandang.name,
|
||||
})),
|
||||
vendor: initialValues?.vendor
|
||||
supplier: initialValues?.supplier
|
||||
? {
|
||||
value: initialValues.vendor.id,
|
||||
label: initialValues.vendor.name,
|
||||
value: initialValues.supplier.id,
|
||||
label: initialValues.supplier.name,
|
||||
}
|
||||
: undefined,
|
||||
existing_documents: initialValues?.request_documents,
|
||||
request_documents: [],
|
||||
kandangExpenses: initialValues?.kandang_expenses
|
||||
? initialValues.kandang_expenses.map((kandangExpense) => ({
|
||||
kandangId: kandangExpense.kandang.id,
|
||||
expenses: kandangExpense.expenses.map((expenseItem) => ({
|
||||
nonstock: {
|
||||
value: expenseItem.nonstock.id,
|
||||
label: expenseItem.nonstock.name,
|
||||
},
|
||||
totalQuantity: expenseItem.total_quantity,
|
||||
totalExpense: expenseItem.total_expense,
|
||||
notes: expenseItem.notes,
|
||||
})),
|
||||
existing_documents: initialValues?.documents?.map((doc) => ({
|
||||
id: doc.id,
|
||||
name: doc.path,
|
||||
url: doc.path,
|
||||
})),
|
||||
deleted_documents: [],
|
||||
documents: [],
|
||||
cost_per_kandangs: initialValues?.kandangs
|
||||
? initialValues.kandangs.map((kandangExpense) => ({
|
||||
kandang_id: kandangExpense.kandang_id,
|
||||
cost_items: kandangExpense.pengajuans
|
||||
? kandangExpense.pengajuans.map((expenseItem) => ({
|
||||
nonstock: {
|
||||
value: expenseItem.nonstock.id,
|
||||
label: expenseItem.nonstock.name,
|
||||
},
|
||||
quantity: expenseItem.qty,
|
||||
total_cost: expenseItem.total_price,
|
||||
notes: expenseItem.note,
|
||||
}))
|
||||
: [],
|
||||
}))
|
||||
: [],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user