feat: add asset ID history API

This commit is contained in:
lussierj committed 2026-07-28 19:48:57 +00:00
1 parent a87ab2709f
commit d738b8e27e
6 files changed
+254 -9

No files matched your search

+14
View File
@@ -43,6 +43,14 @@ class InventoryMinerDetail(InventoryMinerSummary):
first_seen_at: datetime | None = None
class InventoryAssetIDHistoryEntry(BaseModel):
old_asset_id: str | None = None
new_asset_id: str
changed_at: datetime
changed_by: UUID
changed_by_display_name: str
class InventoryCreateRequest(BaseModel):
asset_id: str = Field(min_length=1, max_length=120)
current_mac: str | None = Field(default=None, max_length=64)
@@ -112,3 +120,9 @@ class InventoryMinerDetailResponse(BaseModel):
data: InventoryMinerDetail
meta: dict[str, object] = Field(default_factory=dict)
error: None = None
class InventoryAssetIDHistoryResponse(BaseModel):
data: list[InventoryAssetIDHistoryEntry]
meta: dict[str, object] = Field(default_factory=dict)
error: None = None
+13 -9
View File
@@ -347,15 +347,19 @@ class InventoryRepository:
cur.execute(
"""
SELECT
id,
miner_id,
old_asset_id,
new_asset_id,
changed_at,
changed_by
FROM miner_asset_id_history
WHERE miner_id = %s
ORDER BY changed_at, id
history.id,
history.miner_id,
history.old_asset_id,
history.new_asset_id,
history.changed_at,
history.changed_by,
changed_by_user.display_name
AS changed_by_display_name
FROM miner_asset_id_history AS history
JOIN users AS changed_by_user
ON changed_by_user.id = history.changed_by
WHERE history.miner_id = %s
ORDER BY history.changed_at, history.id
""",
(miner_id,),
)
+22
View File
@@ -13,6 +13,7 @@ from app.inventory.exceptions import (
MinerNotFound,
)
from app.inventory.models import (
InventoryAssetIDHistoryResponse,
InventoryCreateRequest,
InventoryListMeta,
InventoryMetadataUpdateRequest,
@@ -120,6 +121,27 @@ def list_inventory_miners(
)
@router.get(
"/{miner_id}/asset-id-history",
response_model=InventoryAssetIDHistoryResponse,
)
def get_inventory_miner_asset_id_history(
miner_id: UUID,
service: InventoryServiceDependency,
current_user: InventoryReadUser,
) -> InventoryAssetIDHistoryResponse:
del current_user
try:
history = service.get_asset_id_history(miner_id)
except MinerNotFound as exc:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Miner not found",
) from exc
return InventoryAssetIDHistoryResponse(data=history)
@router.get("/{miner_id}", response_model=InventoryMinerDetailResponse)
def get_inventory_miner(
miner_id: UUID,
+12
View File
@@ -12,6 +12,7 @@ from app.inventory.exceptions import (
MinerNotFound,
)
from app.inventory.models import (
InventoryAssetIDHistoryEntry,
InventoryCreateRequest,
InventoryMetadataUpdateRequest,
InventoryMinerDetail,
@@ -112,6 +113,17 @@ class InventoryService:
raise MinerNotFound(miner_id)
return self._detail_model(row)
def get_asset_id_history(
self,
miner_id: UUID,
) -> list[InventoryAssetIDHistoryEntry]:
if not self.repository.get_miner_by_id(miner_id):
raise MinerNotFound(miner_id)
return [
InventoryAssetIDHistoryEntry.model_validate(row)
for row in self.repository.list_asset_id_history(miner_id)
]
def list_miners(
self,
*,