"""Global participant endpoints.""" from fastapi import APIRouter, Depends, HTTPException from neo4j import Driver from contributor.api.auth import profiles_from_scope from contributor.api.datasources import get_neo4j_driver from contributor.api.schemas.contributors import Contributor from contributor.api.schemas.global_participants import ( GlobalParticipant, GlobalParticipantContributors, GlobalParticipantDataloaderRequest, ) from contributor.connectors.neo4j import neo4j_session from contributor.logic import global_participants as global_participant_logic router = APIRouter(prefix="/global-participants", tags=["global-participants"]) @router.get( "/{global_participant_uuid}", operation_id="get_global_participant", response_model=GlobalParticipant, summary="Get global participant by UUID", description="Returns a global participant by UUID.", ) def get_global_participant( global_participant_uuid: str, profiles: list[dict] = Depends(profiles_from_scope), driver: Driver = Depends(get_neo4j_driver), ) -> GlobalParticipant: with neo4j_session(driver) as session: result = global_participant_logic.get_global_participant( session, global_participant_id=str(global_participant_uuid), profiles=profiles, ) if result is None: raise HTTPException(status_code=404, detail="Global participant not found") return GlobalParticipant.model_validate(result) @router.get( path="/{global_participant_uuid}/contributors", operation_id="get_contributors_for_global_participant", response_model=GlobalParticipantContributors, summary="Get contributors for a global participant", description="Returns a list of contributors associated with a global participant.", ) def get_contributors_for_global_participant( global_participant_uuid: str, profiles: list[dict] = Depends(profiles_from_scope), driver: Driver = Depends(get_neo4j_driver), ) -> GlobalParticipantContributors: with neo4j_session(driver) as session: contributors = global_participant_logic.get_contributors_for_global_participant( session, global_participant_id=str(global_participant_uuid), profiles=profiles, ) if contributors is None: raise HTTPException(status_code=404, detail="Global participant not found") return GlobalParticipantContributors( global_participant=GlobalParticipant(id=global_participant_uuid), contributors=[ Contributor.model_validate(contributor) for contributor in contributors ], ) @router.post( "/dataloader", operation_id="load_global_participants", response_model=list[GlobalParticipant], summary="Load multiple global participants by key", description="Returns a list of global participants by their IDs.", ) def load_global_participants( request: GlobalParticipantDataloaderRequest, profiles: list[dict] = Depends(profiles_from_scope), driver: Driver = Depends(get_neo4j_driver), ) -> list[GlobalParticipant]: ids = [str(gp.id) for gp in request.global_participants] with neo4j_session(driver) as session: results = global_participant_logic.get_global_participants( session, global_participant_ids=ids, profiles=profiles, ) return [GlobalParticipant.model_validate(result) for result in results]