diff --git a/bc_obps/reporting/api/__init__.py b/bc_obps/reporting/api/__init__.py index 933b502a8d..5de81440eb 100644 --- a/bc_obps/reporting/api/__init__.py +++ b/bc_obps/reporting/api/__init__.py @@ -29,3 +29,4 @@ from .report_emission_allocations import get_emission_allocations, save_emission_allocation_data from .compliance_data import get_compliance_summary_data from .submit import submit_report_version +from .report_review_facilties import get_selected_facilities diff --git a/bc_obps/reporting/api/report_selected_facilties.py b/bc_obps/reporting/api/report_review_facilties.py similarity index 63% rename from bc_obps/reporting/api/report_selected_facilties.py rename to bc_obps/reporting/api/report_review_facilties.py index 46ff16fb88..21fa63c3dd 100644 --- a/bc_obps/reporting/api/report_selected_facilties.py +++ b/bc_obps/reporting/api/report_review_facilties.py @@ -1,32 +1,33 @@ from typing import Literal from uuid import UUID -from bc_obps.reporting.service.report_facilities_service import ReportFacilitiesService -from reporting.constants import EMISSIONS_REPORT_TAGS -from common.api.utils.current_user_utils import get_current_user_guid + from common.permissions import authorize +from reporting.constants import EMISSIONS_REPORT_TAGS from registration.decorators import handle_http_errors from service.error_service.custom_codes_4xx import custom_codes_4xx from django.http import HttpRequest from reporting.schema.generic import Message from .router import router +from ..schema.report_review_facility import ReportReviewFacilitySchemaOut +from ..service.report_facilities_service import ReportFacilitiesService + @router.get( - "report-version/{report_version_id}/selected-facilities", - response={200: list[UUID], custom_codes_4xx: Message}, + "report-version/{report_version_id}/review-facilities", + response={200: ReportReviewFacilitySchemaOut, custom_codes_4xx: Message}, tags=EMISSIONS_REPORT_TAGS, description="""Retrieves the list of selected facilities for a report version""", exclude_none=True, - auyh=authorize("approved_industry_user"), + auth=authorize("approved_industry_user"), ) @handle_http_errors() -def get_selected_facilities( - request: HttpRequest, report_version_id: int -) -> tuple[Literal[200], list[UUID]]: - response_data = ReportFacilitiesService.get_selected_facilities(report_version_id) +def get_selected_facilities(request: HttpRequest, report_version_id: int) -> tuple[int, dict]: + response_data = ReportFacilitiesService.get_all_facilities_for_review(report_version_id) return 200, response_data + @router.post( - "report-version/{report_version_id}/selected-facilities", + "report-version/{report_version_id}/review-facilities", response={200: int, custom_codes_4xx: Message}, tags=EMISSIONS_REPORT_TAGS, description="""Saves the list of selected facilities for a report version""", @@ -41,7 +42,6 @@ def save_selected_facilities( ReportFacilitiesService.save_selected_facilities( report_version_id, payload, - get_current_user_guid(request), ) return 200 diff --git a/bc_obps/reporting/models/report_selected_facility.py b/bc_obps/reporting/models/report_selected_facility.py deleted file mode 100644 index 9fe4ab5262..0000000000 --- a/bc_obps/reporting/models/report_selected_facility.py +++ /dev/null @@ -1,36 +0,0 @@ -from django.db import models -from registration.models.facility import Facility -from registration.models.time_stamped_model import TimeStampedModel -from reporting.models import ReportVersion - - -class ReportSelectedFacility(TimeStampedModel): - ''' - Model representing a the facilities selected for a report. - A report (each report version) may contain multiple facilities. - ''' - - facility = models.ForeignKey( - Facility, - on_delete=models.CASCADE, - db_comment="The facility selected to be included in the report", - related_name="report_selected_facilities", - ) - - report_version = models.ForeignKey( - ReportVersion, - on_delete=models.CASCADE, - db_comment="The report this facility is selected for", - related_name="reprot_selected_facilities", - ) - - class Meta: - db_table_comment = "A table to store the facilities selected for a report" - db_table = 'erc"."report_selected_facility' - app_label = 'reporting' - constraints = [ - models.UniqueConstraint( - fields=['report_version', 'facility_id'], - name="unique_selected_facility_per_facility_and_report_version", - ) - ] diff --git a/bc_obps/reporting/schema/report_review_facility.py b/bc_obps/reporting/schema/report_review_facility.py new file mode 100644 index 0000000000..d9b076eeac --- /dev/null +++ b/bc_obps/reporting/schema/report_review_facility.py @@ -0,0 +1,20 @@ +from uuid import UUID +from ninja import ModelSchema, Schema +from registration.models import FacilityDesignatedOperationTimeline + + +class FacilityDesignatedOperationForReviewReport(ModelSchema): + class Meta: + model = FacilityDesignatedOperationTimeline + fields = ('facility', 'end_date', 'status') + + facility__name: str + + +class ReportReviewFacilitySchemaOut(Schema): + """ + Schema for the get selected facilities endpoint response + """ + + selected_facilities: list[UUID] + available_facilities: list[FacilityDesignatedOperationForReviewReport] diff --git a/bc_obps/reporting/schema/report_selected_facility.py b/bc_obps/reporting/schema/report_selected_facility.py deleted file mode 100644 index feb10e9866..0000000000 --- a/bc_obps/reporting/schema/report_selected_facility.py +++ /dev/null @@ -1,10 +0,0 @@ -from uuid import UUID - - -class ReportSelectedFacilitySchemaOut(Schema): - """ - Schema for the get selected facilities endpoint response - """ - facility_id: UUID - is_selected: bool - is_current: bool diff --git a/bc_obps/reporting/service/report_facilities_service.py b/bc_obps/reporting/service/report_facilities_service.py index edfba601c8..941eb3f918 100644 --- a/bc_obps/reporting/service/report_facilities_service.py +++ b/bc_obps/reporting/service/report_facilities_service.py @@ -1,7 +1,7 @@ from django.db import transaction -from reporting.models.report_selected_facility import ReportSelectedFacility -from reporting.models import ReportVersion -from registration.models import Facility + +from reporting.models import ReportVersion, FacilityReport +from registration.models import Facility, FacilityDesignatedOperationTimeline from typing import List, Dict from uuid import UUID @@ -29,51 +29,74 @@ def get_report_facility_list_by_version_id(version_id: int) -> Dict[str, List[st ) return {"facilities": facilities_list} - + @classmethod @transaction.atomic def save_selected_facilities( cls, - version_id: int, - facility_list: List[UUID], - user_guid: UUID, - ) -> None: + version_id: int, + facility_list: list[UUID], + ) -> None: """ Save selected facility to report version. Args: version_id: The report version ID - facility_list: The facility IDs of the selected facilities - user_guid: The user GUID of the user making the save request + facility_list: The facility UUIDs of the selected facilities + """ + report_version = ReportVersion.objects.get(id=version_id) - """ - - # Delete existing selected facilities that are no longer selected - ReportSelectedFacility.objects.filter(report_version_id=version_id).exclude(facility_id__in=facility_list).delete() - - for facility_id in facility_list: - selected_facility_record, created = ReportSelectedFacility.objects.get_or_create( - report_version_id=version_id, - facility_id=facility_id, - ) - if created: - selected_facility_record.set_create_or_update(user_guid) + # Delete unselected facilities + FacilityReport.objects.filter(report_version=report_version).exclude(facility_id__in=facility_list).delete() + + # Bulk create new facilities that are not already in Facility Report + FacilityReport.objects.bulk_create( + [ + FacilityReport( + report_version=report_version, + facility=facility, + facility_name=facility.name, + facility_type=facility.type, + facility_bcghgid=str(facility.bcghg_id.id) if facility.bcghg_id else None, + ) + for facility in Facility.objects.filter( + id__in=set(facility_list) + - set( + FacilityReport.objects.filter(report_version=report_version).values_list( + 'facility_id', flat=True + ) + ) + ) + ] + ) @staticmethod @transaction.atomic - def get_selected_facilities( - version_id: int, - ) -> List[UUID]: + def get_all_facilities_for_review(version_id: int) -> dict: """ - Get selected facilities for a report version. + Get facilities associated with a report version, including selected and available facilities. Args: version_id: The report version ID Returns: - List of facility IDs + Dictionary with: + - "selected_facilities": List of selected facility details + - "available_facilities": List of all available facility details """ - return list(ReportSelectedFacility.objects.filter(report_version_id=version_id).values_list('facility_id', flat=True)) - - + selected_facilities = FacilityReport.objects.filter(report_version_id=version_id).values_list( + 'facility_id', flat=True + ) + + report_version = ReportVersion.objects.select_related('report__operation').get(id=version_id) + available_facilities = ( + FacilityDesignatedOperationTimeline.objects.filter(operation_id=report_version.report.operation.id) + .distinct() + .values('facility_id', 'facility__name', 'status', 'end_date') + ) + + return { + "selected_facilities": selected_facilities, + "available_facilities": available_facilities, + } diff --git a/bc_obps/reporting/tests/api/test_report_selected_facilities_api.py b/bc_obps/reporting/tests/api/test_report_selected_facilities_api.py deleted file mode 100644 index e69de29bb2..0000000000