The Inventory API manages all stock-related operations in HDB Service. Stock entries are scoped to a specific plant and track both bulk consumables (e.g., filters, cartridges counted by quantity) and serialized consumables that require a unique tracking ID per unit. When stock is shared between plants in an emergency, the system automatically records an inter-plant debt that must later be resolved either manually or via a follow-up transfer. All endpoints require an authenticated session with a valid Supabase Auth cookie. Permissions are enforced through the RBAC matrix — see the permission column on each endpoint below.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/GianlucaBessone/HDB-Service/llms.txt
Use this file to discover all available pages before exploring further.
GET /api/stock
Returns a list of stock entries visible to the authenticated user. Results are automatically scoped by role:CLIENT_REQUESTER users see only entries for their assigned plants, while ADMIN and SUPERVISOR users see all entries. Each entry includes a serialNumbers array populated from active Consumable records that have a uniqueId.
Required permission: stock:read
Filter entries by plant ID.
Filter by item type. Must be
CONSUMABLE or SPARE_PART.When
true, returns only entries where cantidad < minLevel.Unique stock entry ID.
Owning client ID.
Owning plant ID.
CONSUMABLE or SPARE_PART.Unique catalog material code.
Material display name.
Current stock quantity.
Low-stock alert threshold.
Reorder target quantity.
Unit of measure (e.g.,
"unidad", "litro").Array of unique IDs of active serialized consumables for this plant + material combination.
| Status | Description |
|---|---|
401 | Not authenticated. |
403 | Insufficient permission (stock:read required). |
500 | Internal server error fetching stock entries. |
POST /api/stock
Creates a new stock entry or increments an existing one. ThematerialCode must match a record in the MaterialCatalog. If the catalog item has requiresSerial: true, a uniqueId is required and a Consumable record is created before the stock entry is upserted. For serialized materials, quantity is always incremented by 1 regardless of the cantidad value sent.
Required permission: stock:write
ID of the client that owns this stock entry.
ID of the plant where stock is held.
Code of an existing
MaterialCatalog entry. Returns 400 if not found.Quantity to add. For bulk (non-serialized) materials, this value is incremented onto the existing entry. Ignored for serialized materials (always
+1). Defaults to 0 if omitted.Low-stock alert threshold. Updates the existing entry if one already exists.
Reorder target quantity. Updates the existing entry if one already exists.
Unit of measure. Defaults to
"unidad".Serial number for the consumable unit. Required when the catalog item has
requiresSerial: true. Must be globally unique across all Consumable records.Stock entry ID.
Owning client ID.
Owning plant ID.
Derived from the catalog item’s
type field.Catalog material code.
Material name sourced from the catalog.
Updated stock quantity after upsert.
Alert threshold.
Reorder target.
Unit of measure.
To add a serialized consumable (e.g., a membrane with a serial number), include
"uniqueId": "SN-00123". The system will create a Consumable record and increment the stock entry by exactly 1.| Status | Description |
|---|---|
400 | Missing required fields (clientId, plantId, or materialCode). |
400 | materialCode not found in the material catalog. |
400 | Material requires a serial number (requiresSerial: true) but uniqueId was not provided. |
400 | The uniqueId is already registered to another consumable record. |
401 | Not authenticated. |
403 | Insufficient permission (stock:write required). |
500 | Internal server error. |
POST /api/stock/[id]/adjust
Performs an inventory adjustment on an existing stock entry. Supports two actions:ADJUST_QTY sets the quantity to a new absolute value, and DELETE removes the stock entry entirely and deactivates all associated Consumable records. A justification is always required. All adjustments are written to the audit log and push a push notification to all SUPERVISOR and ADMIN users via OneSignal.
Required permission: stock:write
ADJUST_QTY to set a new quantity, or DELETE to remove the entry entirely.Human-readable reason for the adjustment. Cannot be blank.
New absolute quantity. Required when
action is ADJUST_QTY. Must be >= 0.true on successful completion.Echo of the action performed:
STOCK_ADJUST or STOCK_DELETE.| Status | Description |
|---|---|
400 | justificacion is blank or missing. |
400 | action is not DELETE or ADJUST_QTY. |
400 | newCantidad is not a non-negative number (when using ADJUST_QTY). |
404 | Stock entry with the given id not found. |
401 | Not authenticated. |
403 | Insufficient permission (stock:write required). |
500 | Internal server error. |
GET /api/stock/available
Returns all consumables and spare parts that are currently in stock and uninstalled for a given plant. The response combines two sources: bulk stock entries (wherecantidad > 0, after subtracting out serialized units) and individual active Consumable records that have a uniqueId. This endpoint is used to populate installation selection lists in the UI.
Required permission: stock:read
Plant ID to filter available items. Pass
"all" or omit to retrieve across all plants visible to the authenticated user.BULK for non-serialized quantities; SERIALIZED for individual tracked units.Plant where the item is held.
Catalog material code.
Material display name.
Maximum installable quantity. Always
1 for SERIALIZED items.Human-readable display label for UI dropdowns.
Consumable record ID. Present on
SERIALIZED items only.Serial number. Present on
SERIALIZED items only.Months until expiration after installation. Present on
SERIALIZED items only (nullable).| Status | Description |
|---|---|
401 | Not authenticated. |
403 | Insufficient permission (stock:read required). |
500 | Internal server error. |
GET /api/stock/transfer
Returns a list of past stock transfers. Results are automatically scoped by role:CLIENT_REQUESTER users see only transfers involving their assigned plants; CLIENT_RESPONSIBLE users see transfers for their client’s plants; ADMIN and SUPERVISOR users see all transfers.
Required permission: stock:read
Further filter to transfers where this plant is either the source or the destination.
Transfer record ID.
Source plant ID.
Destination plant ID.
CONSUMABLE or SPARE_PART.Catalog material code.
Material name at time of transfer.
Units transferred.
COMPLETED, PENDING, or CANCELLED.ISO 8601 timestamp of when the transfer was initiated.
ISO 8601 timestamp of completion (nullable).
POST /api/stock/transfer
Transfers stock from one plant to another within the same client. The operation is executed inside a single database transaction: the source stock is decremented, the destination is upserted, the transfer record is created with statusCOMPLETED, and any matching PENDING inter-plant debts from the source plant to the destination plant for the same material are automatically resolved (partially or fully, in FIFO order).
Required permission: stock:transfer
ID of the plant sending the stock.
ID of the plant receiving the stock. Must be different from
fromPlantId.CONSUMABLE or SPARE_PART.Catalog material code to transfer.
Units to transfer. Must be greater than
0 and not exceed the source plant’s current stock.Override the material name on the transfer record. Falls back to the source stock entry’s
nombre.Transfer record ID.
Source plant ID.
Destination plant ID.
Item type transferred.
Material code transferred.
Material name on the record.
Units transferred.
Always
COMPLETED for transfers created via this endpoint.ISO 8601 creation timestamp.
ISO 8601 completion timestamp.
If the source plant has outstanding debts to the destination plant for the same
materialCode, those debts are resolved automatically in FIFO order as part of the same transaction. No separate debt-resolution call is needed.| Status | Description |
|---|---|
400 | Missing required fields. |
400 | cantidad is <= 0. |
400 | fromPlantId equals toPlantId. |
400 | Source stock entry does not exist or has insufficient quantity. |
404 | Destination plant not found. |
401 | Not authenticated. |
403 | Insufficient permission (stock:transfer required). |
500 | Internal server error (transaction rolled back). |
GET /api/stock/debts
Returns inter-plant debt records. A debt is created when one plant uses consumables that belong to another plant (e.g., during an urgent repair). Results are scoped by role in the same way as the transfers endpoint. Required permission:stock:read
Filter to debts where this plant is either the creditor or the debtor.
Filter by debt status. One of
PENDING (default) or RESOLVED.Debt record ID.
ID of the plant that is owed stock.
ID of the plant that owes stock.
Catalog material code.
Material name.
Units owed.
Linked
Consumable record ID (nullable).PENDING or RESOLVED.ISO 8601 creation timestamp.
ISO 8601 resolution timestamp (nullable).
| Status | Description |
|---|---|
401 | Not authenticated. |
403 | Insufficient permission (stock:read required). |
500 | Internal server error. |
POST /api/stock/debts/[id]/resolve
Marks an inter-plant debt asRESOLVED and records the resolution timestamp. This endpoint accepts both POST and PATCH methods. Note that if the debt was created as part of a transfer, it may already have been resolved automatically — calling this endpoint on an already-resolved debt returns a 400 error. The action is written to the audit log.
Required permission: stock:write
This endpoint accepts both
POST and PATCH with identical behavior. No request body is required.Debt record ID.
Creditor plant ID.
Debtor plant ID.
Material code.
Material name.
Units that were owed.
Always
RESOLVED on a successful response.ISO 8601 resolution timestamp.
| Status | Description |
|---|---|
400 | Debt is already in RESOLVED status. |
404 | Debt record not found. |
401 | Not authenticated. |
403 | Insufficient permission (stock:write required). |
500 | Internal server error. |
GET /api/catalog
Returns active material catalog entries. The catalog is the source of truth for all material codes used in stock entries and transfers. Each entry defines whether a material requires serial tracking (requiresSerial) and optionally how many months after installation it expires (expirationMonths).
Required permission: stock:read
Filter catalog entries to a specific client. Returns all active entries when omitted.
Catalog entry ID.
Unique material code (used as the key for stock entries).
Material display name.
Optional description (nullable).
CONSUMABLE or SPARE_PART.Whether each unit must have a unique serial number.
Months until expiration after installation (nullable).
Whether this catalog entry is active.
Owning client ID (nullable for shared catalog items).
| Status | Description |
|---|---|
401 | Not authenticated. |
403 | Insufficient permission (stock:read required). |
500 | Internal server error. |