From f2e7f3e175981742c684b7a697e4fc14fe2e010b Mon Sep 17 00:00:00 2001 From: naiv Date: Mon, 18 Dec 2023 22:37:18 +0200 Subject: [PATCH] implemented fixes for errors --- borrowing_service/signals.py | 2 ++ notifications_service/bot_commands.py | 6 ----- .../management/commands/run_telegram_bot.py | 7 +++--- .../migrations/0003_alter_payment_status.py | 25 +++++++++++++++++++ payment_service/services.py | 13 ++++++++-- payment_service/views.py | 1 - 6 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 payment_service/migrations/0003_alter_payment_status.py diff --git a/borrowing_service/signals.py b/borrowing_service/signals.py index 9197fc9..b3ed7d6 100644 --- a/borrowing_service/signals.py +++ b/borrowing_service/signals.py @@ -23,3 +23,5 @@ def notify_new_borrowing(sender, instance, created, **kwargs): ) except TelegramUser.DoesNotExist: pass + except Exception as e: + print(str(e)) diff --git a/notifications_service/bot_commands.py b/notifications_service/bot_commands.py index 4098c62..6068e97 100644 --- a/notifications_service/bot_commands.py +++ b/notifications_service/bot_commands.py @@ -48,9 +48,3 @@ def user_borrowings(bot, message) -> None: ]) bot.reply_to(message, borrowings_info) - - -def send_payment_notification(bot, user_id, message) -> None: - chat_id = get_object_or_404(TelegramUser, user_id=user_id) - - bot.send_message(chat_id=chat_id, text=message) diff --git a/notifications_service/management/commands/run_telegram_bot.py b/notifications_service/management/commands/run_telegram_bot.py index 79be42a..d8dcf8d 100644 --- a/notifications_service/management/commands/run_telegram_bot.py +++ b/notifications_service/management/commands/run_telegram_bot.py @@ -6,8 +6,7 @@ welcome_message, help_information, user_borrowings, - is_user, - send_payment_notification + is_user ) bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN) @@ -36,8 +35,8 @@ def send_users_borrowings(message: telebot.types.Message) -> None: user_borrowings(bot, message) -def send_notification(user_id, message): - send_payment_notification(bot, user_id, message) +def send_notification(chat_id, message): + bot.send_message(chat_id, message) class Command(BaseCommand): diff --git a/payment_service/migrations/0003_alter_payment_status.py b/payment_service/migrations/0003_alter_payment_status.py new file mode 100644 index 0000000..6c02982 --- /dev/null +++ b/payment_service/migrations/0003_alter_payment_status.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.8 on 2023-12-18 18:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("payment_service", "0002_alter_payment_session_url"), + ] + + operations = [ + migrations.AlterField( + model_name="payment", + name="status", + field=models.CharField( + choices=[ + ("PENDING", "pending"), + ("PAID", "paid"), + ("EXPIRED", "expired"), + ], + default="PENDING", + max_length=40, + ), + ), + ] diff --git a/payment_service/services.py b/payment_service/services.py index 21b42bd..a1fb898 100644 --- a/payment_service/services.py +++ b/payment_service/services.py @@ -8,6 +8,8 @@ from borrowing_service.models import Borrowing from core import settings from notifications_service.management.commands import run_telegram_bot +from notifications_service.management.commands.run_telegram_bot import send_notification +from notifications_service.models import TelegramUser from payment_service.models import Payment @@ -100,5 +102,12 @@ def successful_payment_notification(payment: Payment) -> None: message = (f"Your payment of {amount_total} dollars " f"for {book_name} was successful") - - run_telegram_bot.send_notification(user.id, message) + try: + send_notification( + TelegramUser.objects.get(user_id=user.id).chat_id, + message + ) + except TelegramUser.DoesNotExist: + pass + except Exception as e: + print(str(e)) diff --git a/payment_service/views.py b/payment_service/views.py index 9a43cea..75b65b5 100644 --- a/payment_service/views.py +++ b/payment_service/views.py @@ -41,7 +41,6 @@ def payment_successful(self, request, pk: None): """Endpoint for redirection after successful payment""" payment = self.get_object() session = stripe.checkout.Session.retrieve(payment.session_id) - dockerize-bot status = session.status if status != "complete":