feat: enhance calendar ICS generation with event type filtering
All checks were successful
CI / lint-and-test (push) Successful in 22s

- Added support for filtering calendar events by type in the ICS generation API endpoint, allowing users to specify whether to include only duty shifts or all event types (duty, unavailable, vacation).
- Updated the `get_duties_for_user` function to accept an optional `event_types` parameter, enabling more flexible data retrieval based on user preferences.
- Enhanced unit tests to cover the new event type filtering functionality, ensuring correct behavior and reliability of the ICS generation process.
This commit is contained in:
2026-02-20 17:47:52 +03:00
parent e25eb7be2f
commit aa89494bd5
12 changed files with 160 additions and 30 deletions

View File

@@ -213,14 +213,19 @@ def get_duties_for_user(
user_id: int,
from_date: str,
to_date: str,
event_types: list[str] | None = None,
) -> list[tuple[Duty, str]]:
"""Return duties for one user overlapping the date range.
Optionally filter by event_type (e.g. "duty", "unavailable", "vacation").
When event_types is None, all event types are returned.
Args:
session: DB session.
user_id: User id.
from_date: Start date YYYY-MM-DD.
to_date: End date YYYY-MM-DD.
event_types: If not None, only return duties whose event_type is in this list.
Returns:
List of (Duty, full_name) tuples.
@@ -228,14 +233,17 @@ def get_duties_for_user(
to_date_next = (
datetime.fromisoformat(to_date + "T00:00:00") + timedelta(days=1)
).strftime("%Y-%m-%d")
filters = [
Duty.user_id == user_id,
Duty.start_at < to_date_next,
Duty.end_at >= from_date,
]
if event_types is not None:
filters.append(Duty.event_type.in_(event_types))
q = (
session.query(Duty, User.full_name)
.join(User, Duty.user_id == User.id)
.filter(
Duty.user_id == user_id,
Duty.start_at < to_date_next,
Duty.end_at >= from_date,
)
.filter(*filters)
)
return list(q.all())