diff --git a/backend/hitas/models/housing_company.py b/backend/hitas/models/housing_company.py index e9dfa900c..e701bcb32 100644 --- a/backend/hitas/models/housing_company.py +++ b/backend/hitas/models/housing_company.py @@ -279,6 +279,24 @@ def release_date(self) -> Optional[datetime.date]: # Release date was not found return None + @property + def property_manager_changed_at(self) -> Optional[datetime.datetime]: + latest_logentry: LogEntry = ( + LogEntry.objects.get_for_object(self) + .annotate(changes_as_json=Cast("changes", output_field=models.JSONField())) + .filter( + action__in=[LogEntry.Action.UPDATE, LogEntry.Action.CREATE], + changes_as_json__property_manager__isnull=False, + ) + # Ignore cases where property manager was not actually changed + .exclude(changes_as_json__property_manager__0=F("changes_as_json__property_manager__1")) + .order_by("-timestamp", "-id") + .first() + ) + if latest_logentry is not None: + return latest_logentry.timestamp + return None + class Meta: verbose_name = _("Housing company") verbose_name_plural = _("Housing companies") diff --git a/backend/hitas/tests/apis/test_api_housing_company.py b/backend/hitas/tests/apis/test_api_housing_company.py index 12144d5d5..3b58e46db 100644 --- a/backend/hitas/tests/apis/test_api_housing_company.py +++ b/backend/hitas/tests/apis/test_api_housing_company.py @@ -364,6 +364,7 @@ def test__api__housing_company__retrieve(api_client: HitasAPIClient, apt_with_nu "email": hc1.property_manager.email, "modified_at": serializers.DateTimeField().to_representation(hc1.property_manager.modified_at), }, + "property_manager_changed_at": serializers.DateTimeField().to_representation(hc1.property_manager_changed_at), "acquisition_price": float(hc1.acquisition_price), "primary_loan": float(hc1.primary_loan), "sales_price_catalogue_confirmation_date": str(hc1.sales_price_catalogue_confirmation_date), diff --git a/backend/hitas/views/housing_company.py b/backend/hitas/views/housing_company.py index 48c685014..f1489857b 100644 --- a/backend/hitas/views/housing_company.py +++ b/backend/hitas/views/housing_company.py @@ -197,6 +197,7 @@ class HousingCompanyDetailSerializer(EnumSupportSerializerMixin, HitasModelSeria building_type = ReadOnlyBuildingTypeSerializer() developer = ReadOnlyDeveloperSerializer() property_manager = ReadOnlyPropertyManagerSerializer(allow_null=True, required=False) + property_manager_changed_at = serializers.DateTimeField(read_only=True) acquisition_price = HitasDecimalField() notes = serializers.CharField(required=False, allow_blank=True) archive_id = serializers.IntegerField(source="id", read_only=True) @@ -348,6 +349,7 @@ class Meta: "building_type", "developer", "property_manager", + "property_manager_changed_at", "acquisition_price", "primary_loan", "sales_price_catalogue_confirmation_date", diff --git a/backend/openapi.yaml b/backend/openapi.yaml index 730984abc..e33c2f028 100644 --- a/backend/openapi.yaml +++ b/backend/openapi.yaml @@ -5652,6 +5652,13 @@ components: property_manager: nullable: true $ref: "#/components/schemas/ReadOnlyPropertyManager" + property_manager_changed_at: + description: When the property manager was last changed + type: string + nullable: true + readOnly: true + format: date-time + example: 1985-12-10T00:00:00+00:00 summary: description: Summary data for this housing company across all apartments type: object diff --git a/frontend/src/common/schemas/housingCompany.ts b/frontend/src/common/schemas/housingCompany.ts index fff66b212..4daf2e4a4 100644 --- a/frontend/src/common/schemas/housingCompany.ts +++ b/frontend/src/common/schemas/housingCompany.ts @@ -105,6 +105,7 @@ export const HousingCompanyDetailsSchema = object({ building_type: CodeSchema, developer: CodeSchema, property_manager: PropertyManagerSchema.nullable(), + property_manager_changed_at: string().optional(), acquisition_price: number(), primary_loan: number().optional(), sales_price_catalogue_confirmation_date: string().nullable(), diff --git a/frontend/src/features/housingCompany/HousingCompanyDetailsPage.tsx b/frontend/src/features/housingCompany/HousingCompanyDetailsPage.tsx index ebde3885d..fe1262111 100644 --- a/frontend/src/features/housingCompany/HousingCompanyDetailsPage.tsx +++ b/frontend/src/features/housingCompany/HousingCompanyDetailsPage.tsx @@ -109,10 +109,14 @@ const LoadedHousingCompanyDetails = (): React.JSX.Element => { )} {housingCompany.property_manager ? (
- Päivitetty viimeksi:{" "} + Vaihdettu:{" "} {housingCompany.property_manager.modified_at ? formatDate(housingCompany.property_manager.modified_at) : "tieto puuttuu"} + , muokattu:{" "} + {housingCompany.property_manager_changed_at + ? formatDate(housingCompany.property_manager_changed_at) + : "tieto puuttuu"}
) : null}