feat: Create activation notifications for appointments when status changes to active
This commit is contained in:
+10
-4
@@ -1123,6 +1123,8 @@ def update_appointment_statuses():
|
|||||||
|
|
||||||
for appointment in appointments_to_check:
|
for appointment in appointments_to_check:
|
||||||
old_status = appointment.get('Status')
|
old_status = appointment.get('Status')
|
||||||
|
activation_user = str(appointment.get('User') or '').strip()
|
||||||
|
activation_item_name = str(appointment.get('Item') or 'Termin')
|
||||||
|
|
||||||
# Aktuellen Status bestimmen
|
# Aktuellen Status bestimmen
|
||||||
new_status = au.get_current_status(appointment, log_changes=True, user='scheduler')
|
new_status = au.get_current_status(appointment, log_changes=True, user='scheduler')
|
||||||
@@ -1147,6 +1149,7 @@ def update_appointment_statuses():
|
|||||||
)
|
)
|
||||||
if item_doc:
|
if item_doc:
|
||||||
item_name = item_doc.get('Name', item_id_str)
|
item_name = item_doc.get('Name', item_id_str)
|
||||||
|
activation_item_name = item_name
|
||||||
total_exemplare = int(item_doc.get('Exemplare', 1))
|
total_exemplare = int(item_doc.get('Exemplare', 1))
|
||||||
# Count how many active (non-planned) borrows currently hold this item
|
# Count how many active (non-planned) borrows currently hold this item
|
||||||
active_borrows = ausleihungen.count_documents({
|
active_borrows = ausleihungen.count_documents({
|
||||||
@@ -1193,6 +1196,11 @@ def update_appointment_statuses():
|
|||||||
updated_count += 1
|
updated_count += 1
|
||||||
if new_status == 'active':
|
if new_status == 'active':
|
||||||
activated_count += 1
|
activated_count += 1
|
||||||
|
elif new_status == 'completed':
|
||||||
|
completed_count += 1
|
||||||
|
|
||||||
|
# Create activation notification even if another worker already updated the status.
|
||||||
|
if old_status == 'planned' and new_status == 'active' and activation_user:
|
||||||
try:
|
try:
|
||||||
_create_notification(
|
_create_notification(
|
||||||
db,
|
db,
|
||||||
@@ -1200,9 +1208,9 @@ def update_appointment_statuses():
|
|||||||
notif_type='appointment_activated',
|
notif_type='appointment_activated',
|
||||||
title='Reservierung ist jetzt aktiv',
|
title='Reservierung ist jetzt aktiv',
|
||||||
message=(
|
message=(
|
||||||
f"Deine geplante Ausleihe für {item_name} startet jetzt."
|
f"Deine geplante Ausleihe für {activation_item_name} startet jetzt."
|
||||||
),
|
),
|
||||||
target_user=str(appointment.get('User') or '').strip() or None,
|
target_user=activation_user,
|
||||||
reference={
|
reference={
|
||||||
'appointment_id': str(appointment.get('_id')),
|
'appointment_id': str(appointment.get('_id')),
|
||||||
'item_id': str(appointment.get('Item') or ''),
|
'item_id': str(appointment.get('Item') or ''),
|
||||||
@@ -1215,8 +1223,6 @@ def update_appointment_statuses():
|
|||||||
app.logger.warning(
|
app.logger.warning(
|
||||||
f"Failed to create activation notification for {appointment.get('_id')}: {notif_err}"
|
f"Failed to create activation notification for {appointment.get('_id')}: {notif_err}"
|
||||||
)
|
)
|
||||||
elif new_status == 'completed':
|
|
||||||
completed_count += 1
|
|
||||||
|
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user