Skip to content

Commit

Permalink
feat: Switch total_deposits() implementation to use Ledger function
Browse files Browse the repository at this point in the history
ENT-9075
  • Loading branch information
pwnage101 committed Jun 28, 2024
1 parent 2288649 commit 499f25c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
6 changes: 1 addition & 5 deletions enterprise_subsidy/apps/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions enterprise_subsidy/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 10 additions & 10 deletions enterprise_subsidy/apps/subsidy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 499f25c

Please sign in to comment.