Skip to content

Commit

Permalink
chore: implemented more PR review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-williams committed Jan 8, 2025
1 parent 7c65cac commit 4a36d22
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from uuid import UUID
from service.operation_service import OperationService
from registration.schema.v2.operation import OptedInOperationDetailIn
Expand All @@ -8,21 +9,28 @@

class OptedInOperationDataAccessService:
@classmethod
def update_opted_in_operation_detail(
def create_or_update_opted_in_operation_detail(
cls,
user_guid: UUID,
opted_in_operation_detail_id: int,
opted_in_operation_detail_data: OptedInOperationDetailIn,
opted_in_operation_detail_id: Optional[int],
) -> OptedInOperationDetail:
opted_in_operation_detail = OptedInOperationDetail.objects.get(id=opted_in_operation_detail_id)
updated_opted_in_operation_detail_instance = update_model_instance(
opted_in_operation_detail,
opted_in_operation_detail_data.dict().keys(),
opted_in_operation_detail_data.dict(),
)
updated_opted_in_operation_detail_instance.save()
updated_opted_in_operation_detail_instance.set_create_or_update(user_guid)
return updated_opted_in_operation_detail_instance
if opted_in_operation_detail_id:
# Fetch the existing instance if an ID has been provided
opted_in_operation_detail_instance = OptedInOperationDetail.objects.get(id=opted_in_operation_detail_id)
# Update the existing instance with the new data
opted_in_operation_detail_instance = update_model_instance(
opted_in_operation_detail_instance,
opted_in_operation_detail_data.dict().keys(),
opted_in_operation_detail_data.dict(),
)
else:
# If no ID has been provided, create a new instance of OptedInOperationDetail and insert the provided data
opted_in_operation_detail_instance = OptedInOperationDetail.objects.create(*opted_in_operation_detail_data)

opted_in_operation_detail_instance.save()
opted_in_operation_detail_instance.set_create_or_update(user_guid)
return opted_in_operation_detail_instance

@classmethod
def archive_or_delete_opted_in_operation_detail(cls, user_guid: UUID, operation_id: UUID) -> None:
Expand Down
4 changes: 2 additions & 2 deletions bc_obps/service/operation_service_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def update_opted_in_operation_detail(
operation = OperationService.get_if_authorized(user_guid, operation_id)
if not operation.opted_in_operation:
raise Exception("Operation does not have an opted-in operation.")
return OptedInOperationDataAccessService.update_opted_in_operation_detail(
user_guid, operation.opted_in_operation.id, payload
return OptedInOperationDataAccessService.create_or_update_opted_in_operation_detail(
user_guid, payload, operation.opted_in_operation.id
)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def test_update_opted_in_operation_detail():
meets_reporting_and_regulated_obligations=False,
meets_notification_to_director_on_criteria_change=False,
)
opted_in_operation_detail = OptedInOperationDataAccessService.update_opted_in_operation_detail(
opted_in_operation_detail = OptedInOperationDataAccessService.create_or_update_opted_in_operation_detail(
approved_user_operator.user.user_guid,
users_operation.opted_in_operation.id,
opted_in_operation_detail_payload,
users_operation.opted_in_operation.id,
)
users_operation.refresh_from_db()
assert opted_in_operation_detail.id == users_operation.opted_in_operation.id
Expand Down
11 changes: 5 additions & 6 deletions bc_obps/service/tests/test_document_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ def test_archive_or_delete_operation_document(self, registration_status):
assert operation.documents.count() == 1

if registration_status == Operation.Statuses.REGISTERED:
"""if the registration has been completed, the document should be archived"""
print(b_map.file)
print(b_map.archived_at, b_map.archived_by)
"""if the registration has been completed, the document should have been archived"""
b_map.refresh_from_db()
assert b_map.archived_at is not None
assert b_map.archived_by is not None
elif registration_status == Operation.Statuses.DRAFT:
"""if the registration wasn't completed, the document should be deleted"""
print(b_map.file)
assert b_map is None
"""if the registration wasn't completed, the document should have been deleted"""
with pytest.raises(Document.DoesNotExist):
b_map.refresh_from_db() # this should raise an exception because it no longer exists in the db

0 comments on commit 4a36d22

Please sign in to comment.