Skip to content

Commit

Permalink
Merge pull request #536 from biocore/csymons_perk_fulfillment_toggle
Browse files Browse the repository at this point in the history
Add Toggle for Perk Fulfillment to Settings
  • Loading branch information
cassidysymons authored Sep 19, 2023
2 parents b4a39b1 + 11b6eb5 commit 736f52f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 32 deletions.
2 changes: 2 additions & 0 deletions microsetta_private_api/db/patches/0131.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add column to settings that controls whether Perk Fulfillment jobs run
ALTER TABLE ag.settings ADD COLUMN perk_fulfillment_active BOOLEAN DEFAULT FALSE NOT NULL;
9 changes: 9 additions & 0 deletions microsetta_private_api/repo/perk_fulfillment_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,12 @@ def _log_email(self, template, email_address, email_args):
"template_args": email_args
})
EventLogRepo(self._transaction).add_event(event)

def check_perk_fulfillment_active(self):
with self._transaction.dict_cursor() as cur:
cur.execute(
"SELECT perk_fulfillment_active "
"FROM ag.settings "
)
row = cur.fetchone()
return row['perk_fulfillment_active']
26 changes: 26 additions & 0 deletions microsetta_private_api/repo/tests/test_perk_fulfillment_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,32 @@ def test_get_subscription_fulfillments(
error_list = pfr.process_subscription_fulfillment(f_id)
self.assertEqual(len(error_list), 0)

def test_check_perk_fulfillment_active_true(self):
with Transaction() as t:
# Update the database column to reflect a state of True
cur = t.cursor()
cur.execute(
"UPDATE ag.settings SET perk_fulfillment_active = TRUE"
)

# Now verify that our helper function returns the correct state
pfr = PerkFulfillmentRepo(t)
res = pfr.check_perk_fulfillment_active()
self.assertTrue(res)

def test_check_perk_fulfillment_active_false(self):
with Transaction() as t:
# Update the database column to reflect a state of False
cur = t.cursor()
cur.execute(
"UPDATE ag.settings SET perk_fulfillment_active = FALSE"
)

# Now verify that our helper function returns the correct state
pfr = PerkFulfillmentRepo(t)
res = pfr.check_perk_fulfillment_active()
self.assertFalse(res)

def _count_ffq_registration_codes(self, t):
cur = t.cursor()
cur.execute(
Expand Down
76 changes: 44 additions & 32 deletions microsetta_private_api/util/perk_fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ def fulfill_new_transactions():

with Transaction() as t:
pfr = PerkFulfillmentRepo(t)
ftp_ids = pfr.get_pending_fulfillments()
pf_active = pfr.check_perk_fulfillment_active()

for ftp_id in ftp_ids:
if pf_active:
with Transaction() as t:
pfr = PerkFulfillmentRepo(t)
error_list = pfr.process_pending_fulfillment(ftp_id)
if len(error_list) > 0:
for error in error_list:
error_report.append(error)
ftp_ids = pfr.get_pending_fulfillments()

t.commit()
for ftp_id in ftp_ids:
with Transaction() as t:
pfr = PerkFulfillmentRepo(t)
error_list = pfr.process_pending_fulfillment(ftp_id)
if len(error_list) > 0:
for error in error_list:
error_report.append(error)

if len(error_report) > 0:
try:
send_email(SERVER_CONFIG['pester_email'], "pester_daniel",
{"what": "Perk Fulfillment Errors",
"content": error_report},
EN_US)
except: # noqa
# try our best to email
pass
t.commit()

if len(error_report) > 0:
try:
send_email(SERVER_CONFIG['pester_email'], "pester_daniel",
{"what": "Perk Fulfillment Errors",
"content": error_report},
EN_US)
except: # noqa
# try our best to email
pass


@celery.task(ignore_result=True)
Expand All @@ -42,28 +47,35 @@ def process_subscription_fulfillments():

with Transaction() as t:
pfr = PerkFulfillmentRepo(t)
fulfillment_ids = pfr.get_subscription_fulfillments()
pf_active = pfr.check_perk_fulfillment_active()

for fulfillment_id in fulfillment_ids:
if pf_active:
with Transaction() as t:
pfr = PerkFulfillmentRepo(t)
error_list = pfr.process_subscription_fulfillment(fulfillment_id)
fulfillment_ids = pfr.get_subscription_fulfillments()

if len(error_list) > 0:
for error in error_list:
error_report.append(error)
for fulfillment_id in fulfillment_ids:
with Transaction() as t:
pfr = PerkFulfillmentRepo(t)
error_list = pfr.process_subscription_fulfillment(
fulfillment_id
)

t.commit()
if len(error_list) > 0:
for error in error_list:
error_report.append(error)

if len(error_report) > 0:
try:
send_email(SERVER_CONFIG['pester_email'], "pester_daniel",
{"what": "Subscription Fulfillment Errors",
"content": error_report},
EN_US)
except: # noqa
# try our best to email
pass
t.commit()

if len(error_report) > 0:
try:
send_email(SERVER_CONFIG['pester_email'], "pester_daniel",
{"what": "Subscription Fulfillment Errors",
"content": error_report},
EN_US)
except: # noqa
# try our best to email
pass


@celery.task(ignore_result=True)
Expand Down

0 comments on commit 736f52f

Please sign in to comment.