"""Requests to ows-abacus-state.""" from http import HTTPStatus from typing import Optional from src.constants import ActionStatuses from src.exceptions import OwsAbacusStateException from src.models import AbacusStateData from src.requests import get, put SERVICE = 'ows-abacus-state' BANKING_DETAILS_REVIEW_ACTION = 'banking_details_review' PAYMENT_ELIGIBILITY_ACTION = 'payment_eligibility' def get_banking_details_review_status( account_payee_id: int, ) -> Optional[str]: """Get action_status of the banking_details_review state for an account payee. Args: account_payee_id: The account payee ID. Returns: The action_status string, or None if no banking_details_review state exists. Raises: OwsAbacusStateException: If the API request fails. """ states = _get_account_payee_states(account_payee_id) for state in states: if state.action_name == BANKING_DETAILS_REVIEW_ACTION: return state.action_status return None def set_payment_eligibility_error( account_payee_id: int, message: str, ) -> None: """Set payment_eligibility state to error for an account payee. Fetches all states for the account payee, finds the payment_eligibility state, and updates it with action_status="error" and the provided message. Args: account_payee_id: The account payee ID. message: The error message to set on the state. Raises: OwsAbacusStateException: If the API request fails or no payment_eligibility state is found. """ states = _get_account_payee_states(account_payee_id) abacus_state_id = None for state in states: if state.action_name == PAYMENT_ELIGIBILITY_ACTION: abacus_state_id = state.abacus_state_id break if abacus_state_id is None: raise OwsAbacusStateException( f'No {PAYMENT_ELIGIBILITY_ACTION} state found for account_payee_id={account_payee_id}' ) path = f'/abacus-state/{abacus_state_id}' response = put( SERVICE, path, { 'action_status': 'error', 'message': message, }, ) if response.status_code != HTTPStatus.OK: raise OwsAbacusStateException( f'Failed to set payment eligibility error: {response.text}' ) def banking_details_rejected(account_payee_id: int) -> bool: """ Check if any banking details related states are rejected. Args: account_payee_id: The account payee ID. """ states = _get_account_payee_states(account_payee_id) return any( state.action_status == ActionStatuses.REJECTED for state in states if state.action_name in (BANKING_DETAILS_REVIEW_ACTION, PAYMENT_ELIGIBILITY_ACTION) ) def _get_account_payee_states(account_payee_id: int) -> list[AbacusStateData]: """Fetch all abacus states for an account payee. Args: account_payee_id: The account payee ID. Returns: List of AbacusStateData. Raises: OwsAbacusStateException: If the API request fails. """ path = f'/abacus-state/account_payee/{account_payee_id}' response = get(SERVICE, path) if response.status_code != HTTPStatus.OK: raise OwsAbacusStateException(f'ERROR in GET {path}') return [AbacusStateData.model_validate(item) for item in response.json()]