Skip to content

Commit

Permalink
adding defaults to pds model and changing LG validator
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Apr 25, 2024
1 parent 91b2159 commit 754982b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lambdas/models/pds_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class Address(BaseModel):
model_config = conf

use: str
period: Period
period: Optional[Period] = None
postal_code: Optional[str] = ""


class Name(BaseModel):
use: str
period: Period
period: Optional[Period] = None
given: list[str]
family: str

Expand All @@ -36,14 +36,14 @@ class Meta(BaseModel):


class GPIdentifier(BaseModel):
system: Optional[str]
system: Optional[str] = ""
value: str
period: Optional[Period]
period: Optional[Period] = None


class GeneralPractitioner(BaseModel):
id: Optional[str]
type: Optional[str]
id: Optional[str] = ""
type: Optional[str] = ""
identifier: GPIdentifier


Expand Down
5 changes: 5 additions & 0 deletions lambdas/tests/unit/helpers/data/pds/pds_patient_response.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

PDS_PATIENT = {
"resourceType": "Patient",
"id": "9000000009",
Expand Down Expand Up @@ -524,3 +526,6 @@
},
},
}

PDS_PATIENT_WITH_MIDDLE_NAME = copy.deepcopy(PDS_PATIENT)
PDS_PATIENT_WITH_MIDDLE_NAME["name"][0]["given"].append("Jake")
35 changes: 32 additions & 3 deletions lambdas/tests/unit/utils/test_lloyd_george_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from models.pds_models import Patient
from services.document_service import DocumentService
from tests.unit.conftest import TEST_NHS_NUMBER
from tests.unit.helpers.data.pds.pds_patient_response import PDS_PATIENT
from tests.unit.helpers.data.pds.pds_patient_response import PDS_PATIENT, PDS_PATIENT_WITH_MIDDLE_NAME
from tests.unit.models.test_document_reference import MOCK_DOCUMENT_REFERENCE
from utils.exceptions import (
PatientRecordAlreadyExistException,
Expand Down Expand Up @@ -259,11 +259,40 @@ def test_mismatch_nhs_id_no_pds_service(mocker):
with pytest.raises(LGInvalidFilesException):
validate_lg_file_names(lg_file_list, "9000000009")

def test_order_names_with_pds_service(mocker):
lg_file_list = [
"1of2_Lloyd_George_Record_[Jake Jane Smith]_[9000000009]_[22-10-2010].pdf",
"2of2_Lloyd_George_Record_[Jake Jane Smith]_[9000000009]_[22-10-2010].pdf",
]
patient = Patient.model_validate(PDS_PATIENT_WITH_MIDDLE_NAME)
patient_details = patient.get_minimum_patient_details("9000000009")
mock_odc_code = mocker.patch("utils.lloyd_george_validator.get_user_ods_code")
mock_odc_code.return_value = "Y12345"
try:
validate_with_pds_service(lg_file_list, patient_details)
except LGInvalidFilesException:
assert False


def test_missing_middle_name_names_with_pds_service(mocker):
lg_file_list = [
"1of2_Lloyd_George_Record_[Jane Smith]_[9000000009]_[22-10-2010].pdf",
"2of2_Lloyd_George_Record_[Jane Smith]_[9000000009]_[22-10-2010].pdf",
]
patient = Patient.model_validate(PDS_PATIENT_WITH_MIDDLE_NAME)
patient_details = patient.get_minimum_patient_details("9000000009")
mock_odc_code = mocker.patch("utils.lloyd_george_validator.get_user_ods_code")
mock_odc_code.return_value = "Y12345"

try:
validate_with_pds_service(lg_file_list, patient_details)
except LGInvalidFilesException:
assert False

def test_mismatch_name_with_pds_service(mocker, mock_pds_patient_details):
lg_file_list = [
"1of2_Lloyd_George_Record_[Jane Plain Smith]_[9000000009]_[22-10-2010].pdf",
"2of2_Lloyd_George_Record_[Jane Plain Smith]_[9000000009]_[22-10-2010].pdf",
"1of2_Lloyd_George_Record_[Rob Plain Smith]_[9000000009]_[22-10-2010].pdf",
"2of2_Lloyd_George_Record_[Rob Plain Smith]_[9000000009]_[22-10-2010].pdf",
]
mock_odc_code = mocker.patch("utils.lloyd_george_validator.get_user_ods_code")

Expand Down
22 changes: 14 additions & 8 deletions lambdas/utils/lloyd_george_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,27 @@ def validate_with_pds_service(
try:
file_name_info = extract_info_from_filename(file_name_list[0])

patient_name = file_name_info["patient_name"]
file_patient_name = file_name_info["patient_name"]
date_of_birth = file_name_info["date_of_birth"]

date_of_birth = datetime.datetime.strptime(date_of_birth, "%d-%m-%Y").date()
if patient_details.birth_date != date_of_birth:
raise LGInvalidFilesException("Patient DoB does not match our records")
patient_full_name = (
" ".join([name for name in patient_details.given_Name])
+ " "
+ patient_details.family_name
)
patient_name_split = file_patient_name.split(" ")
file_patient_first_name = patient_name_split[0]
file_patient_last_name = patient_name_split[-1]

logger.info("Verifying patient name against the record in PDS...")

if not names_are_matching(patient_name, patient_full_name):
raise LGInvalidFilesException("Patient name does not match our records")
is_file_first_name_in_patient_details = False
for patient_name in patient_details.given_Name:
if names_are_matching(file_patient_first_name, patient_name):
is_file_first_name_in_patient_details = True
break

if not is_file_first_name_in_patient_details or not names_are_matching(
file_patient_last_name, patient_details.family_name
): raise LGInvalidFilesException("Patient name does not match our records")

current_user_ods = get_user_ods_code()
if patient_details.general_practice_ods != current_user_ods:
Expand Down

0 comments on commit 754982b

Please sign in to comment.