-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4397 from unicef/staging
Staging back
- Loading branch information
Showing
6 changed files
with
162 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "frontend", | ||
"version": "2.12.0", | ||
"version": "2.12.1", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/hct_mis_api/one_time_scripts/remove_production_duplicates_after_enrollment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
|
||
from django.db.models import Count | ||
|
||
from hct_mis_api.apps.household.models import Household | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def remove_production_duplicates_after_enrollment() -> None: | ||
# Exceptions from the further rules | ||
for household in Household.objects.filter( | ||
id__in=[ | ||
"8b9bf768-4837-49aa-a598-5ad3c5822ca8", | ||
"33a7bdf0-650d-49b4-b333-c49a7eb05356", | ||
] | ||
): | ||
household.delete(soft=False) | ||
|
||
households_with_duplicates = ( | ||
Household.objects.values("unicef_id", "program") | ||
.annotate(household_count=Count("id")) | ||
.filter(household_count__gt=1) | ||
.order_by("copied_from__registration_data_import") | ||
) | ||
logger.info(f"Found {households_with_duplicates.count()} households with duplicates") | ||
|
||
for i, entry in enumerate(households_with_duplicates, 1): | ||
unicef_id = entry["unicef_id"] | ||
program = entry["program"] | ||
|
||
households = Household.objects.filter(unicef_id=unicef_id, program=program).order_by("created_at") | ||
|
||
# Keep the first household and delete the duplicates | ||
first = True | ||
households_to_remove = [] | ||
for household in households: | ||
if first: | ||
first = False | ||
continue | ||
if household.payment_set.exists(): | ||
logger.info(f"Skipping {household.id} because it has payments") | ||
continue | ||
else: | ||
households_to_remove.append(household) | ||
for duplicate in households_to_remove: | ||
duplicate.delete(soft=False) | ||
|
||
if i % 100 == 0: | ||
logger.info(f"Processed {i}/{households_with_duplicates.count()} households") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/unit/one_time_scripts/test_remove_production_duplicates_after_enrollment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from django.test import TestCase | ||
|
||
from hct_mis_api.apps.core.fixtures import create_afghanistan | ||
from hct_mis_api.apps.household.fixtures import create_household_and_individuals | ||
from hct_mis_api.apps.household.models import Household, Individual | ||
from hct_mis_api.apps.payment.fixtures import PaymentFactory | ||
from hct_mis_api.apps.program.fixtures import ProgramFactory | ||
from hct_mis_api.one_time_scripts.remove_production_duplicates_after_enrollment import ( | ||
remove_production_duplicates_after_enrollment, | ||
) | ||
|
||
|
||
class TestRemoveProductionDuplicatesAfterEnrollment(TestCase): | ||
def test_remove_production_duplicates_after_enrollment(self) -> None: | ||
business_area = create_afghanistan() | ||
program = ProgramFactory(business_area=business_area) | ||
program2 = ProgramFactory(business_area=business_area) | ||
hh_unicef_id = "HH-20-0000.0001" | ||
household_special_case1, individuals_special_case1 = create_household_and_individuals( | ||
household_data={ | ||
"id": "8b9bf768-4837-49aa-a598-5ad3c5822ca8", | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program.business_area, | ||
"program": program, | ||
}, | ||
individuals_data=[{}], | ||
) | ||
household_special_case2, individuals_special_case2 = create_household_and_individuals( | ||
household_data={ | ||
"id": "33a7bdf0-650d-49b4-b333-c49a7eb05356", | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program.business_area, | ||
"program": program, | ||
}, | ||
individuals_data=[{}], | ||
) | ||
household1, individuals1 = create_household_and_individuals( | ||
household_data={ | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program.business_area, | ||
"program": program, | ||
}, | ||
individuals_data=[{}, {}], | ||
) | ||
household2, individuals2 = create_household_and_individuals( | ||
household_data={ | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program.business_area, | ||
"program": program, | ||
}, | ||
individuals_data=[{}, {}], | ||
) | ||
household3, individuals3 = create_household_and_individuals( | ||
household_data={ | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program.business_area, | ||
"program": program, | ||
}, | ||
individuals_data=[{}], | ||
) | ||
PaymentFactory(household=household3) | ||
|
||
household4, individuals4 = create_household_and_individuals( | ||
household_data={ | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program.business_area, | ||
"program": program, | ||
}, | ||
individuals_data=[{}], | ||
) | ||
household_from_another_program, individuals_from_another_program = create_household_and_individuals( | ||
household_data={ | ||
"unicef_id": hh_unicef_id, | ||
"business_area": program2.business_area, | ||
"program": program2, | ||
}, | ||
individuals_data=[{}], | ||
) | ||
|
||
remove_production_duplicates_after_enrollment() | ||
|
||
self.assertIsNotNone(Household.all_objects.filter(id=household1.id).first()) | ||
self.assertIsNotNone(Individual.all_objects.filter(id=individuals1[0].id).first()) | ||
self.assertIsNotNone(Individual.all_objects.filter(id=individuals1[1].id).first()) | ||
|
||
self.assertIsNone(Household.all_objects.filter(id=household2.id).first()) | ||
self.assertIsNone(Individual.all_objects.filter(id=individuals2[0].id).first()) | ||
self.assertIsNone(Individual.all_objects.filter(id=individuals2[1].id).first()) | ||
|
||
self.assertIsNotNone(Individual.all_objects.filter(id=individuals3[0].id).first()) | ||
self.assertIsNotNone(Household.all_objects.filter(id=household3.id).first()) | ||
|
||
self.assertIsNone(Household.all_objects.filter(id=household4.id).first()) | ||
self.assertIsNone(Individual.all_objects.filter(id=individuals4[0].id).first()) | ||
|
||
self.assertIsNotNone(Household.all_objects.filter(id=household_from_another_program.id).first()) | ||
self.assertIsNotNone(Individual.all_objects.filter(id=individuals_from_another_program[0].id).first()) | ||
|
||
self.assertIsNone(Household.all_objects.filter(id=household_special_case1.id).first()) | ||
self.assertIsNone(Individual.all_objects.filter(id=individuals_special_case1[0].id).first()) | ||
|
||
self.assertIsNone(Household.all_objects.filter(id=household_special_case2.id).first()) | ||
self.assertIsNone(Individual.all_objects.filter(id=individuals_special_case2[0].id).first()) |