Skip to content

Commit

Permalink
feat: improve backpopulate job specificity (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnagro authored Nov 22, 2023
1 parent 23e712f commit d77eb45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def process_transaction(self, subsidy, txn):
"""
logger.info(f"Processing {subsidy.uuid} transaction {txn.uuid}")

if txn.lms_user_email is None:
if txn.lms_user_email is None and txn.lms_user_id is not None:
lms_user_email = subsidy.email_for_learner(txn.lms_user_id)
txn.lms_user_email = lms_user_email
logger.info(f"Found {lms_user_email} for {subsidy.uuid} transaction {txn.uuid}")
if txn.content_title is None:
if txn.content_title is None and txn.content_key is not None:
content_title = subsidy.title_for_content(txn.content_key)
txn.content_title = content_title
logger.info(f"Found {content_title} for {subsidy.uuid} transaction {txn.uuid}")
Expand Down Expand Up @@ -85,9 +85,13 @@ def handle(self, *args, **options):
for subsidies in batch_by_pk(Subsidy, extra_filter=subsidy_filter):
for subsidy in subsidies:
logger.info(f"Processing subsidy {subsidy.uuid}")

subsidy_filter = Q(ledger=subsidy.ledger)
incomplete_only_filter = Q(lms_user_email__isnull=True) | Q(content_title__isnull=True)
incomplete_email = Q(lms_user_email__isnull=True) & Q(lms_user_id__isnull=False)
incomplete_title = Q(content_title__isnull=True) & Q(content_key__isnull=False)
incomplete_only_filter = incomplete_email | incomplete_title
txn_filter = subsidy_filter & incomplete_only_filter

for txns in batch_by_pk(Transaction, extra_filter=txn_filter):
for txn in txns:
self.process_transaction(subsidy, txn)
15 changes: 13 additions & 2 deletions enterprise_subsidy/apps/transaction/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ def setUp(self):
ledger=self.internal_ledger,
lms_user_email=None,
content_title=None,
quantity=100,
fulfillment_identifier=self.fulfillment_identifier
)
self.transaction_not_to_backpopulate = TransactionFactory(
ledger=self.ledger,
lms_user_id=None,
lms_user_email=None,
content_key=None,
content_title=None,
)

@mock.patch('enterprise_subsidy.apps.api_client.base_oauth.OAuthAPIClient', return_value=mock.MagicMock())
Expand Down Expand Up @@ -933,10 +938,13 @@ def test_backpopulate_transaction_email_and_title(
call_command('backpopulate_transaction_email_and_title')
self.transaction_to_backpopulate.refresh_from_db()
self.internal_transaction_to_backpopulate.refresh_from_db()
self.transaction_not_to_backpopulate.refresh_from_db()
assert self.transaction_to_backpopulate.lms_user_email == expected_email_address
assert self.transaction_to_backpopulate.content_title == expected_content_title
assert self.internal_transaction_to_backpopulate.lms_user_email is None
assert self.internal_transaction_to_backpopulate.content_title is None
assert self.transaction_not_to_backpopulate.lms_user_email is None
assert self.transaction_not_to_backpopulate.content_title is None

@mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.lms_user_client")
@mock.patch("enterprise_subsidy.apps.content_metadata.api.ContentMetadataApi.get_content_summary")
Expand Down Expand Up @@ -965,7 +973,10 @@ def test_backpopulate_transaction_email_and_title_include_internal(
call_command('backpopulate_transaction_email_and_title', include_internal_subsidies=True)
self.transaction_to_backpopulate.refresh_from_db()
self.internal_transaction_to_backpopulate.refresh_from_db()
self.transaction_not_to_backpopulate.refresh_from_db()
assert self.transaction_to_backpopulate.lms_user_email == expected_email_address
assert self.transaction_to_backpopulate.content_title == expected_content_title
assert self.internal_transaction_to_backpopulate.lms_user_email == expected_email_address
assert self.internal_transaction_to_backpopulate.content_title == expected_content_title
assert self.transaction_not_to_backpopulate.lms_user_email is None
assert self.transaction_not_to_backpopulate.content_title is None

0 comments on commit d77eb45

Please sign in to comment.