"""Model layer for graph based product queries.""" from connector_neo4j import get_session from owsresponse import response from notifications.utils.neo4j import strip_query def get_product_details_by_id(product_id: int) -> response.Response: """Get product by id. Args: product_id (int): Product Id. Returns: Response: product object. """ session = get_session() query = strip_query( """MATCH (p:Product)<-[r:INCLUDES]-(pr:Project)-[ha:HAS_ARTIST]->(ai:ArtistInfo) WHERE p.id = $productId RETURN p as product, pr as project, ai as artist""" ) result = session.run(query, productId=int(product_id)).single() if not result: return response.create_not_found_response(f'No product found with id {product_id}.') if result['product'].get('deletions'): return response.create_not_found_response(f'Product with id {product_id} is deleted.') details = dict(result['product']) details['project'] = dict(result['project']) details['artist'] = dict(result['artist']) return response.Response(details)