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

add action to link users to accounts #696

Merged
merged 5 commits into from
Jul 23, 2024
Merged

add action to link users to accounts #696

merged 5 commits into from
Jul 23, 2024

Conversation

nora-codecov
Copy link
Contributor

Purpose/Motivation

Gives us a method to do the initial setup when an Account is created. This action will create the link between the Account object and the User object for any Owner who is a plan_activated_user on any of the Organizations linked to the Account.

Can be run multiple times safely (in case it errors or times out)
Won't do the link if the Account doesn't have enough seats

For "auditing" its behavior - it gives messages about how many Users and AccountUsers were created. You can see the Users linked to the Account on the Account edit page (when you click into an Account on the admin). You can remove a AccountUsers connection on the User admin page.

Links to relevant tickets

Uses codecov/shared#295
part of codecov/engineering-team#2058

@nora-codecov nora-codecov requested review from michelletran-codecov and a team July 18, 2024 23:47
@codecov-notifications
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codecov-qa
Copy link

codecov-qa bot commented Jul 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.65%. Comparing base (4fde36b) to head (b9f1a9a).

✅ All tests successful. No failed tests found.

@@            Coverage Diff             @@
##             main     #696      +/-   ##
==========================================
+ Coverage   91.63%   91.65%   +0.02%     
==========================================
  Files         632      632              
  Lines       16872    16917      +45     
==========================================
+ Hits        15461    15506      +45     
  Misses       1411     1411              
Flag Coverage Δ
unit 91.65% <100.00%> (+0.02%) ⬆️
unit-latest-uploader 91.65% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
codecov_auth/admin.py 100.00% <100.00%> (ø)

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

Copy link

codecov-public-qa bot commented Jul 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.65%. Comparing base (4fde36b) to head (b9f1a9a).

✅ All tests successful. No failed tests found.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #696      +/-   ##
==========================================
+ Coverage   91.63%   91.65%   +0.02%     
==========================================
  Files         632      632              
  Lines       16872    16917      +45     
==========================================
+ Hits        15461    15506      +45     
  Misses       1411     1411              
Flag Coverage Δ
unit 91.65% <100.00%> (+0.02%) ⬆️
unit-latest-uploader 91.65% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
codecov_auth/admin.py 100.00% <100.00%> (ø)

Impacted file tree graph

Copy link

codecov bot commented Jul 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.98%. Comparing base (4fde36b) to head (b9f1a9a).

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##               main       #696   +/-   ##
===========================================
  Coverage   95.98000   95.98000           
===========================================
  Files           812        812           
  Lines         18209      18254   +45     
===========================================
+ Hits          17477      17522   +45     
  Misses          732        732           
Flag Coverage Δ
unit 91.65% <100.00%> (+0.02%) ⬆️
unit-latest-uploader 91.65% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nora-codecov nora-codecov force-pushed the nora/2058 branch 2 times, most recently from f5fc9d2 to daf2712 Compare July 19, 2024 19:39
@admin.action(description="Link Users to Account")
def link_users_to_account(self, request, queryset):
for account in queryset:
all_plan_activated_user_ids = set()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit/Optional: I find the "all" a bit redundant since the variable is plural. I also got tripped up a bit reading this like "(all plan) (activated user ids)" instead of "(all) (plan activated user ids)" 😅, but that might be me. I think calling this "activated_user_ids_in_orgs" represents this a bit better?

Similar idea for the rest of the "alls" as well

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 see what you mean. I'm naming it after the field on the Owner model, plan_activated_users, which is actually a list of ownerids (😭)
what I want this to represent is that they have been collected and deduped, so they represent the unique owner objects that need to be connected to the Account (through their User)
account_plan_activated_user_ownerids?

Copy link
Contributor

Choose a reason for hiding this comment

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

haha yeah naming is hard :/. That sounds good to me, I'll leave it up to you for final naming, you're the most familiar w/ the code so as long as it makes sense to you 👍

ownerid__in=all_plan_activated_user_ids
).prefetch_related("user")
non_student_count = all_plan_activated_users.exclude(student=True).count()
total_seats_for_account = account.plan_seat_count + account.free_seat_count
Copy link
Contributor

Choose a reason for hiding this comment

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

What's a free seat count? I see it's defaulted to 0, is this for like free plans?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's a thing that exists on Owners currently (named free), but it's a way for us to give Orgs additional seats not in the count they pay for (plan_seat_count) 🤷

Copy link
Contributor

Choose a reason for hiding this comment

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

damn, was not aware of this

all_plan_activated_users = Owner.objects.filter(
ownerid__in=all_plan_activated_user_ids
).prefetch_related("user")
non_student_count = all_plan_activated_users.exclude(student=True).count()
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: maybe adding a comment on why we exclude students. Thanks for being so attentive and catching this this early, this is a edge case for suuuure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmm, not really an edge case - check out activated_user_count or can_activate_user methods on Owner and Account - it's our built in policy.
I'm following the methods defined on the models to calculate things like can_activate_user - but having to write it out instead of using the pre-built methods so that I can do bulk operations 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, what I meant was that this is an easy thing to slip since the majority of our customers aren't students, so I was praising you for being diligent, looking at previous code and learning about our policy

)
userless_owners = []
for userless_owner in owners_without_user_objects:
new_user = User.objects.create()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this new User object meant to be empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no! fixed!
ty ty ty

total = Owner.objects.bulk_update(userless_owners, ["user"])
self.message_user(
request,
f"Created a User for {total} Owners",
Copy link
Contributor

Choose a reason for hiding this comment

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

Would probably be handy to log the owner ids we did this for 👀, up to you

Copy link
Contributor

@adrian-codecov adrian-codecov left a comment

Choose a reason for hiding this comment

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

Left some comments, overall lgtm! Lmk and I can approve

@nora-codecov nora-codecov force-pushed the nora/2058 branch 2 times, most recently from 268e8f7 to 0bda337 Compare July 20, 2024 00:00
Copy link
Contributor

@michelletran-codecov michelletran-codecov left a comment

Choose a reason for hiding this comment

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

Generally LGTM! This is only for adding orgs for now, correct? We would need some different mechanism if we were to remove orgs from accounts...

@nora-codecov
Copy link
Contributor Author

updated and added based on @adrian-codecov and @michelletran-codecov feedback! ty for the comments ❤️

  • added logs 🤖
  • added logic to remove AccountUsers so now the action is a "true" sync 🔄
  • improved names 💪

@nora-codecov nora-codecov added this pull request to the merge queue Jul 23, 2024
Merged via the queue into main with commit b3c6343 Jul 23, 2024
21 of 22 checks passed
@nora-codecov nora-codecov deleted the nora/2058 branch July 23, 2024 18:07
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