Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/293-Update Operation Review Information Page #2390

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def suppress_django_request_logs(caplog):
class TestEndpointPermissions(TestCase):
client = Client()
mock_uuid = "e1300fd7-2dee-47d1-b655-2ad3fd10f052"
mock_version = '1'
mock_int = 1
endpoints_to_test = {
"authorized_roles": [
Expand Down Expand Up @@ -146,6 +147,7 @@ class TestEndpointPermissions(TestCase):
"endpoint_name": "remove_operation_representative",
"kwargs": {"operation_id": mock_uuid},
},
{"method": "get", "endpoint_name": "get_report_type_by_version", "kwargs": {'version_id': mock_version}},
],
"all_roles": [
{"method": "get", "endpoint_name": "get_reporting_year"},
Expand Down
1 change: 1 addition & 0 deletions bc_obps/reporting/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .report_person_responsible import get_report_person_responsible_by_version_id
from .report_person_responsible import save_report_contact
from .report_additional_data import get_registration_purpose_by_version_id
from .reports import get_report_type_by_version
from .gas_type import get_gas_type
from .emission_category import get_emission_category
from .production_data import save_production_data
Expand Down
16 changes: 15 additions & 1 deletion bc_obps/reporting/api/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from reporting.schema.report_operation import ReportOperationOut, ReportOperationIn
from reporting.schema.reporting_year import ReportingYearOut
from .router import router
from ..models import ReportingYear
from ..models import ReportingYear, ReportVersion
from ..schema.report_version import ReportingVersionOut


@router.post(
Expand Down Expand Up @@ -71,3 +72,16 @@ def save_report(
@handle_http_errors()
def get_reporting_year(request: HttpRequest) -> Tuple[Literal[200], ReportingYear]:
return 200, ReportingYearService.get_current_reporting_year()


@router.get(
"/report-version/{version_id}/report-type",
response={200: ReportingVersionOut, custom_codes_4xx: Message},
tags=EMISSIONS_REPORT_TAGS,
description="Retrieve the report type for a specific reporting version, including the reporting year and due date.",
auth=authorize("approved_industry_user"),
)
@handle_http_errors()
def get_report_type_by_version(request: HttpRequest, version_id: int) -> tuple[Literal[200], ReportVersion]:
report_type = ReportService.get_report_type_by_version_id(version_id)
return 200, report_type
18 changes: 18 additions & 0 deletions bc_obps/reporting/migrations/0032_reportversion_report_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.9 on 2024-10-17 21:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('reporting', '0031_alter_reportemission_managers'),
]

operations = [
migrations.AddField(
model_name='reportversion',
name='report_type',
field=models.CharField(db_comment='Report type', default='Annual Report', max_length=1000),
),
]
5 changes: 5 additions & 0 deletions bc_obps/reporting/models/report_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class ReportVersion(TimeStampedModel):
db_comment="True if this version is the latest submitted one",
default=False,
)
report_type = models.CharField(
max_length=1000,
db_comment="Report type",
default="Annual Report",
)

class ReportVersionStatus(models.TextChoices):
Draft = 'draft'
Expand Down
7 changes: 4 additions & 3 deletions bc_obps/reporting/schema/report_operation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ninja import ModelSchema
from ninja import ModelSchema, Schema
from reporting.models.report_operation import ReportOperation
from pydantic import alias_generators
from typing import List
Expand Down Expand Up @@ -33,7 +33,7 @@ class Meta:
]


class ReportOperationIn(ModelSchema):
class ReportOperationIn(Schema):
"""
Schema for the save report operation endpoint request input
"""
Expand All @@ -47,10 +47,10 @@ class ReportOperationIn(ModelSchema):
activities: List[str]
regulated_products: List[str]
operation_representative_name: str
operation_report_type: str

class Meta:
alias_generator = to_snake
model = ReportOperation
fields = [
'operator_legal_name',
'operator_trade_name',
Expand All @@ -61,4 +61,5 @@ class Meta:
'activities',
'regulated_products',
'operation_representative_name',
'operation_report_type',
]
13 changes: 13 additions & 0 deletions bc_obps/reporting/schema/report_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ninja import ModelSchema

from reporting.models import ReportVersion


class ReportingVersionOut(ModelSchema):
"""
Schema for the get reporting year endpoint request output
"""

class Meta:
model = ReportVersion
fields = ['report_type']
7 changes: 4 additions & 3 deletions bc_obps/reporting/tests/api/test_report_version_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_authorized_users_can_get_report_version(self):

# POST report-operation
def test_authorized_users_can_post_updates_to_report_version(self):
report_version = report_version_baker()
report_version = report_version_baker(report_type="Initial Report")

TestUtils.authorize_current_user_as_operator_user(self, operator=report_version.report.operator)

endpoint_under_test = f'/api/reporting/report-version/{report_version.id}/report-operation'
Expand All @@ -35,22 +36,22 @@ def test_authorized_users_can_post_updates_to_report_version(self):
"activities": [],
"regulated_products": [],
"operation_representative_name": "new operation representative name",
"operation_report_type": "Annual Report",
}

assert report_version.report_operation.operator_legal_name != data['operator_legal_name']
assert report_version.report_operation.operator_trade_name != data['operator_trade_name']
assert report_version.report_operation.operation_name != data['operation_name']
assert report_version.report_operation.operation_bcghgid != data['operation_bcghgid']
assert report_version.report_operation.bc_obps_regulated_operation_id != data['bc_obps_regulated_operation_id']
assert report_version.report_operation.operation_representative_name != data['operation_representative_name']
assert report_version.report_type != data['operation_report_type']

response = TestUtils.mock_post_with_auth_role(
self, 'industry_user', self.content_type, data, endpoint_under_test
)

assert response.status_code == 201
response_json = response.json()

assert response_json['operator_legal_name'] == data['operator_legal_name']
assert response_json['operator_trade_name'] == data['operator_trade_name']
assert response_json['operation_name'] == data['operation_name']
Expand Down
1 change: 1 addition & 0 deletions bc_obps/reporting/tests/models/test_report_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def setUpTestData(cls):
*TIMESTAMP_COMMON_FIELDS,
("id", "ID", None, None),
("report", "report", None, None),
("report_type", "report type", None, None),
("is_latest_submitted", "is latest submitted", None, None),
("status", "status", 1000, None),
("facility_reports", "facility report", None, 0),
Expand Down
9 changes: 8 additions & 1 deletion bc_obps/reporting/tests/utils/bakers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ def report_baker(**props) -> Report:
return baker.make(Report, **props)


def report_version_baker(**props) -> ReportVersion:
def report_version_baker(report_type="Annual Report", **props) -> ReportVersion:
# Ensure that a report is created if not provided
if "report" not in props and "report_id" not in props:
props["report"] = report_baker()

# Explicitly set report_type in props if not already provided
props.setdefault("report_type", report_type)

# Create the ReportVersion instance with given properties
version = baker.make(ReportVersion, **props)

# Ensure that ReportOperation is created and linked to ReportVersion
if "report_operation" not in props:
# You can set additional properties for ReportOperation here if needed
baker.make(ReportOperation, report_version=version)

return version
Expand Down
12 changes: 7 additions & 5 deletions bc_obps/service/report_additional_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ def get_registration_purpose_by_version_id(version_id: int) -> dict:
id=Report.objects.get(id=ReportVersion.objects.get(id=version_id).report_id).operation_id
)

# Use list comprehension to collect registration purposes directly
purposes_list = list(
RegistrationPurpose.objects.filter(operation=operation).values_list('registration_purpose', flat=True)
# Fetch the single registration purpose
registration_purpose = (
RegistrationPurpose.objects.filter(operation=operation)
.values_list('registration_purpose', flat=True)
.first()
)

# Always return the result as a dictionary with an array (empty or populated)
return {"registration_purposes": purposes_list}
# Return the result as a dictionary with a single purpose or None if not found
return {"registration_purpose": registration_purpose}

@staticmethod
def save_report_additional_data(version_id: int, data: ReportAdditionalDataIn) -> ReportAdditionalData:
Expand Down
10 changes: 9 additions & 1 deletion bc_obps/service/report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def get_report_operation_by_version_id(cls, report_version_id: int) -> ReportOpe
return ReportOperation.objects.get(report_version__id=report_version_id)

@classmethod
@transaction.atomic
def save_report_operation(cls, report_version_id: int, data: ReportOperationIn) -> ReportOperation:
# Fetch the existing report operation
report_operation = ReportOperation.objects.get(report_version__id=report_version_id)
Expand All @@ -98,7 +99,14 @@ def save_report_operation(cls, report_version_id: int, data: ReportOperationIn)

report_operation.activities.set(activities)
report_operation.regulated_products.set(regulated_products)
# Save the updated report operation
report_operation.save()

report_version = ReportVersion.objects.get(id=report_version_id)
report_version.report_type = data.operation_report_type
report_version.save()

return report_operation

@staticmethod
def get_report_type_by_version_id(version_id: int) -> ReportVersion:
return ReportVersion.objects.get(id=version_id)
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ export default async function AdditionalReportingData({
}: {
versionId: number;
}) {
const registrationPurposes =
(await getRegistrationPurpose(versionId))?.registration_purposes || [];
const includeElectricityGenerated = registrationPurposes.includes(
"OBPS Regulated Operation",
);
const registrationPurpose = (await getRegistrationPurpose(versionId))
?.registration_purpose;
const includeElectricityGenerated =
registrationPurpose === "OBPS Regulated Operation";

return (
<Suspense fallback={<Loading />}>
Expand Down
Loading