Skip to content

Commit

Permalink
Add an invite email and new route that creates a subscriber and email…
Browse files Browse the repository at this point in the history
…s them!
  • Loading branch information
MelissaAutumn committed May 8, 2024
1 parent 21c6912 commit 1e0e94f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
16 changes: 16 additions & 0 deletions backend/src/appointment/controller/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,19 @@ def text(self):

def html(self):
return get_template("support.jinja2").render(requestee=self.requestee, topic=self.topic, details=self.details)


class InviteAccountMail(Mailer):
def __init__(self, *args, **kwargs):
default_kwargs = {
"subject": l10n('new-account-mail-subject')
}
super(InviteAccountMail, self).__init__(*args, **default_kwargs, **kwargs)

def text(self):
return l10n('new-account-mail-plain', {
'homepage_url': os.getenv('FRONTEND_URL'),
})

def html(self):
return get_template("new_account.jinja2").render(homepage_url=os.getenv('FRONTEND_URL'))
5 changes: 1 addition & 4 deletions backend/src/appointment/dependencies/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ def get_subscriber(

def get_admin_subscriber(
user: models.Subscriber = Depends(get_subscriber),
db: Session = Depends(get_db),
):
if user is None:
raise InvalidTokenException()

"""Retrieve the subscriber and check if they're an admin"""
# check admin allow list
admin_emails = os.getenv("APP_ADMIN_ALLOW_LIST", '').split(',')
if not any([user.email.endswith(allowed_email) for allowed_email in admin_emails]):
Expand Down
11 changes: 11 additions & 0 deletions backend/src/appointment/l10n/en/email.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,14 @@ support-mail-plain = { $requestee_name } ({ $requestee_email }) sent the followi
Topic: { $topic }
Details: { $details }
{-brand-footer}
## New/Invited Account Email
new-account-mail-subject = You've been invited to Thunderbird Appointment
new-account-mail-action = Continue to Thunderbird Appointment
new-account-mail-html-heading = You've been invited to Thunderbird Appointment. Login with this email address to continue.
# Variables:
# $homepage_url (String) - URL to Thunderbird Appointment
new-account-mail-plain = You've been invited to Thunderbird Appointment.
Login with this email address to continue.
{ $homepage_url }
{-brand-footer}
31 changes: 29 additions & 2 deletions backend/src/appointment/routes/invite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, BackgroundTasks

from sqlalchemy.orm import Session

Expand All @@ -9,6 +8,7 @@
from ..dependencies.database import get_db

from ..exceptions import validation
from ..tasks.emails import send_invite_account_email

router = APIRouter()

Expand Down Expand Up @@ -48,3 +48,30 @@ def revoke_invite_code(code: str, db: Session = Depends(get_db), admin: Subscrib
if not repo.invite.code_is_available(db, code):
raise validation.InviteCodeNotAvailableException()
return repo.invite.revoke_code(db, code)


@router.post("/send", response_model=schemas.Invite)
def send_invite_email(
background_tasks: BackgroundTasks,
email: str,
db: Session = Depends(get_db),
# Note admin must be here to for permission reasons
_admin: Subscriber = Depends(get_admin_subscriber)
):
"""With a given email address, generate a subscriber and email them, welcoming them to Thunderbird Appointment."""
invite_code = repo.invite.generate_codes(db, 1)[0]
subscriber = repo.subscriber.create(db, schemas.SubscriberBase(
email=email,
username=email,
))

if not subscriber:
raise RuntimeError

invite_code.subscriber_id = subscriber.id
db.add(invite_code)
db.commit()

background_tasks.add_task(send_invite_account_email, to=email)

return invite_code
7 changes: 6 additions & 1 deletion backend/src/appointment/tasks/emails.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from appointment.controller.mailer import PendingRequestMail, ConfirmationMail, InvitationMail, ZoomMeetingFailedMail, \
RejectionMail, SupportRequestMail
RejectionMail, SupportRequestMail, InviteAccountMail


def send_invite_email(to, attachment):
Expand Down Expand Up @@ -49,3 +49,8 @@ def send_support_email(requestee, topic, details):
details=details,
)
mail.send()


def send_invite_account_email(to):
mail = InviteAccountMail(to=to)
mail.send()
21 changes: 21 additions & 0 deletions backend/src/appointment/templates/email/new_account.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html lang="{{ l10n('locale') }}">
<body>
<p>
{{ l10n('new-account-html-heading') }}
</p>
<div>
<a
href="{{ homepage_url }}"
style="
display:inline-block;
padding:.5rem 1rem;
color:white;
background:#0e757f;
border-radius:6px;
text-decoration:none;
"
>{{ l10n('new-account-mail-action') }}</a>
</div>
{% include 'includes/footer.jinja2' %}
</body>
</html>

0 comments on commit 1e0e94f

Please sign in to comment.