diff --git a/src/weekly_post.py b/src/weekly_post.py index fa7f1e2..41c79e7 100644 --- a/src/weekly_post.py +++ b/src/weekly_post.py @@ -1,5 +1,5 @@ """Lambda handler — posts the weekly on-call schedule and previous week's pay summary -to Slack every Monday at 7am ET.""" +to Slack every Monday at 7am ET, and emails the pay summary to payroll.""" import logging import os @@ -74,6 +74,66 @@ def _calculate_weekly_pay(schedule: ShiftSchedule, week_start: datetime) -> dict } +def _build_pay_email_html(week_label: str, pay_record: dict) -> str: + """Build an HTML email body for the weekly pay summary.""" + rows = "" + for entry in pay_record["breakdown"]: + rows += ( + f"
| Day | Date | Name | Extension | Rate |
|---|
| Name | Shifts | Rate | Total |
|---|
This is an automated report from the After-Hours Shift Manager.
+ +""" + + +def _send_pay_email(week_label: str, pay_record: dict) -> None: + """Send the weekly pay summary email via SES.""" + sender = os.environ.get("SES_SENDER", "noreply@seahaven.com") + recipients = os.environ.get("PAYROLL_RECIPIENTS", "").split(",") + recipients = [r.strip() for r in recipients if r.strip()] + + if not recipients: + logger.warning("No PAYROLL_RECIPIENTS configured — skipping email") + return + + ses = boto3.client("ses") + html_body = _build_pay_email_html(week_label, pay_record) + + ses.send_email( + Source=sender, + Destination={"ToAddresses": recipients}, + Message={ + "Subject": {"Data": f"After-Hours Pay Summary — {week_label}"}, + "Body": {"Html": {"Data": html_body}}, + }, + ) + logger.info("Sent pay email to %s", recipients) + + def handler(event, context): now = datetime.now(EASTERN) @@ -114,6 +174,9 @@ def handler(event, context): ) logger.info("Posted pay summary for week of %s", week_key) + # Email pay summary to payroll + _send_pay_email(week_label, pay_record) + # --- Two-week schedule --- blocks = build_week_schedule(schedule, start_date=now) diff --git a/template.yaml b/template.yaml index 018d6bc..d0ccf39 100644 --- a/template.yaml +++ b/template.yaml @@ -89,9 +89,11 @@ Resources: SHIFT_TABLE: !Ref ShiftTable SLACK_BOT_TOKEN_PARAM: /afterhours-shift-manager/slack-bot-token SHIFT_CHANNEL_PARAM: /afterhours-shift-manager/channel-id + SES_SENDER: noreply@seahaven.com + PAYROLL_RECIPIENTS: adam@seahaven.com TZ: !Ref Timezone Policies: - - DynamoDBReadPolicy: + - DynamoDBCrudPolicy: TableName: !Ref ShiftTable - Statement: - Effect: Allow @@ -106,6 +108,11 @@ Resources: Condition: StringEquals: "kms:ViaService": !Sub "ssm.${AWS::Region}.amazonaws.com" + - Effect: Allow + Action: + - ses:SendEmail + Resource: + - !Sub "arn:aws:ses:${AWS::Region}:${AWS::AccountId}:identity/seahaven.com" Events: # EST: 7am ET = 12:00 UTC (Nov-Mar) WeeklyPostEST: