mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 05:18:11 +00:00
242 lines
8.5 KiB
TypeScript
242 lines
8.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
|
|
import { warehouseService } from '@/services/warehouse.service';
|
|
import type {
|
|
InventoryFilter,
|
|
InventoryInquiryFilter,
|
|
MoveInventoryPayload,
|
|
ReceiveInventoryPayload,
|
|
ReserveInventoryPayload,
|
|
SaveWarehousePayload,
|
|
SaveYardPayload,
|
|
SaveZonePayload,
|
|
WarehouseFilter,
|
|
} from '@/types/warehouse';
|
|
|
|
export const warehouseKeys = {
|
|
all: ['warehouses'] as const,
|
|
list: (filter?: WarehouseFilter) => ['warehouses', 'list', filter ?? {}] as const,
|
|
facilities: () => ['warehouses', 'facilities'] as const,
|
|
detail: (id: string) => ['warehouses', 'detail', id] as const,
|
|
yards: (warehouseId: string) => ['warehouses', warehouseId, 'yards'] as const,
|
|
zones: (yardId: string) => ['warehouse-yards', yardId, 'zones'] as const,
|
|
inventory: (filter?: InventoryFilter) => ['warehouse-inventory', 'list', filter ?? {}] as const,
|
|
dashboardSummary: (filter?: InventoryFilter) => ['warehouse-dashboard', 'summary', filter ?? {}] as const,
|
|
inquiry: (filter: InventoryInquiryFilter) => ['warehouse-inventory', 'inquiry', filter] as const,
|
|
};
|
|
|
|
// ── Warehouses ─────────────────────────────────────────────────────────────
|
|
|
|
export function useWarehouses(filter?: WarehouseFilter) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.list(filter),
|
|
queryFn: () => warehouseService.list(filter).then((r) => r.data),
|
|
});
|
|
}
|
|
|
|
export function useWarehouse(id?: string) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.detail(id ?? ''),
|
|
queryFn: () => warehouseService.getById(id as string).then((r) => r.data),
|
|
enabled: Boolean(id),
|
|
});
|
|
}
|
|
|
|
export function useWarehouseFacilities() {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.facilities(),
|
|
queryFn: () => warehouseService.listFacilities().then((r) => r.data),
|
|
});
|
|
}
|
|
|
|
export function useCreateWarehouse() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (payload: SaveWarehousePayload) => warehouseService.create(payload),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: warehouseKeys.all }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateWarehouse() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, payload }: { id: string; payload: Partial<SaveWarehousePayload> }) =>
|
|
warehouseService.update(id, payload),
|
|
onSuccess: (_, { id }) => {
|
|
qc.invalidateQueries({ queryKey: warehouseKeys.all });
|
|
qc.invalidateQueries({ queryKey: warehouseKeys.detail(id) });
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Yards ────────────────────────────────────────────────────────────────
|
|
|
|
export function useWarehouseYards(warehouseId?: string) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.yards(warehouseId ?? ''),
|
|
queryFn: () => warehouseService.listYards(warehouseId as string).then((r) => r.data),
|
|
enabled: Boolean(warehouseId),
|
|
});
|
|
}
|
|
|
|
export function useCreateYard() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ warehouseId, payload }: { warehouseId: string; payload: SaveYardPayload }) =>
|
|
warehouseService.createYard(warehouseId, payload),
|
|
onSuccess: (_, { warehouseId }) => {
|
|
qc.invalidateQueries({ queryKey: warehouseKeys.yards(warehouseId) });
|
|
qc.invalidateQueries({ queryKey: warehouseKeys.detail(warehouseId) });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateYard() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, payload }: { id: string; payload: Partial<SaveYardPayload> }) =>
|
|
warehouseService.updateYard(id, payload),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: warehouseKeys.all }),
|
|
});
|
|
}
|
|
|
|
// ── Zones ──────────────────────────────────────────────────────────────────
|
|
|
|
export function useWarehouseZones(yardId?: string) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.zones(yardId ?? ''),
|
|
queryFn: () => warehouseService.listZones(yardId as string).then((r) => r.data),
|
|
enabled: Boolean(yardId),
|
|
});
|
|
}
|
|
|
|
export function useCreateZone() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ yardId, payload }: { yardId: string; payload: SaveZonePayload }) =>
|
|
warehouseService.createZone(yardId, payload),
|
|
onSuccess: (_, { yardId }) => qc.invalidateQueries({ queryKey: warehouseKeys.zones(yardId) }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateZone() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, payload }: { id: string; payload: Partial<SaveZonePayload> }) =>
|
|
warehouseService.updateZone(id, payload),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['warehouse-yards'] }),
|
|
});
|
|
}
|
|
|
|
// ── Inventory ──────────────────────────────────────────────────────────────
|
|
|
|
export function useWarehouseInventory(filter?: InventoryFilter) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.inventory(filter),
|
|
queryFn: () => warehouseService.listInventory(filter).then((r) => r.data),
|
|
});
|
|
}
|
|
|
|
export function useWarehouseDashboardSummary(filter?: InventoryFilter) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.dashboardSummary(filter),
|
|
queryFn: () => warehouseService.getDashboardSummary(filter).then((r) => r.data),
|
|
});
|
|
}
|
|
|
|
export function useReceiveInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (payload: ReceiveInventoryPayload) => warehouseService.receiveInventory(payload),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: warehouseKeys.all });
|
|
qc.invalidateQueries({ queryKey: ['warehouse-dashboard'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useStoreInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => warehouseService.storeInventory(id),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: ['warehouse-dashboard'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useReserveInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, payload }: { id: string; payload: ReserveInventoryPayload }) =>
|
|
warehouseService.reserveInventory(id, payload),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: ['warehouse-dashboard'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useInspectInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => warehouseService.inspectInventory(id),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['warehouse-inventory'] }),
|
|
});
|
|
}
|
|
|
|
export function useMarkReadyForLoading() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => warehouseService.markReadyForLoading(id),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: ['warehouse-dashboard'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useLoadInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => warehouseService.loadInventory(id),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: ['warehouse-dashboard'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDispatchInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => warehouseService.dispatchInventory(id),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: ['warehouse-dashboard'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useMoveInventory() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, payload }: { id: string; payload: MoveInventoryPayload }) =>
|
|
warehouseService.moveInventory(id, payload),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['warehouse-inventory'] });
|
|
qc.invalidateQueries({ queryKey: warehouseKeys.all });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useInventoryInquiry(filter: InventoryInquiryFilter, enabled = true) {
|
|
return useQuery({
|
|
queryKey: warehouseKeys.inquiry(filter),
|
|
queryFn: () => warehouseService.inquiry(filter).then((r) => r.data),
|
|
enabled,
|
|
});
|
|
}
|