2026-06-17 11:14:29 -04:00
|
|
|
"""Lambda handler — repoints 3CX call flow for a holiday day-shift.
|
|
|
|
|
|
|
|
|
|
A holiday is a day-only shift (08:00-17:00 ET), one HOLIDAY record per date.
|
|
|
|
|
At 08:00 this Lambda is invoked with ``{"action": "activate", "date": ...}`` and
|
|
|
|
|
at 17:00 with ``{"action": "deactivate", ...}``.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
|
2026-08-29 20:52:28 +00:00
|
|
|
import shared.sentry_init # noqa: F401
|
2026-09-21 19:13:30 +00:00
|
|
|
from shared.holiday_flow import activate, deactivate
|
|
|
|
|
from shared.schedule import ShiftSchedule
|
2026-06-17 11:14:29 -04:00
|
|
|
from shared.secrets import get_secret
|
|
|
|
|
from shared.three_cx_client import ThreeCXClient
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _make_client() -> ThreeCXClient:
|
|
|
|
|
secret_prefix = os.environ["TCX_SECRET_PREFIX"]
|
|
|
|
|
return ThreeCXClient(
|
|
|
|
|
domain=get_secret(f"{secret_prefix}domain"),
|
|
|
|
|
auth_mode="oauth",
|
|
|
|
|
client_id=get_secret(f"{secret_prefix}client-id"),
|
|
|
|
|
client_secret=get_secret(f"{secret_prefix}client-secret"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _activate(schedule: ShiftSchedule, date: str) -> dict:
|
2026-09-21 19:13:30 +00:00
|
|
|
return activate(schedule, date, client_factory=_make_client)
|
2026-06-17 11:14:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _deactivate(schedule: ShiftSchedule, date: str) -> dict:
|
2026-09-21 19:13:30 +00:00
|
|
|
return deactivate(schedule, date, client_factory=_make_client)
|
2026-06-17 11:14:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def handler(event, context):
|
2026-09-29 19:10:21 +00:00
|
|
|
if os.environ.get("STAGE", "prod") != "prod":
|
|
|
|
|
logger.info("Skipping holiday router because STAGE is not prod")
|
|
|
|
|
return {"skipped": "non_prod"}
|
|
|
|
|
|
2026-06-17 11:14:29 -04:00
|
|
|
action = event.get("action")
|
|
|
|
|
date = event.get("date")
|
|
|
|
|
logger.info("Holiday router invoked: action=%s date=%s", action, date)
|
|
|
|
|
|
|
|
|
|
if not date:
|
|
|
|
|
return {"error": True, "reason": "missing_date"}
|
|
|
|
|
|
|
|
|
|
schedule = ShiftSchedule()
|
|
|
|
|
try:
|
|
|
|
|
if action == "activate":
|
|
|
|
|
result = _activate(schedule, date)
|
|
|
|
|
elif action == "deactivate":
|
|
|
|
|
result = _deactivate(schedule, date)
|
|
|
|
|
else:
|
|
|
|
|
return {"error": True, "reason": "unknown_action", "action": action}
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.exception("Holiday router failed for action=%s date=%s", action, date)
|
|
|
|
|
return {"error": True, "action": action, "date": date}
|
|
|
|
|
|
|
|
|
|
logger.info("Result: %s", json.dumps(result, default=str))
|
|
|
|
|
return result
|