"""Logic for updating payment_group_payment payment_status state.""" from owsresponse import response from abacus_state.constants.constants import ( ACTION_STATUSES, PAYMENT_GROUP_PAYMENT_ACTION_NAMES, ) from abacus_state.constants.error import ERROR_PREVIOUS_ACTIONS_STATUS from abacus_state.models.abacus_state import AbacusState from abacus_state.schemas.abacus_state import AbacusStateDetailSchema from abacus_state.utils.format_error import validation_error ORDERED_ACTIONS = [ PAYMENT_GROUP_PAYMENT_ACTION_NAMES.CALCULATE_PAYMENTS, PAYMENT_GROUP_PAYMENT_ACTION_NAMES.GENERATE_PAYMENTS, PAYMENT_GROUP_PAYMENT_ACTION_NAMES.GENERATE_EXPORT, PAYMENT_GROUP_PAYMENT_ACTION_NAMES.UPLOAD_APPROVAL, PAYMENT_GROUP_PAYMENT_ACTION_NAMES.SEND_PAYMENTS, ] def update_payment_group_payment_state(abacus_state, **params): """Update payment_group_payment state.""" parent_table_id = abacus_state.parent_table_id actions = AbacusState.get_formatted_action_statuses( 'payment_group_payment', parent_table_id ) if not _are_previous_actions_completed(actions, abacus_state.action_name): return validation_error( ERROR_PREVIOUS_ACTIONS_STATUS.format(abacus_state.action_name) ) abacus_state.update_attributes(**params) AbacusState.commit_changes() return response.Response( message=AbacusStateDetailSchema().dump(abacus_state), status=200 ) def _are_previous_actions_completed(actions, current_action_name): """Validate payment group payments' earlier actions are completed or not.""" current_action_index = ORDERED_ACTIONS.index(current_action_name) validation_status = all( [ actions.get(action_name) == ACTION_STATUSES.COMPLETE for action_name in ORDERED_ACTIONS[:current_action_index] ] ) return validation_status