Skip to content

Commit

Permalink
Merge pull request #2500 from bcgov/refactor-custom-error-codes
Browse files Browse the repository at this point in the history
Refactor custom error codes
  • Loading branch information
Sepehr-Sobhani authored Nov 20, 2024
2 parents 72c6098 + e7c151a commit cfdc8a5
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 53 deletions.
4 changes: 2 additions & 2 deletions bc_obps/common/api/v1/dashboard_data/tiles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.http import HttpRequest
from django.db.models import QuerySet
from ninja.responses import codes_4xx
from typing import List, Literal, Tuple
from uuid import UUID

Expand All @@ -16,11 +15,12 @@
from registration.schema.generic import Message
from service.data_access_service.user_service import UserDataAccessService
from service.data_access_service.dashboard_service import DashboardDataService
from service.error_service.custom_codes_4xx import custom_codes_4xx


@router.get(
"/dashboard-data",
response={200: List[DashboardDataSchemaOut], codes_4xx: Message},
response={200: List[DashboardDataSchemaOut], custom_codes_4xx: Message},
tags=TAG_DASHBOARD_TILES,
auth=authorize("authorized_roles"),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.http import HttpRequest
from common.api.utils import get_current_user_guid
from registration.constants import OPERATION_TAGS
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.operation_service import OperationService
from registration.decorators import handle_http_errors
from registration.api.router import router
Expand All @@ -13,12 +14,11 @@
OperationUpdateStatusOut,
)
from registration.schema.generic import Message
from ninja.responses import codes_4xx, codes_5xx


@router.put(
"/operations/{operation_id}/update-status",
response={200: OperationUpdateStatusOut, codes_4xx: Message, codes_5xx: Message},
response={200: OperationUpdateStatusOut, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="""Updates the status of an operation.
When an operation is approved or declined, it is marked as verified with the current timestamp and the user who performed the action.
Expand Down
6 changes: 3 additions & 3 deletions bc_obps/registration/api/v1/_operations/operation_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from common.permissions import authorize
from django.http import HttpRequest
from registration.constants import OPERATION_TAGS
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.operation_service import OperationService
from common.api.utils import get_current_user_guid
from registration.decorators import handle_http_errors
Expand All @@ -14,12 +15,11 @@
OperationUpdateOut,
)
from registration.schema.generic import Message
from ninja.responses import codes_4xx


@router.get(
"/operations/{operation_id}",
response={200: OperationOut, codes_4xx: Message},
response={200: OperationOut, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="""Retrieves the details of a specific operation by its ID. The endpoint checks if the current user is authorized to access the operation.
Industry users can only access operations they are permitted to view. If an unauthorized user attempts to access the operation, an error is raised.""",
Expand All @@ -32,7 +32,7 @@ def get_operation(request: HttpRequest, operation_id: UUID) -> Tuple[Literal[200

@router.put(
"/operations/{operation_id}",
response={200: OperationUpdateOut, codes_4xx: Message},
response={200: OperationUpdateOut, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="""Updates the details of a specific operation by its ID. The endpoint ensures that only authorized industry users can edit operations belonging to their operator.
Updates are processed based on the form section being edited and can include updating basic operation details, the point of contact, or the statutory declaration document.
Expand Down
4 changes: 2 additions & 2 deletions bc_obps/registration/api/v1/_users/user_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from registration.decorators import handle_http_errors
from registration.api.router import router
from registration.schema.generic import Message
from ninja.responses import codes_4xx
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.user_service import UserService


@router.get(
"/users/{user_id}",
response={200: UserContactPageOut, codes_4xx: Message},
response={200: UserContactPageOut, custom_codes_4xx: Message},
tags=USER_TAGS,
description="""Retrieves the details of a specific user by its ID.
We check if the user is authorized to access the user's details by comparing the business_guid of the user with the business_guid of the current user.""",
Expand Down
8 changes: 4 additions & 4 deletions bc_obps/registration/api/v1/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from registration.decorators import handle_http_errors
from ..router import router
from registration.models import Operator
from ninja.responses import codes_4xx, codes_5xx
from service.error_service.custom_codes_4xx import custom_codes_4xx
from registration.schema.v1 import OperatorOut, OperatorIn, OperatorSearchOut, ConfirmSelectedOperatorOut
from registration.schema.generic import Message
from django.db.models import QuerySet
Expand All @@ -19,7 +19,7 @@

@router.get(
"/operators",
response={200: Union[List[OperatorSearchOut], OperatorSearchOut], codes_4xx: Message, codes_5xx: Message},
response={200: Union[List[OperatorSearchOut], OperatorSearchOut], custom_codes_4xx: Message},
tags=OPERATOR_TAGS,
description="""Retrieves operator(s) based on the provided CRA business number or legal name.
This endpoint is accessible to both approved and unapproved users, allowing them to view operator information when selected.
Expand All @@ -36,7 +36,7 @@ def get_operators_by_cra_number_or_legal_name(
# We have to let unapproved users to reach this endpoint otherwise they can't see operator info when they select it
@router.get(
"/operators/{operator_id}",
response={200: ConfirmSelectedOperatorOut, codes_4xx: Message},
response={200: ConfirmSelectedOperatorOut, custom_codes_4xx: Message},
tags=OPERATOR_TAGS,
description="""Retrieves information about a specific operator by its ID.
This endpoint is accessible to both approved and unapproved users, allowing them to view operator information when selected.""",
Expand All @@ -52,7 +52,7 @@ def get_operator(request: HttpRequest, operator_id: UUID) -> Tuple[Literal[200],

@router.put(
"/operators/{operator_id}",
response={200: OperatorOut, codes_4xx: Message},
response={200: OperatorOut, custom_codes_4xx: Message},
tags=OPERATOR_TAGS,
description="""Updates the status of a specific operator by its ID.
The endpoint allows authorized users to update the operator's status and perform additional actions based on the new status.
Expand Down
6 changes: 2 additions & 4 deletions bc_obps/registration/api/v1/registration_purposes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
from registration.api.router import router
from typing import Literal, Tuple
from common.permissions import authorize


from registration.schema.generic import Message
from ninja.responses import codes_4xx
from service.error_service.custom_codes_4xx import custom_codes_4xx

##### GET #####

Expand All @@ -25,7 +23,7 @@

@router.get(
"/registration_purposes",
response={200: List[REGISTRATION_PURPOSES_LITERALS], codes_4xx: Message},
response={200: List[REGISTRATION_PURPOSES_LITERALS], custom_codes_4xx: Message},
auth=authorize("approved_authorized_roles"),
)
@handle_http_errors()
Expand Down
4 changes: 2 additions & 2 deletions bc_obps/registration/api/v1/user/user_app_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from registration.schema.v1 import UserAppRoleOut
from registration.schema.generic import Message
from registration.api.router import router
from ninja.responses import codes_4xx
from service.error_service.custom_codes_4xx import custom_codes_4xx


@router.get(
"user/user-app-role",
response={200: UserAppRoleOut, codes_4xx: Message},
response={200: UserAppRoleOut, custom_codes_4xx: Message},
tags=USER_TAGS,
description="""Retrieves the application role of the current user.
The endpoint uses the user's GUID to look up and return their associated application role.""",
Expand Down
6 changes: 3 additions & 3 deletions bc_obps/registration/api/v1/user/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from registration.schema.v1 import UserOut, UserUpdateIn
from registration.schema.generic import Message
from registration.api.router import router
from ninja.responses import codes_4xx
from service.error_service.custom_codes_4xx import custom_codes_4xx


# endpoint to return user data if user exists in user table
@router.get(
"/user/user-profile",
response={200: UserOut, codes_4xx: Message},
response={200: UserOut, custom_codes_4xx: Message},
tags=USER_TAGS,
description="""Retrieves the profile data of the current user.
The endpoint uses the user GUID from the authorization header to look up and return the user's profile information, including their application role.""",
Expand All @@ -32,7 +32,7 @@ def get_user_profile(request: HttpRequest) -> Tuple[Literal[200], User]:
# Endpoint to update a user
@router.put(
"/user/user-profile",
response={200: UserOut, codes_4xx: Message},
response={200: UserOut, custom_codes_4xx: Message},
tags=USER_TAGS,
description="""Updates the profile data of the current user.
The user's data is retrieved and updated with the new values from the payload.""",
Expand Down
4 changes: 2 additions & 2 deletions bc_obps/registration/api/v1/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from registration.schema.v1 import UserOut
from registration.schema.generic import Message
from registration.api.router import router
from ninja.responses import codes_4xx
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.user_profile_service import UserProfileService

##### POST #####
Expand All @@ -17,7 +17,7 @@
# Endpoint to create a new user
@router.post(
"/users",
response={200: UserOut, codes_4xx: Message},
response={200: UserOut, custom_codes_4xx: Message},
tags=USER_TAGS,
description="""Creates a new user.
The endpoint determines the user's role based on the identity provider specified in the payload and assigns the appropriate application role.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from uuid import UUID
from django.http import HttpRequest
from registration.constants import OPERATION_TAGS
from ninja.responses import codes_4xx

from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.operation_service import OperationService
from registration.schema.v2.operation import (
OperationUpdateOut,
Expand All @@ -23,7 +24,7 @@

@router.get(
"/v2/operations/{uuid:operation_id}/registration/new-entrant-application",
response={200: OperationNewEntrantApplicationOut, codes_4xx: Message},
response={200: OperationNewEntrantApplicationOut, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="""Retrieves the new entrant application document of a specific operation by its ID. The endpoint checks if the current user is authorized to access the operation.
Industry users can only access operations they are permitted to view. If an unauthorized user attempts to access the operation, an error is raised.""",
Expand All @@ -37,7 +38,7 @@ def get_operation_new_entrant_application(request: HttpRequest, operation_id: UU

@router.put(
"/v2/operations/{uuid:operation_id}/registration/new-entrant-application",
response={200: OperationUpdateOut, codes_4xx: Message},
response={200: OperationUpdateOut, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="Creates or replaces a new entrant application document for an Operation",
auth=authorize("approved_industry_user"),
Expand Down
4 changes: 2 additions & 2 deletions bc_obps/registration/api/v2/_operations/current.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
from registration.schema.v2.operation import (
OperationCurrentOut,
)
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.operation_service_v2 import OperationServiceV2
from registration.decorators import handle_http_errors
from registration.api.router import router
from common.permissions import authorize

from registration.schema.generic import Message
from ninja.responses import codes_4xx
from django.db.models import QuerySet

##### GET #####


@router.get(
"/v2/operations/current",
response={200: List[OperationCurrentOut], codes_4xx: Message},
response={200: List[OperationCurrentOut], custom_codes_4xx: Message},
tags=["V2"],
auth=authorize('approved_industry_user'),
description="""Gets the list of operations associated with the current user's operator that are not yet registered.
Expand Down
8 changes: 4 additions & 4 deletions bc_obps/registration/api/v2/_operations/operation_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
from common.permissions import authorize
from django.http import HttpRequest
from registration.constants import OPERATION_TAGS
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.operation_service import OperationService
from service.operation_service_v2 import OperationServiceV2
from common.api.utils import get_current_user_guid
from registration.decorators import handle_http_errors
from registration.api.router import router
from registration.models import Operation
from registration.schema.generic import Message
from ninja.responses import codes_4xx


##### GET #####


@router.get(
"/v2/operations/{uuid:operation_id}",
response={200: OperationOutV2, codes_4xx: Message},
response={200: OperationOutV2, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="""Retrieves the details of a specific operation by its ID. Unlike the v1 endpoint, this endpoint does not
return the new entrant application field as it can be quite large and cause slow requests. If you need the new entrant application field,
Expand All @@ -34,7 +34,7 @@ def get_operation_v2(request: HttpRequest, operation_id: UUID) -> Tuple[Literal[

@router.get(
"/v2/operations/{uuid:operation_id}/with-documents",
response={200: OperationOutWithDocuments, codes_4xx: Message},
response={200: OperationOutWithDocuments, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="""Retrieves the details of a specific operation by its ID along with it's documents""",
exclude_none=True,
Expand All @@ -50,7 +50,7 @@ def get_operation_with_documents(request: HttpRequest, operation_id: UUID) -> Tu

@router.put(
"/v2/operations/{uuid:operation_id}",
response={200: OperationOutV2, codes_4xx: Message},
response={200: OperationOutV2, custom_codes_4xx: Message},
tags=OPERATION_TAGS,
description="Updates the details of a specific operation by its ID.",
auth=authorize("approved_industry_user"),
Expand Down
7 changes: 3 additions & 4 deletions bc_obps/reporting/api/activity_data.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from service.activity_service import ActivityService
from common.permissions import authorize
from service.error_service.custom_codes_4xx import custom_codes_4xx
from .router import router
from registration.decorators import handle_http_errors
from django.http import HttpRequest
from typing import Tuple
from uuid import UUID

from reporting.schema.generic import Message
from ninja.responses import codes_4xx, codes_5xx


##### GET #####


@router.get(
"/report-version/{version_id}/facility-report/{facility_id}/initial-activity-data",
response={200: str, codes_4xx: Message, codes_5xx: Message},
response={200: str, custom_codes_4xx: Message},
auth=authorize("approved_industry_user"),
)
@handle_http_errors()
Expand All @@ -25,7 +24,7 @@ def get_initial_activity_data(
return 200, ActivityService.get_initial_activity_data(version_id, facility_id, activity_id)


@router.get("/activities", response={200: list, codes_4xx: Message, codes_5xx: Message})
@router.get("/activities", response={200: list, custom_codes_4xx: Message})
@handle_http_errors()
def get_activities(request: HttpRequest) -> Tuple[int, list]:
return 200, ActivityService.get_all_activities()
4 changes: 2 additions & 2 deletions bc_obps/reporting/api/build_form_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from service.error_service.custom_codes_4xx import custom_codes_4xx
from service.form_builder_service import FormBuilderService
from common.permissions import authorize
from .router import router
Expand All @@ -6,14 +7,13 @@
from typing import Tuple

from registration.schema.generic import Message
from ninja.responses import codes_4xx, codes_5xx

##### GET #####


@router.get(
"/build-form-schema",
response={200: str, codes_4xx: Message, codes_5xx: Message},
response={200: str, custom_codes_4xx: Message},
auth=authorize("approved_authorized_roles"),
)
@handle_http_errors()
Expand Down
8 changes: 3 additions & 5 deletions bc_obps/reporting/api/emission_category.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from common.permissions import authorize
from reporting.service.emission_category_service import EmissionCategoryService
from reporting.models import FacilityReport
from service.error_service.custom_codes_4xx import custom_codes_4xx
from .router import router
from registration.decorators import handle_http_errors
from django.http import HttpRequest
from typing import Tuple, List, Dict
from uuid import UUID
from decimal import Decimal

from reporting.schema.generic import Message
from ninja.responses import codes_4xx, codes_5xx

from reporting.models import EmissionCategory
from reporting.schema.emission_category import EmissionCategorySchema

Expand All @@ -20,7 +18,7 @@

@router.get(
"/emission-category",
response={200: List[EmissionCategorySchema], codes_4xx: Message, codes_5xx: Message},
response={200: List[EmissionCategorySchema], custom_codes_4xx: Message},
auth=authorize("approved_industry_user"),
)
@handle_http_errors()
Expand All @@ -30,7 +28,7 @@ def get_emission_category(request: HttpRequest) -> Tuple[int, List[EmissionCateg

@router.get(
"/report-version/{version_id}/facility-report/{facility_id}/emission-summary",
response={200: Dict[str, Decimal | int], codes_4xx: Message, codes_5xx: Message},
response={200: Dict[str, Decimal | int], custom_codes_4xx: Message},
url_name="get_emission_summary_totals",
auth=authorize("approved_authorized_roles"),
)
Expand Down
Loading

0 comments on commit cfdc8a5

Please sign in to comment.