2025-09-16 17:29:37 +00:00
|
|
|
import { useDataSource } from "@erp/core/hooks";
|
2025-09-23 16:38:20 +00:00
|
|
|
import { ValidationErrorCollection } from "@repo/rdx-ddd";
|
2025-09-16 17:29:37 +00:00
|
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
2025-09-23 16:38:20 +00:00
|
|
|
import { UpdateCustomerByIdRequestDTO, UpdateCustomerByIdRequestSchema } from "../../common";
|
2025-09-23 17:21:16 +00:00
|
|
|
import { CustomerFormData } from "../schemas";
|
2025-09-16 17:29:37 +00:00
|
|
|
import { CUSTOMER_QUERY_KEY } from "./use-customer-query";
|
|
|
|
|
|
|
|
|
|
export const CUSTOMERS_LIST_KEY = ["customers"] as const;
|
|
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
type UpdateCustomerPayload = {
|
|
|
|
|
id: string;
|
2025-09-23 17:21:16 +00:00
|
|
|
data: CustomerFormData;
|
2025-09-17 17:37:41 +00:00
|
|
|
};
|
2025-09-16 17:29:37 +00:00
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
export function useUpdateCustomerMutation() {
|
2025-09-16 17:29:37 +00:00
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const dataSource = useDataSource();
|
2025-09-23 16:38:20 +00:00
|
|
|
const schema = UpdateCustomerByIdRequestSchema;
|
2025-09-16 17:29:37 +00:00
|
|
|
|
2025-09-23 16:38:20 +00:00
|
|
|
return useMutation<UpdateCustomerByIdRequestDTO, Error, UpdateCustomerPayload>({
|
2025-09-17 17:37:41 +00:00
|
|
|
mutationKey: ["customer:update"], //, customerId],
|
|
|
|
|
|
|
|
|
|
mutationFn: async (payload) => {
|
|
|
|
|
const { id: customerId, data } = payload;
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
throw new Error("customerId is required");
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 16:38:20 +00:00
|
|
|
const result = schema.safeParse(data);
|
|
|
|
|
if (!result.success) {
|
|
|
|
|
// Construye errores detallados
|
|
|
|
|
const validationErrors = result.error.issues.map((err) => ({
|
|
|
|
|
field: err.path.join("."),
|
|
|
|
|
message: err.message,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
throw new ValidationErrorCollection("Validation failed", validationErrors);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
const updated = await dataSource.updateOne("customers", customerId, data);
|
2025-09-23 16:38:20 +00:00
|
|
|
return updated as UpdateCustomerByIdRequestDTO;
|
2025-09-16 17:29:37 +00:00
|
|
|
},
|
2025-09-17 17:37:41 +00:00
|
|
|
onSuccess: (updated, variables) => {
|
|
|
|
|
const { id: customerId } = variables;
|
|
|
|
|
|
2025-09-16 17:29:37 +00:00
|
|
|
// Refresca inmediatamente el detalle
|
2025-09-23 16:38:20 +00:00
|
|
|
queryClient.setQueryData<UpdateCustomerByIdRequestDTO>(
|
|
|
|
|
CUSTOMER_QUERY_KEY(customerId),
|
|
|
|
|
updated
|
|
|
|
|
);
|
2025-09-16 17:29:37 +00:00
|
|
|
|
|
|
|
|
// Otra opción es invalidar el detalle para forzar refetch:
|
|
|
|
|
// queryClient.invalidateQueries({ queryKey: CUSTOMER_QUERY_KEY(customerId) });
|
|
|
|
|
|
|
|
|
|
// Invalida el listado para refrescar desde servidor
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: CUSTOMERS_LIST_KEY });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|