from decimal import Decimal from typing import Dict, List, Optional from src import constants from src.connectors.ows_account import account_payment_term_dataloader, get_accounts from src.models import Account, AccountPaymentTerm def fetch_all_accounts( reference_payment_type_id: Optional[int] = None, agreement_type_ids: Optional[List[int]] = None, ) -> List[Account]: """Get all accounts.""" offset = 0 limit = constants.ACCOUNT_BATCH_SIZE accounts: List[Account] = [] while True: batch = get_accounts( reference_payment_type_id=reference_payment_type_id, agreement_type_ids=agreement_type_ids, limit=limit, offset=offset, ) offset = offset + limit accounts.extend(batch.items) if offset >= batch.total_count: break payment_terms = account_payment_term_dataloader( [account.account_id for account in accounts] ) # enrich fields missed in generic list endpoint accounts = [ account.model_copy(update=_get_enrichment_data(account, payment_terms)) for account in accounts ] return accounts def _get_enrichment_data( account: Account, payment_terms: Dict[int, AccountPaymentTerm] ) -> Optional[Dict[str, str | Decimal | None]]: """ Get extra fields that present in the eligible accounts endpoint, but missed in the generic accounts list endpoint.""" term = payment_terms.get(account.account_id) if not term: return None return { 'currency_code': term.currency_code, 'payment_minimum': term.payment_minimum, }