前一回,根據入庫需求,實作了 POST /api/inventory/scan
接著來做出庫需要的 API
POST /api/inventory/remove
{
"barcode": "471xxxxxxx"
}
barcode
↓
Product Lookup
↓
find earliest-expiring InventoryItem
↓
create REMOVE event
↓
update InventoryItem
{
"status": "ok",
"product": {
"name": "光泉鮮乳"
},
"inventory_item": {
"id": 101,
"expiration_date": "2026-10-01"
},
"event": "REMOVE"
}
def remove_by_barcode(barcode: str):
product = product_repository.find_by_barcode(barcode)
if product is None:
raise ProductNotFoundError()
item = inventory_repository.find_next_for_removal(
product_id=product["id"]
)
if item is None:
raise InventoryNotFoundError()
inventory_event_repository.create(
inventory_item_id=item["id"],
event_type="REMOVE"
)
inventory_repository.update_status(
item["id"],
"REMOVED"
)
return item