"""Model layer for graph based profile queries.""" from connector_neo4j import get_session from owsresponse import response, status as ows_status from notifications.connectors import sentry from notifications.constants import error from notifications.utils.neo4j import strip_query def get_default_brand_for_label(feed_type: str, feed_id: int) -> response.Response: """Get default_brand for label. Args: feed_type (str): Vendor or Subaccount. feed_id (int): label_profile id or vend_contact vendor_id. Returns: Response: default_brand. """ session = get_session() if feed_type == 'Subaccount': query = strip_query( f"""MATCH (cb:CompanyBrand)-[:HAS_LABEL]->(v:Vendor)-[:OWNS]->(sa:Subaccount) WHERE sa.id = {feed_id} RETURN cb.name""" ) else: query = strip_query( f"""MATCH (cb:CompanyBrand)-[:HAS_LABEL]->(v:Vendor) WHERE v.id = {feed_id} RETURN cb.name""" ) try: result = session.run(query, feed_id=feed_id).peek().data() except AttributeError: res = response.create_error_response( status=ows_status.NOT_FOUND, code=error.ERROR_CODE_NOT_FOUND, message=f'No Company Brand found that associated with {feed_type} {feed_id}.', ) sentry.send_response_to_sentry(res, error.ERROR_CODE_NOT_FOUND) return response.Response(None) return response.Response(result['cb.name']) def get_name_for_label(feed_type: str, feed_id: int) -> response.Response: """Get name for label. Args: feed_type (str): Vendor or Subaccount. feed_id (int): label_profile id or vend_contact vendor_id. Returns: Response: name of the label. """ session = get_session() query = strip_query( f"""MATCH (n:{feed_type} {{id: $feed_id}}) RETURN n.name""" ) result = session.run(query, feed_id=feed_id).single() try: resp = response.Response(result['n.name']) except TypeError: res = response.create_error_response( status=ows_status.NOT_FOUND, code=error.ERROR_CODE_NOT_FOUND, message=f'No Label with {feed_type} {feed_id}.', ) sentry.send_response_to_sentry(res, error.ERROR_CODE_NOT_FOUND) return response.Response(None) return resp