diff --git a/backend/requests_app/notification_signals.py b/backend/requests_app/notification_signals.py index 268c3f58..79219340 100644 --- a/backend/requests_app/notification_signals.py +++ b/backend/requests_app/notification_signals.py @@ -35,32 +35,36 @@ def create_request_notifications(sender, instance, created, **kwargs): 1. Новое заявление - помечаем для отправки уведомлений после установки recipients 2. Изменение статуса - уведомление автору """ - request_obj = instance + try: + request_obj = instance - # Логирование для отладки - print( - f"🔔 [SIGNAL] create_request_notifications: created={created}, status={request_obj.status}, id={request_obj.id}" - ) - - if created and request_obj.status != "draft": - # Помечаем заявление как ожидающее отправки уведомлений - # Уведомления будут отправлены после установки recipients через m2m_changed + # Логирование для отладки print( - f"📌 [SIGNAL] Помечаем заявление #{request_obj.id} для отправки уведомлений после установки recipients" + f"🔔 [SIGNAL] create_request_notifications: created={created}, status={request_obj.status}, id={request_obj.id}" ) - _pending_new_requests.add(request_obj.id) - else: - print(f"⏭️ [SIGNAL] Пропускаем: created={created}, status={request_obj.status}") - if not created: - # Проверяем изменение статуса через сохраненный атрибут _old_status - if hasattr(request_obj, "_old_status"): - old_status = request_obj._old_status - new_status = request_obj.status + if created and request_obj.status != "draft": + # Помечаем заявление как ожидающее отправки уведомлений + # Уведомления будут отправлены после установки recipients через m2m_changed + print( + f"📌 [SIGNAL] Помечаем заявление #{request_obj.id} для отправки уведомлений после установки recipients" + ) + _pending_new_requests.add(request_obj.id) + else: + print(f"⏭️ [SIGNAL] Пропускаем: created={created}, status={request_obj.status}") - if old_status != new_status: - # Передаем текущий request_obj с актуальным approver_id - notify_status_change(request_obj, old_status, new_status) + if not created: + # Проверяем изменение статуса через сохраненный атрибут _old_status + if hasattr(request_obj, "_old_status"): + old_status = request_obj._old_status + new_status = request_obj.status + + if old_status != new_status: + # Передаем текущий request_obj с актуальным approver_id + notify_status_change(request_obj, old_status, new_status) + except Exception as e: + logger.exception(f"[SIGNAL ERROR] create_request_notifications: {e}") + print(f"❌ [SIGNAL ERROR] create_request_notifications: {e}") @receiver(pre_save, sender=Request) @@ -83,13 +87,17 @@ def notify_on_recipients_changed(sender, instance, action, **kwargs): Срабатывает когда recipients устанавливаются через .set(), .add() и т.д. """ - # Проверяем что это завершение операции установки recipients - if action == "post_add" and instance.id in _pending_new_requests: - print( - f"🎯 [M2M_SIGNAL] Recipients установлены для заявления #{instance.id}, отправляем уведомления" - ) - _pending_new_requests.discard(instance.id) - notify_new_request(instance) + try: + # Проверяем что это завершение операции установки recipients + if action == "post_add" and instance.id in _pending_new_requests: + print( + f"🎯 [M2M_SIGNAL] Recipients установлены для заявления #{instance.id}, отправляем уведомления" + ) + _pending_new_requests.discard(instance.id) + notify_new_request(instance) + except Exception as e: + logger.exception(f"[SIGNAL ERROR] notify_on_recipients_changed: {e}") + print(f"❌ [SIGNAL ERROR] notify_on_recipients_changed: {e}") @receiver(m2m_changed, sender=Request.cc_users.through) @@ -100,15 +108,19 @@ def notify_on_cc_users_changed(sender, instance, action, **kwargs): Если recipients не были установлены, но установлены cc_users, также отправляем уведомления. """ - # Если это завершение добавления cc_users И заявление все еще ожидает уведомлений - if action == "post_add" and instance.id in _pending_new_requests: - # Проверяем что recipients пусты (значит уведомления еще не отправлены) - if instance.recipients.count() == 0: - print( - f"🎯 [M2M_SIGNAL] CC users установлены для заявления #{instance.id} (без recipients), отправляем уведомления" - ) - _pending_new_requests.discard(instance.id) - notify_new_request(instance) + try: + # Если это завершение добавления cc_users И заявление все еще ожидает уведомлений + if action == "post_add" and instance.id in _pending_new_requests: + # Проверяем что recipients пусты (значит уведомления еще не отправлены) + if instance.recipients.count() == 0: + print( + f"🎯 [M2M_SIGNAL] CC users установлены для заявления #{instance.id} (без recipients), отправляем уведомления" + ) + _pending_new_requests.discard(instance.id) + notify_new_request(instance) + except Exception as e: + logger.exception(f"[SIGNAL ERROR] notify_on_cc_users_changed: {e}") + print(f"❌ [SIGNAL ERROR] notify_on_cc_users_changed: {e}") @receiver(post_save, sender=RequestComment) diff --git a/backend/static/js/notifications/notification-list.js b/backend/static/js/notifications/notification-list.js index 2c32875a..aae78f11 100644 --- a/backend/static/js/notifications/notification-list.js +++ b/backend/static/js/notifications/notification-list.js @@ -101,7 +101,7 @@ class NotificationListManager { params.append('search', this.currentFilter.search); } - const response = await fetch(`/api/notifications/?${params}`, { + const response = await fetch(`/api/v1/notifications/?${params}`, { credentials: 'same-origin', headers: { 'Accept': 'application/json', @@ -318,7 +318,7 @@ class NotificationListManager { async markAsRead(id) { try { - const response = await fetch(`/api/notifications/${id}/read/`, { + const response = await fetch(`/api/v1/notifications/${id}/read/`, { method: 'POST', credentials: 'same-origin', headers: { @@ -359,7 +359,7 @@ class NotificationListManager { } try { - const response = await fetch('/api/notifications/read-all/', { + const response = await fetch('/api/v1/notifications/read-all/', { method: 'POST', credentials: 'same-origin', headers: { @@ -385,7 +385,7 @@ class NotificationListManager { async deleteNotification(id) { try { - const response = await fetch(`/api/notifications/${id}/`, { + const response = await fetch(`/api/v1/notifications/${id}/`, { method: 'DELETE', credentials: 'same-origin', headers: { diff --git a/backend/static/js/notifications/notification-manager.js b/backend/static/js/notifications/notification-manager.js index 55d5919d..10e72227 100644 --- a/backend/static/js/notifications/notification-manager.js +++ b/backend/static/js/notifications/notification-manager.js @@ -508,7 +508,7 @@ class NotificationManager { async markAsRead(notificationId) { try { const response = await fetch( - `/api/notifications/${notificationId}/read/`, + `/api/v1/notifications/${notificationId}/read/`, { method: "POST", headers: { @@ -528,7 +528,7 @@ class NotificationManager { async markAllAsRead() { try { - const response = await fetch("/api/notifications/read-all/", { + const response = await fetch("/api/v1/notifications/read-all/", { method: "POST", headers: { "Content-Type": "application/json", diff --git a/backend/static/js/notifications/notification-settings.js b/backend/static/js/notifications/notification-settings.js index 722f92ab..3ae0cdb7 100644 --- a/backend/static/js/notifications/notification-settings.js +++ b/backend/static/js/notifications/notification-settings.js @@ -17,7 +17,7 @@ class NotificationSettingsManager { if (!container) return; try { - const response = await fetch('/api/notifications/settings/', { + const response = await fetch('/api/v1/notifications/settings/', { credentials: 'same-origin', headers: { 'Accept': 'application/json', @@ -211,7 +211,7 @@ class NotificationSettingsManager { async updateSetting(category, field, value) { try { - const response = await fetch('/api/notifications/settings/category/update/', { + const response = await fetch('/api/v1/notifications/settings/category/update/', { method: 'PUT', credentials: 'same-origin', headers: {