"""TermsAndAgreements logic tier.""" from collaborator.constants import error from collaborator.logic.payee import is_account_payment_eligible from collaborator.models.ows import ows_royalties from collaborator.models.rds.terms_and_conditions_agreement_persister import ( TermsAndConditionsAgreementPersister, ) from collaborator.models.rds.terms_and_conditions_persister import ( TermsAndConditionsPersister, ) from collaborator.utils.error import OwsError from collaborator.utils.typing import Account def get_terms_and_conditions(account: Account) -> dict: """Get terms and conditions for account. Args: account (Account): the account that creates the terms and conditions Returns: (dict): the terms and conditions """ latest_ts_and_cs = TermsAndConditionsPersister.get_latest() latest_agreement = TermsAndConditionsAgreementPersister.get_latest_for_account( account ) if not latest_ts_and_cs: error_message = error.ERROR_MESSAGE_TERMS_AND_CONDITIONS_LAST_VERSION_NOT_FOUND raise OwsError.not_found(message=error_message) if not latest_agreement: latest_agreement = { "terms_and_conditions_id": None, "agreed_date": None, } return { "latest_version": latest_ts_and_cs["id"], "latest_template": latest_ts_and_cs["template"], "agreed_version": latest_agreement["terms_and_conditions_id"], "agreed_date": latest_agreement["agreed_date"], } def agree_terms_and_conditions( identity_metadata: dict, vendor_id: int, ts_and_cs_agreed_version_id: int ) -> dict: """Agree terms and conditions for account. Args: identity_metadata (dict): the account that agrees the terms and conditions vendor_id (int): the account ID ts_and_cs_agreed_version_id (int): the agreed terms and conditions version ID Returns: (dict): the terms and conditions agreement """ latest_ts_and_cs = TermsAndConditionsPersister.get_latest() if not latest_ts_and_cs: error_message = error.ERROR_MESSAGE_TERMS_AND_CONDITIONS_LAST_VERSION_NOT_FOUND raise OwsError.not_found(message=error_message) if ts_and_cs_agreed_version_id != latest_ts_and_cs["id"]: error_message = ( error.ERROR_MESSAGE_TERMS_AND_CONDITIONS_TRYING_TO_SIGN_OUTDATED_VERSION ) raise OwsError.not_found(message=error_message) account_is_payment_eligible = is_account_payment_eligible(int(vendor_id)) if not account_is_payment_eligible: error_message = ( error.ERROR_MESSAGE_TERMS_AND_CONDITIONS_CLIENT_NOT_PAYMENT_ELIGIBLE ) raise OwsError.not_found(message=error_message) return TermsAndConditionsAgreementPersister.agree_version( identity_metadata, vendor_id, ts_and_cs_agreed_version_id, abacus_statement_period=ows_royalties.get_current_abacus_statement_period(), )