"""Contract Flowthrough Model.""" from abacus_common_logic.connectors.database import db from abacus_common_logic.models.base import BaseModel from sqlalchemy import Enum from abacus_contract.constants.constants import \ CONTRACT_FLOWTHROUGH_PREVIOUS_STATUSES from abacus_contract.constants.constants import \ CONTRACT_FLOWTHROUGH_STATUSES class ContractFlowthrough(BaseModel): """ContractFlowthrough 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.Integer, 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(db.DateTime, nullable=True) deleted_at = db.Column(db.DateTime, 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()