diff --git a/lib/seattleflu/id3c/cli/command/etl/redcap_det_uw_reopening.py b/lib/seattleflu/id3c/cli/command/etl/redcap_det_uw_reopening.py index bc1452eea..5b77d94aa 100644 --- a/lib/seattleflu/id3c/cli/command/etl/redcap_det_uw_reopening.py +++ b/lib/seattleflu/id3c/cli/command/etl/redcap_det_uw_reopening.py @@ -125,7 +125,7 @@ def redcap_det_uw_reopening(*, db: DatabaseSession, cache: TTLCache, det: dict, return None patient_entry, patient_reference = create_patient(enrollment) - birthdate = datetime.strptime(enrollment['core_birthdate'], '%Y-%m-%d') + birthdate = parse_birth_date(enrollment) if not patient_entry: LOG.warning("Skipping record with insufficient information to construct patient") @@ -253,6 +253,17 @@ def redcap_det_uw_reopening(*, db: DatabaseSession, cache: TTLCache, det: dict, ) +def parse_birth_date(record: dict) -> Optional[datetime]: + """ Returns a participant's birth date from a given *record* as a datetime + object if it can be parsed. Otherwise, emits a warning and returns None. """ + try: + birth_date = datetime.strptime(record['core_birthdate'], '%Y-%m-%d') + except ValueError: + LOG.warning("Invalid `core_birthdate`.") + birth_date = None + + return birth_date + def create_site_reference(record: dict, collection_method: CollectionMethod, event_type: EventType) -> Optional[Dict[str,dict]]: """ Create a Location reference for site of the sample collection encounter based @@ -1147,8 +1158,13 @@ def create_computed_questionnaire_response(record: dict, patient_reference: dict For example, a computed question captures the participant's age on the date of the encounter. """ - - record['age'] = relativedelta(encounter_date, birthdate).years + # A birthdate of None will return a falsy relativedelta() object + delta = relativedelta(encounter_date, birthdate) + if not delta: + age = None + else: + age = delta.years + record['age'] = age integer_questions = [ 'age'