Skip to content

Commit

Permalink
Merge pull request #1570 from BLSQ/WC2-476-Exclude-beneficiary-with-r…
Browse files Browse the repository at this point in the history
…egistration-only

WC2-476: ETL: Exclude beneficiary with registration only
  • Loading branch information
butofleury authored Aug 22, 2024
2 parents 261f678 + ec19423 commit de4aeab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plugins/wfp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def calculate_birth_date(self, current_record):
age = current_record.get("age__int__", None)
registration_date = current_record.get("registration_date", None)
calculated_date = None
if age_entry is not None and age_entry != "":
if (age_entry is not None and age_entry != "") and (age is not None and age != ""):
beneficiary_age = int(age)
registered_at = datetime.strptime(registration_date[:10], "%Y-%m-%d").date()
if age_entry == "years":
Expand Down
14 changes: 6 additions & 8 deletions plugins/wfp/management/commands/wfp_etl_Under5.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def group_visit_by_entity(self, entities):
i = i + 1
return list(
filter(
lambda instance: (instance.get("visits") and len(instance.get("visits")) > 0)
lambda instance: (instance.get("visits") and len(instance.get("visits")) > 1)
and instance.get("gender") is not None
and instance.get("birth_date") is not None
and instance.get("birth_date") != ""
Expand Down Expand Up @@ -192,23 +192,21 @@ def run(self):
logger.info(
f"---------------------------------------- Beneficiary N° {(index+1)} {instance['entity_id']}-----------------------------------"
)
instance["journey"] = self.journeyMapper(instance["visits"])
beneficiary = Beneficiary()
if instance["entity_id"] not in existing_beneficiaries:
if instance["entity_id"] not in existing_beneficiaries and len(instance["journey"][0]["visits"]) > 0:
beneficiary.gender = instance["gender"]
beneficiary.birth_date = instance["birth_date"]
beneficiary.entity_id = instance["entity_id"]
beneficiary.save()
logger.info(f"Created new beneficiary")
else:
beneficiary = Beneficiary.objects.get(entity_id=instance["entity_id"])
instance["journey"] = self.journeyMapper(instance["visits"])
beneficiary = Beneficiary.objects.filter(entity_id=instance["entity_id"]).first()

logger.info("Retrieving journey linked to beneficiary")

for journey_instance in instance["journey"]:
if (
len(journey_instance["visits"]) > 0
and journey_instance.get("nutrition_programme", None) is not None
):
if len(journey_instance["visits"]) > 0:
journey = self.save_journey(beneficiary, journey_instance)
visits = ETL().save_visit(journey_instance["visits"], journey)
logger.info(f"Inserted {len(visits)} Visits")
Expand Down
16 changes: 10 additions & 6 deletions plugins/wfp/management/commands/wfp_etl_pbwg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@ def run(self):
logger.info(
f"---------------------------------------- Beneficiary N° {(index+1)} {instance['entity_id']}-----------------------------------"
)
instance["journey"] = self.journeyMapper(instance["visits"])
beneficiary = Beneficiary()
if instance["entity_id"] not in existing_beneficiaries:
if instance["entity_id"] not in existing_beneficiaries and len(instance["journey"][0]["visits"]) > 0:
beneficiary.gender = ""
beneficiary.entity_id = instance["entity_id"]
if instance.get("birth_date") is not None:
beneficiary.birth_date = instance["birth_date"]
beneficiary.save()
logger.info(f"Created new beneficiary")
else:
beneficiary = Beneficiary.objects.get(entity_id=instance["entity_id"])

instance["journey"] = self.journeyMapper(instance["visits"])
beneficiary = Beneficiary.objects.filter(entity_id=instance["entity_id"]).first()

logger.info("Retrieving journey linked to beneficiary")

for journey_instance in instance["journey"]:
if len(journey_instance["visits"]) > 0 and journey_instance.get("nutrition_programme") is not None:
if len(journey_instance["visits"]) > 0:
journey = self.save_journey(beneficiary, journey_instance)
visits = ETL().save_visit(journey_instance["visits"], journey)
logger.info(f"Inserted {len(visits)} Visits")
Expand Down Expand Up @@ -154,4 +153,9 @@ def group_visit_by_entity(self, entities):
current_record["form_id"] = form_id
instances[i]["visits"].append(current_record)
i = i + 1
return instances
return list(
filter(
lambda instance: (instance.get("visits") and len(instance.get("visits")) > 1),
instances,
)
)

0 comments on commit de4aeab

Please sign in to comment.