Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from registration.schema.generic import Message
from compliance.constants import BCCR
from compliance.service.bc_carbon_registry.account_service import BCCarbonRegistryAccountService
from compliance.service.bc_carbon_registry.exceptions import BCCarbonRegistryError
from compliance.api.permissions import approved_industry_user_compliance_report_version_composite_auth

bccr_service = BCCarbonRegistryAccountService()
Expand All @@ -20,7 +21,12 @@
)
def get_bccr_account_details(
request: HttpRequest, account_id: FifteenDigitString, compliance_report_version_id: int
) -> Tuple[Literal[200], Dict[str, Optional[str]]]:
account_details = bccr_service.get_account_details(account_id=account_id)
trading_name = getattr(account_details, "trading_name", None) if account_details else None
return 200, {"bccr_trading_name": trading_name}
) -> Tuple[Literal[200], Dict[str, Optional[str | bool]]]:
try:
account_details = bccr_service.get_account_details(account_id=account_id)
trading_name = getattr(account_details, "trading_name", None) if account_details else None
return 200, {"bccr_trading_name": trading_name, "has_remote_bccr_errors": False}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 200, {"bccr_trading_name": trading_name, "has_remote_bccr_errors": False}
return 200, {"bccr_trading_name": trading_name}

We can probably omit that flag, it's already optional on the frontend. Just less code!


except BCCarbonRegistryError:
# Handle exceptions that come from BCCR API
return 200, {"bccr_trading_name": None, "has_remote_bccr_errors": True}
19 changes: 10 additions & 9 deletions bc_obps/compliance/tests/api/_bccr/_accounts/test_account_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_successful_account_details_retrieval(self, mock_permission, mock_servic
# Assert
mock_service.assert_called_once_with(account_id=VALID_ACCOUNT_ID)
assert response.status_code == 200
assert response.json() == {"bccr_trading_name": "Test Account Inc."}
assert response.json() == {"bccr_trading_name": "Test Account Inc.", "has_remote_bccr_errors": False}

@patch(VALIDATE_PERMISSION_PATH)
def test_invalid_account_id_format(self, mock_permission):
Expand All @@ -68,7 +68,7 @@ def test_empty_account_details_response(self, mock_permission, mock_service):
response = self.client.get(self._get_endpoint_url(VALID_ACCOUNT_ID, COMPLIANCE_REPORT_VERSION_ID))
# Assert
assert response.status_code == 200
assert response.json() == {"bccr_trading_name": None}
assert response.json() == {"bccr_trading_name": None, "has_remote_bccr_errors": False}

@patch(BCCR_SERVICE_PATH)
@patch(VALIDATE_PERMISSION_PATH)
Expand All @@ -79,12 +79,12 @@ def test_service_error_handling(self, mock_permission, mock_service):
# Act
response = self.client.get(self._get_endpoint_url(VALID_ACCOUNT_ID, COMPLIANCE_REPORT_VERSION_ID))
# Assert
message = "The system cannot connect to the external application. Please try again later. If the problem persists, contact GHGRegulator@gov.bc.ca for help."
assert_error_response(
response,
status_code=400,
message=message,
)
assert response.status_code == 200

response_json = response.json()

assert response_json["bccr_trading_name"] is None
assert response_json["has_remote_bccr_errors"] is True

@patch(BCCR_SERVICE_PATH)
@patch(VALIDATE_PERMISSION_PATH)
Expand All @@ -104,7 +104,8 @@ def test_account_details_with_null_type_of_account_holder(self, mock_permission,
mock_service.assert_called_once_with(account_id=VALID_ACCOUNT_ID)
assert response.status_code == 200
assert response.json() == {
"bccr_trading_name": "Test Account Inc."
"bccr_trading_name": "Test Account Inc.",
"has_remote_bccr_errors": False,
} # Should still work with null type_of_account_holder

@patch(BCCR_SERVICE_PATH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ const WRONG_ACCOUNT_TYPE_MESSAGE = (
</span>
);

const REMOTE_BCCR_ISSUE_MESSAGE = (
<span className="text-bc-error-red">
Remote BC Carbon Registry system issues, please try again later or contact{" "}
<a href={ghgRegulatorEmail} className="text-bc-link-blue hover:underline">
GHGRegulator@gov.bc.ca
</a>{" "}
if you have any questions.
</span>
);

const BccrHoldingAccountWidget = (props: WidgetProps) => {
const { id, value, disabled, readonly, onChange, registry } = props;
const { formContext } = registry;
Expand Down Expand Up @@ -66,7 +76,12 @@ const BccrHoldingAccountWidget = (props: WidgetProps) => {
complianceReportVersionId,
);

if (response?.bccr_trading_name === null) {
if (response?.has_remote_bccr_errors) {
setIsValid(false);
setShowError(true);
setErrorMessage(REMOTE_BCCR_ISSUE_MESSAGE);
onValidAccountResolved?.(undefined);
} else if (response?.bccr_trading_name === null) {
setIsValid(false);
setShowError(true);
setErrorMessage(INVALID_ACCOUNT_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ describe("BccrHoldingAccountWidget", () => {
});
});

it("shows error message for remote BCCR API errors", async () => {
render(<BccrHoldingAccountWidget {...defaultProps} />);
mockValidateBccrAccount.mockResolvedValueOnce({
bccr_trading_name: null,
has_remote_bccr_errors: true,
});
const input = screen.getByRole("textbox");

fireEvent.change(input, { target: { value: "123456789012345" } });

await waitFor(() => {
expect(input).toHaveAttribute("aria-invalid", "true");
expect(
screen.getByText(
/Remote BC Carbon Registry system issues, please try again later or contact/i,
),
).toBeVisible();
expect(
screen.getByRole("link", {
name: /ghgregulator@gov\.bc\.ca/i,
}),
).toHaveAttribute("href", "mailto:GHGRegulator@gov.bc.ca");
});
});

it("renders input with correct help text", () => {
render(<BccrHoldingAccountWidget {...defaultProps} />);
expect(screen.getByText(/no account\? in bccr\./i)).toBeVisible();
Expand Down
Loading