Bank Services tested and completed
This commit is contained in:
34
BankServices/RoutineEmailService/Dockerfile
Normal file
34
BankServices/RoutineEmailService/Dockerfile
Normal file
@@ -0,0 +1,34 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /
|
||||
|
||||
# Install system dependencies and Poetry
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends gcc && rm -rf /var/lib/apt/lists/* && pip install --no-cache-dir poetry
|
||||
|
||||
# Copy Poetry configuration
|
||||
COPY /BankServices/RoutineEmailService/pyproject.toml ./pyproject.toml
|
||||
|
||||
# Configure Poetry and install dependencies with optimizations
|
||||
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi --no-root --only main && pip cache purge && rm -rf ~/.cache/pypoetry
|
||||
|
||||
# Install cron for scheduling tasks
|
||||
RUN apt-get update && apt-get install -y cron
|
||||
|
||||
# 11:00 Istanbul Time (UTC+3) system time is 08:00 UTC
|
||||
RUN echo "0 8 * * * /usr/local/bin/python /app.py >> /var/log/cron.log 2>&1" > /tmp/crontab_list && crontab /tmp/crontab_list
|
||||
|
||||
# Copy application code
|
||||
COPY /BankServices/RoutineEmailService /
|
||||
|
||||
COPY /Schemas /Schemas
|
||||
COPY /Controllers /Controllers
|
||||
COPY /BankServices/ServiceDepends /
|
||||
|
||||
# Set Python path to include app directory
|
||||
ENV PYTHONPATH=/ PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
|
||||
|
||||
# Create log file to grab cron logs
|
||||
RUN touch /var/log/cron.log
|
||||
|
||||
# Run cron setup and tail the log file for user to monitor logs
|
||||
CMD cron && tail -f /var/log/cron.log
|
||||
0
BankServices/RoutineEmailService/README.md
Normal file
0
BankServices/RoutineEmailService/README.md
Normal file
144
BankServices/RoutineEmailService/app.py
Normal file
144
BankServices/RoutineEmailService/app.py
Normal file
@@ -0,0 +1,144 @@
|
||||
import arrow
|
||||
from typing import List, Any
|
||||
|
||||
from Schemas import AccountRecords
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from Controllers.Email.send_email import EmailSendModel, EmailService
|
||||
|
||||
|
||||
def render_email_template(
|
||||
headers: List[str], rows: List[List[Any]], balance_error: bool, bank_balance: str
|
||||
) -> str:
|
||||
"""
|
||||
Render the HTML email template with the provided data.
|
||||
|
||||
Args:
|
||||
headers: List of column headers for the table
|
||||
rows: List of data rows for the table
|
||||
balance_error: Flag indicating if there's a balance discrepancy
|
||||
bank_balance: Current bank balance formatted as string
|
||||
|
||||
Returns:
|
||||
Rendered HTML template as string
|
||||
"""
|
||||
try:
|
||||
# Look for template in ServiceDepends directory
|
||||
env = Environment(loader=FileSystemLoader("/"))
|
||||
template = env.get_template("template_accounts.html")
|
||||
|
||||
# Render template with variables
|
||||
return template.render(
|
||||
headers=headers,
|
||||
rows=rows,
|
||||
bank_balance=bank_balance,
|
||||
balance_error=balance_error,
|
||||
today=str(arrow.now().date()),
|
||||
)
|
||||
except Exception as e:
|
||||
print('Exception render template:',e)
|
||||
err = e
|
||||
raise
|
||||
|
||||
|
||||
def send_email_to_given_address(send_to: str, html_template: str) -> bool:
|
||||
"""
|
||||
Send email with the rendered HTML template to the specified address.
|
||||
|
||||
Args:
|
||||
send_to: Email address of the recipient
|
||||
html_template: Rendered HTML template content
|
||||
|
||||
Returns:
|
||||
Boolean indicating if the email was sent successfully
|
||||
"""
|
||||
today = arrow.now()
|
||||
subject = f"{str(today.date())} Gunes Apt. Cari Durum Bilgilendirme Raporu"
|
||||
|
||||
# Create email parameters using EmailSendModel
|
||||
email_params = EmailSendModel(
|
||||
subject=subject,
|
||||
html=html_template,
|
||||
receivers=[send_to],
|
||||
text=f"Gunes Apt. Cari Durum Bilgilendirme Raporu - {today.date()}"
|
||||
)
|
||||
|
||||
try:
|
||||
# Use the context manager to handle connection errors
|
||||
with EmailService.new_session() as email_session:
|
||||
# Send email through the service
|
||||
EmailService.send_email(email_session, email_params)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f'Exception send email: {e}')
|
||||
return False
|
||||
|
||||
|
||||
def set_account_records_to_send_email() -> bool:
|
||||
"""
|
||||
Retrieve account records from the database, format them, and send an email report.
|
||||
|
||||
Usage:
|
||||
from app import set_account_records_to_send_email
|
||||
|
||||
Returns:
|
||||
Boolean indicating if the process completed successfully
|
||||
"""
|
||||
# Get database session and retrieve records
|
||||
with AccountRecords.new_session() as db_session:
|
||||
account_records_query = AccountRecords.filter_all(db=db_session).query
|
||||
|
||||
# Get the 3 most recent records
|
||||
account_records: List[AccountRecords] | [] = (
|
||||
account_records_query.order_by(
|
||||
AccountRecords.bank_date.desc(),
|
||||
AccountRecords.bank_reference_code.desc()
|
||||
)
|
||||
.limit(3)
|
||||
.all()
|
||||
)
|
||||
|
||||
# Check if we have enough records
|
||||
if len(account_records) < 2:
|
||||
return False
|
||||
|
||||
# Check for balance discrepancy
|
||||
first_record, second_record = account_records[0], account_records[1]
|
||||
expected_second_balance = first_record.bank_balance - first_record.currency_value
|
||||
balance_error = expected_second_balance != second_record.bank_balance
|
||||
|
||||
if balance_error:
|
||||
return False
|
||||
|
||||
# Format rows for the email template
|
||||
list_of_rows = []
|
||||
for record in account_records:
|
||||
list_of_rows.append([
|
||||
record.bank_date.strftime("%d/%m/%Y %H:%M"),
|
||||
record.process_comment,
|
||||
f"{record.currency_value:,.2f}",
|
||||
f"{record.bank_balance:,.2f}"
|
||||
])
|
||||
# Get the most recent bank balance
|
||||
last_bank_balance = sorted(account_records, key=lambda x: x.bank_date, reverse=True)[0].bank_balance
|
||||
# Define headers for the table
|
||||
headers = ["Ulaştığı Tarih", "Banka Transaksiyonu Ek Bilgi", "Aktarım Değeri", "Banka Bakiyesi"]
|
||||
|
||||
# Recipient email address
|
||||
send_to = "karatay@mehmetkaratay.com.tr"
|
||||
|
||||
# Render email template
|
||||
html_template = render_email_template(
|
||||
headers=headers,
|
||||
rows=list_of_rows,
|
||||
balance_error=balance_error,
|
||||
bank_balance=f"{last_bank_balance:,.2f}",
|
||||
)
|
||||
|
||||
# Send the email
|
||||
return send_email_to_given_address(send_to=send_to, html_template=html_template)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = set_account_records_to_send_email()
|
||||
exit_code = 0 if success else 1
|
||||
exit(exit_code)
|
||||
17
BankServices/RoutineEmailService/pyproject.toml
Normal file
17
BankServices/RoutineEmailService/pyproject.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[project]
|
||||
name = "routineemailservice"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"arrow>=1.3.0",
|
||||
"redbox>=0.2.1",
|
||||
"redis>=5.2.1",
|
||||
"pydantic-settings>=2.8.1",
|
||||
"sqlalchemy-mixins>=2.0.5",
|
||||
"fastapi>=0.115.11",
|
||||
"jinja2>=3.1.6",
|
||||
"psycopg2-binary>=2.9.10",
|
||||
"redmail>=0.6.0",
|
||||
]
|
||||
0
BankServices/RoutineEmailService/templates/a.txt
Normal file
0
BankServices/RoutineEmailService/templates/a.txt
Normal file
Reference in New Issue
Block a user