"""Requests to ows-abacus-account.""" from typing import Any, Dict, List, Optional import urllib.parse from src.connectors.exceptions import OwsAccountException from src.connectors.requests import get, post from src.models import ( Account, AccountPayee, AccountPayeeDataLoaderResponse, AccountPaymentTerm, AccountPaymentTermDataloaderItem, GetAccountsResponse, GetAccountTaxInfoResponse, PaymentHoldList, ) SERVICE = 'ows-abacus-account' def get_eligible_accounts(payment_group_id: int) -> List[Account]: """Get accounts eligible for payment based on the payment_group group_criteria.""" path = f'/payment-group/{payment_group_id}/eligible-accounts' response = get(SERVICE, path) if response.status_code != 200: raise OwsAccountException(f'ERROR in GET {path}') return [Account.model_validate(elem) for elem in response.json()] def get_accounts( limit: int, offset: int, reference_payment_type_id: Optional[int] = None, agreement_type_ids: Optional[List[int]] = None, ) -> GetAccountsResponse: """Get accounts filtered by params.""" params: dict[str, Any] = { 'limit': limit, 'offset': offset, } if reference_payment_type_id: params['reference_payment_type_id'] = reference_payment_type_id if agreement_type_ids: params['agreement_type_ids'] = agreement_type_ids query_string = urllib.parse.urlencode(params, doseq=True) path = f'/accounts/?{query_string}' response = get(SERVICE, path) if response.status_code != 200: raise OwsAccountException(f'ERROR in GET {path}') return GetAccountsResponse.model_validate(response.json()) def bulk_get_payment_holds( account_ids: List[int], limit: int = 100, offset: int = 0 ) -> PaymentHoldList: """Get payment holds for multiple accounts.""" params = { 'limit': limit, 'offset': offset, } query_string = urllib.parse.urlencode(params) path = f'/payment-holds/?{query_string}' response = post(SERVICE, path, account_ids) if response.status_code != 200: raise OwsAccountException(f'ERROR in POST {path} with body {account_ids}') return PaymentHoldList.model_validate(response.json()) def get_account_tax_info( account_ids: List[int], limit: int, offset: int ) -> GetAccountTaxInfoResponse: """Get account tax info for accounts list.""" path = f'/accounts/account-tax-info/?limit={limit}&offset={offset}' response = post(SERVICE, path, account_ids) if response.status_code != 200: raise OwsAccountException(f'ERROR in POST {path} with body {account_ids}') return GetAccountTaxInfoResponse.model_validate(response.json()) def account_payment_term_dataloader( account_ids: List[int], ) -> Dict[int, AccountPaymentTerm]: """Account payment term dataloader.""" path = f'/account/account-payment-term/dataloader' response = post(SERVICE, path, account_ids) if response.status_code != 200: raise OwsAccountException(f'ERROR in POST {path} with body {account_ids}') return { item.data.account_id: item.data for item in AccountPaymentTermDataloaderItem.list_validate(response.json()) if item.data } def account_payee_dataloader(account_ids: List[int]) -> Dict[int, AccountPayee]: """Account payee dataloader.""" path = f'/account-payee/dataloader/account' response = post(SERVICE, path, account_ids) if response.status_code != 200: raise OwsAccountException(f'ERROR in POST {path} with body {account_ids}') return { item.data.account_id: item.data for item in AccountPayeeDataLoaderResponse.model_validate(response.json()).items if item.data }