Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2680 m2m fields refactor #2716

Merged
merged 8 commits into from
Jan 31, 2025
Merged

2680 m2m fields refactor #2716

merged 8 commits into from
Jan 31, 2025

Conversation

BCerki
Copy link
Contributor

@BCerki BCerki commented Jan 21, 2025

card: https://github.com/orgs/bcgov/projects/123/views/16?pane=issue&itemId=93581679&issue=bcgov%7Ccas-registration%7C2680

Notes:

  • I've made the operator field on contacts optional to support reg1
  • I also deleted user_documents and operator_documents in addition to what's in the card's AC
  • Where I've refactored from operation_baker() (etc) to baker recipes, it's because operation_contact() now creates an operator, and that can make it hard to keep track of counts of things (e.g. the facility tests were failing because if there's an operation_baker and an operation_contact, we end up with two operations, two addresses, etc.)
  • In the v2 mock data, I've checked that all operators have a contact, and operation contacts have to belong to the operation's operator
  • Sonar failures are repetition, which is ok because it'll get fixed when we remove the reg1 stuff
  • I had to make a couple changes to v1, but luckily we weren't using the fields that I changed/deleted, so the e2e tests and the app still seem to be happy
  • I manually clicked around in both reg1 and reg2 to try and break anything operation/contact/document related

@BCerki BCerki force-pushed the 2680-m2m-fields-refactor branch 5 times, most recently from f331364 to 2e0c8af Compare January 24, 2025 21:54
Comment on lines -75 to -84
documents = models.ManyToManyField(
Document,
blank=True,
related_name="operators",
)
contacts = models.ManyToManyField(
Contact,
blank=True,
related_name="operators",
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we're using either of these (@Sepehr-Sobhani , would you mind checking metabase? I don't have access to prod data)

Copy link
Contributor

Choose a reason for hiding this comment

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

No data!

@@ -13,11 +13,6 @@ class User(UserAndContactCommonInfo):
max_length=1000,
db_comment="The name of the business the user is associated with as per their Business BCeID account",
)
documents = models.ManyToManyField(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Sepehr-Sobhani also this one

Copy link
Contributor

Choose a reason for hiding this comment

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

No data!

@BCerki BCerki marked this pull request as ready for review January 24, 2025 22:49
@@ -32,3 +40,5 @@ def delete(self, *args, **kwargs):
default_storage.delete(self.file.name)

super().delete(*args, **kwargs)

#
Copy link
Contributor

Choose a reason for hiding this comment

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

tedious nit: seems unnecessary?

@@ -277,6 +277,7 @@ def update_status(
if user_operator.status == UserOperator.Statuses.DECLINED:
# Set role to pending for now but we may want to add a new role for declined
user_operator.role = UserOperator.Roles.PENDING
# brianna you're going to have to go deal with thiss
Copy link
Contributor

Choose a reason for hiding this comment

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

do you plan to deal with "thiss" in this PR or a follow up one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't have to deal with it in the end, actually, nothing in reg1 broke because we learned you can use set/add for both m2m and fk

return row.id + operator;
}

console.log("initialdata", initialData);
Copy link
Contributor

Choose a reason for hiding this comment

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

delete?

@@ -149,6 +149,7 @@ def test_cas_director_approves_admin_access_request_with_new_operator(self, mock
)

def test_cas_analyst_declines_access_request(self, mocker):
# brianna this will be fixed after rebase
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, leftover

def create_contact(cls, user_guid: UUID, payload: Union[ContactIn, OperationRepresentativeIn]) -> Contact:
contact_data: dict = payload.dict(include={*ContactIn.Meta.fields})
# `business_role` is a mandatory field in the DB but we don't collect it from the user
# so we set it to a default value here and we can change it later if needed
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can probably delete the business_role field on Contact, I don't think we use it anywhere? (but this is tech debt we can leave for later)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a couple places where we filter by business_role Operation Representative

Comment on lines +82 to +87
existing_contact_address = contact.address
if existing_contact_address:
contact.address = None
contact.save(update_fields=['address'])
# contact has an address and the payload has no address data, remove the address
existing_contact_address.delete()
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this part confusing and overly complex. Couldn't it be simplified to:

if contact.address:
    contact.address.delete()
    contact.address = None  # not even sure this line is needed
    contact.save(update_fields=['address'])

side note - I'm having major deja vu reading this code. Was this copied over from v1 of the service? If so there might be an argument for keeping this code as-is until we throw away all our v1 code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's copied from v1. I'll leave it as-is for now


class DocumentDataAccessServiceV2:
@classmethod
def get_operation_document_by_type(cls, operation_id: UUID, document_type: str) -> Document | None:
Copy link
Contributor

Choose a reason for hiding this comment

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

I got lost trying to follow the paths of where this function gets called, so apologies if this question is wrong, but do we need to add a check here based on user_guid to confirm that the user has permission to access data for the operation in question? (I couldn't tell if this gets checked somewhere else)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made a get_if_authorized function to this, that's how we've generally done it in other services. Thanks for catching this!

assert created is False

@staticmethod
def test_update_operation_document():
Copy link
Contributor

Choose a reason for hiding this comment

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

could I request another unit test here to confirm that an industry_user is NOT allowed to update/view a document associated with an operation outside of their operator? (this relates to my previous comment above)

@BCerki BCerki force-pushed the 2680-m2m-fields-refactor branch 4 times, most recently from adedb9b to 5113692 Compare January 28, 2025 22:07
Copy link
Contributor

@andrea-williams andrea-williams left a comment

Choose a reason for hiding this comment

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

Wonderful! Thanks for adding that extra unit test in <3

@BCerki BCerki force-pushed the 2680-m2m-fields-refactor branch 3 times, most recently from a4bbe6e to 9dfe44c Compare January 31, 2025 16:02
@BCerki BCerki force-pushed the 2680-m2m-fields-refactor branch from 9dfe44c to 82a20d2 Compare January 31, 2025 16:14
@BCerki BCerki merged commit 4189377 into develop Jan 31, 2025
41 of 42 checks passed
@BCerki BCerki deleted the 2680-m2m-fields-refactor branch January 31, 2025 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants