"""Logic for updating statement_period_adjustment_file actions state.""" from owsresponse import response from abacus_state.constants.constants import ( ACTION_STATUSES, AUTO_GENERATED_STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES, STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES, ) from abacus_state.constants.error import ( ERROR_ALREADY_RUNNING, ERROR_PREVIOUS_ACTIONS_STATUS, ) from abacus_state.models.abacus_state import AbacusState from abacus_state.models.statement_period_adjustment_file import ( StatementPeriodAdjustmentFile, ) from abacus_state.schemas.abacus_state import AbacusStateDetailSchema from abacus_state.utils.features import is_abacus_flowthrough_automation_enabled from abacus_state.utils.format_error import validation_error from abacus_state.utils.validate_action_status import are_previous_actions_completed def update_statement_period_adjustment_file_state(abacus_state, **params): """Update statement_period_adjustment_file_state by action_state_id.""" parent_table_id = abacus_state.parent_table_id actions = AbacusState.get_formatted_action_statuses( 'statement_period_adjustment_file', parent_table_id ) ordered_actions = list(STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES) if is_abacus_flowthrough_automation_enabled(): adjustment_file = ( StatementPeriodAdjustmentFile.get_statement_period_adjustment_file( parent_table_id ) ) if adjustment_file and adjustment_file['batch_type'] == 'auto': ordered_actions = list( AUTO_GENERATED_STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES ) if not are_previous_actions_completed( actions, abacus_state.action_name, ordered_actions ): return validation_error( ERROR_PREVIOUS_ACTIONS_STATUS.format(abacus_state.action_name) ) if _is_updating_to_running_when_already_running(abacus_state, params): return validation_error(ERROR_ALREADY_RUNNING) abacus_state.update_attributes(**params) AbacusState.commit_changes() return response.Response( message=AbacusStateDetailSchema().dump(abacus_state), status=200 ) def _is_updating_to_running_when_already_running(abacus_state, params): """Disallow updating to running state if already running.""" return ( params.get('action_status') == ACTION_STATUSES.RUNNING and abacus_state.action_status == ACTION_STATUSES.RUNNING )