-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from GooeyAI/send-low-balance-email
Send Low Balance Email
- Loading branch information
Showing
10 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app_users/migrations/0012_appuser_low_balance_email_sent_at.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.7 on 2024-02-14 07:23 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('app_users', '0011_appusertransaction_charged_amount_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='appuser', | ||
name='low_balance_email_sent_at', | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import smtplib | ||
import sys | ||
import typing | ||
from email.mime.application import MIMEApplication | ||
from email.mime.multipart import MIMEMultipart | ||
|
@@ -12,6 +13,7 @@ | |
from daras_ai_v2 import settings | ||
from daras_ai_v2.settings import templates | ||
from gooey_ui import UploadedFile | ||
from routers.billing import account_url | ||
|
||
|
||
def send_reported_run_email( | ||
|
@@ -43,6 +45,31 @@ def send_reported_run_email( | |
) | ||
|
||
|
||
def send_low_balance_email( | ||
*, | ||
user: AppUser, | ||
total_credits_consumed: int, | ||
): | ||
recipeints = "[email protected], [email protected]" | ||
html_body = templates.get_template("low_balance_email.html").render( | ||
user=user, | ||
url=account_url, | ||
total_credits_consumed=total_credits_consumed, | ||
settings=settings, | ||
) | ||
send_email_via_postmark( | ||
from_address=settings.SUPPORT_EMAIL, | ||
to_address=user.email or recipeints, | ||
bcc=recipeints, | ||
subject="Your Gooey.AI credit balance is low", | ||
html_body=html_body, | ||
) | ||
|
||
|
||
is_running_pytest = "pytest" in sys.modules | ||
pytest_outbox = [] | ||
|
||
|
||
def send_email_via_postmark( | ||
*, | ||
from_address: str, | ||
|
@@ -56,6 +83,21 @@ def send_email_via_postmark( | |
"outbound", "gooey-ai-workflows", "announcements" | ||
] = "outbound", | ||
): | ||
if is_running_pytest: | ||
pytest_outbox.append( | ||
dict( | ||
from_address=from_address, | ||
to_address=to_address, | ||
cc=cc, | ||
bcc=bcc, | ||
subject=subject, | ||
html_body=html_body, | ||
text_body=text_body, | ||
message_stream=message_stream, | ||
), | ||
) | ||
return | ||
|
||
r = requests.post( | ||
"https://api.postmarkapp.com/email", | ||
headers={ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<p> | ||
Hey {{ user.display_name }}! | ||
</p> | ||
|
||
<p> | ||
This is a friendly reminder that your Gooey.AI balance is now just {{ user.balance }}. | ||
Your account has consumed {{ total_credits_consumed }} credits in the last {{ settings.LOW_BALANCE_EMAIL_DAYS }} days. | ||
<br><br> | ||
To buy more credits, please visit <a href="{{ url }}">https://gooey.ai/account</a>. | ||
<br><br> | ||
As always, email us at [email protected] if you have any questions too. | ||
<br><br> | ||
Thanks again for your business, | ||
<br> | ||
Sean and the Gooey.AI team | ||
</p> | ||
{{ "{{{ pm:unsubscribe }}}" }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
from django.utils import timezone | ||
|
||
from app_users.models import AppUserTransaction | ||
from bots.models import AppUser | ||
from celeryapp.tasks import run_low_balance_email_check | ||
from daras_ai_v2 import settings | ||
from daras_ai_v2.send_email import pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_feature_is_disabled(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", is_paying=True, balance=0, is_anonymous=False | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = False | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_user_is_not_paying(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", is_paying=False, balance=0, is_anonymous=False | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_user_has_enough_balance(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", is_paying=True, balance=500, is_anonymous=False | ||
) | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_user_has_been_emailed_recently(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", | ||
is_paying=True, | ||
balance=66, | ||
is_anonymous=False, | ||
low_balance_email_sent_at=timezone.now(), | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
settings.LOW_BALANCE_EMAIL_DAYS = 1 | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_send_email_if_user_has_been_email_recently_but_made_a_purchase( | ||
transactional_db, | ||
): | ||
user = AppUser.objects.create( | ||
uid="test_user", | ||
is_paying=True, | ||
balance=22, | ||
is_anonymous=False, | ||
low_balance_email_sent_at=timezone.now(), | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_1", | ||
user=user, | ||
amount=100, | ||
created_at=timezone.now(), | ||
end_balance=100, | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_2", | ||
user=user, | ||
amount=-78, | ||
created_at=timezone.now(), | ||
end_balance=22, | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
settings.LOW_BALANCE_EMAIL_DAYS = 1 | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
run_low_balance_email_check(user.uid) | ||
|
||
assert len(pytest_outbox) == 1 | ||
assert " 22" in pytest_outbox[0]["html_body"] | ||
assert " 78" in pytest_outbox[0]["html_body"] | ||
|
||
pytest_outbox.clear() | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_send_email(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", | ||
is_paying=True, | ||
balance=66, | ||
is_anonymous=False, | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_1", | ||
user=user, | ||
amount=-100, | ||
created_at=timezone.now() - timezone.timedelta(days=2), | ||
end_balance=150 + 66, | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_2", | ||
user=user, | ||
amount=-150, | ||
created_at=timezone.now(), | ||
end_balance=66, | ||
) | ||
|
||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
settings.LOW_BALANCE_EMAIL_DAYS = 1 | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
|
||
run_low_balance_email_check(user.uid) | ||
assert len(pytest_outbox) == 1 | ||
body = pytest_outbox[0]["html_body"] | ||
assert " 66" in body | ||
assert " 150" in body | ||
assert " pm:unsubscribe" in body | ||
assert " 100" not in body | ||
|
||
pytest_outbox.clear() | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox |