Skip to content

Commit

Permalink
chore:added api, service and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshmcg committed Jan 17, 2025
1 parent 0f9defa commit e7e895f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 88 deletions.
1 change: 1 addition & 0 deletions bc_obps/reporting/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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""",
Expand All @@ -41,7 +42,6 @@ def save_selected_facilities(
ReportFacilitiesService.save_selected_facilities(
report_version_id,
payload,
get_current_user_guid(request),
)

return 200
36 changes: 0 additions & 36 deletions bc_obps/reporting/models/report_selected_facility.py

This file was deleted.

20 changes: 20 additions & 0 deletions bc_obps/reporting/schema/report_review_facility.py
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 0 additions & 10 deletions bc_obps/reporting/schema/report_selected_facility.py

This file was deleted.

83 changes: 53 additions & 30 deletions bc_obps/reporting/service/report_facilities_service.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
}
Empty file.

0 comments on commit e7e895f

Please sign in to comment.