Service Runner Finder and complete task chain completed

This commit is contained in:
2025-08-11 19:26:49 +03:00
parent 405ba2e95d
commit ca98adc338
18 changed files with 1205 additions and 257 deletions

View File

@@ -0,0 +1,14 @@
__pycache__/
*.pyc
*.pyo
*.pyd
*.db
*.sqlite3
*.log
*.env
venv/
.env.*
node_modules/
.prisma/
.prisma-cache/
ServicesRunnner/AccountRecordServices/Test/venv/

View File

@@ -0,0 +1,22 @@
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV VIRTUAL_ENV=/opt/venv
ENV PRISMA_SCHEMA_PATH=/app/Depends/schema.prisma
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV PYTHONPATH=/app
RUN apt-get update && apt-get install -y --no-install-recommends gcc curl && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY ServicesRunner/Depends/ /app/Depends/
COPY ServicesRunner/AccountRecordServices/Finder/Comment /app/
COPY ServicesRunner/requirements.txt /app/requirements.txt
COPY ServicesRunner/AccountRecordServices/Finder/Comment/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

View File

@@ -0,0 +1,174 @@
import time
import arrow
import pprint
from json import dumps
from decimal import Decimal
from pydantic import BaseModel
from datetime import datetime
from typing import Optional
from Depends.prisma_client import PrismaService
from Depends.service_handler import ProcessCommentFinderService
from Depends.config import ConfigServices, MailSendModel, RedisMailSender, Status, RedisTaskObject, FinderComment
class BankReceive(BaseModel):
import_file_name: str
iban: str
bank_date: datetime
channel_branch: str
currency: Optional[str] = "TL"
currency_value: Decimal
bank_balance: Decimal
additional_balance: Decimal
process_name: str
process_type: str
process_comment: str
bank_reference_code: str
bank_date_w: int
bank_date_m: int
bank_date_d: int
bank_date_y: int
def check_task_belong_to_this_service(task: RedisTaskObject):
if not task.service == ConfigServices.SERVICE_PREFIX_FINDER_IBAN:
return False
if not task.completed:
return False
if task.is_completed:
return False
if not task.data:
return False
return True
def write_account_records_row_from_finder_comment(finder_comments: list[FinderComment], prisma_service: PrismaService, saved_list_of_account_records: dict):
finder_comments = list(finder_comments)
for finder_comment in finder_comments:
bank_date = arrow.get(finder_comment.bank_date).replace(tzinfo='GMT+3').datetime
bank_receive_record = BankReceive(
import_file_name=finder_comment.filename, iban=finder_comment.iban, bank_date=bank_date, channel_branch=finder_comment.channel_branch, currency="TL", currency_value=finder_comment.currency_value,
bank_balance=finder_comment.balance, additional_balance=finder_comment.additional_balance, process_name=finder_comment.process_name, process_type=finder_comment.process_type,
process_comment=finder_comment.process_comment, bank_reference_code=finder_comment.bank_reference_code, build_id=finder_comment.build_id, build_uu_id=finder_comment.build_uu_id,
decision_book_id=finder_comment.decision_book_id, decision_book_uu_id=finder_comment.decision_book_uu_id, bank_date_w=bank_date.weekday(), bank_date_m=bank_date.month,
bank_date_d=bank_date.day, bank_date_y=bank_date.year
)
account_record_found = prisma_service.find_first(table="account_records", query={"iban": bank_receive_record.iban, "bank_reference_code": bank_receive_record.bank_reference_code,
"bank_date": bank_receive_record.bank_date, "bank_balance": bank_receive_record.bank_balance, "currency_value": bank_receive_record.currency_value},
select={"id": True, "iban": True, "bank_reference_code": True, "bank_date": True, "bank_balance": True}
)
if not account_record_found:
created_account_record = prisma_service.create(table="account_records", data=bank_receive_record.dict(), select={"id": True, "iban": True, "bank_reference_code": True, "bank_date": True, "bank_balance": True} )
if created_account_record['build_id'] in saved_list_of_account_records.keys():
saved_list_of_account_records[created_account_record['build_id']] = [*saved_list_of_account_records[created_account_record['build_id']], created_account_record]
else:
saved_list_of_account_records[created_account_record['build_id']] = [created_account_record]
return saved_list_of_account_records
def enclose_task_and_send_mail_to_build_manager(prisma_service: PrismaService, saved_list_of_account_records: dict, process_comment_finder_service: ProcessCommentFinderService, task: RedisTaskObject):
"""
Enclose task and send mail to build manager
"""
if not saved_list_of_account_records:
return
today = arrow.now().to('GMT+3').datetime
for build_id, saved_list_of_account_record in saved_list_of_account_records.items():
build_manager_occupant_type = prisma_service.find_first(table="occupant_types", query={"occupant_code":"BU-MNG", "is_confirmed": True, "active": True})
living_space = prisma_service.find_first(
table="build_living_space", query={"build_id": build_id, "occupant_type_id": build_manager_occupant_type['id'], "expiry_starts": {"lte": today}, "expiry_ends": {"gte": today}})
build = prisma_service.find_first(table="builds", query={"id": build_id})
person = prisma_service.find_first(table="people", query={"id": living_space['person_id']})
user = prisma_service.find_first(table="users", query={"person_id": person['id']})
send_object = MailSendModel(
receivers=[user.email], data=saved_list_of_account_record, template_name=ConfigServices.TEMPLATE_ACCOUNT_RECORDS,
subject=f"{build['name']} Cari Durum Bilgilendirme Raporu - {today.strftime('%d/%m/%Y %H:%M')}",
)
set_mail_object = RedisMailSender(task=task, data=send_object, service=ConfigServices.SERVICE_PREFIX_MAIL_SENDER, status=Status.PENDING, completed=False, created_at=today.strftime('%Y-%m-%d %H:%M:%S'))
process_comment_finder_service.service_retriever.redis_client.set(ConfigServices.SERVICE_PREFIX_MAIL_SENDER, dumps(set_mail_object.dict()))
return
if __name__ == "__main__":
prisma_service = PrismaService()
process_comment_finder_service = ProcessCommentFinderService()
print("Process Comment service started")
try:
print("Process Comment service started sleeping for 5 seconds")
while True:
time.sleep(5)
saved_list_of_account_records = dict()
tasks = process_comment_finder_service.fetch_all_tasks()
for task in tasks:
if not check_task_belong_to_this_service(task):
continue
write_account_records_row_from_finder_comment(finder_comments=task.data.FinderComment, prisma_service=prisma_service, saved_list_of_account_records=saved_list_of_account_records)
process_comment_finder_service.update_task_status(task_uuid=task.task, is_completed=True, status=Status.COMPLETED)
process_comment_finder_service.delete_task(task_uuid=task.task)
except Exception as e:
raise
finally:
prisma_service.disconnect()
def fix_account_records_bank_date(prisma_service: PrismaService, bank_receive_record: BankReceive):
account_record_from_other_fields = prisma_service.find_first(
table="account_records",
query={
"iban": bank_receive_record.iban,
"bank_reference_code": bank_receive_record.bank_reference_code,
"bank_balance": bank_receive_record.bank_balance,
"currency_value": bank_receive_record.currency_value,
# "process_comment": {"contains": str(bank_receive_record.process_comment), "mode": "insensitive"},
},
select={
"id": True, "iban": True, "bank_reference_code": True, "bank_date": True,
"bank_balance": True, "currency_value": True, "process_comment": True
}
)
if account_record_from_other_fields:
prisma_service.update(
table="account_records", where={"id": account_record_from_other_fields['id']}, data={"bank_date": bank_receive_record.bank_date},
)
if not account_record_from_other_fields:
pprint.pprint({"not_found_bank_receive_record": bank_receive_record})
# prisma_service.update(
# table="account_records", where={"id": account_record_from_other_fields['id']}, data={"bank_date": bank_receive_record.bank_date},
# )
# from_database = arrow.get(account_record_from_other_fields['bank_date']).to('GMT+3').datetime
# print('old date', from_database, " - new date ", bank_receive_record.bank_date)
def commented_out_code():
account_record_found = None
old_bank_date=arrow.get(finder_comment.bank_date).datetime
if not account_record_found:
account_record_found_with_old_date = prisma_service.find_first(
table="account_records",
query={
"iban": bank_receive_record.iban, "bank_reference_code": bank_receive_record.bank_reference_code,
"bank_date": old_bank_date, "bank_balance": bank_receive_record.bank_balance,
},
)
if account_record_found_with_old_date:
prisma_service.update(
table="account_records", where={"id": account_record_found_with_old_date.id}, data={"bank_date": bank_receive_record.bank_date},
)
if account_record_found:
print('-' * 150)
pprint.pprint(
{
"account_record_found": dict(account_record_found),
"bank_receive_record": bank_receive_record.dict(),
"bank_receive_record.bank_date": bank_receive_record.bank_date,
"account_record_found.bank_date": account_record_found["bank_date"],
}
)
print('-' * 150)
return

View File

@@ -0,0 +1,19 @@
#!/bin/sh
VENV_PATH="/opt/venv"
REQUIREMENTS_PATH="/app/requirements.txt"
SCHEMA_PATH="/app/Depends/schema.prisma"
PRISMA_BINARY_PATH="/root/.cache/prisma-python/binaries"
if [ ! -x "$VENV_PATH/bin/python" ]; then
python -m venv "$VENV_PATH"
"$VENV_PATH/bin/pip" install pip --upgrade
"$VENV_PATH/bin/pip" install --no-cache-dir -r "$REQUIREMENTS_PATH"
"$VENV_PATH/bin/prisma" generate --schema "$SCHEMA_PATH"
fi
if ! find "$PRISMA_BINARY_PATH" -type f -name "prisma-query-engine-debian-openssl-3.0.x" | grep -q .; then
"$VENV_PATH/bin/prisma" py fetch
fi
exec "$VENV_PATH/bin/python" -u app.py