diff --git a/src/regtech_mail_api/internal.py b/src/regtech_mail_api/internal.py index ce3866e..428007b 100644 --- a/src/regtech_mail_api/internal.py +++ b/src/regtech_mail_api/internal.py @@ -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}. + + The beta platform is for testing purposes only and user-supplied data may be removed at any time. Take a moment to email our support staff at sblhelp@cfpb.gov with your feedback or upload a new file to continue testing. +""" + + @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} diff --git a/src/regtech_mail_api/mailer.py b/src/regtech_mail_api/mailer.py index 60d81b1..2a5bbd6 100644 --- a/src/regtech_mail_api/mailer.py +++ b/src/regtech_mail_api/mailer.py @@ -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 diff --git a/src/regtech_mail_api/public.py b/src/regtech_mail_api/public.py index 4b0876d..4c0400a 100644 --- a/src/regtech_mail_api/public.py +++ b/src/regtech_mail_api/public.py @@ -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() @@ -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() diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 19fc6d2..f2292be 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -1,3 +1,4 @@ +import json import pytest from fastapi import FastAPI @@ -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": "test@cfpb.gov", + "signer_name": "Test User", + "contact_email": "test_contact@cfpb.gov", + "timestamp": 1732128696, + } + ), + ) assert res.status_code == 200 def test_authed_endpoints( diff --git a/tests/test_send.py b/tests/test_send.py index 1869e67..1c93d48 100644 --- a/tests/test_send.py +++ b/tests/test_send.py @@ -1,3 +1,4 @@ +import json import pytest from fastapi import FastAPI @@ -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": "test@cfpb.gov", + "signer_name": "Test User", + "contact_email": "test_contact@cfpb.gov", + "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\nThe beta platform is for testing purposes only and user-supplied data may be removed at any time. Take a moment to email our support staff at sblhelp@cfpb.gov with your feedback or upload a new file to continue testing.\n", + "from_addr": "test@cfpb.gov", + "to": ["test_contact@cfpb.gov", "test@cfpb.gov"], + "cc": None, + "bcc": None, + } + assert res.status_code == 200 - assert res.json() == "Called internal send" + assert res.json()["email"] == expected_email