updated and cleaned
This commit is contained in:
@@ -29,21 +29,24 @@ T = TypeVar("T")
|
||||
|
||||
|
||||
def check_payload_already_exists_mongo_database(filename: str, mongo_provider) -> bool:
|
||||
find_one_result = mongo_provider.find_one(filter_query={
|
||||
"filename": filename
|
||||
})
|
||||
find_one_result = mongo_provider.find_one(filter_query={"filename": filename})
|
||||
if find_one_result:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def write_payload_to_mongo_database(payload, filename: str, mail_info:dict, mongo_provider) -> bool:
|
||||
insert_one_result = mongo_provider.insert_one(document={
|
||||
"filename": filename,
|
||||
"payload": payload,
|
||||
"stage": "read",
|
||||
"created_at": str(arrow.now()), **mail_info
|
||||
})
|
||||
def write_payload_to_mongo_database(
|
||||
payload, filename: str, mail_info: dict, mongo_provider
|
||||
) -> bool:
|
||||
insert_one_result = mongo_provider.insert_one(
|
||||
document={
|
||||
"filename": filename,
|
||||
"payload": payload,
|
||||
"stage": "read",
|
||||
"created_at": str(arrow.now()),
|
||||
**mail_info,
|
||||
}
|
||||
)
|
||||
if insert_one_result.acknowledged:
|
||||
return True
|
||||
return False
|
||||
@@ -57,7 +60,7 @@ def read_email_and_write_to_mongo_database(email_message, mail_info: dict) -> bo
|
||||
storage_reason=[mongo_prefix, str(arrow.now().date())],
|
||||
)
|
||||
if email_message.is_multipart(): # Check if email has multipart content
|
||||
for part in email_message.walk(): # Each part can be an attachment
|
||||
for part in email_message.walk(): # Each part can be an attachment
|
||||
content_disposition = part.get("Content-Disposition")
|
||||
if content_disposition and "attachment" in content_disposition:
|
||||
if filename := part.get_filename():
|
||||
@@ -71,7 +74,7 @@ def read_email_and_write_to_mongo_database(email_message, mail_info: dict) -> bo
|
||||
payload=payload,
|
||||
filename=filename,
|
||||
mongo_provider=mongo_provider,
|
||||
mail_info=mail_info
|
||||
mail_info=mail_info,
|
||||
)
|
||||
else: # Handle non-multipart email, though this is rare for emails with attachments
|
||||
content_disposition = email_message.get("Content-Disposition")
|
||||
@@ -81,7 +84,7 @@ def read_email_and_write_to_mongo_database(email_message, mail_info: dict) -> bo
|
||||
filename=filename,
|
||||
mongo_provider=mongo_provider,
|
||||
)
|
||||
is_iban_in_filename= authorized_iban_cleaned in str(filename)
|
||||
is_iban_in_filename = authorized_iban_cleaned in str(filename)
|
||||
if is_iban_in_filename and file_exists:
|
||||
payload = email_message.get_payload(decode=True)
|
||||
return write_payload_to_mongo_database(
|
||||
@@ -114,10 +117,10 @@ def app():
|
||||
if email_message := banks_mail.email:
|
||||
headers = {k.lower(): v for k, v in banks_mail.headers.items()}
|
||||
mail_info = {
|
||||
"from": headers['from'],
|
||||
"to": headers['to'],
|
||||
"subject": headers['subject'],
|
||||
"date": str(headers['date']),
|
||||
"from": headers["from"],
|
||||
"to": headers["to"],
|
||||
"subject": headers["subject"],
|
||||
"date": str(headers["date"]),
|
||||
}
|
||||
read_email_and_write_to_mongo_database(
|
||||
email_message=email_message, mail_info=mail_info
|
||||
@@ -127,6 +130,6 @@ def app():
|
||||
if __name__ == "__main__":
|
||||
|
||||
while True:
|
||||
print('Running email service...')
|
||||
print("Running email service...")
|
||||
app()
|
||||
time.sleep(Config.EMAIL_SLEEP)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
class Config:
|
||||
# IP_ADDRESS: str = "http://10.10.2.46:41575/internal/isbank/retreive"
|
||||
SERVICE_TIMING: int = 900 # 15 min
|
||||
|
||||
@@ -18,7 +18,9 @@ def collect_excel_files_from_mongo_database(mongo_provider) -> list:
|
||||
return mongo_provider.find_many(filter_query={"stage": "read"})
|
||||
|
||||
|
||||
def update_parsed_data_to_mongo_database(mongo_provider, collected_data_dict: dict, filename: str) -> None:
|
||||
def update_parsed_data_to_mongo_database(
|
||||
mongo_provider, collected_data_dict: dict, filename: str
|
||||
) -> None:
|
||||
if collected_data_dict:
|
||||
payload = collected_data_dict[filename]
|
||||
if payload:
|
||||
@@ -43,9 +45,9 @@ def parse_excel_file(excel_frame: DataFrame, excel_name: str) -> dict:
|
||||
if len(str(row[1]).split("/")) > 2:
|
||||
data_dict[excel_name] = dict(
|
||||
iban=str(iban),
|
||||
bank_date=arrow.get(datetime.datetime.strptime(
|
||||
str(row[1]), "%d/%m/%Y-%H:%M:%S"
|
||||
)).__str__(),
|
||||
bank_date=arrow.get(
|
||||
datetime.datetime.strptime(str(row[1]), "%d/%m/%Y-%H:%M:%S")
|
||||
).__str__(),
|
||||
channel_branch=unidecode(str(row[3])),
|
||||
currency_value=(
|
||||
float(str(row[4]).replace(",", "")) if row[4] else 0
|
||||
@@ -83,7 +85,9 @@ def app():
|
||||
# Extract IBAN and root info from the xl file
|
||||
collected_data_dict = parse_excel_file(excel_frame, filename)
|
||||
update_parsed_data_to_mongo_database(
|
||||
mongo_provider=mongo_provider, collected_data_dict=collected_data_dict, filename=filename
|
||||
mongo_provider=mongo_provider,
|
||||
collected_data_dict=collected_data_dict,
|
||||
filename=filename,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ ADD /BankServices/RoutineEmailService /
|
||||
ADD /Configs /Configs
|
||||
ADD /Schemas /Schemas
|
||||
ADD /Commons /Commons
|
||||
ADD /BankServices/ServiceDepends/template_accounts.html /templates/template_accounts.html
|
||||
|
||||
ADD /Services/MongoService /Services/MongoService
|
||||
ADD /Services/PostgresService /Services/PostgresService
|
||||
|
||||
@@ -6,11 +6,24 @@ from jinja2 import Environment, FileSystemLoader
|
||||
from Services.EmailService.provider import send_email
|
||||
|
||||
|
||||
def render_email_template(headers: list, rows: list):
|
||||
def render_email_template(
|
||||
headers: list, rows: list, balance_error: bool, bank_balance: float
|
||||
):
|
||||
template_dir = os.path.join(os.path.dirname(__file__), "templates")
|
||||
env = Environment(loader=FileSystemLoader(template_dir)) # Load templates from the directory
|
||||
template = env.get_template("template_accounts.html") # Load the specific template file
|
||||
return template.render(headers=headers, rows=rows) # Render template with variables
|
||||
env = Environment(
|
||||
loader=FileSystemLoader(template_dir)
|
||||
) # Load templates from the directory
|
||||
template = env.get_template(
|
||||
"template_accounts.html"
|
||||
) # Load the specific template file
|
||||
# Render template with variables
|
||||
return template.render(
|
||||
headers=headers,
|
||||
rows=rows,
|
||||
bank_balance=f"{bank_balance:.4f}",
|
||||
balance_error=balance_error,
|
||||
today=str(arrow.now().date()),
|
||||
)
|
||||
|
||||
|
||||
def send_email_to_given_address(send_to: str, html_template: str):
|
||||
@@ -27,30 +40,42 @@ def send_email_to_given_address(send_to: str, html_template: str):
|
||||
|
||||
|
||||
def set_account_records_to_send_email():
|
||||
"""
|
||||
from app import set_account_records_to_send_email
|
||||
"""
|
||||
db_session = AccountRecords.new_session()
|
||||
account_records = AccountRecords.filter_all(db=db_session).core_query
|
||||
account_records = (
|
||||
AccountRecords.query.filter()
|
||||
.order_by(
|
||||
account_records.order_by(
|
||||
AccountRecords.bank_date.desc(), AccountRecords.bank_reference_code.desc()
|
||||
)
|
||||
.limit(3)
|
||||
.all()
|
||||
)
|
||||
|
||||
first_record, second_record, balance_error = account_records[0], account_records[1], False
|
||||
first_record, second_record, balance_error = (
|
||||
account_records[0],
|
||||
account_records[1],
|
||||
False,
|
||||
)
|
||||
second_balance = first_record.bank_balance - first_record.currency_value
|
||||
if second_balance != second_record.bank_balance:
|
||||
balance_error = True
|
||||
|
||||
rows = [{
|
||||
"date": record.bank_date, "comment": record.bank_comment, "currency": record.currency_value,
|
||||
} for record in account_records]
|
||||
list_of_rows = list()
|
||||
for record in account_records:
|
||||
list_of_rows.append(
|
||||
[record.bank_date, record.process_comment, f"{record.currency_value:.4f}"]
|
||||
)
|
||||
|
||||
send_to = "karatay@mehmetkaratay.com.tr"
|
||||
html_template = render_email_template(
|
||||
headers=["Ulaştığı Tarih", "Banka Transaksiyonu Ek Bilgi", "Aktarım Değeri"],
|
||||
rows=rows,
|
||||
rows=list_of_rows,
|
||||
balance_error=balance_error,
|
||||
bank_balance=account_records[0].bank_balance,
|
||||
)
|
||||
exit()
|
||||
send_email_to_given_address(send_to=send_to, html_template=html_template)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -12,8 +12,6 @@ def check_any_written_stage_in_mongo_database(mongo_provider) -> bool:
|
||||
return mongo_provider.find_one(filter_query={"stage": "written", "send": None})
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
while True:
|
||||
|
||||
@@ -28,6 +28,9 @@
|
||||
<body>
|
||||
<h1>Günaydın, Admin</h1>
|
||||
<br>
|
||||
<p>Banka Kayıtları : {{today}} </p>
|
||||
<p><b>Son Bakiye : {{bank_balance}} </b></p>
|
||||
<p><b>{{"Status : İkinci Bakiye Hatalı" if balance_error else "Status :OK"}}</b></p>
|
||||
<table border="1">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -46,5 +49,6 @@
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<p>Teşekkür ederiz,<br>Evyos Yönetim<br>Saygılarımızla</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -16,7 +16,7 @@ def collect_parsed_data_from_mongo_database(mongo_provider) -> list:
|
||||
|
||||
|
||||
def write_parsed_data_to_account_records(
|
||||
file: str, data_dict: dict, collection_name: str, mongo_provider
|
||||
file: str, data_dict: dict, collection_name: str, mongo_provider
|
||||
):
|
||||
db_session = AccountRecords.new_session()
|
||||
data_dict["bank_balance"] = data_dict.pop("balance")
|
||||
@@ -28,7 +28,9 @@ def write_parsed_data_to_account_records(
|
||||
data_dict["bank_date_d"] = bank_date.day
|
||||
data_dict["bank_date_y"] = bank_date.year
|
||||
data_dict["bank_date"] = str(bank_date)
|
||||
if build_iban := BuildIbans.filter_by_one(iban=data_dict["iban"], db=db_session).data:
|
||||
if build_iban := BuildIbans.filter_by_one(
|
||||
iban=data_dict["iban"], db=db_session
|
||||
).data:
|
||||
data_dict.update(
|
||||
{
|
||||
"build_id": build_iban.build_id,
|
||||
@@ -36,7 +38,7 @@ def write_parsed_data_to_account_records(
|
||||
}
|
||||
)
|
||||
if found_record := AccountRecords.filter_one(
|
||||
AccountRecords.bank_date == data_dict["bank_date"],
|
||||
AccountRecords.bank_date == data_dict["bank_date"],
|
||||
AccountRecords.iban == data_dict["iban"],
|
||||
AccountRecords.bank_reference_code == data_dict["bank_reference_code"],
|
||||
AccountRecords.bank_balance == data_dict["bank_balance"],
|
||||
@@ -48,12 +50,13 @@ def write_parsed_data_to_account_records(
|
||||
new_account_record.is_confirmed = True
|
||||
new_account_record.save(db=db_session)
|
||||
mongo_provider.update_one(
|
||||
filter_query={"filename": file}, update_data={"$set": {"stage": "written"}},
|
||||
filter_query={"filename": file},
|
||||
update_data={"$set": {"stage": "written"}},
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print('Writer Service is running')
|
||||
print("Writer Service is running")
|
||||
while True:
|
||||
with MongoProvider.mongo_client() as mongo_client:
|
||||
provider = MongoProvider(
|
||||
@@ -71,6 +74,6 @@ if __name__ == "__main__":
|
||||
data_dict=parsed_data,
|
||||
collection_name=provider.collection.name,
|
||||
mongo_provider=provider,
|
||||
file=file_name
|
||||
file=file_name,
|
||||
)
|
||||
time.sleep(60)
|
||||
|
||||
@@ -15,4 +15,3 @@ class BankReceive(BaseModel):
|
||||
process_type: str
|
||||
process_comment: str
|
||||
bank_reference_code: str
|
||||
|
||||
|
||||
Reference in New Issue
Block a user