"""Helpers for accounting_period_sales_approve tasks.""" from lib import constants from lib.abacus_event import AbacusEvent from lib.utils import event from lib.utils import ows PARENT_TABLE_NAME = 'accounting_period' def get_event_from_params(dag_run: dict, **kwargs) -> AbacusEvent: """Get event from params passed to task callbacks. Args: dag_run (dict): config of the DAG this task belongs to kwargs (dict): any other optional arguments Returns: instance of an AbacusEvent """ abacus_event = event.get_abacus_event(dag_run, **kwargs) event.validate_event_for_handler( abacus_event, event_name=constants.DAG_ACCOUNTING_PERIOD_SALES_APPROVE_EVENT_NAME, target_type=constants.DAG_ACCOUNTING_PERIOD_SALES_APPROVE_TARGET_TYPE ) return abacus_event def get_abacus_state(accounting_period_id: int) -> dict: """Find the 'approve_sales_files' abacus_state record for the parent accounting_period. Args: accounting_period_id (int): ID of the accounting_period Returns the 'approve_sales_files' abacus_state record """ abacus_states = ows.get_abacus_states(PARENT_TABLE_NAME, accounting_period_id) action_name = constants.ACCOUNTING_PERIOD_ACTIONS.APPROVE_SALES_FILES approve_sales_files_action = next( ( abacus_state for abacus_state in abacus_states if abacus_state['action_name'] == action_name ), None ) if not approve_sales_files_action: raise ValueError(f'{action_name} state not found') return approve_sales_files_action