148 lines
4.3 KiB
JavaScript
148 lines
4.3 KiB
JavaScript
import { boolean, z } from "zod";
|
|
import { INR_CURRENCY_CODE } from "../../utils/constants";
|
|
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
|
|
const ALLOWED_TYPES = [
|
|
"application/pdf",
|
|
"image/png",
|
|
"image/jpg",
|
|
"image/jpeg",
|
|
];
|
|
export const PaymentRequestSchema = (expenseTypes) => {
|
|
return z
|
|
.object({
|
|
title: z.string().min(1, { message: "Project is required" }),
|
|
projectId: z.string().min(1, { message: "Project is required" }),
|
|
expenseCategoryId: z
|
|
.string()
|
|
.min(1, { message: "Expense Category is required" }),
|
|
currencyId: z
|
|
.string()
|
|
.min(1, { message: "Currency is required" }),
|
|
dueDate: z.string().min(1, { message: "Date is required" }),
|
|
description: z.string().min(1, { message: "Description is required" }),
|
|
payee: z.string().min(1, { message: "Supplier name is required" }),
|
|
isAdvancePayment: z.boolean().optional(),
|
|
amount: z.coerce
|
|
.number({
|
|
invalid_type_error: "Amount is required and must be a number",
|
|
})
|
|
.min(1, "Amount must be Enter")
|
|
.refine((val) => /^\d+(\.\d{1,2})?$/.test(val.toString()), {
|
|
message: "Amount must have at most 2 decimal places",
|
|
}),
|
|
billAttachments: z
|
|
.array(
|
|
z.object({
|
|
fileName: z.string().min(1, { message: "Filename is required" }),
|
|
base64Data: z.string().nullable(),
|
|
contentType: z
|
|
.string()
|
|
.refine((val) => ALLOWED_TYPES.includes(val), {
|
|
message: "Only PDF, PNG, JPG, or JPEG files are allowed",
|
|
}),
|
|
documentId: z.string().optional(),
|
|
fileSize: z.number().max(MAX_FILE_SIZE, {
|
|
message: "File size must be less than or equal to 5MB",
|
|
}),
|
|
description: z.string().optional(),
|
|
isActive: z.boolean().default(true),
|
|
})
|
|
)
|
|
,
|
|
})
|
|
};
|
|
|
|
export const defaultPaymentRequest = {
|
|
title:"",
|
|
description: "",
|
|
payee: "",
|
|
currencyId: "",
|
|
amount: "",
|
|
dueDate: "",
|
|
projectId: "",
|
|
expenseCategoryId: "",
|
|
isAdvancePayment:boolean,
|
|
billAttachments: [],
|
|
};
|
|
|
|
|
|
export const SearchPaymentRequestSchema = z.object({
|
|
projectIds: z.array(z.string()).optional(),
|
|
statusIds: z.array(z.string()).optional(),
|
|
createdByIds: z.array(z.string()).optional(),
|
|
currencyIds: z.array(z.string()).optional(),
|
|
expenseCategoryIds: z.array(z.string()).optional(),
|
|
payees: z.array(z.string()).optional(),
|
|
startDate: z.string().optional(),
|
|
endDate: z.string().optional(),
|
|
});
|
|
|
|
export const defaultPaymentRequestFilter = {
|
|
projectIds: [],
|
|
statusIds: [],
|
|
createdByIds: [],
|
|
currencyIds: [],
|
|
expenseCategoryIds: [],
|
|
payees: [],
|
|
startDate: null,
|
|
endDate: null,
|
|
};
|
|
|
|
|
|
export const PaymentRequestActionScheam = (
|
|
isTransaction = false,
|
|
transactionDate
|
|
) => {
|
|
return z
|
|
.object({
|
|
comment: z.string().min(1, { message: "Please leave comment" }),
|
|
statusId: z.string().min(1, { message: "Please select a status" }),
|
|
paymentRequestId: z.string().nullable().optional(),
|
|
paidAt: z.string().nullable().optional(),
|
|
paidById: z.string().nullable().optional(),
|
|
|
|
})
|
|
.superRefine((data, ctx) => {
|
|
if (isTransaction) {
|
|
if (!data.paymentRequestId?.trim()) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
path: ["reimburseTransactionId"],
|
|
message: "Reimburse Transaction ID is required",
|
|
});
|
|
}
|
|
if (!data.paidAt) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
path: ["paidAt"],
|
|
message: "Transacion Date is required",
|
|
});
|
|
}
|
|
// let reimburse_Date = localToUtc(data.reimburseDate);
|
|
// if (transactionDate > reimburse_Date) {
|
|
// ctx.addIssue({
|
|
// code: z.ZodIssueCode.custom,
|
|
// path: ["reimburseDate"],
|
|
// message:
|
|
// "Reimburse Date must be greater than or equal to Expense created Date",
|
|
// });
|
|
// }
|
|
if (!data.paidById) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
path: ["paidById"],
|
|
message: "Paid By is required",
|
|
});
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
export const defaultActionValues = {
|
|
comment: "",
|
|
statusId: "",
|
|
|
|
paidTransactionId: null,
|
|
paidAt: null,
|
|
paidById: null,
|
|
}; |