def get_last_update_timestamp(self, nro_request_id): """Gets a datetime object that holds the last time and part of the NRO Request was modified :param nro_request_id: NRO request.request_id for the request we want to enquire about \ it DOES NOT use the nr_num, as that requires yet another %^&$# join and runs a \ couple of orders of magnitude slower. (really nice db design - NOT) :return: (datetime) the last time that any part of the request was altered :raise: (NROServicesError) with the error information set """ try: cursor = self.connection.cursor() cursor.execute(""" SELECT SYS_EXTRACT_UTC (cast(last_update as timestamp)) as last_update FROM req_instance_max_event WHERE request_id = :req_id""", req_id=nro_request_id) row = cursor.fetchone() if row: return row[0] return None except Exception as err: current_app.logger.error(err.with_traceback(None)) raise NROServicesError( { "code": "unable_to_get_timestamp", "description": "Unable to get the last timestamp for the NR in NRO" }, 500)
def test_nro_exception(): """checking that the error can be raised and that the parts get set correctly """ with pytest.raises(NROServicesError) as e_info: raise NROServicesError ({"code":"test_error", "description": "used to test the error functionality"} ,500) assert e_info.value.status_code == 500 assert e_info.value.error == {"code":"test_error", "description": "used to test the error functionality"}
def get_current_request_state(self, nro_nr_num): """Gets a datetime object that holds the last time and part of the NRO Request was modified :param nro_request_id: NRO request.request_id for the request we want to enquire about \ it DOES NOT use the nr_num, as that requires yet another %^&$# join and runs a \ couple of orders of magnitude slower. (really nice db design - NOT) :return: (datetime) the last time that any part of the request was altered :raise: (NROServicesError) with the error information set """ try: cursor = self.connection.cursor() cursor.execute(""" select rs.STATE_TYPE_CD from request_state rs join request r on rs.request_id=r.request_id where r.nr_num=:req_num and rs.end_event_id is NULL""", req_num=nro_nr_num) row = cursor.fetchone() if row: return row[0] return None except Exception as err: current_app.logger.error(err.with_traceback(None)) raise NROServicesError( { "code": "unable_to_get_request_state", "description": "Unable to get the current state of the NRO Request" }, 500)
def cancel_nr(self, nr, examiner_username): """Sets the status of the Request in NRO to "C" (Cancelled) :param nr: (obj) NR Object :param examiner_username: (str) any valid string will work, but it should be the username from Keycloak :return: naked :raise: (NROServicesError) with the error information set """ try: con = self.connection con.begin( ) # explicit transaction in case we need to do other things than just call the stored proc try: cursor = con.cursor() event_id = _get_event_id(cursor) current_app.logger.debug('got to cancel_nr() for NR:{}'.format( nr.nrNum)) current_app.logger.debug('event ID for NR:{}'.format(event_id)) _create_nro_transaction(cursor, nr, event_id, 'CANCL') # get request_state record, with all fields cursor.execute(""" SELECT * FROM request_state WHERE request_id = :request_id AND end_event_id IS NULL FOR UPDATE """, request_id=nr.requestId) row = cursor.fetchone() req_state_id = int(row[0]) # set the end event for the existing record cursor.execute(""" UPDATE request_state SET end_event_id = :event_id WHERE request_state_id = :req_state_id """, event_id=event_id, req_state_id=req_state_id) # create new request_state record cursor.execute( """ INSERT INTO request_state (request_state_id, request_id, state_type_cd, start_event_id, end_event_id, examiner_idir, examiner_comment, state_comment, batch_id) VALUES (request_state_seq.nextval, :request_id, :state, :event_id, NULL, :examiner_id, NULL, NULL, NULL) """, request_id=nr.requestId, state='C', event_id=event_id, examiner_id=nro_examiner_name(examiner_username)) con.commit() except cx_Oracle.DatabaseError as exc: err, = exc.args current_app.logger.error(err) if con: con.rollback() raise NROServicesError( { "code": "unable_to_set_state", "description": "Unable to set the state of the NR in NRO" }, 500) except Exception as err: current_app.logger.error(err.with_traceback(None)) if con: con.rollback() raise NROServicesError( { "code": "unable_to_set_state", "description": "Unable to set the state of the NR in NRO" }, 500) except Exception as err: # something went wrong, roll it all back current_app.logger.error(err.with_traceback(None)) if con: con.rollback() raise NROServicesError( { "code": "unable_to_set_state", "description": "Unable to set the state of the NR in NRO" }, 500) return None
def set_request_status_to_h(self, nr_num, examiner_username): """Sets the status of the Request in NRO to "H" :param nr_num: (str) the name request number, of the format "NR 9999999" :param examiner_username: (str) any valid string will work, but it should be the username from Keycloak :return: naked :raise: (NROServicesError) with the error information set """ try: con = self.connection con.begin( ) # explicit transaction in case we need to do other things than just call the stored proc try: cursor = con.cursor() # set the fqpn if the schema is required, which is set y the deployer/configurator # if the environment variable is missing from the Flask Config, then skip setting it. if current_app.config.get('NRO_SCHEMA'): func_name = '{}.nro_datapump_pkg.name_examination_func'.format( current_app.config.get('NRO_SCHEMA')) else: func_name = 'nro_datapump_pkg.name_examination_func' func_vars = [ nr_num, # p_nr_number 'H', # p_status '', # p_expiry_date - mandatory, but ignored by the proc '', # p_consent_flag- mandatory, but ignored by the proc nro_examiner_name(examiner_username), # p_examiner_id ] # Call the name_examination function to save complete decision data for a single NR # and get a return if all data was saved ret = cursor.callfunc(func_name, str, func_vars) if ret is not None: current_app.logger.error( 'name_examination_func failed, return message: {}'. format(ret)) raise NROServicesError( { "code": "unable_to_set_state", "description": ret }, 500) con.commit() except cx_Oracle.DatabaseError as exc: error, = exc.args current_app.logger.error( "NR#: %s Oracle-Error-Code: %s Oracle-Error-Message: %s", nr_num, error.code, error.message) if con: con.rollback() raise NROServicesError( { "code": "unable_to_set_state", "description": "Unable to set the state of the NR in NRO" }, 500) except Exception as err: current_app.logger.error("NR#:", nr_num, err.with_traceback(None)) if con: con.rollback() raise NROServicesError( { "code": "unable_to_set_state", "description": "Unable to set the state of the NR in NRO" }, 500) except Exception as err: # something went wrong, roll it all back current_app.logger.error("NR#:", nr_num, err.with_traceback(None)) raise NROServicesError( { "code": "unable_to_set_state", "description": "Unable to set the state of the NR in NRO" }, 500) return None