""" Task to check and trigger accounting_period approve_sales_file event. In order to trigger the event, all sales_files in an accounting_period must be complete state for all action_names. """ from lib import constants from lib.utils import ows from tasks.sales_approve import helpers def trigger_approve_sales_files_task( dag_run: dict, **kwargs ): """Check and trigger accounting_period approve_sales_file event. This task will: * get all the sales files in the accounting period (using the sales_file event) * for each sales file in the accounting period, get its action states * If action state does not have "complete" action status, break out * If all action states have the "complete" action status, call ows-abacus-event to create an accounting_period event "approve_sales_files" DOWNSIDE: These fetches seem rather annoying bc it'll be with ows calls, not graphql UPSIDE: This waits until the most recent sales_file sales_approve has completed Args: dag_run (dict): config of the DAG this task belongs to kwargs (dict): any other optional arguments """ event = helpers.get_event_from_params(dag_run, **kwargs) _, accounting_period = helpers.get_event_records(event) accounting_period_id = accounting_period['accounting_period_id'] sales_files = ows.get_accounting_period_sales_files(accounting_period_id) for sales_file in sales_files: sales_file_states = ows.get_abacus_states( 'sales_file', sales_file['sales_file_id']) all_states_are_complete = all( state['action_status'] == constants.ABACUS_STATE_STATUSES.COMPLETE for state in sales_file_states) if not all_states_are_complete: return True # Every sales file's states' were in complete state # Trigger the accounting period approve_sales_files event ows.create_abacus_event( constants.DAG_ACCOUNTING_PERIOD_SALES_APPROVE_EVENT_NAME, accounting_period_id, constants.DAG_ACCOUNTING_PERIOD_SALES_APPROVE_TARGET_TYPE )