mirror of
https://github.com/Sea-Haven-Industries/meal-order-manager.git
synced 2026-09-30 10:03:13 +00:00
EventBridge can deliver past midnight ET; widen the wall-clock guard so a delayed Thursday 23:59 UTC cron still closes the form. Idempotent when already closed. Adds test for early Friday; past-window skip now starts Fri 04:00 ET. Co-authored-by: Adam Moussa <amoussa1229@users.noreply.github.com>
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import json
|
||
import os
|
||
from datetime import datetime
|
||
from zoneinfo import ZoneInfo
|
||
|
||
import boto3
|
||
|
||
from shared.db import current_week, get_form_status, set_form_status
|
||
|
||
_lambda = boto3.client("lambda")
|
||
EASTERN = ZoneInfo("America/New_York")
|
||
|
||
|
||
def lambda_handler(event, context):
|
||
now_et = datetime.now(EASTERN)
|
||
# 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:
|
||
return {
|
||
"status": "skipped",
|
||
"reason": "outside close window (must be Thu 23:xx or Fri 00–03 ET)",
|
||
}
|
||
|
||
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}
|