from typing import Any from ddtrace import tracer from neo4j import Session from pydantic import UUID4 get_participation_role_categories = """ MATCH (participationRoleCategory:ParticipationRoleCategory) RETURN {uuid: participationRoleCategory.uuid, name: participationRoleCategory.name} AS category """ get_participation_role_by_uuid = """ MATCH (pr:ParticipationRole { uuid: $uuid })-[:BELONGS_TO]->(prc:ParticipationRoleCategory) RETURN { uuid: pr.uuid, apple_role_name: pr.appleRoleName, category: {uuid: prc.uuid, name: prc.name}, ddex_role_name: pr.ddexRoleName, name: pr.name } AS role """ get_participation_roles_by_category_uuid = """ MATCH (prc:ParticipationRoleCategory { uuid: $uuid })<-[:BELONGS_TO]-(pr:ParticipationRole) RETURN { uuid: pr.uuid, apple_role_name: pr.appleRoleName, category: {uuid: prc.uuid, name: prc.name}, ddex_role_name: pr.ddexRoleName, name: pr.name } AS role """ get_participation_roles_by_category_uuids = """ UNWIND $uuids AS category_uuid MATCH (prc:ParticipationRoleCategory { uuid: category_uuid })<-[:BELONGS_TO]-(pr:ParticipationRole) WITH category_uuid, collect({ uuid: pr.uuid, apple_role_name: pr.appleRoleName, category: {uuid: prc.uuid, name: prc.name}, ddex_role_name: pr.ddexRoleName, name: pr.name }) AS roles RETURN { category_uuid: category_uuid, roles: roles } AS category_roles """ @tracer.wrap() def get_role_categories( session: Session, ) -> list[dict[str, Any]]: result = session.run(query=get_participation_role_categories) return [dict(record["category"]) for record in result] @tracer.wrap() def get_role_by_uuid( session: Session, uuid: UUID4, ) -> dict[str, Any] | None: result = session.run( query=get_participation_role_by_uuid, parameters={"uuid": str(uuid)}, ) record = result.single() return dict(record["role"]) if record else None @tracer.wrap() def get_roles_by_category_uuid( session: Session, uuid: UUID4, ) -> list[dict[str, Any]]: result = session.run( query=get_participation_roles_by_category_uuid, parameters={"uuid": str(uuid)}, ) return [dict(record["role"]) for record in result] @tracer.wrap() def get_roles_by_category_uuids( session: Session, uuids: list[str], ) -> list[dict[str, Any]]: result = session.run( query=get_participation_roles_by_category_uuids, parameters={"uuids": uuids}, ) results = [] for record in result: results.append( { "category_uuid": record["category_roles"]["category_uuid"], "roles": record["category_roles"]["roles"], } ) return results