diff --git a/enterprise_subsidy/apps/api/v1/serializers.py b/enterprise_subsidy/apps/api/v1/serializers.py index c102c4ce..aff4185f 100644 --- a/enterprise_subsidy/apps/api/v1/serializers.py +++ b/enterprise_subsidy/apps/api/v1/serializers.py @@ -28,7 +28,7 @@ class SubsidySerializer(serializers.ModelSerializer): """ current_balance = serializers.SerializerMethodField(help_text="The current (remaining) balance of this subsidy.") is_active = serializers.BooleanField(read_only=True, help_text="Whether this subsidy is currently active.") - total_deposits = serializers.SerializerMethodField( + total_deposits = serializers.IntegerField( help_text="The aggregate of the initial balance plus all adjustments made on the subsidy in usd cents" ) @@ -68,10 +68,6 @@ class Meta: def get_current_balance(self, obj) -> int: return obj.current_balance() - @extend_schema_field(serializers.IntegerField) - def get_total_deposits(self, obj) -> int: - return obj.total_deposits() - class ReversalSerializer(serializers.ModelSerializer): """ diff --git a/enterprise_subsidy/apps/api/v1/tests/test_views.py b/enterprise_subsidy/apps/api/v1/tests/test_views.py index 2d74898a..d1f534d7 100644 --- a/enterprise_subsidy/apps/api/v1/tests/test_views.py +++ b/enterprise_subsidy/apps/api/v1/tests/test_views.py @@ -274,7 +274,7 @@ def test_get_adjustments_related_subsidy(self): "internal_only": False, "revenue_category": RevenueCategoryChoices.BULK_ENROLLMENT_PREPAY, "is_active": True, - "total_deposits": self.subsidy_5.total_deposits(), + "total_deposits": self.subsidy_5.total_deposits, } total_deposits_including_positive_adjustment = sum( [self.subsidy_5.starting_balance, APITestBase.adjustment_quantity_1] @@ -312,7 +312,7 @@ def test_get_adjustments_related_subsidy(self): "internal_only": False, "revenue_category": RevenueCategoryChoices.BULK_ENROLLMENT_PREPAY, "is_active": True, - "total_deposits": self.subsidy_5.total_deposits(), + "total_deposits": self.subsidy_5.total_deposits, } total_deposits_including_negative_adjustment = sum( diff --git a/enterprise_subsidy/apps/subsidy/models.py b/enterprise_subsidy/apps/subsidy/models.py index 95d9705b..2f8edd2f 100644 --- a/enterprise_subsidy/apps/subsidy/models.py +++ b/enterprise_subsidy/apps/subsidy/models.py @@ -21,7 +21,7 @@ from edx_rbac.utils import ALL_ACCESS_CONTEXT from model_utils.models import TimeStampedModel from openedx_ledger import api as ledger_api -from openedx_ledger.models import Adjustment, Ledger, TransactionStateChoices, UnitChoices +from openedx_ledger.models import Ledger, TransactionStateChoices, UnitChoices from openedx_ledger.utils import create_idempotency_key_for_transaction from requests.exceptions import HTTPError from rest_framework import status @@ -114,6 +114,8 @@ class Meta: Metaclass for Subsidy. """ ordering = ['-created'] + verbose_name = 'Subsidy' + verbose_name_plural = 'Subsidies' # Please reserve the "subsidy_type" field name for the future when we use it to distinguish between # LearnerCreditSubsidy vs. SubscriptionSubsidy. @@ -349,19 +351,17 @@ def price_for_content(self, content_key): def current_balance(self): return self.ledger.balance() + @property def total_deposits(self): """ - Returns the sum of all adjustments for the subsidy + the starting balance in USD cents. + Returns the sum of all value added to the subsidy. + + At the time of writing, this includes both deposits AND positive adjustments. Returns: - int: Sum of all adjustments and the starting balance in USD cents. - """ - adjustments_for_subsidy = Adjustment.objects.filter(ledger=self.ledger) - total_deposits = sum([ - adjustment.transaction.quantity - for adjustment in adjustments_for_subsidy - ], self.starting_balance) - return total_deposits + int: Sum of all value added to the subsidy, in USD cents. + """ + return self.ledger.total_deposits() def create_transaction( self,