"""Register banking details processor.""" import typing from config import app_logger as logger from src.connectors import ows_abacus_account, ows_payee from src.connectors.exceptions import OwsPayeeException from src.constants import DEFAULT_BATCH_SIZE from src.processors.base import Processor from src.processors.exceptions import ProcessingError from src.utils import chunks class RegisterBankingDetailsProcessor(Processor): """ Processor to register banking details in payoneer, these details should be in the DB. Expects the CSV file with the following columns: - vendor_id (str) The first row is a header. """ _account_ids: list[int] _account_id_account_payee_id: dict[int, int] def process(self) -> None: """Run main logic.""" self._load_data() self._load_account_payee_ids() self._register_banking_details() def _load_data(self) -> None: """Load data from file.""" logger.info('Loading data from csv file') try: raw_data: list[typing.Mapping[str, typing.Any]] = list(self.csv_dict_reader) except Exception as e: raise ProcessingError(f'Unable to read the file: {e}') try: self._account_ids = [int(item['vendor_id']) for item in raw_data] except Exception as e: raise ProcessingError(f'Invalid file content: {e}') if not self._account_ids: raise ProcessingError('No items in file to register') logger.info(f'Loaded {len(self._account_ids)} items.') def _load_account_payee_ids(self) -> None: """Load account payee IDs by account IDs.""" self._account_id_account_payee_id = {} for account_ids in chunks(list(self._account_ids), DEFAULT_BATCH_SIZE): self._account_id_account_payee_id.update( ows_abacus_account.get_payees_by_accounts(account_ids) ) missing_account_ids = ( set(self._account_ids) - self._account_id_account_payee_id.keys() ) if missing_account_ids: self._add_log(f'Missing accounts {missing_account_ids}') def _register_banking_details(self) -> None: """Register banking details.""" processed_items_count = 0 for account_payee_id in self._account_id_account_payee_id.values(): logger.debug(f'Registering payee: {account_payee_id}') try: ows_payee.register_banking_details(account_payee_id) except OwsPayeeException as e: self._add_log(str(e)) continue processed_items_count += 1 logger.info( f'Registered {processed_items_count} items of {len(self._account_id_account_payee_id)}' )