Files
ApiCore/ApiCore-Frontend/src/lib/honey-batches/api.ts
T
Nick SzaunigandClaude Sonnet 5 1054a845ef
Backend Image / build-and-push (push) Successful in 3m9s
Frontend Image / build-and-push (push) Successful in 2m45s
feat: allow a honey batch to be split across multiple storage containers
A batch that doesn't fit in one container (e.g. 125 kg across 5 x 25 kg
buckets) had no way to be represented - HoneyBatch.storageContainerId
is a single nullable FK. Adds HoneyBatchContainerAllocation as an
additive join table (container + amount per batch), independent of the
existing single-container/relocate() flow which still covers the
simple one-container case unchanged.

- GET/PUT/DELETE /honey-batches/:id/containers[/:containerId], reusing
  the existing honey-batches:relocate permission.
- Assigning a container is an upsert (@@unique on batch+container), and
  the total assigned is validated against the batch's own amount when
  known.
- New "Aufteilung auf mehrere Behälter" card on the honey batch detail
  page, alongside (not replacing) the existing storage card.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-04 19:44:33 +02:00

99 lines
2.7 KiB
TypeScript

import { apiFetch } from "@/lib/auth/api-client";
import {
CreateHoneyBatchInput,
CreateHoneyMeasurementInput,
HoneyBatch,
HoneyBatchContainerAllocation,
HoneyBatchRelocation,
HoneyBatchStatus,
RelocateHoneyBatchInput,
Traceability,
UpdateHoneyBatchInput,
} from "./types";
export interface HoneyBatchFilter {
status?: HoneyBatchStatus;
}
export function fetchHoneyBatches(filter: HoneyBatchFilter = {}): Promise<HoneyBatch[]> {
const params = new URLSearchParams();
if (filter.status) params.set("status", filter.status);
const query = params.toString();
return apiFetch<HoneyBatch[]>(`/honey-batches${query ? `?${query}` : ""}`);
}
export function fetchHoneyBatch(id: string): Promise<HoneyBatch> {
return apiFetch<HoneyBatch>(`/honey-batches/${id}`);
}
export function fetchHoneyBatchTraceability(id: string): Promise<Traceability> {
return apiFetch<Traceability>(`/honey-batches/${id}/traceability`);
}
export function createHoneyBatch(input: CreateHoneyBatchInput): Promise<HoneyBatch> {
return apiFetch<HoneyBatch>("/honey-batches", {
method: "POST",
body: JSON.stringify(input),
});
}
export function updateHoneyBatch(
id: string,
input: UpdateHoneyBatchInput,
): Promise<HoneyBatch> {
return apiFetch<HoneyBatch>(`/honey-batches/${id}`, {
method: "PATCH",
body: JSON.stringify(input),
});
}
export function addHoneyMeasurement(
id: string,
input: CreateHoneyMeasurementInput,
): Promise<HoneyBatch> {
return apiFetch<HoneyBatch>(`/honey-batches/${id}/measurements`, {
method: "POST",
body: JSON.stringify(input),
});
}
export function relocateHoneyBatch(
id: string,
input: RelocateHoneyBatchInput,
): Promise<HoneyBatch> {
return apiFetch<HoneyBatch>(`/honey-batches/${id}/relocate`, {
method: "POST",
body: JSON.stringify(input),
});
}
export function fetchHoneyBatchRelocations(id: string): Promise<HoneyBatchRelocation[]> {
return apiFetch<HoneyBatchRelocation[]>(`/honey-batches/${id}/relocations`);
}
export function fetchHoneyBatchContainerAllocations(
id: string,
): Promise<HoneyBatchContainerAllocation[]> {
return apiFetch<HoneyBatchContainerAllocation[]>(`/honey-batches/${id}/containers`);
}
export function setHoneyBatchContainerAllocation(
id: string,
containerId: string,
amount: number,
): Promise<HoneyBatchContainerAllocation> {
return apiFetch<HoneyBatchContainerAllocation>(
`/honey-batches/${id}/containers/${containerId}`,
{ method: "PUT", body: JSON.stringify({ amount }) },
);
}
export function removeHoneyBatchContainerAllocation(
id: string,
containerId: string,
): Promise<void> {
return apiFetch<void>(`/honey-batches/${id}/containers/${containerId}`, {
method: "DELETE",
});
}