"""contract_flowthrough Model.""" from abacus_common_logic.connectors.database import db from abacus_common_logic.models import BaseModel, NormalizedDateTime from sqlalchemy import Enum from sqlalchemy.dialects.mysql import MEDIUMTEXT from abacus_contract.constants.constants import ( CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES, CONTRACT_FLOWTHROUGH_STATUSES, ) class ContractFlowthrough(BaseModel): """contract_flowthrough Model.""" __tablename__ = 'contract_flowthrough' contract_flowthrough_id = db.Column(db.Integer, primary_key=True) contract_id = db.Column( db.Integer, db.ForeignKey('contract.contract_id'), nullable=False ) reference_flowthrough_calculation_id = db.Column( db.Integer, db.ForeignKey( 'reference_flowthrough_calculation.reference_flowthrough_calculation_id' ), nullable=False, ) flowthrough_rate = db.Column(db.Numeric(5, 2), nullable=False) flowthrough_status = db.Column( Enum( *CONTRACT_FLOWTHROUGH_STATUSES, name='flowthrough_status', create_type=False ), nullable=False, server_default=CONTRACT_FLOWTHROUGH_STATUSES.ACTIVE, ) has_automatic_shutoff = db.Column(db.Boolean, nullable=True, default=True) recoupment_cap = db.Column(db.Numeric(20, 2), nullable=True) calculation_comment = db.Column(MEDIUMTEXT, nullable=True) previous_flowthrough_status = db.Column( Enum( *CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES, name='previous_flowthrough_status', create_type=False, ), nullable=True, ) status_last_modified_by = db.Column(db.String(180), nullable=True) status_last_modified = db.Column(NormalizedDateTime(), nullable=True) deleted_at = db.Column(NormalizedDateTime(), nullable=True) deleted_by = db.Column(db.String(180), nullable=True) @classmethod def get_by_contract_id(cls, contract_id: int) -> dict: """Get a contract_flowthrough by contract_id. Args: contract_id (int): id of the contract Returns: a contract_flowthrough record if exist """ return cls.query.filter( cls.contract_id == contract_id, cls.deleted_at.is_(None), cls.deleted_by.is_(None), ).first() @classmethod def get_by_contract_ids(cls, contract_ids: list) -> list: """Get non-deleted contract_flowthrough records for a batch of contract_ids. Args: contract_ids (list): ids of the contracts Returns: a list of non-deleted contract_flowthrough records """ return cls.query.filter( cls.contract_id.in_(contract_ids), cls.deleted_at.is_(None), cls.deleted_by.is_(None), ).all()