diff --git a/microsetta_private_api/celery_utils.py b/microsetta_private_api/celery_utils.py index 17a2d837d..49e776ca0 100644 --- a/microsetta_private_api/celery_utils.py +++ b/microsetta_private_api/celery_utils.py @@ -45,18 +45,18 @@ def __call__(self, *args, **kwargs): "task": "microsetta_private_api.util.fundrazr.get_fundrazr_transactions", # noqa "schedule": 60 * 60 # every hour }, - "fulfill_new_transactions": { - "task": "microsetta_private_api.util.perk_fulfillment.fulfill_new_transactions", # noqa - "schedule": 60 * 60 # every hour - }, + # "fulfill_new_transactions": { + # "task": "microsetta_private_api.util.perk_fulfillment.fulfill_new_transactions", # noqa + # "schedule": 60 * 60 # every hour + # }, # "fulfill_subscriptions": { # "task": "microsetta_private_api.util.perk_fulfillment.process_subscription_fulfillments", # noqa # "schedule": 60 * 60 * 24 # every 24 hours # }, - # "check_shipping_updates": { - # "task": "microsetta_private_api.util.perk_fulfillment.check_shipping_updates", # noqa - # "schedule": 60 * 60 * 4 # every 4 hours - # }, + "check_shipping_updates": { + "task": "microsetta_private_api.util.perk_fulfillment.check_shipping_updates", # noqa + "schedule": 60 * 60 * 4 # every 4 hours + }, # "perks_without_fulfillment_details": { # "task": "microsetta_private_api.util.perk_fulfillment.perks_without_fulfillment_details", # noqa # "schedule": 60 * 60 * 24 # every 24 hours diff --git a/microsetta_private_api/repo/perk_fulfillment_repo.py b/microsetta_private_api/repo/perk_fulfillment_repo.py index b52d705c6..011a80268 100644 --- a/microsetta_private_api/repo/perk_fulfillment_repo.py +++ b/microsetta_private_api/repo/perk_fulfillment_repo.py @@ -78,12 +78,12 @@ def process_pending_fulfillment(self, ftp_id): # been processed and collect the necessary fields cur.execute( "SELECT ftp.id ftp_id, ftp.transaction_id, ftp.perk_id, " - "ftp.quantity, ft.payer_email, fpfd.ffq_quantity, " + "ftp.quantity, fpfd.ffq_quantity, " "fpfd.kit_quantity, fpfd.dak_article_code, " "fpfd.fulfillment_spacing_number, " "fpfd.fulfillment_spacing_unit, iu.first_name, iu.last_name, " "iu.phone, iu.address_1, iu.address_2, iu.city, iu.state, " - "iu.postal_code, iu.country, iu.campaign_id " + "iu.postal_code, iu.country, iu.campaign_id, iu.email " "FROM campaign.fundrazr_transaction_perk ftp " "INNER JOIN campaign.transaction ft " "ON ftp.transaction_id = ft.id " @@ -99,7 +99,7 @@ def process_pending_fulfillment(self, ftp_id): if row is not None: if self._is_subscription(row): subscription_id = \ - self._create_subscription(row['payer_email'], + self._create_subscription(row['email'], row['transaction_id'], row['ftp_id']) else: @@ -119,7 +119,7 @@ def process_pending_fulfillment(self, ftp_id): error_info = self._fulfill_ffq( row['ftp_id'], template, - row['payer_email'], + row['email'], row['first_name'], subscription_id ) @@ -148,7 +148,7 @@ def process_pending_fulfillment(self, ftp_id): error_info = self._fulfill_ffq( row['ftp_id'], row['kit_quantity'], - row['payer_email'], + row['email'], row['first_name'] ) if error_info is not None: @@ -230,7 +230,7 @@ def process_subscription_fulfillment(self, fulfillment_id): cur.execute( "SELECT sf.fulfillment_id, sf.fulfillment_type, " "sf.dak_article_code, sf.subscription_id, ftp.id ftp_id, " - "ft.payer_email, iu.first_name, iu.last_name, iu.phone, " + "iu.email iu_email, iu.first_name, iu.last_name, iu.phone, " "iu.address_1, iu.address_2, iu.city, iu.state, " "iu.postal_code, iu.country, iu.campaign_id, s.account_id, " "a.email a_email, a.first_name a_first_name, " @@ -265,7 +265,7 @@ def process_subscription_fulfillment(self, fulfillment_id): first_name = row['a_first_name'] # If no account, fall back to original Fundrazr data else: - email = row['payer_email'] + email = row['iu_email'] first_name = row['first_name'] email_error = self._fulfill_ffq( @@ -321,7 +321,7 @@ def check_for_shipping_updates(self): cur.execute( "SELECT fdo.fundrazr_transaction_perk_id ftp_id, " "fdo.dak_order_id, kit.outbound_fedex_tracking, " - "t.payer_email, t.payer_first_name " + "iu.email, iu.first_name " "FROM campaign.fundrazr_daklapack_orders fdo " "INNER JOIN barcodes.daklapack_order dako " "ON fdo.dak_order_id = dako.dak_order_id " @@ -334,6 +334,8 @@ def check_for_shipping_updates(self): "ON fdo.fundrazr_transaction_perk_id = ftp.id " "INNER JOIN campaign.transaction t " "ON ftp.transaction_id = t.id " + "INNER JOIN campaign.interested_users iu " + "ON t.interested_user_id = iu.interested_user_id " "WHERE fdo.tracking_sent = false" ) rows = cur.fetchall() @@ -341,11 +343,11 @@ def check_for_shipping_updates(self): for r in rows: try: email_args = { - "first_name": r['payer_first_name'], + "first_name": r['first_name'], "tracking_number": r['outbound_fedex_tracking'] } - email_address = r['payer_email'] + email_address = r['email'] send_email( email_address, @@ -663,7 +665,9 @@ def get_unclaimed_subscriptions_by_email(self, email): "FROM campaign.subscriptions s " "INNER JOIN campaign.transaction t " "ON s.transaction_id = t.id " - "WHERE account_id IS NULL AND LOWER(t.payer_email) = %s", + "INNER JOIN campaign.interested_users iu " + "ON t.interested_user_id = iu.interested_user_id " + "WHERE account_id IS NULL AND LOWER(iu.email) = %s", (email, ) ) rows = cur.fetchall() diff --git a/microsetta_private_api/repo/transaction.py b/microsetta_private_api/repo/transaction.py index 94648d89b..79826b356 100644 --- a/microsetta_private_api/repo/transaction.py +++ b/microsetta_private_api/repo/transaction.py @@ -6,10 +6,10 @@ class Transaction: - # Note: SimpleConnectionPool works only for single threaded applications - # Should we make the server multi threaded, we must switch to a - # ThreadedConnectionPool - _POOL = psycopg2.pool.SimpleConnectionPool( + # Note: Using ThreadedConnectionPool as we've switched Celery to threaded + # mode. If we change that back to prefork, recommend changing the pool + # back to SimpleConnectionPool. + _POOL = psycopg2.pool.ThreadedConnectionPool( 1, 20, user=AMGUT_CONFIG.user, diff --git a/microsetta_private_api/util/perk_fulfillment.py b/microsetta_private_api/util/perk_fulfillment.py index a8d0b9cad..2a4cfd3bb 100644 --- a/microsetta_private_api/util/perk_fulfillment.py +++ b/microsetta_private_api/util/perk_fulfillment.py @@ -73,6 +73,8 @@ def check_shipping_updates(): emails_sent, error_report = pfr.check_for_shipping_updates() if emails_sent > 0 or len(error_report) > 0: + t.commit() + email_content = f"Emails sent: {emails_sent}\n"\ f"Errors: {error_report}" try: