mirror of
https://github.com/olegvodyanov/docbot.git
synced 2025-12-20 08:07:04 +03:00
Merge pull request #1 from olegvodyanov/refactor_consultation_handler_via_AI
changes made by AI
This commit is contained in:
commit
c4988533c4
4
src/core/exceptions.py
Normal file
4
src/core/exceptions.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class DatabaseError(Exception):
|
||||||
|
"""Custom exception for database-related errors."""
|
||||||
|
def __init__(self, message: str = "A database error occurred"):
|
||||||
|
super().__init__(message)
|
||||||
@ -22,6 +22,7 @@ from docbot.services.doctors_service import (
|
|||||||
)
|
)
|
||||||
from docbot.services.session_service import create_session
|
from docbot.services.session_service import create_session
|
||||||
from core.logging import logger
|
from core.logging import logger
|
||||||
|
from core.exceptions import DatabaseError # Assuming a custom exception for database-related issues
|
||||||
|
|
||||||
|
|
||||||
SEND_ACKNOWLEDGEMENT_INFO = 1
|
SEND_ACKNOWLEDGEMENT_INFO = 1
|
||||||
@ -73,7 +74,14 @@ async def accept_personal_data_agreement(update: Update, context: ContextTypes.D
|
|||||||
user_data = context.user_data
|
user_data = context.user_data
|
||||||
user_data['telegram_id'] = user_id = update.effective_user.id
|
user_data['telegram_id'] = user_id = update.effective_user.id
|
||||||
|
|
||||||
registered = await get_patient_by_telegram_id(user_id)
|
try:
|
||||||
|
registered = await get_patient_by_telegram_id(user_id)
|
||||||
|
except DatabaseError as e:
|
||||||
|
logger.error(f"Database error while fetching patient by Telegram ID {user_id}: {e}")
|
||||||
|
await update.message.reply_text(
|
||||||
|
"❌ Произошла ошибка при обработке вашего запроса. Пожалуйста, попробуйте позже."
|
||||||
|
)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
logger.info(f"User {user_id} initiated consultation process.")
|
logger.info(f"User {user_id} initiated consultation process.")
|
||||||
logger.info(f"User exists? {registered}")
|
logger.info(f"User exists? {registered}")
|
||||||
@ -95,14 +103,21 @@ async def accept_personal_data_agreement(update: Update, context: ContextTypes.D
|
|||||||
|
|
||||||
|
|
||||||
async def receive_patient_aceptance(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
async def receive_patient_aceptance(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
||||||
logger.info(f"Next function receive_patient_aceptance")
|
|
||||||
user_data = context.user_data
|
user_data = context.user_data
|
||||||
await update.callback_query.answer()
|
await update.callback_query.answer()
|
||||||
|
|
||||||
if update.callback_query.data == "consult:accepted":
|
if update.callback_query.data == "consult:accepted":
|
||||||
user_data['accepted'] = True
|
user_data['accepted'] = True
|
||||||
user_id = user_data['telegram_id']
|
user_id = user_data['telegram_id']
|
||||||
await create_patient(telegram_id=user_id, terms_acceptance=True) # Создаем пациента в БД
|
try:
|
||||||
|
await create_patient(telegram_id=user_id, terms_acceptance=True) # Создаем пациента в БД
|
||||||
|
except DatabaseError as e:
|
||||||
|
logger.error(f"Failed to create patient for user {user_id}: {e}")
|
||||||
|
await update.callback_query.edit_message_text(
|
||||||
|
text="❌ Произошла ошибка при создании вашей записи. Пожалуйста, попробуйте позже."
|
||||||
|
)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
await update.callback_query.edit_message_text(
|
await update.callback_query.edit_message_text(
|
||||||
text="✅ Спасибо за подтверждение. Вы можете продолжить запись на консультацию.",
|
text="✅ Спасибо за подтверждение. Вы можете продолжить запись на консультацию.",
|
||||||
parse_mode="Markdown",
|
parse_mode="Markdown",
|
||||||
@ -279,16 +294,30 @@ async def receive_consultation_date(update: Update, context: ContextTypes.DEFAUL
|
|||||||
async def pay_consultation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
async def pay_consultation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
||||||
user_id = context.user_data['telegram_id']
|
user_id = context.user_data['telegram_id']
|
||||||
consultation_date_time = context.user_data['consultation_date_time']
|
consultation_date_time = context.user_data['consultation_date_time']
|
||||||
patient = await get_patient_by_telegram_id(user_id)
|
try:
|
||||||
doctor = await get_doctor_by_code(context.user_data['doctor_number'])
|
patient = await get_patient_by_telegram_id(user_id)
|
||||||
|
doctor = await get_doctor_by_code(context.user_data['doctor_number'])
|
||||||
|
except DatabaseError as e:
|
||||||
|
logger.error(f"Database error during payment process for user {user_id}: {e}")
|
||||||
|
await update.callback_query.message.reply_text(
|
||||||
|
"❌ Произошла ошибка при обработке вашего запроса. Пожалуйста, попробуйте позже."
|
||||||
|
)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
await create_session(
|
try:
|
||||||
telegram_id=user_id,
|
await create_session(
|
||||||
phone=patient.phone,
|
telegram_id=user_id,
|
||||||
consultation_date_time=consultation_date_time,
|
phone=patient.phone,
|
||||||
patient=patient,
|
consultation_date_time=consultation_date_time,
|
||||||
doctor_id=doctor.id if doctor else None
|
patient=patient,
|
||||||
)
|
doctor_id=doctor.id if doctor else None
|
||||||
|
)
|
||||||
|
except DatabaseError as e:
|
||||||
|
logger.error(f"Failed to create session for user {user_id}: {e}")
|
||||||
|
await update.callback_query.message.reply_text(
|
||||||
|
"❌ Произошла ошибка при создании записи на консультацию. Пожалуйста, попробуйте позже."
|
||||||
|
)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
link = await get_doctors_payment_link(context.user_data['doctor_number'])
|
link = await get_doctors_payment_link(context.user_data['doctor_number'])
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user