Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions apps/codecov-api/api/internal/owner/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,24 @@ class StripeScheduledPhaseSerializer(serializers.Serializer):
start_date = serializers.IntegerField()
plan = serializers.SerializerMethodField()
quantity = serializers.SerializerMethodField()
billing_rate = serializers.SerializerMethodField()

def _get_plan_object(self, phase: dict[str, Any]) -> Plan:
"""Helper method to get and cache the Plan object."""
if not hasattr(self, "_cached_plan"):
plan_id = phase["items"][0]["plan"]
self._cached_plan = Plan.objects.get(stripe_id=plan_id)
return self._cached_plan

def get_plan(self, phase: dict[str, Any]) -> str:
plan_id = phase["items"][0]["plan"]
marketing_plan_name = Plan.objects.get(stripe_id=plan_id).marketing_name
return marketing_plan_name
return self._get_plan_object(phase).marketing_name

def get_quantity(self, phase: dict[str, Any]) -> int:
return phase["items"][0]["quantity"]

def get_billing_rate(self, phase: dict[str, Any]) -> str | None:
return self._get_plan_object(phase).billing_rate


class ScheduleDetailSerializer(serializers.Serializer):
id = serializers.CharField()
Expand Down
22 changes: 19 additions & 3 deletions apps/codecov-api/api/internal/tests/views/test_account_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
OwnerFactory,
UserFactory,
)
from shared.plan.constants import DEFAULT_FREE_PLAN, PlanName, TrialStatus
from shared.plan.constants import (
DEFAULT_FREE_PLAN,
PlanName,
TrialStatus,
)
from utils.test_utils import APIClient

curr_path = os.path.dirname(__file__)
Expand Down Expand Up @@ -295,6 +299,7 @@ def test_retrieve_account_gets_account_fields_when_there_are_scheduled_details(
"start_date": schedule_params["start_date"],
"plan": "Pro",
"quantity": schedule_params["quantity"],
"billing_rate": "annually",
},
},
"student_count": 0,
Expand Down Expand Up @@ -354,11 +359,21 @@ def test_retrieve_account_returns_last_phase_when_more_than_one_scheduled_phases
phases = [
{
"start_date": 123689126536,
"items": [{"plan": "test_plan_123", "quantity": 4}],
"items": [
{
"plan": "test_plan_123",
"quantity": 4,
}
],
},
{
"start_date": 123689126636,
"items": [{"plan": "test_plan_456", "quantity": 5}],
"items": [
{
"plan": "test_plan_456",
"quantity": 5,
}
],
},
{
"start_date": schedule_params["start_date"],
Expand Down Expand Up @@ -430,6 +445,7 @@ def test_retrieve_account_returns_last_phase_when_more_than_one_scheduled_phases
"plan": "Pro",
"quantity": schedule_params["quantity"],
"start_date": schedule_params["start_date"],
"billing_rate": "annually",
},
},
"uses_invoice": False,
Expand Down
Loading