1010from utils .screening_subject_page_searcher import verify_subject_event_status_by_nhs_no
1111from utils .oracle .oracle_specific_functions import get_nhs_no_from_batch_id
1212from utils .oracle .oracle import OracleDB
13+ from utils .pdf_reader import extract_nhs_no_from_pdf
1314import os
1415import pytest
1516from playwright .sync_api import Page
1617import logging
18+ import pandas as pd
1719
1820
1921def batch_processing (
@@ -22,6 +24,7 @@ def batch_processing(
2224 batch_description : str ,
2325 latest_event_status : str ,
2426 run_timed_events : bool = False ,
27+ get_subjects_from_pdf : bool = False ,
2528) -> None :
2629 """
2730 This util is used to process batches. It expects the following inputs:
@@ -30,6 +33,7 @@ def batch_processing(
3033 - batch_description: This is the description of the batch. E.g. Pre-invitation (FIT)
3134 - latest_event_status: This is the status the subject will get updated to after the batch has been processed.
3235 - run_timed_events: This is an optional input that executes bcss_timed_events if set to True
36+ - get_subjects_from_pdf: This is an optial input to change the method of retrieving subjects from the batch from the Db to the PDF file.
3337 """
3438 logging .info (f"Processing { batch_type } - { batch_description } batch" )
3539 BasePage (page ).click_main_menu_link ()
@@ -39,8 +43,9 @@ def batch_processing(
3943
4044 batch_description_cells = page .locator (f"//td[text()='{ batch_description } ']" )
4145
42- if batch_description_cells .count () == 0 and page .locator (
43- "td" , has_text = "No matching records found"
46+ if (
47+ batch_description_cells .count () == 0
48+ and page .locator ("td" , has_text = "No matching records found" ).is_visible ()
4449 ):
4550 pytest .fail (f"No { batch_type } { batch_description } batch found" )
4651
@@ -55,41 +60,31 @@ def batch_processing(
5560 logging .info (
5661 f"Successfully found open '{ batch_type } - { batch_description } ' batch"
5762 )
58- try :
59- logging .info (
60- f"Attempting to get NHS Numbers for batch { link_text } from the DB"
61- )
62- nhs_no_df = get_nhs_no_from_batch_id (link_text )
63- logging .info (
64- f"Successfully retrieved NHS Numbers from batch { link_text } "
65- )
66- except Exception as e :
67- pytest .fail (
68- f"Failed to retrieve NHS Numbers from batch { link_text } , { str (e )} "
69- )
7063 link .click ()
7164 break
7265 elif (i + 1 ) == batch_description_cells .count ():
7366 pytest .fail (f"No open '{ batch_type } - { batch_description } ' batch found" )
7467
75- prepare_and_print_batch (page , link_text )
68+ if get_subjects_from_pdf :
69+ logging .info (f"Getting NHS Numbers for batch { link_text } from the PDF File" )
70+ nhs_no_df = prepare_and_print_batch (page , link_text , get_subjects_from_pdf )
71+ else :
72+ logging .info (f"Getting NHS Numbers for batch { link_text } from the DB" )
73+ prepare_and_print_batch (page , link_text , get_subjects_from_pdf )
74+ nhs_no_df = get_nhs_no_from_batch_id (link_text )
7675
7776 check_batch_in_archived_batch_list (page , link_text )
7877
7978 first_nhs_no = nhs_no_df ["subject_nhs_number" ].iloc [0 ]
80- try :
81- verify_subject_event_status_by_nhs_no (page , first_nhs_no , latest_event_status )
82- logging .info (
83- f"Successfully verified NHS number { first_nhs_no } with status { latest_event_status } "
84- )
85- except Exception as e :
86- pytest .fail (f"Verification failed for NHS number { first_nhs_no } : { str (e )} " )
79+ verify_subject_event_status_by_nhs_no (page , first_nhs_no , latest_event_status )
8780
8881 if run_timed_events :
8982 OracleDB ().exec_bcss_timed_events (nhs_no_df )
9083
9184
92- def prepare_and_print_batch (page : Page , link_text ) -> None :
85+ def prepare_and_print_batch (
86+ page : Page , link_text : str , get_subjects_from_pdf : bool = False
87+ ) -> pd .DataFrame | None :
9388 """
9489 This method prepares the batch, retreives the files and confirms them as printed
9590 Once those buttons have been pressed it waits for the message 'Batch Successfully Archived'
@@ -114,6 +109,11 @@ def prepare_and_print_batch(page: Page, link_text) -> None:
114109 file = download_file .suggested_filename
115110 # Wait for the download process to complete and save the downloaded file in a temp folder
116111 download_file .save_as (file )
112+ nhs_no_df = (
113+ extract_nhs_no_from_pdf (file )
114+ if file .endswith (".pdf" ) and get_subjects_from_pdf
115+ else None
116+ )
117117 os .remove (file ) # Deletes the file after extracting the necessary data
118118 except Exception as e :
119119 pytest .fail (f"No retrieve button available to click: { str (e )} " )
@@ -137,6 +137,8 @@ def prepare_and_print_batch(page: Page, link_text) -> None:
137137 except Exception as e :
138138 pytest .fail (f"Batch successfully archived message is not shown: { str (e )} " )
139139
140+ return nhs_no_df
141+
140142
141143def check_batch_in_archived_batch_list (page : Page , link_text ) -> None :
142144 """
0 commit comments