|
| 1 | +import logging |
| 2 | +import oracledb |
| 3 | +from utils.oracle.oracle import OracleDB |
| 4 | +from classes.repositories.general_repository import GeneralRepository |
| 5 | +from classes.repositories.database_transition_parameters import ( |
| 6 | + DatabaseTransitionParameters, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class CallAndRecallUtils: |
| 11 | + """ |
| 12 | + This contains utility methods to do with Call and Recall |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + self.oracledb = OracleDB() |
| 17 | + |
| 18 | + def run_failsafe(self, nhs_no: str) -> None: |
| 19 | + """ |
| 20 | + Run the failsafe trawl for the given NHS number. |
| 21 | + Args: |
| 22 | + nhs_no: The NHS number of the subject |
| 23 | + """ |
| 24 | + subject_id = int(self.oracledb.get_subject_id_from_nhs_number(nhs_no)) |
| 25 | + conn = self.oracledb.connect_to_db() |
| 26 | + conn.callTimeout = 30000 # Setting call timeout to 30 seconds |
| 27 | + cur = conn.cursor() |
| 28 | + |
| 29 | + pi = cur.var(oracledb.NUMBER) |
| 30 | + pi.setvalue(0, subject_id) |
| 31 | + |
| 32 | + out_cursor = cur.var(oracledb.CURSOR) |
| 33 | + |
| 34 | + cur.execute( |
| 35 | + """ |
| 36 | + BEGIN |
| 37 | + pkg_fobt_call.p_failsafe_trawl( |
| 38 | + pi_subject_id => :pi, |
| 39 | + po_cur_error => :po |
| 40 | + ); |
| 41 | + END;""", |
| 42 | + {"pi": str(subject_id), "po": out_cursor}, |
| 43 | + ) |
| 44 | + |
| 45 | + result_cursor = out_cursor.getvalue() |
| 46 | + row = result_cursor.fetchone() |
| 47 | + assert ( |
| 48 | + "The action was performed successfully" in row |
| 49 | + ), f"Error when executing failsafe for {nhs_no}: {row}" |
| 50 | + |
| 51 | + # Clean up |
| 52 | + result_cursor.close() |
| 53 | + cur.close() |
| 54 | + conn.close() |
| 55 | + logging.info(f"END: failsafe stored proc executed for {nhs_no}") |
| 56 | + |
| 57 | + def invite_subject_for_fobt_screening(self, nhs_no: str) -> None: |
| 58 | + """ |
| 59 | + Runs the database transition to 'invite' the subject for FOBT screening and create an FOBT episode. |
| 60 | + Uses OracleDB to execute the stored procedure. |
| 61 | + Args: |
| 62 | + nhs_no: The NHS number of the subject |
| 63 | + """ |
| 64 | + logging.info(f"START: invite_subject_for_fobt_screening for NHS No: {nhs_no}") |
| 65 | + |
| 66 | + try: |
| 67 | + # Prepare parameters for the stored procedure |
| 68 | + general_repository = GeneralRepository() |
| 69 | + database_transition_parameters = DatabaseTransitionParameters( |
| 70 | + transition_id=58, |
| 71 | + subject_id=int(self.oracledb.get_subject_id_from_nhs_number(nhs_no)), |
| 72 | + user_id=2, |
| 73 | + rollback_on_failure="Y", |
| 74 | + ) |
| 75 | + general_repository.run_database_transition(database_transition_parameters) |
| 76 | + except Exception as e: |
| 77 | + raise Exception( |
| 78 | + f"Error in invite_subject_for_fobt_screening for NHS No {nhs_no}: {e}" |
| 79 | + ) from e |
| 80 | + logging.info(f"END: invite_subject_for_fobt_screening for NHS No: {nhs_no}") |
0 commit comments