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

feat: add additional fields to EnterpriseLearnerOfferApiSerializer #3963

Merged
merged 5 commits into from
May 10, 2023
Merged
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
Next Next commit
feat: add additional fields to EnterpriseLearnerOfferApiSerializer
adamstankiewicz committed May 8, 2023
commit 69830e5c09667d54d2e297e3482628424f036659
39 changes: 33 additions & 6 deletions ecommerce/extensions/api/serializers.py
Original file line number Diff line number Diff line change
@@ -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:
@@ -1153,30 +1153,57 @@ 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