"""TermsAndConditionsAgreement Persister. Handles doing CRUD operations on the terms_and_conditions_agreement table. """ from typing import Optional from sqlalchemy import desc from sqlalchemy.orm.session import Session from collaborator.connectors import mysql from collaborator.constants import error from collaborator.models.rds.statement_period_persister import StatementPeriodPersister from collaborator.models.rds.terms_and_conditions_agreement import ( TermsAndConditionsAgreement, ) from collaborator.utils.error import OwsError from collaborator.utils.typing import Account class TermsAndConditionsAgreementPersister: """Handles high level operations for terms and conditonss.""" @classmethod @mysql.db_session def get_latest_for_account(cls, account: Account, session: Session) -> dict | None: """Get latest terms and conditons agreement for account. Args: account (Account): Account to limit by. session (sqlalchemy.orm.session.Session): database session. Returns: Response: the terms and conditons for the requested vendor """ result = ( session.query(TermsAndConditionsAgreement) .filter(TermsAndConditionsAgreement.vendor_id == int(account.id)) .order_by( desc(TermsAndConditionsAgreement.terms_and_conditions_agreement_id) ) .first() ) return result.to_dict() if result else None @classmethod @mysql.db_session def agree_version( cls, identity_metadata: dict, vendor_id: str, ts_and_cs_agreed_version_id: str, abacus_statement_period: Optional[dict], session: Session, ) -> dict: """Agree to some version of terms and conditons. Args: dict (response): the account that agrees the terms and conditions. vendor_id (str): the account ID. ts_and_cs_version_id (str): the agreed terms and conditions version ID. session (sqlalchemy.orm.session.Session): database session. Returns: Response: the terms and conditons for the requested vendor """ open_statement_period = StatementPeriodPersister.get_open_statement_period( account_id=vendor_id, session=session, ) if open_statement_period: if not abacus_statement_period: raise OwsError.not_found( message=error.ERROR_MESSAGE_TERMS_AND_CONDITIONS_AGREEMENT_NO_ABACUS_STATEMENT_PERIOD ) abacus_statement_period_id = abacus_statement_period["statement_period_id"] open_statement_period.abacus_statement_period_id = ( abacus_statement_period_id ) new_agreement = TermsAndConditionsAgreement( terms_and_conditions_id=ts_and_cs_agreed_version_id, vendor_id=vendor_id, vendor_brand=identity_metadata["default_brand"], user_id=identity_metadata["id"], ) session.add(new_agreement) session.commit() return new_agreement.to_dict()