2026-05-12 18:25:15 -04:00
|
|
|
|
import json
|
|
|
|
|
|
import os
|
2026-05-13 13:39:18 -04:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
from zoneinfo import ZoneInfo
|
2026-05-12 18:25:15 -04:00
|
|
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
|
|
|
|
|
|
|
|
from shared.db import current_week, get_form_status, set_form_status
|
|
|
|
|
|
|
|
|
|
|
|
_lambda = boto3.client("lambda")
|
2026-05-13 13:39:18 -04:00
|
|
|
|
EASTERN = ZoneInfo("America/New_York")
|
2026-05-12 18:25:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lambda_handler(event, context):
|
2026-05-13 13:39:18 -04:00
|
|
|
|
now_et = datetime.now(EASTERN)
|
2026-05-13 19:11:32 +00:00
|
|
|
|
# EventBridge can fire slightly after midnight ET; accept Thu 23:xx or Fri 00–03
|
|
|
|
|
|
# ET so a delayed cron still closes the form. Idempotency: already-closed is a no-op.
|
|
|
|
|
|
in_close_window = (now_et.weekday() == 3 and now_et.hour == 23) or (
|
|
|
|
|
|
now_et.weekday() == 4 and now_et.hour < 4
|
|
|
|
|
|
)
|
|
|
|
|
|
if not in_close_window:
|
2026-05-13 13:39:18 -04:00
|
|
|
|
return {
|
|
|
|
|
|
"status": "skipped",
|
2026-05-13 19:11:32 +00:00
|
|
|
|
"reason": "outside close window (must be Thu 23:xx or Fri 00–03 ET)",
|
2026-05-13 13:39:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-12 18:25:15 -04:00
|
|
|
|
week = event.get("week", current_week())
|
|
|
|
|
|
|
|
|
|
|
|
status = get_form_status(week)
|
|
|
|
|
|
if status == "closed":
|
|
|
|
|
|
return {"status": "already_closed", "week": week}
|
|
|
|
|
|
|
|
|
|
|
|
set_form_status(week, "closed")
|
|
|
|
|
|
|
|
|
|
|
|
_lambda.invoke(
|
|
|
|
|
|
FunctionName=os.environ["AGGREGATE_FUNCTION_ARN"],
|
|
|
|
|
|
InvocationType="Event",
|
|
|
|
|
|
Payload=json.dumps({"week": week}),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return {"status": "closed", "week": week, "aggregate_triggered": True}
|