From 130f34f937cc290df47e405796ae96f1ec9a55aa Mon Sep 17 00:00:00 2001 From: William Wills Date: Mon, 14 Oct 2024 11:16:48 -0400 Subject: [PATCH] feat: revised by-cw-unit-id endpoint to select by warehouseUnitId, activity mode, and asset id feat: renamed /activities/by-cw-unit-id to /activities/activity-record --- app/api/v1/activities.py | 21 +++++++++++++++------ app/schemas/__init__.py | 2 +- app/schemas/activity.py | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/api/v1/activities.py b/app/api/v1/activities.py index 1f42a83..e3d821c 100644 --- a/app/api/v1/activities.py +++ b/app/api/v1/activities.py @@ -124,12 +124,14 @@ async def get_activity( return schemas.ActivitiesResponse(activities=activities_with_cw, total=total) -@router.get("/by-cw-unit-id", response_model=schemas.ActivityByCwUnitIdResponse) +@router.get("/activity-record", response_model=schemas.ActivityRecordResponse) @disallow([ExecutionMode.REGISTRY, ExecutionMode.CLIENT]) async def get_activity_by_cw_unit_id( cw_unit_id: str, + asset_token_id: str, + action_mode: str, db: Session = Depends(deps.get_db_session), -) -> schemas.ActivityByCwUnitIdResponse: +) -> schemas.ActivityRecordResponse: """Get a single activity based on the unit's unitWarehouseId. This endpoint is to be called by the explorer. @@ -146,7 +148,7 @@ async def get_activity_by_cw_unit_id( ).combine_climate_units_and_metadata(search=cw_filters) if len(climate_data) == 0: logger.warning(f"Failed to retrieve unit from climate warehouse. search:{cw_filters}") - return schemas.ActivityByCwUnitIdResponse() + return schemas.ActivityRecordResponse() units = {unit["marketplaceIdentifier"]: unit for unit in climate_data} if len(units) != 0: @@ -166,9 +168,16 @@ async def get_activity_by_cw_unit_id( ) if len(activities) == 0: logger.warning(f"No data to get from activities. filters:{activity_filters} page:{page} limit:{limit}") - return schemas.ActivityByCwUnitIdResponse() + return schemas.ActivityRecordResponse() + + try: + activity = next((activity for activity in activities if activity.asset_id == asset_token_id and activity.mode == action_mode), None) + if activity is None: + return schemas.ActivityRecordResponse() + except: + logger.warning(f"an exception occurred while processing activity record") + return schemas.ActivityRecordResponse() - activity = activities[0] unit = units.get(activity.asset_id) if unit is None: logger.warning(f"Activity asset ID does not match cw_unit ID. requested cw_unit_id (warehouseUnitId): {cw_unit_id}") @@ -193,4 +202,4 @@ async def get_activity_by_cw_unit_id( **jsonable_encoder(activity), ) - return schemas.ActivityByCwUnitIdResponse(activity=activity_with_cw) + return schemas.ActivityRecordResponse(activity=activity_with_cw) diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py index 6ff7174..90e48ea 100644 --- a/app/schemas/__init__.py +++ b/app/schemas/__init__.py @@ -6,7 +6,7 @@ ActivityBase, ActivitySearchBy, ActivityWithCW, - ActivityByCwUnitIdResponse + ActivityRecordResponse ) from app.schemas.key import Key # noqa: F401 from app.schemas.metadata import ( # noqa: F401 diff --git a/app/schemas/activity.py b/app/schemas/activity.py index 72e204a..894d45c 100644 --- a/app/schemas/activity.py +++ b/app/schemas/activity.py @@ -59,5 +59,5 @@ class ActivitiesResponse(BaseModel): activities: List[ActivityWithCW] = Field(default_factory=list) total: int = 0 -class ActivityByCwUnitIdResponse(BaseModel): +class ActivityRecordResponse(BaseModel): activity: ActivityWithCW = Field(default=None) \ No newline at end of file