Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: add additional fields to EnterpriseLearnerOfferApiSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed May 8, 2023
1 parent b21e122 commit 879bcb3
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions ecommerce/extensions/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ class Meta:

def _serialize_remaining_balance_value(conditional_offer):
"""
Change value into string and return it unless it is None.
Calculate and return remaining balance on the offer.
"""
remaining_balance = calculate_remaining_offer_balance(conditional_offer)
if remaining_balance is not None:
Expand All @@ -1153,30 +1153,55 @@ class EnterpriseLearnerOfferApiSerializer(serializers.BaseSerializer): # pylint
"""

def _serialize_remaining_balance_for_user(self, instance):
"""
Determines the remaining balance for the user.
"""
request = self.context.get('request')

if request and instance.max_user_discount is not None:
return str(instance.max_user_discount - sum_user_discounts_for_offer(request.user, instance))

return None

def _serialize_remaining_applications_value(self, instance):
"""
Calculate and return remaining number of applications on the offer.
"""
if instance.max_global_applications is not None:
return instance.max_global_applications - instance.num_applications

return None

def _serialize_remaining_applications_for_user(self, instance):
"""
Determines the remaining number of applications (enrollments) for the user.
"""
request = self.context.get('request')
if request and instance.max_user_applications is not None:
return instance.max_user_applications - instance.get_num_user_applications(request.user)
return None

def to_representation(self, instance):
representation = OrderedDict()

representation['id'] = instance.id
representation['max_discount'] = instance.max_discount
representation['enterprise_customer_uuid'] = instance.condition.enterprise_customer_uuid
representation['enterprise_catalog_uuid'] = instance.condition.enterprise_customer_catalog_uuid
representation['is_current'] = instance.is_current
representation['status'] = instance.status
representation['start_datetime'] = instance.start_datetime
representation['end_datetime'] = instance.end_datetime
representation['enterprise_catalog_uuid'] = instance.condition.enterprise_customer_catalog_uuid
representation['usage_type'] = get_benefit_type(instance.benefit)
representation['discount_value'] = instance.benefit.value
representation['status'] = instance.status
representation['remaining_balance'] = _serialize_remaining_balance_value(instance)
representation['is_current'] = instance.is_current
representation['max_discount'] = instance.max_discount
representation['max_global_applications'] = instance.max_global_applications
representation['max_user_applications'] = instance.max_user_applications
representation['max_user_discount'] = instance.max_user_discount
representation['num_applications'] = instance.num_applications
representation['remaining_balance'] = _serialize_remaining_balance_value(instance)
representation['remaining_applications'] = self._serialize_remaining_applications_value(instance)
representation['remaining_balance_for_user'] = self._serialize_remaining_balance_for_user(instance)
representation['remaining_applications_for_user'] = self._serialize_remaining_applications_for_user(instance)

return representation

Expand Down

0 comments on commit 879bcb3

Please sign in to comment.