| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- from datetime import date
- from typing import TYPE_CHECKING
- from room_cal.closed import Closed
- from room_cal._utils import safe_json_loads
- if TYPE_CHECKING:
- from room_cal.service_api import ServiceAPI
- ACTION_SPEC = ('push-closed', [])
- async def push_closed(
- start_date: date,
- end_date: date,
- *,
- api: 'ServiceAPI | None' = None,
- ) -> list[dict]:
- """Read local 'Closed' CalDAV events and push them to the service API.
- Dates with a local Closed event are blocked on all services.
- Dates without a local Closed event that are blocked in the API are opened.
- Skips booked dates (handled by set_available internally).
- Returns list of {date, action} for every date changed ('closed' or 'opened').
- """
- from room_cal.service_api import ServiceAPI as _ServiceAPI
- if api is None:
- api = _ServiceAPI()
- closed_cal = Closed()
- cal_events = await closed_cal.list(start_date, end_date)
- cal_by_date: set[str] = set()
- for e in cal_events:
- ds = e.start.date().isoformat()
- d = safe_json_loads(e.x_data)
- if d is None or d.get('closed_services'):
- cal_by_date.add(ds)
- api_by_date = await api.get_closed_by_date(start_date, end_date)
- # Always push locally-closed dates: set_availability skips if already fully closed
- # (checks both available and rooms_to_sell), catching hidden=1/rooms_to_sell=1 drift.
- to_close = sorted(cal_by_date)
- to_open = sorted(set(api_by_date) - cal_by_date)
- results: list[dict] = []
- for ds in to_close:
- await api.set_available(date.fromisoformat(ds), False)
- results.append({'date': date.fromisoformat(ds), 'action': 'closed'})
- for ds in to_open:
- await api.set_available(date.fromisoformat(ds), True)
- results.append({'date': date.fromisoformat(ds), 'action': 'opened'})
- results.sort(key=lambda r: r['date'])
- return results
|