Skip to content

Commit

Permalink
chore: conditional verification routing
Browse files Browse the repository at this point in the history
chore: prettier

chore: update vitest

chore: prettier

chore: prettier

chore: prettier

chore: prettier
  • Loading branch information
shon-button committed Dec 17, 2024
1 parent 230398d commit 22a7c63
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class TestEndpointPermissions(TestCase):
"endpoint_name": "get_emission_allocations",
"kwargs": {"report_version_id": mock_int, "facility_id": mock_uuid},
},
{
"method": "get",
"endpoint_name": "get_attributable_emissions",
"kwargs": {"report_version_id": mock_int},
},
{"method": "post", "endpoint_name": "create_facilities"},
{"method": "post", "endpoint_name": "create_contact"},
{
Expand Down
2 changes: 1 addition & 1 deletion bc_obps/reporting/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
from .report_verification import get_report_verification_by_version_id, save_report_verification
from .report_attachments import save_report_attachments, get_report_attachments
from .report_emission_allocations import get_emission_allocations, save_emission_allocation_data
from .compliance_data import get_compliance_summary_data
from .compliance_data import get_compliance_summary_data, get_attributable_emissions
from .submit import submit_report_version
20 changes: 20 additions & 0 deletions bc_obps/reporting/api/compliance_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal
from typing import Literal, Tuple
from common.permissions import authorize
from typing import Literal, Tuple
from django.http import HttpRequest
Expand Down Expand Up @@ -25,3 +27,21 @@ def get_compliance_summary_data(
compliance_data = ComplianceService.get_calculated_compliance_data(report_version_id)

return 200, compliance_data


@router.get(
"report-version/{report_version_id}/attributable-emissions",
response={200: Decimal, custom_codes_4xx: Message},
tags=EMISSIONS_REPORT_TAGS,
description="""Retrieves the total attributable emissions for a given report version.""",
exclude_none=True,
auth=authorize("approved_industry_user"),
)
@handle_http_errors()
def get_attributable_emissions(request: HttpRequest, report_version_id: int) -> Tuple[Literal[200], Decimal | int]:
"""
Endpoint to retrieve the total emissions attributable for reporting.
"""
attributable_emissions = ComplianceService.get_emissions_attributable_for_reporting(report_version_id)

return 200, attributable_emissions
45 changes: 45 additions & 0 deletions bc_obps/reporting/tests/api/test_compliance_data_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from model_bakery import baker
from unittest.mock import patch, MagicMock
from registration.tests.utils.helpers import CommonTestSetup, TestUtils
from registration.utils import custom_reverse_lazy


class TestComplianceDataApi(CommonTestSetup):
def setup_method(self):
self.report_version = baker.make_recipe('reporting.tests.utils.report_version')
self.mock_emission = 54321.1234
super().setup_method()
TestUtils.authorize_current_user_as_operator_user(self, operator=self.report_version.report.operator)

"""Tests for the get_attributable_emissions endpoint."""

@patch(
"reporting.service.compliance_service.ComplianceService.get_emissions_attributable_for_reporting", autospec=True
)
def test_returns_attributable_emissions(
self,
mock_get_attributable_emissions: MagicMock,
):
# Arrange: Mock report version and report verification data
mock_get_attributable_emissions.return_value = self.mock_emission

# Act: Authorize user and perform GET request
response = TestUtils.mock_get_with_auth_role(
self,
"industry_user",
custom_reverse_lazy(
"get_attributable_emissions",
kwargs={"report_version_id": self.report_version.id},
),
)

# Assert: Verify the response status
assert response.status_code == 200

# Assert: Verify the service was called with the correct version ID
mock_get_attributable_emissions.assert_called_once_with(self.report_version.id)

# Assert: Validate the response data
response_json = response.json()

assert float(response_json) == self.mock_emission
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import { Suspense } from "react";
import ComplianceSummaryData from "@reporting/src/app/components/complianceSummary/ComplianceSummaryData";
import defaultPageFactory from "@bciers/components/nextPageFactory/defaultPageFactory";
import ComplianceSummaryPage from "@reporting/src/app/components/complianceSummary/ComplianceSummaryPage";

export default async function Page(router: any) {
return (
<>
<Suspense fallback="Loading Schema">
<ComplianceSummaryData
versionId={parseInt(router.params?.version_id)}
/>
</Suspense>
</>
);
}
export default defaultPageFactory(ComplianceSummaryPage);
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import { Suspense } from "react";
import ComplianceSummaryData from "@reporting/src/app/components/complianceSummary/ComplianceSummaryData";
import defaultPageFactory from "@bciers/components/nextPageFactory/defaultPageFactory";
import ComplianceSummaryPage from "@reporting/src/app/components/complianceSummary/ComplianceSummaryPage";

export default async function Page(router: any) {
return (
<>
<Suspense fallback="Loading Schema">
<ComplianceSummaryData
versionId={parseInt(router.params?.version_id)}
/>
</Suspense>
</>
);
}
export default defaultPageFactory(ComplianceSummaryPage);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ReportingStepButtons from "@bciers/components/form/components/ReportingSt

interface Props {
versionId: number;
needsVerification: boolean;
summaryFormData: {
attributableForReporting: string;
reportingOnlyEmission: string;
Expand All @@ -37,8 +38,9 @@ interface Props {
taskListElements: TaskListElement[];
}

const ComplianceSummary: React.FC<Props> = ({
const ComplianceSummaryForm: React.FC<Props> = ({
versionId,
needsVerification,
summaryFormData,
taskListElements,
}) => {
Expand All @@ -50,8 +52,10 @@ const ComplianceSummary: React.FC<Props> = ({
"Sign-off & Submit",
];

const backRef = `/reports/${versionId}/additional-reporting-data`;
const continueRef = `/reports/${versionId}/verification`;
const backUrl = `/reports/${versionId}/additional-reporting-data`;
const verificationUrl = `/reports/${versionId}/verification`;
const finalReviewUrl = `/reports/${versionId}/final-review`;
const continueUrl = needsVerification ? verificationUrl : finalReviewUrl;

return (
<Box sx={{ p: 3 }}>
Expand All @@ -67,8 +71,8 @@ const ComplianceSummary: React.FC<Props> = ({
formData={summaryFormData}
>
<ReportingStepButtons
backUrl={backRef}
continueUrl={continueRef}
backUrl={backUrl}
continueUrl={continueUrl}
saveButtonDisabled={true}
/>
</FormBase>
Expand All @@ -78,4 +82,4 @@ const ComplianceSummary: React.FC<Props> = ({
);
};

export default ComplianceSummary;
export default ComplianceSummaryForm;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";
import { actionHandler } from "@bciers/actions";
import ComplianceSummaryForm from "./ComplianceSummaryForm";
import { tasklistData } from "./TaskListElements";
import { HasReportVersion } from "@reporting/src/app/utils/defaultPageFactoryTypes";
import { getRegistrationPurpose } from "@reporting/src/app/utils/getRegistrationPurpose";
import { getAttributableEmissions } from "@reporting/src/app/utils/getAttributableEmissions";
import {
RegistrationPurposes,
regulatedOperationPurposes,
} from "@/registration/app/components/operations/registration/enums";

const getComplianceData = async (versionId: number) => {
return actionHandler(
`reporting/report-version/${versionId}/compliance-data`,
"GET",
`reporting/report-version/${versionId}/compliance-data`,
);
};
export default async function ComplianceSummaryPage({
version_id,
}: HasReportVersion) {
const complianceData = await getComplianceData(version_id);
//🔍 Check if reports need verification step...
let needsVerification = false;
//🔍 Check if registration purpose is OBPS Regulated Operation, Opt-in or New Entrants
const registrationPurpose = (await getRegistrationPurpose(version_id))
?.registration_purpose;
needsVerification = regulatedOperationPurposes.includes(
registrationPurpose as RegistrationPurposes,
);
if (
needsVerification === false &&
registrationPurpose === RegistrationPurposes.REPORTING_OPERATION
) {
//🔍 Check if the registration purpose is Reporting Operation AND their total emissions attributable for reporting threshold is = or > than 25,000 TCo2
const attributableEmissionThreshold = 25000000;
const attributableEmissions = await getAttributableEmissions(version_id);
needsVerification = attributableEmissions >= attributableEmissionThreshold;
}
return (
<ComplianceSummaryForm
versionId={version_id}
needsVerification={needsVerification}
summaryFormData={complianceData}
taskListElements={tasklistData}
/>
);
}
15 changes: 15 additions & 0 deletions bciers/apps/reporting/src/app/utils/getAttributableEmissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { actionHandler } from "@bciers/actions";

export async function getAttributableEmissions(versionId: number) {
const response = await actionHandler(
`reporting/report-version/${versionId}/attributable-emissions`,
"GET",
"",
);
if (response.error) {
throw new Error(
"We couldn't find the attributable emissions for this report.",
);
}
return response;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import ComplianceSummary from "@reporting/src/app/components/complianceSummary/ComplianceSummary";
import ComplianceSummaryForm from "@reporting/src/app/components/complianceSummary/ComplianceSummaryForm";
import { vi, Mock } from "vitest"; // If you are using Vitest for mocking

import { actionHandler } from "@bciers/actions";
Expand Down Expand Up @@ -38,7 +38,7 @@ const mockSummaryData = {
],
};

describe("ComplianceSummary", () => {
describe("ComplianceSummaryForm", () => {
const mockPush = vi.fn();

beforeEach(() => {
Expand Down Expand Up @@ -112,8 +112,9 @@ describe("ComplianceSummary", () => {

it("should render a back button that navigates to the additional information page", async () => {
render(
<ComplianceSummary
<ComplianceSummaryForm
versionId={1}
needsVerification={true}
summaryFormData={mockSummaryData}
taskListElements={[]}
/>,
Expand All @@ -130,10 +131,11 @@ describe("ComplianceSummary", () => {
);
});

it("should render a continue button that navigates to the signoff page", async () => {
it("should render a continue button that navigates to the verification page", async () => {
render(
<ComplianceSummary
<ComplianceSummaryForm
versionId={1}
needsVerification={true}
summaryFormData={mockSummaryData}
taskListElements={[]}
/>,
Expand All @@ -149,4 +151,25 @@ describe("ComplianceSummary", () => {

expect(mockPush).toHaveBeenCalledWith(`/reports/1/verification`);
});

it("should render a continue button that navigates to the final review page", async () => {
render(
<ComplianceSummaryForm
versionId={1}
needsVerification={false}
summaryFormData={mockSummaryData}
taskListElements={[]}
/>,
);

const button = screen.getByRole("button", {
name: /Continue/i,
});

expect(button).toBeVisible();

fireEvent.click(button);

expect(mockPush).toHaveBeenCalledWith(`/reports/1/final-review`);
});
});

0 comments on commit 22a7c63

Please sign in to comment.