74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
import type { Cheque } from "@/components/PaymentMethods/Cheque";
|
|
import type { Form } from "./forms";
|
|
import type { Product } from "./products";
|
|
import type { Shipment } from "./shipments";
|
|
|
|
export type Contract = {
|
|
id: number;
|
|
form_id: number;
|
|
products: ContractProduct;
|
|
form: Form;
|
|
firstname: string;
|
|
lastname: string;
|
|
email: string;
|
|
phone: string;
|
|
payment_method: string;
|
|
cheque_quantity: number;
|
|
total_price: number;
|
|
};
|
|
|
|
export type ContractCreate = {
|
|
form_id: number;
|
|
firstname: string;
|
|
lastname: string;
|
|
email: string;
|
|
phone: string;
|
|
payment_method: string;
|
|
cheque_quantity: number;
|
|
products: ContractProductCreate[];
|
|
cheques: Cheque[];
|
|
};
|
|
|
|
export type ContractInputs = {
|
|
firstname: string;
|
|
lastname: string;
|
|
email: string;
|
|
phone: string;
|
|
products: Record<string, string | number>;
|
|
payment_method: string;
|
|
cheques: Cheque[];
|
|
cheque_quantity: number;
|
|
};
|
|
|
|
export type ContractProduct = {
|
|
id: number;
|
|
product_id: number;
|
|
shipment_id: number;
|
|
quantity: number;
|
|
contract: Contract;
|
|
product: Product;
|
|
shipment?: Shipment | null;
|
|
};
|
|
|
|
export type ContractProductCreate = {
|
|
product_id: number;
|
|
shipment_id: number | null;
|
|
quantity: number;
|
|
};
|
|
|
|
export function tranformProducts(
|
|
products: Record<string, string | number>,
|
|
): ContractProductCreate[] {
|
|
return Object.entries(products).map(([key, value]) => {
|
|
const quantity = value;
|
|
const parts = key.split("-");
|
|
const shipment_id = parts[0] === "occasional" ? Number(parts[1]) : null;
|
|
const product_id = parts[0] === "occasional" ? Number(parts[2]) : Number(parts[1]);
|
|
return {
|
|
quantity: Number(quantity),
|
|
shipment_id: shipment_id,
|
|
product_id: product_id,
|
|
};
|
|
});
|
|
}
|