"""Requests to ows-abacus-account .""" from config import app_logger as logger from src.connectors.exceptions import OwsAbacusAccountException from src.constants import DEFAULT_BATCH_SIZE from src.models import ( AccountPayeeDataloaderAccount, AccountTaxInfo, AccountTaxInfoBulk, PaymentHold, PaymentHoldResult, UpdateAccountTaxInfo, ) from src.requests import post, put SERVICE = 'ows-abacus-account' def create_or_update_payment_hold( payment_hold: PaymentHold, ) -> PaymentHoldResult | None: """Create or update payment hold.""" logger.info( f'Upserting payment hold account_id={payment_hold.account_id} is_on_hold={payment_hold.is_on_hold}.' ) path = f'/account/{payment_hold.account_id}/payment-hold/' response = post( SERVICE, path, payment_hold.model_dump(exclude={'payment_hold_id', 'account_id'}, mode='json'), ) if response.status_code != 201: error_json = response.json() if 'message' in error_json: error_message = error_json['message'] if isinstance(error_message, str) and error_message.startswith( "Account's payment status" ): logger.warning( f'Payment hold for account_id={payment_hold.account_id}' f' and is_on_hold={payment_hold.is_on_hold} already exists' ) return None raise OwsAbacusAccountException(f'ERROR in POST {path} {response.json()}') result = PaymentHoldResult.model_validate(response.json()) logger.info(f'Payment hold id={result.payment_hold_id} is set') return result def get_payees_by_accounts(account_ids: list[int] | list[str]) -> dict[int, int]: """Get account_id to account_payee_id map by account_id list.""" path = '/account-payee/dataloader/account' try: response = post(SERVICE, path, body=account_ids, raise_for_status=True) except Exception as e: raise OwsAbacusAccountException(f'Failed to get payees by accounts: {e}') data = AccountPayeeDataloaderAccount.model_validate(response.json()) return { item.data.account_id: item.data.account_payee_id for item in data.items if item.data is not None } def get_account_tax_info_bulk( account_ids: list[int], offset: int = 0, limit: int = DEFAULT_BATCH_SIZE ) -> AccountTaxInfoBulk: """Get account tax info by account IDs bulk.""" try: response = post( SERVICE, f'/accounts/account-tax-info?offset={offset}&limit={limit}', body=account_ids, raise_for_status=True, ) except Exception as e: raise OwsAbacusAccountException( f'Failed to get tax info for accounts {account_ids}: {e}' ) return AccountTaxInfoBulk.model_validate(response.json()) def update_account_tax_info( account_tax_info_id: int, payload: UpdateAccountTaxInfo ) -> AccountTaxInfo: """Update account tax info.""" try: response = put( SERVICE, f'/account-tax-info/{account_tax_info_id}', body=payload.model_dump(mode='json', exclude_none=True), raise_for_status=True, ) except Exception as e: raise OwsAbacusAccountException( f'Failed to update account tax info {account_tax_info_id}: {e}' ) return AccountTaxInfo.model_validate(response.json())