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

81 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-09-17 17:37:41 +00:00
import { useDataSource } from "@erp/core/hooks";
2025-09-23 11:48:19 +00:00
import { UniqueID, ValidationErrorCollection } from "@repo/rdx-ddd";
2025-09-24 10:34:04 +00:00
import { DefaultError, useMutation, useQueryClient } from "@tanstack/react-query";
2025-10-20 18:40:28 +00:00
import { ZodError } from "zod/v4";
2025-09-24 10:34:04 +00:00
import { CreateCustomerRequestSchema } from "../../common";
2025-09-30 15:58:04 +00:00
import { Customer, CustomerFormData } from "../schemas";
2025-10-20 18:40:28 +00:00
import { CUSTOMERS_LIST_KEY, invalidateCustomerListCache } from "./use-customer-list-query";
2025-10-23 17:29:52 +00:00
import { setCustomerDetailCache } from "./use-customer-query";
2025-10-20 18:40:28 +00:00
export const CUSTOMER_CREATE_KEY = ["customers", "create"] as const;
2025-08-11 17:49:52 +00:00
2025-09-22 17:43:55 +00:00
type CreateCustomerPayload = {
2025-09-23 17:21:16 +00:00
data: CustomerFormData;
2025-09-22 17:43:55 +00:00
};
2025-10-20 18:40:28 +00:00
// Helpers de validación a errores de dominio
export function toValidationErrors(error: ZodError<unknown>) {
return error.issues.map((err) => ({
field: err.path.join("."),
message: err.message,
}));
}
2025-09-24 17:30:35 +00:00
export function useCreateCustomer() {
2025-08-11 17:49:52 +00:00
const queryClient = useQueryClient();
const dataSource = useDataSource();
2025-09-23 11:48:19 +00:00
const schema = CreateCustomerRequestSchema;
2025-08-11 17:49:52 +00:00
2025-09-30 15:58:04 +00:00
return useMutation<Customer, DefaultError, CreateCustomerPayload>({
2025-10-20 18:40:28 +00:00
mutationKey: CUSTOMER_CREATE_KEY,
2025-09-22 17:43:55 +00:00
2025-10-21 18:05:12 +00:00
mutationFn: async ({ data }, context) => {
2025-10-20 18:40:28 +00:00
const id = UniqueID.generateNewID().toString();
const payload = { ...data, id };
2025-09-22 17:43:55 +00:00
2025-10-20 18:40:28 +00:00
const result = schema.safeParse(payload);
2025-09-23 11:48:19 +00:00
2025-10-20 18:40:28 +00:00
if (!result.success) {
2025-10-23 17:29:52 +00:00
throw new ValidationErrorCollection("Validation failed", toValidationErrors(result.error));
2025-09-23 11:48:19 +00:00
}
2025-10-23 17:29:52 +00:00
2025-10-20 18:40:28 +00:00
const created = await dataSource.createOne("customers", payload);
2025-09-30 15:58:04 +00:00
return created as Customer;
2025-08-11 17:49:52 +00:00
},
2025-09-22 17:43:55 +00:00
2025-10-23 17:29:52 +00:00
onSuccess: (created: Customer, variables) => {
const { id: customerId } = created;
2025-09-22 17:43:55 +00:00
// Invalida el listado para refrescar desde servidor
2025-10-20 18:40:28 +00:00
invalidateCustomerListCache(queryClient);
// Sincroniza detalle
2025-10-23 17:29:52 +00:00
setCustomerDetailCache(queryClient, customerId, created);
2025-10-20 18:40:28 +00:00
},
onSettled: () => {
// Refresca todos los listados
invalidateCustomerListCache(queryClient);
},
2025-10-23 17:29:52 +00:00
onMutate: async ({ data }, context) => {
2025-10-20 18:40:28 +00:00
// Cancelar queries del listado para evitar overwrite
await queryClient.cancelQueries({ queryKey: [CUSTOMERS_LIST_KEY] });
const optimisticId = UniqueID.generateNewID().toString();
const optimisticCustomer: Customer = { ...data, id: optimisticId } as Customer;
// Snapshot previo
const previous = queryClient.getQueryData<Customer[]>([CUSTOMERS_LIST_KEY]);
// Optimista: prepend
queryClient.setQueryData<Customer[]>([CUSTOMERS_LIST_KEY], (old) => [
optimisticCustomer,
...(old ?? []),
]);
return { previous, optimisticId };
2025-08-11 17:49:52 +00:00
},
});
2025-09-17 17:37:41 +00:00
}