"""Neo4j queries for product participations.""" from typing import Any from ddtrace import tracer from neo4j import Session get_label_participants_by_product_ids = """ UNWIND $product_ids AS product_id MATCH (pr:Product {id: product_id}) OPTIONAL MATCH (lp:LabelParticipant)-[rel:PARTICIPATED_IN]->(pr) WHERE lp IS NULL OR $is_admin OR EXISTS { MATCH (prof:Profile {profileType: 'LabelProfile'}) -[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->() -[:HAS_LABEL_PARTICIPANT]->(lp) WHERE prof.profileId IN $profile_ids } WITH pr, rel, lp OPTIONAL MATCH (lp)-[:CREATED_FROM]-> (ra:ReleaseArtist {role: rel.participated_as})<-[:HAS_SOURCE_ARTIST]-(pr) USING JOIN ON ra WITH pr, rel, lp, min(ra.id) AS release_artist_id, CASE rel.participated_as WHEN 'performer' THEN 1 WHEN 'featuring' THEN 2 WHEN 'remixer' THEN 3 WHEN 'producer' THEN 4 ELSE 5 END AS role_value ORDER BY role_value, release_artist_id RETURN {product_id: pr.id} AS product, collect( CASE WHEN rel IS NOT NULL THEN { contributor: {uuid: lp.uuid}, participated_as: rel.participated_as, product: {product_id: pr.id}, release_artist_id: release_artist_id } END ) AS contributors """ get_product_participations_by_label_participant_uuid = """ MATCH (lp:LabelParticipant {uuid: $uuid}) WHERE $is_admin OR EXISTS { MATCH (p:Profile {profileType: 'LabelProfile'}) -[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->() -[:HAS_LABEL_PARTICIPANT]->(lp) WHERE p.profileId IN $profile_ids } WITH DISTINCT lp OPTIONAL MATCH (lp)-[:CREATED_FROM]-> (ra:ReleaseArtist { role: rel.participated_as })<-[:HAS_SOURCE_ARTIST]- (pr:Product)<-[rel:PARTICIPATED_IN]-(lp) USING JOIN ON ra WITH lp, rel, min(ra.id) AS release_artist_id, CASE rel.participated_as WHEN 'performer' THEN 1 WHEN 'featuring' THEN 2 WHEN 'remixer' THEN 3 WHEN 'producer' THEN 4 ELSE 5 END AS role_value ORDER BY role_value, release_artist_id RETURN {uuid: lp.uuid} AS contributor, collect({ contributor: {uuid: lp.uuid}, participated_as: rel.participated_as, product: {product_id: endNode(rel).id}, release_artist_id: release_artist_id }) AS participations """ @tracer.wrap() def get_by_product_id( session: Session, product_id: int, profile_ids: list[int], is_admin: bool = False, ) -> dict[str, Any] | None: results = get_by_product_ids( session, product_ids=[product_id], profile_ids=profile_ids, is_admin=is_admin, ) return results[0] if results else None @tracer.wrap() def get_by_product_ids( session: Session, product_ids: list[int], profile_ids: list[int], is_admin: bool = False, ) -> list[dict[str, Any]]: result = session.run( query=get_label_participants_by_product_ids, product_ids=product_ids, profile_ids=profile_ids, is_admin=is_admin, ) products_data = [] for record in result: products_data.append( { "product": record["product"], "contributors": record["contributors"], } ) return products_data @tracer.wrap() def get_by_label_participant_uuid( session: Session, uuid: str, profile_ids: list[int], is_admin: bool = False, ) -> dict[str, Any] | None: result = session.run( get_product_participations_by_label_participant_uuid, uuid=uuid, profile_ids=profile_ids, is_admin=is_admin, ) record = result.single() if record is None: return None participations = [ { "contributor": {"uuid": p["contributor"]["uuid"]}, "participated_as": p["participated_as"], "product": p["product"], "release_artist_id": p["release_artist_id"], } for p in record["participations"] if p.get("participated_as") is not None ] return { "contributor": record["contributor"], "participations": participations, }