diff --git a/care/facility/models/facility.py b/care/facility/models/facility.py index aa929acc7c..d2a1db7360 100644 --- a/care/facility/models/facility.py +++ b/care/facility/models/facility.py @@ -2,7 +2,7 @@ from django.contrib.auth import get_user_model from django.contrib.postgres.fields import ArrayField from django.core.validators import MinValueValidator -from django.db import models +from django.db import models, transaction from django.db.models import IntegerChoices from django.db.models.constraints import CheckConstraint, UniqueConstraint from django.utils.translation import gettext_lazy as _ @@ -299,6 +299,18 @@ def save(self, *args, **kwargs) -> None: facility=self, user=self.created_by, created_by=self.created_by ) + @transaction.atomic + def delete(self, *args): + from care.facility.models.asset import Asset, AssetLocation + + AssetLocation.objects.filter(facility_id=self.id).update(deleted=True) + Asset.objects.filter( + current_location_id__in=AssetLocation._base_manager.filter( # noqa: SLF001 + facility_id=self.id + ).values_list("id", flat=True) + ).update(deleted=True) + return super().delete(*args) + @property def get_features_display(self): if not self.features: