-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhanced email sending function --------- Signed-off-by: Trey <[email protected]>
- Loading branch information
Showing
12 changed files
with
172 additions
and
88 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
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
Empty file.
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,64 @@ | ||
from string import Template | ||
from textwrap import dedent | ||
|
||
from backend.models import Invoice, InvoiceRecurringProfile, User, EmailSendStatus | ||
from backend.utils.dataclasses import BaseServiceResponse | ||
from backend.utils.service_retry import retry_handler | ||
from settings.helpers import send_email | ||
|
||
|
||
class OnCreateInvoiceEmailServiceResponse(BaseServiceResponse[str]): ... | ||
|
||
|
||
def on_create_invoice_service(users_email: str, invoice: Invoice) -> OnCreateInvoiceEmailServiceResponse: | ||
if not users_email: | ||
return OnCreateInvoiceEmailServiceResponse(error_message="User email not found") | ||
|
||
if not invoice: | ||
return OnCreateInvoiceEmailServiceResponse(error_message="Invoice not found") | ||
|
||
email_message = dedent( | ||
""" | ||
Hi $first_name, | ||
The invoice #$invoice_id has been created for you to pay, due on the $due_date. Please pay at your earliest convenience. | ||
Balance Due: $currency_symbol$amount_due $currency | ||
Many thanks, | ||
$company_name | ||
""" | ||
) | ||
|
||
user_data = { | ||
"first_name": invoice.client_to.name.split(" ")[0] if invoice.client_to else invoice.client_name, | ||
"invoice_id": invoice.id, | ||
"invoice_ref": invoice.reference or invoice.invoice_number or invoice.id, | ||
"due_date": invoice.date_due.strftime("%a %m %Y"), | ||
"amount_due": invoice.get_total_price(), | ||
"currency": invoice.currency, | ||
"currency_symbol": invoice.get_currency_symbol(), | ||
"product_list": [], # todo | ||
"company_name": invoice.self_company or invoice.self_name, | ||
} | ||
|
||
output: str = Template(email_message).substitute(user_data) | ||
|
||
email_svc_response = retry_handler( | ||
send_email, | ||
destination=invoice.client_to.email or invoice.client_email if invoice.client_to else invoice.client_email, | ||
subject=f"Invoice #{invoice.id} from {invoice.self_company or invoice.self_name}", | ||
content=output, | ||
) | ||
|
||
if email_svc_response.failed: | ||
return OnCreateInvoiceEmailServiceResponse(False, error_message="Failed to send email") | ||
|
||
EmailSendStatus.objects.create( | ||
status="send", | ||
owner=invoice.owner, | ||
recipient=users_email, | ||
aws_message_id=email_svc_response.response.get("MessageId"), | ||
) | ||
|
||
return OnCreateInvoiceEmailServiceResponse(True, response="Email sent successfully") |
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
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,19 @@ | ||
from typing import Callable, TypeVar, Generic | ||
from backend.utils.dataclasses import BaseServiceResponse | ||
|
||
T = TypeVar("T", bound=BaseServiceResponse) | ||
|
||
|
||
def retry_handler(function: Callable[..., T], *args, retry_max_attempts: int = 3, **kwargs) -> T: | ||
attempts: int = 0 | ||
|
||
while attempts < retry_max_attempts: | ||
response: T = function(*args, **kwargs) | ||
|
||
if response.failed: | ||
attempts += 1 | ||
if attempts == retry_max_attempts: | ||
return response | ||
continue | ||
return response | ||
return response |
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
Oops, something went wrong.