Skip to content

Commit

Permalink
Merge pull request #1636 from BLSQ/POLIO-1663-Reorder-lines-correct-o…
Browse files Browse the repository at this point in the history
…rder-to-match-VARs-with-their-PO

POLIO-1663: Supply Chain: Reorder lines so that summary table has the correct order to match VARs with their PO
  • Loading branch information
hakifran authored Sep 18, 2024
2 parents df016b2 + 87eadc7 commit fe381d4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
63 changes: 50 additions & 13 deletions plugins/polio/api/vaccines/supply_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,35 @@ class Meta:
"updated_at",
]

# comma separated list of all
def get_po_numbers(self, obj):
def get_prefetched_data(self, obj):
# Prefetch vaccineprea_lert and vaccinearrival_report to reduce the number of queries in the DB
pre_alerts = obj.vaccineprealert_set.all().order_by("-estimated_arrival_time")
if not pre_alerts:
return ""
return ", ".join([pre_alert.po_number for pre_alert in pre_alerts])
arrival_reports = obj.vaccinearrivalreport_set.filter(po_number__in=[p.po_number for p in pre_alerts]).order_by(
"-arrival_report_date"
)

# Get arrival reports matching by po_number
arrival_report_matching = {}
for report in arrival_reports:
if report.po_number not in arrival_report_matching:
arrival_report_matching[report.po_number] = []
arrival_report_matching[report.po_number].append(report)

return pre_alerts, arrival_report_matching

# Comma separated list of all
def get_po_numbers(self, obj):
pre_alerts, arrival_report_matching = self.get_prefetched_data(obj)

po_numbers = []
for pre_alert in pre_alerts:
matching_reports = arrival_report_matching.get(pre_alert.po_number, [])
if matching_reports:
for report in matching_reports:
po_numbers.append(str(report.po_number))
else:
po_numbers.append(pre_alert.po_number)
return ",".join(po_numbers)

def get_start_date(self, obj):
rounds = obj.rounds.all()
Expand All @@ -422,17 +445,31 @@ def get_doses_shipped(self, obj):

# Comma Separated List of all estimated arrival times
def get_eta(self, obj):
pre_alerts = obj.vaccineprealert_set.all().order_by("-estimated_arrival_time")
if not pre_alerts:
return ""
return ", ".join([str(pre_alert.estimated_arrival_time) for pre_alert in pre_alerts])
pre_alerts, arrival_report_matching = self.get_prefetched_data(obj)

estimated_arrival_dates = []
for pre_alert in pre_alerts:
matching_reports = arrival_report_matching.get(pre_alert.po_number, [])
if matching_reports:
for _ in matching_reports:
estimated_arrival_dates.append(str(pre_alert.estimated_arrival_time))
else:
estimated_arrival_dates.append(str(pre_alert.estimated_arrival_time))
return ",".join(estimated_arrival_dates)

# Comma Separated List of all arrival report dates
def get_var(self, obj):
arrival_reports = obj.vaccinearrivalreport_set.all().order_by("-arrival_report_date")
if not arrival_reports:
return ""
return ", ".join([str(report.arrival_report_date) for report in arrival_reports])
pre_alerts, arrival_report_matching = self.get_prefetched_data(obj)

arrival_report_dates = []
for pre_alert in pre_alerts:
matching_reports = arrival_report_matching.get(pre_alert.po_number, [])
if matching_reports:
for report in matching_reports:
arrival_report_dates.append(str(report.arrival_report_date))
else:
arrival_report_dates.append("")
return ",".join(arrival_report_dates)


class VRFCustomOrderingFilter(filters.BaseFilterBackend):
Expand Down
14 changes: 12 additions & 2 deletions plugins/polio/tests/test_vaccine_supply_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,23 @@ def test_var_field_ordered_reversely(self):
request_form = pm.VaccineRequestForm.objects.first()

# Create multiple VaccineArrivalReport instances with different dates
po_numbers = ["777777-1", "777777-2", "777777-3"]

dates = ["2024-04-20", "2024-04-19", "2024-04-18"]
for date in dates:
for i, date in enumerate(dates):
pm.VaccineArrivalReport.objects.create(
request_form=request_form,
po_number=po_numbers[i],
arrival_report_date=datetime.datetime.strptime(date, "%Y-%m-%d").date(),
doses_received=1000,
)
pm.VaccinePreAlert.objects.create(
date_pre_alert_reception=datetime.datetime.strptime(date, "%Y-%m-%d").date(),
request_form=request_form,
po_number=po_numbers[i],
estimated_arrival_time=datetime.datetime.strptime(date, "%Y-%m-%d").date(),
doses_shipped=100,
)

# Make a GET request to the list endpoint with ordering by start_date
response = self.client.get(
Expand All @@ -680,5 +690,5 @@ def test_var_field_ordered_reversely(self):
self.assertEqual(response.status_code, 200)

# Verify that the "var" field is a comma-separated list of dates in reverse order
expected_var = ", ".join(dates)
expected_var = ",".join(dates)
self.assertEqual(response.data["results"][1]["var"], expected_var)

0 comments on commit fe381d4

Please sign in to comment.