inital commit
This commit is contained in:
74
server/app/mailer/commands/email_campaign_report.py
Normal file
74
server/app/mailer/commands/email_campaign_report.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
|
||||
from app.audit.logging import audit_event
|
||||
from app.db.bootstrap import create_all_tables
|
||||
from app.db.session import SessionLocal
|
||||
from app.mailer.reports.emailing import CampaignReportEmailError, send_campaign_report_email
|
||||
from app.mailer.reports.campaigns import CampaignReportError
|
||||
from app.security.api_keys import authenticate_api_key
|
||||
from app.settings import settings
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Email a campaign report to one or more recipients.")
|
||||
parser.add_argument("--campaign-id", required=True, help="Database campaign UUID")
|
||||
parser.add_argument("--to", action="append", required=True, help="Report recipient. Repeat for multiple recipients.")
|
||||
parser.add_argument("--api-key", default=settings.dev_bootstrap_api_key)
|
||||
parser.add_argument("--include-jobs", action="store_true", help="Include per-job rows in the JSON report payload before rendering")
|
||||
parser.add_argument("--no-jobs-csv", action="store_true", help="Do not attach the per-job CSV report")
|
||||
parser.add_argument("--attach-report-json", action="store_true", help="Attach the complete report JSON")
|
||||
parser.add_argument("--dry-run", action="store_true", help="Build and validate the report email without SMTP sending")
|
||||
parser.add_argument("--json", action="store_true", help="Print machine-readable JSON")
|
||||
args = parser.parse_args()
|
||||
|
||||
create_all_tables()
|
||||
with SessionLocal() as session:
|
||||
api_key = authenticate_api_key(session, args.api_key)
|
||||
if not api_key:
|
||||
raise SystemExit("Invalid API key")
|
||||
try:
|
||||
result = send_campaign_report_email(
|
||||
session,
|
||||
tenant_id=api_key.tenant_id,
|
||||
campaign_id=args.campaign_id,
|
||||
to=args.to,
|
||||
include_jobs=args.include_jobs,
|
||||
attach_jobs_csv=not args.no_jobs_csv,
|
||||
attach_report_json=args.attach_report_json,
|
||||
dry_run=args.dry_run,
|
||||
)
|
||||
audit_event(
|
||||
session,
|
||||
tenant_id=api_key.tenant_id,
|
||||
user_id=api_key.user_id,
|
||||
api_key_id=api_key.id,
|
||||
action="report.email_sent" if not args.dry_run else "report.email_dry_run",
|
||||
object_type="campaign",
|
||||
object_id=args.campaign_id,
|
||||
details=result.as_dict(),
|
||||
commit=True,
|
||||
)
|
||||
except (CampaignReportError, CampaignReportEmailError) as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
except Exception as exc:
|
||||
raise SystemExit(f"Could not email campaign report: {exc}") from exc
|
||||
|
||||
if args.json:
|
||||
print(json.dumps(result.as_dict(), indent=2, ensure_ascii=False))
|
||||
else:
|
||||
print(f"Campaign: {result.campaign_id}")
|
||||
print(f"To: {', '.join(result.to)}")
|
||||
print(f"Subject: {result.subject}")
|
||||
print(f"Dry run: {result.dry_run}")
|
||||
print(f"Sent: {result.sent}")
|
||||
print(f"CSV: {result.attached_jobs_csv}")
|
||||
print(f"JSON: {result.attached_report_json}")
|
||||
if result.smtp_host:
|
||||
print(f"SMTP: {result.smtp_host}:{result.smtp_port}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user