Skip to content

Commit

Permalink
chore:fixed endpoint and service
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshmcg committed Jan 14, 2025
1 parent 49a12c6 commit 31296a2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 24 deletions.
10 changes: 5 additions & 5 deletions bc_obps/reporting/api/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from service.report_service import ReportService
from service.reporting_year_service import ReportingYearService
from service.error_service.custom_codes_4xx import custom_codes_4xx
from reporting.schema.report_operation import ReportOperationIn, ReportOperationSchemaOut
from reporting.schema.report_operation import ReportOperationIn, ReportOperationSchemaOut, ReportOperationOut
from reporting.schema.reporting_year import ReportingYearOut
from .router import router
from ..schema.report_regulated_products import RegulatedProductOut
Expand Down Expand Up @@ -54,7 +54,7 @@ def get_report_operation_by_version_id(request: HttpRequest, version_id: int) ->

@router.post(
"/report-version/{version_id}/report-operation",
response={200: ReportOperationIn, custom_codes_4xx: Message},
response={201: ReportOperationOut, custom_codes_4xx: Message},
tags=EMISSIONS_REPORT_TAGS,
description="""Updates given report operation with fields: Operator Legal Name, Operator Trade Name, Operation Name, Operation Type,
Operation BC GHG ID, BC OBPS Regulated Operation ID, Operation Representative Name, and Activities.""",
Expand All @@ -63,9 +63,9 @@ def get_report_operation_by_version_id(request: HttpRequest, version_id: int) ->
@handle_http_errors()
def save_report(
request: HttpRequest, version_id: int, payload: ReportOperationIn
) -> Tuple[Literal[200], ReportOperationIn]:
) -> Tuple[Literal[201], ReportOperationOut]:
report_operation = ReportService.save_report_operation(version_id, payload)
return 200, report_operation # type: ignore
return 201, report_operation # type: ignore


@router.get(
Expand Down Expand Up @@ -105,4 +105,4 @@ def get_regulated_products_by_version_id(
@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
return 200, report_type
2 changes: 1 addition & 1 deletion bc_obps/reporting/schema/report_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class ReportOperationIn(Schema):
bc_obps_regulated_operation_id: str
activities: List[str]
regulated_products: List[str]
operation_representative_name: str
operation_report_type: str
operation_representative_name: List[int]

class Meta:
alias_generator = to_snake
Expand Down
11 changes: 9 additions & 2 deletions bc_obps/service/report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def create_report(cls, operation_id: UUID, reporting_year: int) -> int:
.prefetch_related('activities', 'regulated_products')
.get(id=operation_id)
)

operator = operation.operator
facilities = FacilityDataAccessService.get_current_facilities_by_operation(operation)

Expand Down Expand Up @@ -93,15 +93,22 @@ def get_report_operation_by_version_id(cls, report_version_id: int) -> ReportOpe
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)
report_operation_representatives = ReportOperationRepresentative.objects.filter(
report_version=report_version_id
)

# Update the selected_for_report field based on the provided data
representative_ids_in_data = data.operation_representative_name # List of IDs from input
for representative in report_operation_representatives:
representative.selected_for_report = representative.id in representative_ids_in_data
representative.save()
# Update fields from data
report_operation.operator_legal_name = data.operator_legal_name
report_operation.operator_trade_name = data.operator_trade_name
report_operation.operation_name = data.operation_name
report_operation.operation_type = data.operation_type
report_operation.operation_bcghgid = data.operation_bcghgid
report_operation.bc_obps_regulated_operation_id = data.bc_obps_regulated_operation_id
report_operation.operation_representative_name = data.operation_representative_name

# Fetch and set ManyToMany fields
activities = Activity.objects.filter(name__in=data.activities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { TaskListElement } from "@bciers/components/navigation/reportingTaskList/types";
import { actionHandler } from "@bciers/actions";
import { formatDate } from "@reporting/src/app/utils/formatDate";
import safeJsonParse from "@bciers/utils/src/safeJsonParse";

interface Props {
formData: any;
Expand All @@ -31,7 +30,11 @@ interface Props {
facility_id: number;
operation_type: string;
};
allRepresentatives: { id: number; representative_name: string }[];
allRepresentatives: {
selected_for_report: boolean;
id: number;
representative_name: string;
}[];
}

export default function OperationReviewForm({
Expand Down Expand Up @@ -120,11 +123,18 @@ export default function OperationReviewForm({

useEffect(() => {
if (formData && allActivities && allRegulatedProducts) {
const selectedRepresentatives =
formData.operation_representative_name
?.filter(
(rep: { selected_for_report: any }) => rep.selected_for_report,
)
.map((rep: { id: any }) => rep.id) || [];
const updatedFormData = {
...formData,
operation_report_type: reportType?.report_type || "Annual Report",
activities: formData.activities || [],
regulated_products: formData.regulated_products || [],
operation_representative_name: selectedRepresentatives,
};
setFormDataState(updatedFormData);

Expand Down Expand Up @@ -152,6 +162,7 @@ export default function OperationReviewForm({
allRegulatedProducts,
registrationPurpose,
reportingWindowEnd,
allRepresentatives,
]);

const saveHandler = async (
Expand All @@ -161,7 +172,7 @@ export default function OperationReviewForm({
const method = "POST";
const endpoint = `reporting/report-version/${reportVersionId}/report-operation`;
const preparedData = prepareFormData(data.formData);
const payload = safeJsonParse(JSON.stringify(preparedData));
const payload = JSON.stringify(preparedData);
const response = await actionHandler(endpoint, method, endpoint, {
body: payload,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export default async function OperationReviewPage({
const transformedOperation = {
...reportOperation.report_operation,
operation_representative_name:
reportOperation.report_operation_representative.map(
(rep: { id: number }) => rep.id,
),
reportOperation.report_operation_representative,
};
console.log("transformedOperation", transformedOperation);
const allRepresentatives = reportOperation.report_operation_representative;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,53 @@ describe("OperationReviewForm Component", () => {
expect(screen.getByText(/Back/i)).toBeInTheDocument();
expect(screen.getByText(/Save & Continue/i)).toBeInTheDocument();
});

it(
"submits the form and navigates to the next page",
{ timeout: 10000 },
{
timeout: 10000,
},
async () => {
const { push } = useRouter();

renderForm({
formData: {
...defaultProps.formData,
operation_name: "Operation Name",
operation_bcghgid: "your-bcghg-id",
},
});
render(
<OperationReviewForm
formData={{
activities: [1],
regulated_products: [1],
operation_representative_name: [
{
id: 4,
representative_name: "Shon Doe",
selected_for_report: true,
},
],
operation_bcghgid: "your-bcghg-id",
operation_name: "Operation Name",
operation_type: "Test Operation",
}}
version_id={1}
reportType={{ report_type: "Annual Report" }}
reportingYear={{
reporting_year: 2024,
report_due_date: "2024-12-31",
reporting_window_end: "2024-12-31",
}}
allActivities={[{ id: 1, name: "Activity 1" }]}
allRegulatedProducts={[{ id: 1, name: "Product 1" }]}
registrationPurpose="Test Purpose"
facilityReport={{
facility_id: "fake-guid",
operation_type: "Single Facility Operation",
}}
allRepresentatives={[
{
id: 4,
representative_name: "Shon Doe",
selected_for_report: true,
},
]}
/>,
);

// Simulate form submission
fireEvent.click(screen.getByText(/Save & Continue/i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
cancelUrl?: string;
backUrl?: string;
continueUrl: string;
onSubmit: (data: any) => Promise<void>;
onSubmit: (data: any) => Promise<any>;
buttonText?: string;
onChange?: (data: any) => void;
error?: any;
Expand Down

0 comments on commit 31296a2

Please sign in to comment.