Skip to content

Commit

Permalink
add missing imports + refactor to call 1 query
Browse files Browse the repository at this point in the history
  • Loading branch information
RulaKhaled committed Jan 20, 2025
1 parent 16f7fba commit 3a5b3af
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
19 changes: 13 additions & 6 deletions api/internal/owner/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from shared.plan.service import PlanService

from codecov_auth.models import Owner
from codecov_auth.models import Owner, Plan
from services.billing import BillingService
from services.sentry import send_user_webhook as send_sentry_webhook

Expand Down Expand Up @@ -147,10 +147,18 @@ def validate(self, plan: Dict[str, Any]) -> Dict[str, Any]:
detail="You cannot update your plan manually, for help or changes to plan, connect with [email protected]"
)

active_plans = list(
Plan.objects.filter(paid_plan=True, is_active=True).values_list(
"name", "tier"
)
)
active_plan_names = {name for name, _ in active_plans}
team_tier_plans = {
name for name, tier in active_plans if tier == TierName.TEAM.value
}

# Validate quantity here because we need access to whole plan object
if plan["value"] in Plan.objects.filter(
paid_plan=True, is_active=True
).values_list("name", flat=True):
if plan["value"] in active_plan_names:
if "quantity" not in plan:
raise serializers.ValidationError(
"Field 'quantity' required for updating to paid plans"
Expand Down Expand Up @@ -179,8 +187,7 @@ def validate(self, plan: Dict[str, Any]) -> Dict[str, Any]:
"Quantity or plan for paid plan must be different from the existing one"
)
if (
plan["value"]
in Plan.objects.filter(tier=TierName.TEAM.value, is_active=True)
plan["value"] in team_tier_plans
and plan["quantity"] > TEAM_PLAN_MAX_USERS
):
raise serializers.ValidationError(
Expand Down
11 changes: 10 additions & 1 deletion api/internal/tests/views/test_account_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
AccountFactory,
InvoiceBillingFactory,
OwnerFactory,
PlanFactory,
TierFactory,
UserFactory,
)
from shared.plan.constants import PlanName, TrialStatus
from shared.plan.constants import PlanName, TierName, TrialStatus
from stripe import StripeError

from api.internal.tests.test_utils import GetAdminProviderAdapter
Expand Down Expand Up @@ -187,6 +189,13 @@ def test_retrieve_account_gets_account_fields(self):
def test_retrieve_account_gets_account_fields_when_there_are_scheduled_details(
self, mock_retrieve_subscription, mock_retrieve_schedule
):
tier = TierFactory(tier_name=TierName.BASIC.value)

PlanFactory(
name="users-basic",
tier=tier,
is_active=True,
)
owner = OwnerFactory(
admins=[self.current_owner.ownerid], stripe_subscription_id="sub_123"
)
Expand Down
2 changes: 1 addition & 1 deletion billing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import QuerySet
from shared.plan.constants import TierName

from codecov_auth.models import Owner
from codecov_auth.models import Owner, Plan


def on_enterprise_plan(owner: Owner) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion codecov_auth/services/org_level_token_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.dispatch import receiver
from django.forms import ValidationError

from codecov_auth.models import OrganizationLevelToken, Owner
from codecov_auth.models import OrganizationLevelToken, Owner, Plan

log = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions graphql_api/types/owner/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Account,
GithubAppInstallation,
Owner,
Plan,
)
from codecov_auth.views.okta_cloud import OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY
from core.models import Repository
Expand Down
3 changes: 2 additions & 1 deletion services/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
from django.conf import settings
from shared.plan.constants import (
PlanBillingRate,
TierName,
)
from shared.plan.service import PlanService

from billing.constants import REMOVED_INVOICE_STATUSES
from codecov_auth.models import Owner
from codecov_auth.models import Owner, Plan

log = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions upload/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SERVICE_GITHUB_ENTERPRISE,
GithubAppInstallation,
Owner,
Plan,
)
from core.models import Commit, Repository
from reports.models import CommitReport, ReportSession
Expand Down

0 comments on commit 3a5b3af

Please sign in to comment.