"""Logic for Network Participant. This module holds the logic for performing actions against Network Participant entities. """ import participant.constants.network_participant_queries as q from neobolt.exceptions import ConstraintError from owsresponse import response from participant.constants import error from participant.models import base as base_models from participant.models.ows import ows_podcast as ows_podcast_model from participant.utils.convert import format_response def search_network_participant(name, network_id): """Search for a Network Participant by name. Args: name (str): the name to search for network_id (str): Network id. Returns: Response: the network participant info. """ name = name.strip() results = base_models.get_nodes( q.search_network_participant, wildcard_query=f'{name}*', network_id=network_id, ) return response.Response(format_response(results)) def get_network_participant_by_id(network_participant_id): """Get a network participant by id. Args: network_participant_id (int): The network participant's unique identifier. Returns: response.Response: containing a network participant record. """ result = base_models.get_node( q.get_network_participant_by_id, id=network_participant_id, ) if not result: return response.create_not_found_response('Resource not found.') return response.Response(format_response(result)) def create_network_participant(name, network_id): """Create Network Participant node in neo4j. Args: name (str): Network Participant name. network_id (str): Network id. Returns: response.Response: Response with newly created network participant. Or error response if a participant with the passed parameters already exists. """ try: result = base_models.create_node( q.create_network_participant, name=name, network_id=network_id ) except ConstraintError as err: return response.create_error_response( error.ERROR_CODE_DUPLICATE_NODE, message=str(err) ) return response.Response(format_response(result)) def update_network_participant(network_participant_id, network_id, update_params): """Update network participant node in neo4j. Args: network_participant_id (int): Network Participant id. network_id (int): Network id. Returns: response.Response: Response with newly updated network participant. Or error response. """ result = base_models.update_node( q.update_network_participant, id=network_participant_id, network_id=network_id, update_params=update_params, ) if not result: return response.create_not_found_response('Could not update resource.') return response.Response(format_response(result)) def current_user_owns_network_id(network_id): """Check that current user has access to given network. Args: network_id (int): Network id Returns: response.Response: Response with newly updated network participant. Or error response. """ return ows_podcast_model.current_user_owns_network_id(network_id)