Skip to content

Commit

Permalink
Add correlation ID to pilot data
Browse files Browse the repository at this point in the history
  • Loading branch information
steventux committed Oct 24, 2024
1 parent aea16d5 commit a5bb368
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/functions/process_pilot_data/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import requests
import dateutil.parser
import uuid

FIELDNAMES = ("nhs_number", "date_of_birth", "appointment_date", "appointment_time", "appointment_location", "appointment_type")
HEADERS = {
Expand All @@ -11,7 +12,7 @@
}


def process_data(raw_data):
def process_data(raw_data) -> str:
data = valid_csv_data(raw_data)
if not data:
logging.error("No valid data found")
Expand All @@ -27,37 +28,42 @@ def process_data(raw_data):
return response.text


def valid_csv_data(raw_data):
def valid_csv_data(raw_data) -> list:
data = []
try:
reader = csv.DictReader(raw_data, FIELDNAMES)
for row in reader:
if valid_row(row):
row["correlation_id"] = str(uuid.uuid4())
data.append(row)
except csv.Error:
logging.error("Invalid CSV data")
return []
return data


def valid_row(row):
def valid_row(row) -> bool:
return (
valid_nhs_number(row["nhs_number"]) and
valid_date_of_birth(row["date_of_birth"])
valid_date_or_time(row["date_of_birth"]) and
valid_date_or_time(row["appointment_date"]) and
valid_date_or_time(row["appointment_time"]) and
row["appointment_location"] and
row["appointment_type"]
)


def valid_nhs_number(nhs_number):
def valid_nhs_number(nhs_number: str) -> bool:
if not nhs_number:
return False
return len(nhs_number) == 10 and nhs_number.isdigit()


def valid_date_of_birth(date_of_birth):
if not date_of_birth:
def valid_date_or_time(val: str) -> bool:
if not val:
return False
try:
dateutil.parser.parse(date_of_birth)
dateutil.parser.parse(val)
return True
except ValueError:
return False
Expand Down
6 changes: 6 additions & 0 deletions src/functions/process_pilot_data/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
class TestHelper:
@pytest.fixture
def setup(self, monkeypatch):
monkeypatch.setattr(
"uuid.uuid4",
lambda: "00000000-0000-0000-0000-000000000000",
)
monkeypatch.setenv(
"NOTIFY_FUNCTION_URL",
"http://example.com/api/notify/message/send",
Expand All @@ -29,6 +33,7 @@ def test_process_data(self, setup):
"appointment_time": "10:00",
"appointment_location": "London",
"appointment_type": "Breast Screening",
"correlation_id": "00000000-0000-0000-0000-000000000000",
},
{
"nhs_number": "1111111111",
Expand All @@ -37,6 +42,7 @@ def test_process_data(self, setup):
"appointment_time": "11:00",
"appointment_location": "Croydon",
"appointment_type": "Breast Screening",
"correlation_id": "00000000-0000-0000-0000-000000000000",
},
],
}
Expand Down

0 comments on commit a5bb368

Please sign in to comment.