feat: Refactor appointment retrieval to use dedicated appointment collection and improve time range extraction
This commit is contained in:
+31
-42
@@ -37,6 +37,7 @@ if _CURRENT_DIR not in sys.path:
|
|||||||
import Web.modules.database.user as us
|
import Web.modules.database.user as us
|
||||||
import Web.modules.database.items as it
|
import Web.modules.database.items as it
|
||||||
import Web.modules.database.ausleihung as au
|
import Web.modules.database.ausleihung as au
|
||||||
|
import Web.modules.database.termine as termin
|
||||||
import Web.modules.log.audit_log as al
|
import Web.modules.log.audit_log as al
|
||||||
import push_notifications as pn
|
import push_notifications as pn
|
||||||
import Web.modules.inventarsystem.pdf_export as pdf_export
|
import Web.modules.inventarsystem.pdf_export as pdf_export
|
||||||
@@ -4817,56 +4818,44 @@ def get_user_appointments():
|
|||||||
db = client[MONGODB_DB]
|
db = client[MONGODB_DB]
|
||||||
items_col = db['items']
|
items_col = db['items']
|
||||||
|
|
||||||
# Use the established user-specific route logic for appointments.
|
# Use the appointment collection for the user's appointments
|
||||||
bookings = au.get_ausleihung_by_user(
|
appointments = termin.get_upcoming_for_user(username, limit=250)
|
||||||
username,
|
|
||||||
status=['planned', 'active', 'completed'],
|
|
||||||
use_client_side_verification=True,
|
|
||||||
)
|
|
||||||
bookings = sorted(bookings, key=lambda b: b.get('Start') or datetime.datetime.min)
|
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for booking in bookings:
|
for appt in appointments:
|
||||||
start_dt = booking.get('Start')
|
appt_id = str(appt.get('_id') or '')
|
||||||
if not start_dt:
|
if not appt_id:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
end_dt = booking.get('End')
|
date_start = str(appt.get('date_start') or '')
|
||||||
if not end_dt and isinstance(start_dt, datetime.datetime):
|
date_end = str(appt.get('date_end') or date_start)
|
||||||
end_dt = start_dt + datetime.timedelta(minutes=45)
|
time_span = appt.get('time_span', []) or []
|
||||||
elif not end_dt:
|
|
||||||
end_dt = start_dt
|
|
||||||
|
|
||||||
item_id = str(booking.get('Item') or '')
|
# Try to extract a time range from the first time_span entry
|
||||||
item_doc = None
|
start_dt_str = date_start
|
||||||
if item_id:
|
end_dt_str = date_end
|
||||||
try:
|
try:
|
||||||
item_doc = items_col.find_one({'_id': ObjectId(item_id)})
|
import re as _re
|
||||||
except Exception:
|
if time_span:
|
||||||
item_doc = None
|
first = str(time_span[0])
|
||||||
|
m = _re.search(r"(\d{2}:\d{2})-(\d{2}:\d{2})", first)
|
||||||
|
if m:
|
||||||
|
start_dt_str = f"{date_start}T{m.group(1)}"
|
||||||
|
end_dt_str = f"{date_start}T{m.group(2)}"
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
item_name = item_id or 'Termin'
|
title = appt.get('note') or f"Termin von {appt.get('user') or ''}"
|
||||||
if item_doc:
|
|
||||||
item_name = item_doc.get('Name') or item_doc.get('Code_4') or item_name
|
|
||||||
|
|
||||||
period = booking.get('Period')
|
|
||||||
title = item_name
|
|
||||||
if period:
|
|
||||||
title = f"{title} - {period}. Std"
|
|
||||||
|
|
||||||
status = booking.get('VerifiedStatus') or booking.get('Status') or 'unknown'
|
|
||||||
if status == 'active':
|
|
||||||
status = 'current'
|
|
||||||
result.append({
|
result.append({
|
||||||
'id': str(booking.get('_id')),
|
'id': appt_id,
|
||||||
'title': title,
|
'title': title,
|
||||||
'start': start_dt.isoformat() if isinstance(start_dt, datetime.datetime) else str(start_dt),
|
'start': start_dt_str,
|
||||||
'end': end_dt.isoformat() if isinstance(end_dt, datetime.datetime) else str(end_dt),
|
'end': end_dt_str,
|
||||||
'status': status,
|
'status': 'planned',
|
||||||
'itemId': item_id,
|
'itemId': appt_id,
|
||||||
'userName': str(booking.get('User') or ''),
|
'userName': str(appt.get('user') or ''),
|
||||||
'notes': str(booking.get('Notes') or ''),
|
'notes': str(appt.get('note') or ''),
|
||||||
'period': period,
|
'period': None,
|
||||||
'isCurrentUser': True,
|
'isCurrentUser': True,
|
||||||
'itemBorrower': '',
|
'itemBorrower': '',
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user