From 2f6de240802b07e744bcd84a726e5ca3525a7243 Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 17 Dec 2024 11:51:12 -0500 Subject: [PATCH] Re-enable failed payment emails --- billing/helpers.py | 15 +++++++++++++++ billing/views.py | 22 +++++++++++----------- services/task/task.py | 2 -- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/billing/helpers.py b/billing/helpers.py index a284fddc25..c66e6333cb 100644 --- a/billing/helpers.py +++ b/billing/helpers.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.db.models import QuerySet from shared.plan.constants import ENTERPRISE_CLOUD_USER_PLAN_REPRESENTATIONS from codecov_auth.models import Owner @@ -8,3 +9,17 @@ def on_enterprise_plan(owner: Owner) -> bool: return settings.IS_ENTERPRISE or ( owner.plan in ENTERPRISE_CLOUD_USER_PLAN_REPRESENTATIONS.keys() ) + + +def get_all_admins_for_owners(owners: QuerySet[Owner]): + admin_ids = set() + for owner in owners: + if owner.admins: + admin_ids.update(owner.admins) + + # Add the owner's email as well - for user owners, admins is empty. + if owner.email: + admin_ids.add(owner.ownerid) + + admins: QuerySet[Owner] = Owner.objects.filter(pk__in=admin_ids) + return admins diff --git a/billing/views.py b/billing/views.py index 9094e97959..b4878b54fc 100644 --- a/billing/views.py +++ b/billing/views.py @@ -12,6 +12,7 @@ from rest_framework.views import APIView from shared.plan.service import PlanService +from billing.helpers import get_all_admins_for_owners from codecov_auth.models import Owner from services.task.task import TaskService @@ -66,17 +67,7 @@ def invoice_payment_failed(self, invoice: stripe.Invoice) -> None: self._log_updated(list(owners)) # Send failed payment email to all owner admins - - admin_ids = set() - for owner in owners: - if owner.admins: - admin_ids.update(owner.admins) - - # Add the owner's email as well - for user owners, admins is empty. - if owner.email: - admin_ids.add(owner.ownerid) - - admins: QuerySet[Owner] = Owner.objects.filter(pk__in=admin_ids) + admins = get_all_admins_for_owners(owners) task_service = TaskService() card = ( @@ -102,6 +93,15 @@ def invoice_payment_failed(self, invoice: stripe.Invoice) -> None: **template_vars, ) + # temporary just making sure these look okay in the real world + task_service.send_email( + to_addr="spencer.murray@sentry.io", + subject="Your Codecov payment failed", + template_name="failed-payment", + name="spalmurray-codecov", + **template_vars, + ) + def customer_subscription_deleted(self, subscription: stripe.Subscription) -> None: log.info( "Customer Subscription Deleted - Setting free plan and deactivating repos for stripe customer", diff --git a/services/task/task.py b/services/task/task.py index 793f7ff04a..0aa1baf8ca 100644 --- a/services/task/task.py +++ b/services/task/task.py @@ -402,8 +402,6 @@ def send_email( from_addr: str | None = None, **kwargs, ): - # Disabling this while we fix HTML templates. - return # Templates can be found in worker/templates self._create_signature( "app.tasks.send_email.SendEmail",