""" subaccount royalty collection model. Model for getting information about subaccount royalty collections. """ from sqlalchemy import Boolean, Column, Integer from contracts import response from contracts.connectors import mysql class SubaccountRoyaltyCollection(mysql.BaseModel): """SubaccountRoyaltyCollection class.""" __tablename__ = 'subaccount_royalty_collection' subaccount_royalty_collection_id = Column(Integer, primary_key=True) subaccount_id = Column(Integer) active = Column(Boolean, nullable=False) class SubaccountRoyaltyCollectionTerrittories(mysql.BaseModel): """SubaccountRoyaltyCollectionTerrittories class.""" __tablename__ = 'subaccount_royalty_collection_territories' subaccount_royalty_collection_territories_id = Column(Integer, primary_key=True) subaccount_royalty_collection_id = Column(Integer) subaccount_royalty_collection_territory = Column(Integer) def get_active_subaccount_royalty_collection(subaccount_id): """Get active subaccount royalty collection information. Args: subaccount_id (int): unique identifier for the subaccount. Returns: response.Response: row with subaccount royalty collection information if available. """ with mysql.db_session() as session: row = ( session.query(SubaccountRoyaltyCollection) .filter( SubaccountRoyaltyCollection.subaccount_id == subaccount_id, SubaccountRoyaltyCollection.active, ) .one_or_none() ) if row: data = {} for column in row.__table__.columns: data[column.name] = getattr(row, column.name) return response.Response(data) return response.create_not_found_response() def get_subaccount_royalty_collection_territories(subaccount_royalty_collection_id): """Get subaccount royalty collection territories. Args: subaccount_royalty_collection_id (int): unique identifier for the subaccount royalty collection. Returns: response.Response: list with subaccount royalty collection territories list. """ with mysql.db_session() as session: rows = ( session.query(SubaccountRoyaltyCollectionTerrittories) .filter( SubaccountRoyaltyCollectionTerrittories.subaccount_royalty_collection_id == subaccount_royalty_collection_id, ) .order_by( SubaccountRoyaltyCollectionTerrittories.subaccount_royalty_collection_territory ) .all() ) data = [row.subaccount_royalty_collection_territory for row in rows] return response.Response(data) def get_subaccount_id_by_royalty_collection_id(subaccount_royalty_collection_id): """Get subaccount id by royalty collection id. Args: subaccount_royalty_collection_id (int): unique identifier for the subaccount royalty collection. Returns: response.Response: subaccount id if available. """ with mysql.db_session() as session: row = ( session.query(SubaccountRoyaltyCollection) .filter( SubaccountRoyaltyCollection.subaccount_royalty_collection_id == subaccount_royalty_collection_id, ) .one_or_none() ) if row: return response.Response(row.subaccount_id) return response.create_not_found_response() def get_royalty_collection(subaccount_royalty_collection_id): """Get everything about a subaccount_royalty_collection_id. Args: subaccount_royalty_collection_id (int): unique identifier for the subaccount royalty collection. Returns: response.Response: . """ with mysql.db_session() as session: rows = ( session.query( SubaccountRoyaltyCollection, SubaccountRoyaltyCollectionTerrittories, ) .join( SubaccountRoyaltyCollectionTerrittories, SubaccountRoyaltyCollection.subaccount_royalty_collection_id == SubaccountRoyaltyCollectionTerrittories.subaccount_royalty_collection_id, ) .filter( SubaccountRoyaltyCollection.subaccount_royalty_collection_id == subaccount_royalty_collection_id, ) .all() ) if rows: territories = [ row[1].subaccount_royalty_collection_territory for row in rows ] royalty_collection_id = rows[0][0].subaccount_royalty_collection_id subaccount_id = rows[0][0].subaccount_id active = rows[0][0].active data = { 'subaccount_royalty_collection_id': royalty_collection_id, 'subaccount_id': subaccount_id, 'active': active, 'territories': territories, } return response.Response(data) return response.create_not_found_response()