"""TransferWise Batch Persister. Handles doing CRUD operations on the transferwise_batch table. """ from typing import List, Optional from sqlalchemy.orm.session import Session from collaborator.connectors import mysql from collaborator.constants import error from collaborator.models.rds.transferwise_batch import TransferwiseBatch from collaborator.utils import logging from collaborator.utils.error import OwsError from collaborator.utils.typing import User class TransferwiseBatchPersister: """Handles high level operations for TransferWise Batches.""" @classmethod @mysql.db_session def create_batch(cls, user: User, data: dict, session: Session) -> dict: """Create a TransferWise batch. Args: user (User): User which is creating the profile. batch_data (dic): Dictionary with the batch data session (sqlalchemy.orm.session.Session): Database session. Returns: dict: the newly created batch """ batch = TransferwiseBatch(**data) session.add(batch) session.commit() batch_dict = batch.to_dict() logging.log_event( logging.LOG_EVENT_CREATE, "transferwise_batch", batch_dict["id"], None, batch_dict, user, ) return batch_dict @classmethod @mysql.db_session def get_by_id(cls, batch_id: int, session: Session) -> Optional[dict]: """Get a TransferWise batch by ID. Args: batch_id (int): ID of the batch to retrieve. session (sqlalchemy.orm.session.Session): Database session. Returns: dict: The batch. """ result = ( session.query(TransferwiseBatch) .filter_by(transferwise_batch_id=batch_id) .first() ) if not result: raise OwsError.not_found( code=error.ERROR_CODE_TRANSFERWISE_BATCH_NOT_FOUND, message=error.ERROR_MESSAGE_TRANSFERWISE_BATCH_NOT_FOUND, ) return result.to_dict() @classmethod @mysql.db_session def get_by_batch_uuid(cls, batch_id: str, session: Session) -> Optional[dict]: """Get a TransferWise batch by ID. Args: batch_id (str): UUID of the batch to retrieve. session (sqlalchemy.orm.session.Session): Database session. Returns: dict: The batch. """ result = session.query(TransferwiseBatch).filter_by(batch_id=batch_id).first() if not result: raise OwsError.not_found( code=error.ERROR_CODE_TRANSFERWISE_BATCH_NOT_FOUND, message=error.ERROR_MESSAGE_TRANSFERWISE_BATCH_NOT_FOUND, ) return result.to_dict() @classmethod @mysql.db_session def get_by_vendor_id(cls, vendor_id: str, session: Session) -> List[dict]: """Get a TransferWise batch by ID. Args: vendor_id (str): id of account for which we are retrieving batches. session (sqlalchemy.orm.session.Session): Database session. Returns: batches: The list of batches for the given vendor_id. """ result = session.query(TransferwiseBatch).filter_by(vendor_id=vendor_id).all() batches = [batch.to_dict() for batch in result] return batches @classmethod @mysql.db_session def update_batch( cls, batch_id: int, data: dict, user: User, session: Session ) -> dict: """Update a TransferWise batch. Args: batch_id (int): ID of the batch to update. data (dict): Data to update with. session (sqlalchemy.orm.session.Session): Database session. Returns: dict: The updated batch. """ query = session.query(TransferwiseBatch).filter( TransferwiseBatch.transferwise_batch_id == batch_id ) batch = query.first() if not batch: raise OwsError.not_found( code=error.ERROR_CODE_TRANSFERWISE_BATCH_NOT_FOUND, message=error.ERROR_MESSAGE_TRANSFERWISE_BATCH_NOT_FOUND, ) existing_data = batch.to_dict() query.update(data) session.refresh(batch) result = batch.to_dict() logging.log_event( logging.LOG_EVENT_UPDATE, "transferwise_batch", result["id"], existing_data, result, user, ) return result