Skip to content

Commit

Permalink
Updated confirmation send with expected email format (#49)
Browse files Browse the repository at this point in the history
* Updated confirmation send with expected email format

* Updated based on updated email text
  • Loading branch information
jcadam14 authored Nov 22, 2024
1 parent 1e55993 commit 305c371
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
49 changes: 43 additions & 6 deletions src/regtech_mail_api/internal.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
from datetime import datetime
from fastapi import Request
from pydantic import BaseModel, ConfigDict, EmailStr
from textwrap import dedent
from zoneinfo import ZoneInfo

from regtech_api_commons.api.router_wrapper import Router

from regtech_mail_api.settings import EmailApiSettings
from regtech_mail_api.models import Email
from regtech_mail_api.mailer import create_mailer
from regtech_mail_api.mailer import create_mailer, get_header

settings = EmailApiSettings()

router = Router()


class ConfirmationRequest(BaseModel):
model_config = ConfigDict(from_attributes=True)
contact_email: EmailStr
signer_email: EmailStr
signer_name: str
timestamp: datetime
confirmation_id: str


body_template = """
Congratulations! This email confirms that the filing submitted by {signer_name} on {formatted_date} was successful. The confirmation number is {confirmation_id}.
To make any changes to the filing, please return to the Small Business Lending Data Filing Platform and follow the provided instructions. If you have any questions or need additional support, email our support staff at [email protected].
"""


@router.post("/confirmation/send")
async def send_email(request: Request):
async def send_email(request: Request, confirmation_request: ConfirmationRequest):
mailer = create_mailer()
# build email
to_list = ["contact email", "signer email"]
email = Email("Subject", "Body", settings.from_addr, to=to_list)

timestamp_est = confirmation_request.timestamp.astimezone(
ZoneInfo("America/New_York")
)
formatted_date = timestamp_est.strftime("%B %d, %Y at %-I:%M %p %Z")
body_text = dedent(
body_template.format(
signer_name=confirmation_request.signer_name,
formatted_date=formatted_date,
confirmation_id=confirmation_request.confirmation_id,
)
)

to_list = [confirmation_request.contact_email, confirmation_request.signer_email]
email = Email(
f"{get_header(confirmation_request.signer_email)} Small Business Lending Data Filing Confirmation",
body_text,
settings.from_addr,
to=to_list,
)
mailer.send(email)
return "Called internal send"
return {"email": email}
10 changes: 10 additions & 0 deletions src/regtech_mail_api/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ def create_mailer():
raise ValueError(f"Mailer type {settings.email_mailer} not supported")

return mailer


def get_header(email):
settings = EmailApiSettings() # type: ignore
header = "[BETA]"
if "cfpb" in email.lower().split("@")[-1]:
header = "[CFPB BETA]"
if settings.environment:
header = f"[{settings.environment}]"
return header
10 changes: 2 additions & 8 deletions src/regtech_mail_api/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from regtech_mail_api.settings import EmailApiSettings
from regtech_mail_api.models import Email
from regtech_mail_api.mailer import create_mailer
from regtech_mail_api.mailer import create_mailer, get_header

settings = EmailApiSettings()

Expand All @@ -19,13 +19,7 @@ async def send_email(request: Request):
sender_name = request.user.name if request.user.name else ""
type = request.headers["case-type"]

header = "[BETA]"
if "cfpb" in sender_addr.lower().split("@")[-1]:
header = "[CFPB BETA]"
if settings.environment:
header = f"[{settings.environment}]"

subject = f"{header} SBL User Request for {type}"
subject = f"{get_header(sender_addr)} SBL User Request for {type}"

form_data = await request.form()

Expand Down
14 changes: 13 additions & 1 deletion tests/test_authentication.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pytest

from fastapi import FastAPI
Expand Down Expand Up @@ -55,7 +56,18 @@ def test_unauthed_endpoints(
)
assert res.status_code == 403

res = client.post("/internal/confirmation/send")
res = client.post(
"/internal/confirmation/send",
data=json.dumps(
{
"confirmation_id": "test",
"signer_email": "[email protected]",
"signer_name": "Test User",
"contact_email": "[email protected]",
"timestamp": 1732128696,
}
),
)
assert res.status_code == 200

def test_authed_endpoints(
Expand Down
26 changes: 24 additions & 2 deletions tests/test_send.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pytest

from fastapi import FastAPI
Expand Down Expand Up @@ -116,6 +117,27 @@ def test_confirmation_send(
self, mocker: MockerFixture, app_fixture: FastAPI, full_user_mock: Mock
):
client = TestClient(app_fixture)
res = client.post("/internal/confirmation/send")
res = client.post(
"/internal/confirmation/send",
data=json.dumps(
{
"confirmation_id": "test",
"signer_email": "[email protected]",
"signer_name": "Test User",
"contact_email": "[email protected]",
"timestamp": 1732128696,
}
),
)

expected_email = {
"subject": "[CFPB BETA] Small Business Lending Data Filing Confirmation",
"body": "\nCongratulations! This email confirms that the filing submitted by Test User on November 20, 2024 at 1:51 PM EST was successful. The confirmation number is test.\n\nTo make any changes to the filing, please return to the Small Business Lending Data Filing Platform and follow the provided instructions. If you have any questions or need additional support, email our support staff at [email protected].\n",
"from_addr": "[email protected]",
"to": ["[email protected]", "[email protected]"],
"cc": None,
"bcc": None,
}

assert res.status_code == 200
assert res.json() == "Called internal send"
assert res.json()["email"] == expected_email

0 comments on commit 305c371

Please sign in to comment.