Repaso a proformas

This commit is contained in:
David Arranz 2026-04-05 14:22:36 +02:00
parent 56584d2bfd
commit 898c4a2958
13 changed files with 472 additions and 1 deletions

View File

@ -6,6 +6,7 @@
"ms-vscode.vscode-json",
"formulahendry.auto-rename-tag",
"cweijan.dbclient-jdbc",
"pkief.material-icon-theme"
"pkief.material-icon-theme",
"fralle.copy-code-context"
]
}

View File

@ -0,0 +1,3 @@
export * from "./get-proforma-by-id.adapter";
export * from "./list-proformas.adapter";
export * from "./proforma-to-list-row-patch.adapter";

View File

@ -0,0 +1,5 @@
export * from "./use-proforma-create-mutation";
export * from "./use-proforma-delete-mutation";
export * from "./use-proforma-get-query";
export * from "./use-proforma-update-mutation";
export * from "./use-proformas-list-query";

View File

@ -0,0 +1,46 @@
import type { QueryKey } from "@tanstack/react-query";
import type { ProformasListRequestDTO } from "../../../../common";
/**
* Prefijo base para listados
*/
export const LIST_PROFORMAS_QUERY_KEY_PREFIX = ["proformas"] as const;
/**
* Query key para listado de proformas
*/
export const LIST_PROFORMAS_QUERY_KEY = (criteria?: ProformasListRequestDTO): QueryKey =>
[
...LIST_PROFORMAS_QUERY_KEY_PREFIX,
{
pageNumber: criteria?.pageNumber ?? 1,
pageSize: criteria?.pageSize ?? 10,
q: criteria?.q ?? "",
filters: criteria?.filters ?? [],
orderBy: criteria?.orderBy ?? "",
order: criteria?.order ?? "",
},
] as const;
/**
* Query key para detalle de proforma
*/
export const PROFORMAS_DETAIL_QUERY_KEY_PREFIX = ["proformas:detail"] as const;
export const PROFORMA_QUERY_KEY = (proformaId?: string): QueryKey => [
...PROFORMAS_DETAIL_QUERY_KEY_PREFIX,
{ proformaId },
];
/**
* Keys para mutaciones
*/
export const CREATE_PROFORMA_MUTATION_KEY = ["proformas:create"] as const;
export const UPDATE_PROFORMA_MUTATION_KEY = ["proformas:update"] as const;
export const DELETE_PROFORMA_MUTATION_KEY = ["proformas:delete"] as const;
/**
* Operaciones de dominio
*/
export const CHANGE_STATUS_PROFORMA_MUTATION_KEY = ["proformas:change-status"] as const;
export const ISSUE_PROFORMA_MUTATION_KEY = ["proformas:issue"] as const;

View File

@ -0,0 +1,169 @@
import type { QueryClient, QueryKey } from "@tanstack/react-query";
import { ProformaToListRowPatchAdapter } from "../adapters";
import type { Proforma, ProformaList, ProformaListRow } from "../entities";
import { LIST_PROFORMAS_QUERY_KEY_PREFIX, PROFORMA_QUERY_KEY } from "./keys";
export interface ProformaListCacheSnapshot {
key: QueryKey;
page?: ProformaList;
}
export interface DeleteProformaCacheContext {
snapshots: ProformaListCacheSnapshot[];
}
// Primitivas
export function cancelProformaListQueries(queryClient: QueryClient) {
return queryClient.cancelQueries({
queryKey: LIST_PROFORMAS_QUERY_KEY_PREFIX,
});
}
export function invalidateProformaListQueries(queryClient: QueryClient) {
return queryClient.invalidateQueries({
queryKey: LIST_PROFORMAS_QUERY_KEY_PREFIX,
});
}
export function invalidateProformaDetailQuery(queryClient: QueryClient, proformaId: string) {
return queryClient.invalidateQueries({
queryKey: PROFORMA_QUERY_KEY(proformaId),
exact: true,
});
}
export function setProformaDetailCache(
queryClient: QueryClient,
proformaId: string,
proforma: Proforma
) {
queryClient.setQueryData<Proforma>(PROFORMA_QUERY_KEY(proformaId), proforma);
}
export function removeProformaDetailCache(queryClient: QueryClient, proformaId: string) {
queryClient.removeQueries({
queryKey: PROFORMA_QUERY_KEY(proformaId),
exact: true,
});
}
export function getAllProformaListQueryKeys(queryClient: QueryClient): QueryKey[] {
const entries = queryClient.getQueriesData<ProformaList>({
queryKey: LIST_PROFORMAS_QUERY_KEY_PREFIX,
});
return entries.map(([key]) => key);
}
// Upsert de filas en cache
export function upsertProformaInListCaches(
queryClient: QueryClient,
proforma: Pick<ProformaListRow, "id"> & Partial<ProformaListRow>
) {
const keys = getAllProformaListQueryKeys(queryClient);
for (const key of keys) {
const page = queryClient.getQueryData<ProformaList>(key);
if (!page) continue;
const index = page.items.findIndex((row) => row.id === proforma.id);
if (index === -1) continue;
const nextItems = page.items.slice();
nextItems[index] = {
...page.items[index],
...proforma,
};
queryClient.setQueryData<ProformaList>(key, {
...page,
items: nextItems,
});
}
}
// Remove optimista de listados
export function removeProformaFromListCaches(
queryClient: QueryClient,
proformaId: string
): ProformaListCacheSnapshot[] {
const snapshots = getAllProformaListQueryKeys(queryClient).map((key) => ({
key,
page: queryClient.getQueryData<ProformaList>(key),
}));
for (const { key, page } of snapshots) {
if (!page) continue;
queryClient.setQueryData<ProformaList>(key, {
...page,
items: page.items.filter((row) => row.id !== proformaId),
total_items: Math.max(0, page.total_items - 1),
});
}
return snapshots;
}
// Restore
export function restoreProformaListCaches(
queryClient: QueryClient,
snapshots: ProformaListCacheSnapshot[]
) {
for (const snapshot of snapshots) {
queryClient.setQueryData(snapshot.key, snapshot.page);
}
}
// Estrategias compuestas
export function syncCreatedProformaCaches(queryClient: QueryClient, proforma: Proforma) {
setProformaDetailCache(queryClient, proforma.id, proforma);
return invalidateProformaListQueries(queryClient);
}
export function syncUpdatedProformaCaches(queryClient: QueryClient, proforma: Proforma) {
setProformaDetailCache(queryClient, proforma.id, proforma);
upsertProformaInListCaches(queryClient, ProformaToListRowPatchAdapter.fromProforma(proforma));
return invalidateProformaListQueries(queryClient);
}
export async function prepareDeleteProformaOptimisticUpdate(
queryClient: QueryClient,
proformaId: string
): Promise<DeleteProformaCacheContext> {
await cancelProformaListQueries(queryClient);
const snapshots = removeProformaFromListCaches(queryClient, proformaId);
return { snapshots };
}
export function rollbackDeleteProformaOptimisticUpdate(
queryClient: QueryClient,
context?: DeleteProformaCacheContext
) {
if (!context) return;
restoreProformaListCaches(queryClient, context.snapshots);
}
export function finalizeDeletedProformaCaches(queryClient: QueryClient, proformaId: string) {
removeProformaDetailCache(queryClient, proformaId);
return invalidateProformaListQueries(queryClient);
}
// Cambio de estado e issue
export function syncChangedProformaStatusCaches(queryClient: QueryClient, proforma: Proforma) {
return syncUpdatedProformaCaches(queryClient, proforma);
}
export function syncIssuedProformaCaches(queryClient: QueryClient, proforma: Proforma) {
return syncUpdatedProformaCaches(queryClient, proforma);
}

View File

@ -0,0 +1,15 @@
import type { ZodError } from "zod";
/**
* Convierte un error de validación de Zod en una colección de errores de validación personalizada.
*
* @param error
* @returns array de objetos con el campo y el mensaje de error correspondiente.
*/
export function toValidationErrors(error: ZodError<unknown>) {
return error.issues.map((err) => ({
field: err.path.join("."),
message: err.message,
}));
}

View File

@ -0,0 +1,41 @@
import { useDataSource } from "@erp/core/hooks";
import { ValidationErrorCollection } from "@repo/rdx-ddd";
import { type DefaultError, useMutation, useQueryClient } from "@tanstack/react-query";
import { ChangeStatusProformaByIdRequestSchema } from "../../../../common";
import { type ChangeProformaStatusByIdParams, changeProformaStatusById } from "../api";
import type { Proforma } from "../entities";
import { CHANGE_STATUS_PROFORMA_MUTATION_KEY } from "./keys";
import { syncChangedProformaStatusCaches } from "./proforma-cache-strategy";
import { toValidationErrors } from "./to-validation-errors";
type ChangeProformaStatusContext = {};
export const useProformaChangeStatusMutation = () => {
const queryClient = useQueryClient();
const dataSource = useDataSource();
const schema = ChangeStatusProformaByIdRequestSchema;
useMutation<Proforma, DefaultError, ChangeProformaStatusByIdParams, ChangeProformaStatusContext>({
mutationKey: CHANGE_STATUS_PROFORMA_MUTATION_KEY,
mutationFn: async (params) => {
const { id: proformaId, data } = params;
if (!proformaId) {
throw new Error("proformaId is required");
}
const result = schema.safeParse(data);
if (!result.success) {
throw new ValidationErrorCollection("Validation failed", toValidationErrors(result.error));
}
const dto = await changeProformaStatusById(dataSource, params);
return ChangeStatusProformaByIdAdapter.fromDto(dto);
},
onSuccess: async (proforma) => {
await syncChangedProformaStatusCaches(queryClient, proforma);
},
});
};

View File

@ -0,0 +1,46 @@
import { useDataSource } from "@erp/core/hooks";
import { type DefaultError, useMutation, useQueryClient } from "@tanstack/react-query";
import type { DeleteProformaByIdParams } from "../api";
import { deleteProformaById } from "../api";
import { DELETE_PROFORMA_MUTATION_KEY } from "./keys";
import {
type DeleteProformaCacheContext,
finalizeDeletedProformaCaches,
prepareDeleteProformaOptimisticUpdate,
rollbackDeleteProformaOptimisticUpdate,
} from "./proforma-cache-strategy";
interface DeleteProformaContext extends DeleteProformaCacheContext {}
export const useProformaDeleteMutation = () => {
const queryClient = useQueryClient();
const dataSource = useDataSource();
return useMutation<{ id: string }, DefaultError, DeleteProformaByIdParams, DeleteProformaContext>(
{
mutationKey: DELETE_PROFORMA_MUTATION_KEY,
mutationFn: async ({ id, signal }) => {
if (!id) {
throw new Error("customerId is required");
}
await deleteProformaById(dataSource, { id, signal });
return { id };
},
onMutate: async ({ id }) => {
return prepareDeleteProformaOptimisticUpdate(queryClient, id);
},
onError: (_error, _params, context) => {
rollbackDeleteProformaOptimisticUpdate(queryClient, context);
},
onSuccess: ({ id }) => {
finalizeDeletedProformaCaches(queryClient, id);
},
onSettled: async (_data, _error, { id }) => {
await finalizeDeletedProformaCaches(queryClient, id);
},
}
);
};

View File

@ -0,0 +1,35 @@
import { useDataSource } from "@erp/core/hooks";
import { type UseQueryResult, useQuery } from "@tanstack/react-query";
import { GetProformaByIdAdapter } from "../adapters";
import { getProformaById } from "../api";
import type { Proforma } from "../entities";
import { PROFORMA_QUERY_KEY } from "./keys";
export interface UseProformaGetQueryOptions {
id?: string;
enabled?: boolean;
}
export const useProformaGetQuery = (
options: UseProformaGetQueryOptions
): UseQueryResult<Proforma, Error> => {
const dataSource = useDataSource();
const id = options?.id;
const enabled = options?.enabled ?? Boolean(id);
return useQuery({
queryKey: PROFORMA_QUERY_KEY(id),
queryFn: async ({ signal }) => {
if (!id) throw new Error("proformaId is required");
const dto = await getProformaById(dataSource, {
id: id!,
signal,
});
return GetProformaByIdAdapter.fromDto(dto);
},
enabled: enabled && Boolean(id),
});
};

View File

@ -0,0 +1,29 @@
import { useDataSource } from "@erp/core/hooks";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { ChangeStatusProformaByIdRequestSchema } from "../../../../common";
import { type IssueProformaByIdParams, issueProformaById } from "../api";
import type { Proforma } from "../entities";
import { ISSUE_PROFORMA_MUTATION_KEY } from "./keys";
import { syncIssuedProformaCaches } from "./proforma-cache-strategy";
type IssueProformaByIdContext = {};
export const useProformaChangeStatusMutation = () => {
const queryClient = useQueryClient();
const dataSource = useDataSource();
const schema = ChangeStatusProformaByIdRequestSchema;
useMutation<Proforma, Error, IssueProformaByIdParams, IssueProformaByIdContext>({
mutationKey: ISSUE_PROFORMA_MUTATION_KEY,
mutationFn: async (params) => {
const dto = await issueProformaById(dataSource, params);
return IssueProformaByIdAdapter.fromDto(dto);
},
onSuccess: async (proforma) => {
await syncIssuedProformaCaches(queryClient, proforma);
},
});
};

View File

@ -0,0 +1,46 @@
import { useDataSource } from "@erp/core/hooks";
import { ValidationErrorCollection } from "@repo/rdx-ddd";
import { type DefaultError, useMutation, useQueryClient } from "@tanstack/react-query";
import { UpdateProformaByIdRequestSchema } from "../../../../common";
import { GetProformaByIdAdapter } from "../adapters";
import type { UpdateProformaByIdParams, UpdateProformaByIdResult } from "../api";
import { updateProformaById } from "../api";
import type { Proforma } from "../entities";
import { UPDATE_PROFORMA_MUTATION_KEY } from "./keys";
import { syncUpdatedProformaCaches } from "./proforma-cache-strategy";
import { toValidationErrors } from "./to-validation-errors";
type UpdateProformaContext = {};
export const useProformaUpdateMutation = () => {
const queryClient = useQueryClient();
const dataSource = useDataSource();
const schema = UpdateProformaByIdRequestSchema;
return useMutation<Proforma, DefaultError, UpdateProformaByIdParams, UpdateProformaContext>({
mutationKey: UPDATE_PROFORMA_MUTATION_KEY,
mutationFn: async (params) => {
const { id: proformaId, data } = params;
if (!proformaId) {
throw new Error("proformaId is required");
}
const result = schema.safeParse(data);
if (!result.success) {
throw new ValidationErrorCollection("Validation failed", toValidationErrors(result.error));
}
const dto: UpdateProformaByIdResult = await updateProformaById(
dataSource,
params as UpdateProformaByIdParams
);
return GetProformaByIdAdapter.fromDto(dto);
},
onSuccess: async (proforma) => {
await syncUpdatedProformaCaches(queryClient, proforma);
},
});
};

View File

@ -0,0 +1,35 @@
import type { CriteriaDTO } from "@erp/core";
import { useDataSource } from "@erp/core/hooks";
import { type DefaultError, type UseQueryResult, useQuery } from "@tanstack/react-query";
import { ListProformasAdapter } from "../adapters";
import { type ListProformasResult, getListProformasByCriteria } from "../api";
import type { ProformaList } from "../entities";
import { LIST_PROFORMAS_QUERY_KEY } from "./keys";
export interface ProformasListQueryOptions {
criteria?: Partial<CriteriaDTO>;
enabled?: boolean;
}
export const useProformasListQuery = (
options?: ProformasListQueryOptions
): UseQueryResult<ProformaList, DefaultError> => {
const dataSource = useDataSource();
const enabled = options?.enabled ?? true;
const criteria = options?.criteria ?? {};
return useQuery<ProformaList, DefaultError>({
queryKey: LIST_PROFORMAS_QUERY_KEY(criteria),
queryFn: async ({ signal }) => {
const dto: ListProformasResult = await getListProformasByCriteria(dataSource, {
criteria,
signal,
});
return ListProformasAdapter.fromDto(dto);
},
enabled,
placeholderData: (previousData) => previousData, // Mantiene la página anterior durante refetch por cambio de criteria
});
};