afterhours-shift-manager/src/holiday-router/app.py
Adam Moussa 54ad5a7e34
Some checks failed
Deploy API / Deploy API to dev (push) Has been cancelled
Deploy API / Deploy API to prod (push) Has been cancelled
fix(side-effects): keep non-prod off Slack and 3CX (DEV-306) (#287)
* fix(side-effects): keep non-prod off Slack and 3CX

Dev portal actions could still name the production Slack channel and phone queue. Skip those calls unless STAGE is prod, and leave the identifiers empty on non-prod tasks.

* style: apply formatter

---------

Co-authored-by: sea-haven-auto-fix[bot] <332630863+sea-haven-auto-fix[bot]@users.noreply.github.com>
2026-09-29 19:10:21 +00:00

65 lines
2.1 KiB
Python

"""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
import shared.sentry_init # noqa: F401
from shared.holiday_flow import activate, deactivate
from shared.schedule import ShiftSchedule
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:
return activate(schedule, date, client_factory=_make_client)
def _deactivate(schedule: ShiftSchedule, date: str) -> dict:
return deactivate(schedule, date, client_factory=_make_client)
def handler(event, context):
if os.environ.get("STAGE", "prod") != "prod":
logger.info("Skipping holiday router because STAGE is not prod")
return {"skipped": "non_prod"}
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