"""Helpers for sales_get_eligible tasks.""" from lib.abacus_event import AbacusEvent from lib.constants import DAG_SALES_GET_ELIGIBLE_EVENT_NAME from lib.constants import DAG_SALES_GET_ELIGIBLE_TARGET_TYPE from lib.constants import SALES_FILE_ACTIONS from lib.utils import event from lib.utils import ows PARENT_TABLE_NAME = 'sales_file' 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=DAG_SALES_GET_ELIGIBLE_EVENT_NAME, target_type=DAG_SALES_GET_ELIGIBLE_TARGET_TYPE ) return abacus_event def get_abacus_state(sales_file_id: int) -> dict: """Find the 'get_eligible_sales' abacus_state record for the parent sales_file. Args: sales_file_id (int): ID of the sales_file Returns: the 'get_eligible_sales' abacus_state record """ abacus_states = ows.get_abacus_states(PARENT_TABLE_NAME, sales_file_id) get_eligible_sales_action = next( ( abacus_state for abacus_state in abacus_states if abacus_state['action_name'] == SALES_FILE_ACTIONS.GET_ELIGIBLE_SALES ), None ) if not get_eligible_sales_action: raise ValueError(f'{SALES_FILE_ACTIONS.GET_ELIGIBLE_SALES} state not found') return get_eligible_sales_action