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

Commit

Permalink
feat: use a braze api-triggered campaign for ent offer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Jun 29, 2022
1 parent 551d395 commit e9cd5ce
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"""
import logging
from datetime import datetime
from urllib.parse import urljoin

from django.conf import settings
from django.contrib.sites.models import Site
from django.core.management import BaseCommand
from django.db.models import Sum
from ecommerce_worker.email.v1.api import send_offer_usage_email
from requests.exceptions import RequestException

from ecommerce.extensions.fulfillment.status import ORDER
from ecommerce.core.models import User
from ecommerce.programs.custom import get_model

ConditionalOffer = get_model('offer', 'ConditionalOffer')
Expand All @@ -18,13 +21,6 @@
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# pylint: disable=line-too-long
EMAIL_BODY = """
You have used {percentage_usage}% of the {offer_type} Limit associated with the entitlement offer called "{offer_name}"
{offer_type}s Redeemed: {current_usage}
{offer_type}s Limit: {total_limit}
Please reach out to customersuccess@edx.org, or to your Account Manager or Customer Success representative, if you have any questions.
"""
EMAIL_SUBJECT = 'Offer Usage Notification'


Expand Down Expand Up @@ -62,35 +58,44 @@ def get_enrollment_limits(offer):
return int(offer.max_global_applications), percentage_usage, int(offer.num_orders)

@staticmethod
def get_booking_limits(offer):
def get_booking_limits(site, offer):
"""
Return the total discount limit, percentage usage and current usage of booking limit.
"""
total_used_discount_amount = OrderDiscount.objects.filter(
offer_id=offer.id,
order__status=ORDER.COMPLETE
).aggregate(Sum('amount'))['amount__sum']
total_used_discount_amount = total_used_discount_amount if total_used_discount_amount else 0

percentage_usage = int((total_used_discount_amount / offer.max_discount) * 100)
return int(offer.max_discount), percentage_usage, int(total_used_discount_amount)
api_client = site.siteconfiguration.oauth_api_client
enterprise_customer_uuid = offer.condition.enterprise_customer_uuid
offer_analytics_url = urljoin(
settings.ENTERPRISE_ANALYTICS_API_URL,
f'/enterprise/api/v1/enterprise/{enterprise_customer_uuid}/offers/{offer.id}/',
)
response = api_client.get(offer_analytics_url)
response.raise_for_status()
offer_analytics = response.json()

return (
offer_analytics['max_discount'],
offer_analytics['percent_of_offer_spent'],
offer_analytics['amount_of_offer_spent'],
)

def get_email_content(self, offer):
def get_email_content(self, site, offer):
"""
Return the appropriate email body and subject of given offer.
"""
is_enrollment_limit_offer = bool(offer.max_global_applications)
total_limit, percentage_usage, current_usage = self.get_enrollment_limits(offer) if is_enrollment_limit_offer \
else self.get_booking_limits(offer)

email_body = EMAIL_BODY.format(
percentage_usage=percentage_usage,
total_limit=total_limit if is_enrollment_limit_offer else "{}$".format(total_limit),
offer_type='Enrollment' if is_enrollment_limit_offer else 'Booking',
offer_name=offer.name,
current_usage=current_usage if is_enrollment_limit_offer else "{}$".format(current_usage),
total_limit, percentage_usage, current_usage = (
self.get_enrollment_limits(offer)
if is_enrollment_limit_offer
else self.get_booking_limits(site, offer)
)
return email_body, EMAIL_SUBJECT

return {
'percent_usage': percentage_usage,
'total_limit': total_limit if is_enrollment_limit_offer else "${}".format(total_limit),
'offer_type': 'Enrollment' if is_enrollment_limit_offer else 'Booking',
'offer_name': offer.name,
'current_usage': current_usage if is_enrollment_limit_offer else "${}".format(current_usage),
}

@staticmethod
def _get_enterprise_offers():
Expand All @@ -103,7 +108,7 @@ def _get_enterprise_offers():
).exclude(emails_for_usage_alert='')

def handle(self, *args, **options):
send_enterprise_offer_count = 0
successful_send_count = 0
enterprise_offers = self._get_enterprise_offers()
total_enterprise_offers_count = enterprise_offers.count()
logger.info('[Offer Usage Alert] Total count of enterprise offers is %s.', total_enterprise_offers_count)
Expand All @@ -114,16 +119,40 @@ def handle(self, *args, **options):
enterprise_offer.name,
enterprise_offer.id
)
send_enterprise_offer_count += 1
email_body, email_subject = self.get_email_content(enterprise_offer)
OfferUsageEmail.create_record(enterprise_offer, meta_data={
'email_body': email_body,
'email_subject': email_subject,
'email_addresses': enterprise_offer.emails_for_usage_alert
})
send_offer_usage_email.delay(enterprise_offer.emails_for_usage_alert, email_subject, email_body)
site = Site.objects.get_current()
try:
email_body_variables = self.get_email_content(site, enterprise_offer)
except RequestException as exc:
logger.warning(
'Exception getting offer email content for offer %s. Exception: %s',
enterprise_offer.id,
exc,
)
continue

lms_user_ids_by_email = {
user_email: User.get_lms_user_attribute_using_email(site, user_email, attribute='id')
for user_email in enterprise_offer.emails_for_usage_alert.strip().split(',')
}

task_result = send_offer_usage_email.delay(
lms_user_ids_by_email,
EMAIL_SUBJECT,
email_body_variables,
)
# Block until the task is done, since we're inside a management command
# and likely running from a job scheduler (ex. Jenkins).
# propagate=False means we won't re-raise (and exit this method) if any one task fails.
task_result.get(propagate=False)
if task_result.successful():
successful_send_count += 1
OfferUsageEmail.create_record(enterprise_offer, meta_data={
'email_usage_data': email_body_variables,
'email_subject': EMAIL_SUBJECT,
'email_addresses': enterprise_offer.emails_for_usage_alert
})
logger.info(
'[Offer Usage Alert] %s of %s added to the email sending queue.',
'[Offer Usage Alert] %s of %s offers with usage alerts configured had an email sent.',
successful_send_count,
total_enterprise_offers_count,
send_enterprise_offer_count
)
149 changes: 112 additions & 37 deletions ecommerce/enterprise/tests/test_send_enterprise_offer_limit_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
Contains the tests for sending the enterprise offer limit emails command.
"""
import datetime
import logging
from urllib.parse import urljoin

import mock
import responses
from django.conf import settings
from django.core.management import call_command
from testfixtures import LogCapture

from ecommerce.enterprise.tests.mixins import EnterpriseServiceMockMixin
from ecommerce.extensions.test.factories import EnterpriseOfferFactory
from ecommerce.programs.custom import get_model
from ecommerce.tests.mixins import SiteMixin
from ecommerce.tests.testcases import TestCase

ConditionalOffer = get_model('offer', 'ConditionalOffer')
OfferUsageEmail = get_model('offer', 'OfferUsageEmail')

LOGGER_NAME = 'ecommerce.enterprise.management.commands.send_enterprise_offer_limit_emails'
COMMAND_PATH = 'ecommerce.enterprise.management.commands.send_enterprise_offer_limit_emails'
LOGGER_NAME = COMMAND_PATH


class SendEnterpriseOfferLimitEmailsTests(TestCase):
class SendEnterpriseOfferLimitEmailsTests(TestCase, SiteMixin, EnterpriseServiceMockMixin):
"""
Tests the sending the enterprise offer limit emails command.
"""
Expand All @@ -30,62 +34,133 @@ def setUp(self):
"""
super(SendEnterpriseOfferLimitEmailsTests, self).setUp()

EnterpriseOfferFactory(max_global_applications=10)
EnterpriseOfferFactory(max_discount=100)
self.mock_access_token_response()

self.offer_1 = EnterpriseOfferFactory(max_discount=100)
self.offer_2 = EnterpriseOfferFactory(max_discount=100)

# Make two more offers that are not eligible for alerts
# by setting their max applications/discounts to 0.
EnterpriseOfferFactory(max_global_applications=0)
EnterpriseOfferFactory(max_discount=0)

# Creating conditionaloffer with daily frequency and adding corresponding offer_usage object.
offer_with_daily_frequency = EnterpriseOfferFactory(max_global_applications=10)
offer_usage = OfferUsageEmail.create_record(offer_with_daily_frequency)
self.offer_with_daily_frequency = EnterpriseOfferFactory(max_global_applications=10)
offer_usage = OfferUsageEmail.create_record(self.offer_with_daily_frequency)
offer_usage.created = datetime.datetime.fromordinal(datetime.datetime.now().toordinal() - 2)
offer_usage.save()

# Creating conditionaloffer with weekly frequency and adding corresponding offer_usage object.
offer_with_weekly_frequency = EnterpriseOfferFactory(
self.offer_with_weekly_frequency = EnterpriseOfferFactory(
max_global_applications=10,
usage_email_frequency=ConditionalOffer.WEEKLY
)
offer_usage = OfferUsageEmail.create_record(offer_with_weekly_frequency)
offer_usage = OfferUsageEmail.create_record(self.offer_with_weekly_frequency)
offer_usage.created = datetime.datetime.fromordinal(datetime.datetime.now().toordinal() - 8)
offer_usage.save()

# Creating conditionaloffer with monthly frequency and adding corresponding offer_usage object.
offer_with_monthly_frequency = EnterpriseOfferFactory(
self.offer_with_monthly_frequency = EnterpriseOfferFactory(
max_global_applications=10,
usage_email_frequency=ConditionalOffer.MONTHLY
)
offer_usage = OfferUsageEmail.create_record(offer_with_monthly_frequency)
offer_usage = OfferUsageEmail.create_record(self.offer_with_monthly_frequency)
offer_usage.created = datetime.datetime.fromordinal(datetime.datetime.now().toordinal() - 31)
offer_usage.save()

# Add an offer that is eligible for the usage email,
# but has no corresponding mock response configured,
# so that it hits a 404 and is appropriately handled by the try/except
# block for RequestExceptions inside the command's handle() method.
self.offer_with_404 = EnterpriseOfferFactory(max_discount=100)

def mock_lms_user_responses(self, user_ids_by_email):
api_url = urljoin(f"{self.site.siteconfiguration.user_api_url}/", "accounts/search_emails")

for _, user_id in user_ids_by_email.items():
responses.add(
responses.POST,
api_url,
json=[{'id': user_id}],
content_type='application/json',
)

def mock_offer_analytics_response(self, enterprise_uuid, offer_id):
route = f'/enterprise/api/v1/enterprise/{enterprise_uuid}/offers/{offer_id}/'
api_url = f'{settings.ENTERPRISE_ANALYTICS_API_URL}{route}'
responses.add(
responses.GET,
api_url,
json={
'max_discount': 10000.0,
'percent_of_offer_spent': 50.0,
'amount_of_offer_spent': 5000.0,
},
content_type='application/json',
)

@responses.activate
def test_command(self):
"""
Test the send_enterprise_offer_limit_emails command
"""
offer_usage_count = OfferUsageEmail.objects.all().count()
cmd_path = 'ecommerce.enterprise.management.commands.send_enterprise_offer_limit_emails'
with mock.patch(cmd_path + '.send_offer_usage_email.delay') as mock_send_email:
with LogCapture(level=logging.INFO) as log:
mock_send_email.return_value = mock.Mock()
call_command('send_enterprise_offer_limit_emails')
assert mock_send_email.call_count == 5
assert OfferUsageEmail.objects.all().count() == offer_usage_count + 5
log.check_present(
(
LOGGER_NAME,
'INFO',
'[Offer Usage Alert] Total count of enterprise offers is {total_enterprise_offers_count}.'.format(
total_enterprise_offers_count=7
)
),
(
LOGGER_NAME,
'INFO',
'[Offer Usage Alert] {total_enterprise_offers_count} of {send_enterprise_offer_count} added to the'
' email sending queue.'.format(
total_enterprise_offers_count=7,
send_enterprise_offer_count=5
)
)
)
admin_email_1, admin_email_2 = 'example_1@example.com', 'example_2@example.com'
self.mock_lms_user_responses({
admin_email_1: 22,
admin_email_2: 44,
})

# Don't mock out a response for self.offer_with_404
for offer in ConditionalOffer.objects.exclude(id=self.offer_with_404.id):
self.mock_offer_analytics_response(offer.condition.enterprise_customer_uuid, offer.id)

with mock.patch(COMMAND_PATH + '.send_offer_usage_email.delay') as mock_send_email:
mock_send_email.return_value = mock.Mock()
call_command('send_enterprise_offer_limit_emails')
# if self.offer_with_404 had email content, this 5 would be a 6.
assert mock_send_email.call_count == 5
assert OfferUsageEmail.objects.all().count() == offer_usage_count + 5

mock_send_email.assert_has_calls([
mock.call(
{'example_1@example.com': 22, ' example_2@example.com': 44},
'Offer Usage Notification',
{'percent_usage': 50.0, 'total_limit': '$10000.0', 'offer_type': 'Booking',
'offer_name': self.offer_1.name, 'current_usage': '$5000.0'}
),
mock.call().get(propagate=False),
mock.call().successful(),
mock.call(
{'example_1@example.com': 44, ' example_2@example.com': 44},
'Offer Usage Notification',
{'percent_usage': 50.0, 'total_limit': '$10000.0', 'offer_type': 'Booking',
'offer_name': self.offer_2.name, 'current_usage': '$5000.0'}
),
mock.call().get(propagate=False),
mock.call().successful(),
mock.call(
{'example_1@example.com': 44, ' example_2@example.com': 44},
'Offer Usage Notification',
{'percent_usage': 0, 'total_limit': 10, 'offer_type': 'Enrollment',
'offer_name': self.offer_with_daily_frequency.name, 'current_usage': 0}
),
mock.call().get(propagate=False),
mock.call().successful(),
mock.call(
{'example_1@example.com': 44, ' example_2@example.com': 44},
'Offer Usage Notification',
{'percent_usage': 0, 'total_limit': 10, 'offer_type': 'Enrollment',
'offer_name': self.offer_with_weekly_frequency.name, 'current_usage': 0}
),
mock.call().get(propagate=False),
mock.call().successful(),
mock.call(
{'example_1@example.com': 44, ' example_2@example.com': 44},
'Offer Usage Notification',
{'percent_usage': 0, 'total_limit': 10, 'offer_type': 'Enrollment',
'offer_name': self.offer_with_monthly_frequency.name, 'current_usage': 0}
),
mock.call().get(propagate=False),
mock.call().successful(),
])
2 changes: 2 additions & 0 deletions ecommerce/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@

ENTERPRISE_CATALOG_SERVICE_URL = 'http://localhost:18160/'

ENTERPRISE_ANALYTICS_API_URL = 'http://localhost:19001'

ENTERPRISE_LEARNER_PORTAL_HOSTNAME = os.environ.get('ENTERPRISE_LEARNER_PORTAL_HOSTNAME', 'localhost:8734')

# Name for waffle switch to use for enabling enterprise features on runtime.
Expand Down
2 changes: 2 additions & 0 deletions ecommerce/settings/devstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@

ENTERPRISE_CATALOG_API_URL = urljoin(f"{ENTERPRISE_CATALOG_SERVICE_URL}/", 'api/v1/')

ENTERPRISE_ANALYTICS_API_URL = 'http://edx.devstack.analyticsapi:19001'

# PAYMENT PROCESSING
PAYMENT_PROCESSOR_CONFIG = {
'edx': {
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ edx-drf-extensions==6.6.0
# -c requirements/pins.txt
# -r requirements/base.in
# edx-rbac
edx-ecommerce-worker==3.1.2
edx-ecommerce-worker==3.2.0
# via -r requirements/base.in
edx-opaque-keys==2.2.2
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ edx-drf-extensions==6.6.0
# via
# -r requirements/test.txt
# edx-rbac
edx-ecommerce-worker==3.1.2
edx-ecommerce-worker==3.2.0
# via -r requirements/test.txt
edx-i18n-tools==0.8.1
# via -r requirements/test.txt
Expand Down
Loading

0 comments on commit e9cd5ce

Please sign in to comment.