Uecko_ERP/modules/customers/src/web/hooks/use-create-customer-mutation.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-09-17 17:37:41 +00:00
import { useDataSource } from "@erp/core/hooks";
2025-09-22 17:43:55 +00:00
import { UniqueID } from "@repo/rdx-ddd";
2025-08-11 17:49:52 +00:00
import { useMutation, useQueryClient } from "@tanstack/react-query";
2025-09-17 17:37:41 +00:00
import { CustomerCreateData, CustomerData } from "../schemas";
import { CUSTOMERS_LIST_KEY } from "./use-update-customer-mutation";
2025-08-11 17:49:52 +00:00
2025-09-22 17:43:55 +00:00
type CreateCustomerPayload = {
data: CustomerCreateData;
};
2025-09-17 17:37:41 +00:00
export function useCreateCustomerMutation() {
2025-08-11 17:49:52 +00:00
const queryClient = useQueryClient();
const dataSource = useDataSource();
2025-09-22 17:43:55 +00:00
return useMutation<CustomerData, Error, CreateCustomerPayload>({
2025-09-16 17:29:37 +00:00
mutationKey: ["customer:create"],
2025-09-22 17:43:55 +00:00
mutationFn: async (payload) => {
const { data } = payload;
const customerId = UniqueID.generateNewID();
const created = await dataSource.createOne("customers", {
...data,
id: customerId.toString(),
});
2025-09-17 17:37:41 +00:00
return created as CustomerData;
2025-08-11 17:49:52 +00:00
},
2025-09-22 17:43:55 +00:00
2025-08-11 17:49:52 +00:00
onSuccess: () => {
2025-09-22 17:43:55 +00:00
// Invalida el listado para refrescar desde servidor
2025-09-17 17:37:41 +00:00
queryClient.invalidateQueries({ queryKey: CUSTOMERS_LIST_KEY });
2025-08-11 17:49:52 +00:00
},
});
2025-09-17 17:37:41 +00:00
}