diff --git a/moonstreamapi/moonstreamapi/actions.py b/moonstreamapi/moonstreamapi/actions.py index f4d35cb6..f75715e0 100644 --- a/moonstreamapi/moonstreamapi/actions.py +++ b/moonstreamapi/moonstreamapi/actions.py @@ -1181,10 +1181,10 @@ def create_resource_for_user( return resource -def chekc_user_resource_access( +def check_user_resource_access( customer_id: uuid.UUID, user_token: uuid.UUID, -) -> bool: +) -> Optional[BugoutResource]: """ Check if user has access to customer_id """ @@ -1198,10 +1198,10 @@ def chekc_user_resource_access( except BugoutResponseException as e: if e.status_code == 404: - return False + return None raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail) except Exception as e: logger.error(f"Error get customer: {str(e)}") raise MoonstreamHTTPException(status_code=500, internal_error=e) - return str(response.id) == customer_id + return response diff --git a/moonstreamapi/moonstreamapi/routes/subscriptions.py b/moonstreamapi/moonstreamapi/routes/subscriptions.py index d2f76aa0..10c7925a 100644 --- a/moonstreamapi/moonstreamapi/routes/subscriptions.py +++ b/moonstreamapi/moonstreamapi/routes/subscriptions.py @@ -20,7 +20,7 @@ EntityJournalNotFoundException, apply_moonworm_tasks, check_if_smart_contract, - chekc_user_resource_access, + check_user_resource_access, get_entity_subscription_journal_id, get_list_of_support_interfaces, get_moonworm_tasks, @@ -104,17 +104,19 @@ async def add_subscription_handler( if customer_id is not None: - results = chekc_user_resource_access( + results = check_user_resource_access( customer_id=customer_id, user_token=token, ) - if not results: + if results is None: raise MoonstreamHTTPException( - status_code=403, - detail="User has no access to this customer", + status_code=404, + detail="Not found customer", ) + customer_instance_name = results.resource_data["name"] + active_subscription_types_response = subscription_types.list_subscription_types( active_only=True ) @@ -156,12 +158,12 @@ async def add_subscription_handler( if description: content["description"] = description + excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS + allowed_required_fields: List[Any] = [] if tags: allowed_required_fields = [ - item - for item in tags - if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) + item for item in tags if not any(key in item for key in excluded_keys) ] required_fields: List[Dict[str, Union[str, bool, int, List[Any]]]] = [ @@ -172,6 +174,18 @@ async def add_subscription_handler( {"user_id": f"{user.id}"}, ] + if customer_id is not None and customer_instance_name is not None: + required_fields.extend( + [ + { + "customer_id": f"{customer_id}", + }, + { + "instance_name": f"{customer_instance_name}", + }, + ] + ) + if allowed_required_fields: required_fields.extend(allowed_required_fields) @@ -214,11 +228,15 @@ async def add_subscription_handler( entity_secondary_fields = ( entity.secondary_fields if entity.secondary_fields is not None else {} ) + + # We remove the instance_name for return that tag to the frontend + excluded_keys = excluded_keys - {"instance_name"} + normalized_entity_tags = [ f"{key}:{value}" for tag in entity_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in excluded_keys ] if entity_secondary_fields.get("abi") and customer_id is not None: @@ -310,7 +328,7 @@ async def delete_subscription_handler( f"{key}:{value}" for tag in tags_raw for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in (MOONSTREAM_ENTITIES_RESERVED_TAGS - {"instance_name"}) ] if deleted_entity.secondary_fields is not None: @@ -383,6 +401,9 @@ async def get_subscriptions_handler( List[BugoutSearchResultAsEntity], subscriptions_list.results ) + # We remove the instance_name for return that tag to the frontend + excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS - {"instance_name"} + for subscription in user_subscriptions_results: tags = subscription.required_fields @@ -402,7 +423,7 @@ async def get_subscriptions_handler( f"{key}:{value}" for tag in tags for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in excluded_keys ] subscriptions.append( @@ -460,17 +481,19 @@ async def update_subscriptions_handler( if customer_id is not None: - results = chekc_user_resource_access( + results = check_user_resource_access( customer_id=customer_id, user_token=token, ) - if not results: + if results is None: raise MoonstreamHTTPException( - status_code=403, - detail="User has no access to this customer", + status_code=404, + detail="Not found customer", ) + excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS + try: journal_id = get_entity_subscription_journal_id( resource_type=BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION, @@ -490,7 +513,7 @@ async def update_subscriptions_handler( update_required_fields = [ field for field in subscription_entity.required_fields - if any(key in field for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) + if any(key in field for key in excluded_keys) ] update_secondary_fields = ( @@ -556,9 +579,7 @@ async def update_subscriptions_handler( if tags: allowed_required_fields = [ - item - for item in tags - if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) + item for item in tags if not any(key in item for key in excluded_keys) ] if allowed_required_fields: @@ -619,12 +640,14 @@ async def update_subscriptions_handler( if subscription.secondary_fields is not None else {} ) + # We remove the instance_name for return that tag to the frontend + excluded_keys = excluded_keys - {"instance_name"} normalized_entity_tags = [ f"{key}:{value}" for tag in subscription_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in excluded_keys ] return data.SubscriptionResourceData( diff --git a/moonstreamapi/moonstreamapi/settings.py b/moonstreamapi/moonstreamapi/settings.py index 4e387199..f7848dce 100644 --- a/moonstreamapi/moonstreamapi/settings.py +++ b/moonstreamapi/moonstreamapi/settings.py @@ -301,15 +301,19 @@ ) # Entities reserved tags -MOONSTREAM_ENTITIES_RESERVED_TAGS = [ - "type", - "subscription_type_id", - "color", - "label", - "user_id", - "address", - "blockchain", -] +MOONSTREAM_ENTITIES_RESERVED_TAGS = set( + [ + "type", + "subscription_type_id", + "color", + "label", + "user_id", + "address", + "blockchain", + "customer_id", + "instance_name", + ] +) ## Moonstream resources types