diff --git a/dear_petition/petition/api/tests/test_batch.py b/dear_petition/petition/api/tests/test_batch.py index 26c9004d..7e5b5bf6 100644 --- a/dear_petition/petition/api/tests/test_batch.py +++ b/dear_petition/petition/api/tests/test_batch.py @@ -13,6 +13,7 @@ OffenseFactory, ClientFactory, OffenseRecordFactory, + UserFactory, ) pytestmark = pytest.mark.django_db @@ -77,3 +78,29 @@ def test_adjust_for_new_client_dob(): batch.save() batch.adjust_for_new_client_dob() assert offense_record not in batch.underaged_conviction_records() + + +def test_populate_client_dob_from_batch_dob(api_client): + """ + Tests that when client dob is left empty, it is populated from the batch dob when + batch has an available dob value. + """ + # set up a user that will have access to the batch/client data + user = UserFactory() + api_client.force_authenticate(user=user) + + # set dob to use for the batch record and create batch/record + dob = datetime.date(1992, 1, 28) + batch = BatchFactory(user=user, client=None) + CIPRSRecordFactory(batch=batch, dob=dob) # add record with DOB to batch + + client = ClientFactory(user=user, dob=None) # add client with no DOB + assert client.dob == None + + # use API to assign the client to the batch + data = {"client_id": client.pk} + api_client.post(reverse("api:batch-assign-client-to-batch", args=[batch.pk]), data=data) + + # check that client dob updated appropriately during assignment + client.refresh_from_db() + assert client.dob == dob diff --git a/dear_petition/petition/api/viewsets.py b/dear_petition/petition/api/viewsets.py index 16dae2da..a7baa3ac 100644 --- a/dear_petition/petition/api/viewsets.py +++ b/dear_petition/petition/api/viewsets.py @@ -439,6 +439,9 @@ def assign_client_to_batch(self, request, pk): batch = self.get_object() batch.client = client batch.save() + if not client.dob and batch.dob: + client.dob = batch.dob + client.save() batch.adjust_for_new_client_dob() return Response({"batch_id": batch.pk}) diff --git a/dear_petition/petition/export/documents/records_summary.py b/dear_petition/petition/export/documents/records_summary.py index 8d107dd3..bbb3424c 100644 --- a/dear_petition/petition/export/documents/records_summary.py +++ b/dear_petition/petition/export/documents/records_summary.py @@ -31,7 +31,7 @@ def generate_summary(batch): def generate_context(batch, attorney, client): - dob = batch.dob + dob = client.dob if client.dob else batch.dob birthday_18th = "None" birthday_22nd = "None" diff --git a/dear_petition/petition/export/documents/tests/test_records_summary.py b/dear_petition/petition/export/documents/tests/test_records_summary.py index eb433173..a6a74266 100644 --- a/dear_petition/petition/export/documents/tests/test_records_summary.py +++ b/dear_petition/petition/export/documents/tests/test_records_summary.py @@ -419,6 +419,68 @@ def test_records_summary_context__birthdays( assert context["birthday_22nd"] == formatted_22nd_bday +@pytest.mark.parametrize( + "dob, formatted_dob, formatted_18th_bday, formatted_22nd_bday", + [ + (date(1986, 6, 15), "06/15/1986", "06/15/2004", "06/15/2008"), + # born in leap year + (date(1996, 2, 29), "02/29/1996", "03/01/2014", "03/01/2018"), + ], +) +def test_records_summary_context_no_batch_birthday( + batch, dob, formatted_dob, formatted_18th_bday, formatted_22nd_bday +): + """ + Test generate_context method for a batch that has no date of birth, but client info has + date of birth, (e.g. for portal imported batches) + """ + + PETITIONER_INFO_WITH_DOB = {"name": "Pete Petitioner", "dob": dob} + + offense = create_offense( + batch, "DURHAM", DISTRICT_COURT, "10CR000001", None, "NOT GUILTY", "JURY TRIAL", False + ) + create_offense_record(offense, CHARGED, "SIMPLE ASSAULT", "MISDEMEANOR") + + attorney = AttorneyFactory(name="E. Toruney") + client = ClientFactory(**PETITIONER_INFO_WITH_DOB) + context = generate_context(batch, attorney, client) + + assert context["dob"] == formatted_dob + assert context["birthday_18th"] == formatted_18th_bday + assert context["birthday_22nd"] == formatted_22nd_bday + + +def test_records_summary_context_birthdays_discrepancy(batch): + """ + Test generate_context method where client date of birth does not match batch date of birth. + Client date of birth should be used in this case. + """ + + client_dob, formatted_dob, formatted_18th_bday, formatted_22nd_bday = ( + date(1986, 6, 15), + "06/15/1986", + "06/15/2004", + "06/15/2008", + ) + batch_dob = date(1993, 5, 22) + + PETITIONER_INFO_WITH_DOB = {"name": "Pete Petitioner", "dob": client_dob} + + offense = create_offense( + batch, "DURHAM", DISTRICT_COURT, "10CR000001", batch_dob, "NOT GUILTY", "JURY TRIAL", False + ) + create_offense_record(offense, CHARGED, "SIMPLE ASSAULT", "MISDEMEANOR") + + attorney = AttorneyFactory(name="E. Toruney") + client = ClientFactory(**PETITIONER_INFO_WITH_DOB) + context = generate_context(batch, attorney, client) + + assert context["dob"] == formatted_dob + assert context["birthday_18th"] == formatted_18th_bday + assert context["birthday_22nd"] == formatted_22nd_bday + + def test_records_summary_context__additional_offenses(batch): """ Test generate_context method with many offense records in a table. Check that addl_offense_file_nos in the table has