feat: Create activation notifications for appointments when status changes to active
This commit is contained in:
+28
-22
@@ -1123,6 +1123,8 @@ def update_appointment_statuses():
|
||||
|
||||
for appointment in appointments_to_check:
|
||||
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
|
||||
new_status = au.get_current_status(appointment, log_changes=True, user='scheduler')
|
||||
@@ -1147,6 +1149,7 @@ def update_appointment_statuses():
|
||||
)
|
||||
if item_doc:
|
||||
item_name = item_doc.get('Name', item_id_str)
|
||||
activation_item_name = item_name
|
||||
total_exemplare = int(item_doc.get('Exemplare', 1))
|
||||
# Count how many active (non-planned) borrows currently hold this item
|
||||
active_borrows = ausleihungen.count_documents({
|
||||
@@ -1193,31 +1196,34 @@ def update_appointment_statuses():
|
||||
updated_count += 1
|
||||
if new_status == 'active':
|
||||
activated_count += 1
|
||||
try:
|
||||
_create_notification(
|
||||
db,
|
||||
audience='user',
|
||||
notif_type='appointment_activated',
|
||||
title='Reservierung ist jetzt aktiv',
|
||||
message=(
|
||||
f"Deine geplante Ausleihe für {item_name} startet jetzt."
|
||||
),
|
||||
target_user=str(appointment.get('User') or '').strip() or None,
|
||||
reference={
|
||||
'appointment_id': str(appointment.get('_id')),
|
||||
'item_id': str(appointment.get('Item') or ''),
|
||||
'event': 'activated',
|
||||
},
|
||||
unique_key=f"appointment:activated:{appointment.get('_id')}",
|
||||
severity='info',
|
||||
)
|
||||
except Exception as notif_err:
|
||||
app.logger.warning(
|
||||
f"Failed to create activation notification for {appointment.get('_id')}: {notif_err}"
|
||||
)
|
||||
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:
|
||||
_create_notification(
|
||||
db,
|
||||
audience='user',
|
||||
notif_type='appointment_activated',
|
||||
title='Reservierung ist jetzt aktiv',
|
||||
message=(
|
||||
f"Deine geplante Ausleihe für {activation_item_name} startet jetzt."
|
||||
),
|
||||
target_user=activation_user,
|
||||
reference={
|
||||
'appointment_id': str(appointment.get('_id')),
|
||||
'item_id': str(appointment.get('Item') or ''),
|
||||
'event': 'activated',
|
||||
},
|
||||
unique_key=f"appointment:activated:{appointment.get('_id')}",
|
||||
severity='info',
|
||||
)
|
||||
except Exception as notif_err:
|
||||
app.logger.warning(
|
||||
f"Failed to create activation notification for {appointment.get('_id')}: {notif_err}"
|
||||
)
|
||||
|
||||
client.close()
|
||||
|
||||
if updated_count > 0:
|
||||
|
||||
Reference in New Issue
Block a user