|
1 | 1 | from datetime import datetime, timezone |
2 | 2 | from typing import Any, Dict |
3 | 3 |
|
4 | | -from fastapi import APIRouter, Depends, HTTPException |
| 4 | +from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException |
5 | 5 | from httpx import AsyncClient |
6 | 6 |
|
7 | 7 | from auth.config import Settings, get_settings |
|
14 | 14 | router = APIRouter(prefix="/bpa", tags=["bpa", "registration"]) |
15 | 15 |
|
16 | 16 |
|
| 17 | +def send_approver_email( |
| 18 | + email_service: EmailService, |
| 19 | + approver_email: str, |
| 20 | + registration: BPARegistrationRequest, |
| 21 | + bpa_resources: list[Dict[str, Any]], |
| 22 | +) -> None: |
| 23 | + subject = "New BPA User Access Request" |
| 24 | + |
| 25 | + org_list_html = "".join( |
| 26 | + f"<li>{res['name']} (ID: {res['id']})</li>" for res in bpa_resources |
| 27 | + ) |
| 28 | + |
| 29 | + body_html = f""" |
| 30 | + <p>A new user has requested access to one or more organizations in the BPA service.</p> |
| 31 | + <p><strong>User:</strong> {registration.fullname} ({registration.email})</p> |
| 32 | + <p><strong>Requested access to:</strong></p> |
| 33 | + <ul>{org_list_html}</ul> |
| 34 | + <p>Please log into the AAI Admin Portal to review and approve access.</p> |
| 35 | + """ |
| 36 | + |
| 37 | + email_service.send(approver_email, subject, body_html) |
| 38 | + |
| 39 | + |
17 | 40 | @router.post( |
18 | 41 | "/register", |
19 | 42 | response_model=Dict[str, Any], |
|
24 | 47 | }, |
25 | 48 | ) |
26 | 49 | async def register_bpa_user( |
27 | | - registration: BPARegistrationRequest, settings: Settings = Depends(get_settings) |
| 50 | + registration: BPARegistrationRequest, |
| 51 | + background_tasks: BackgroundTasks, |
| 52 | + settings: Settings = Depends(get_settings), |
28 | 53 | ) -> Dict[str, Any]: |
29 | 54 | """Register a new BPA user with selected organization resources.""" |
30 | 55 | url = f"https://{settings.auth0_domain}/api/v2/users" |
@@ -82,25 +107,14 @@ async def register_bpa_user( |
82 | 107 | if bpa_resources: |
83 | 108 | email_service = EmailService() |
84 | 109 | approver_email = "aai-dev@biocommons.org.au" # ideally move to settings |
85 | | - subject = "New BPA User Access Request" |
86 | 110 |
|
87 | | - org_list_html = "".join( |
88 | | - f"<li>{res['name']} (ID: {res['id']})</li>" for res in bpa_resources |
| 111 | + # Add email sending as a background task |
| 112 | + background_tasks.add_task( |
| 113 | + send_approver_email, email_service, approver_email, registration, bpa_resources |
89 | 114 | ) |
90 | 115 |
|
91 | | - body_html = f""" |
92 | | - <p>A new user has requested access to one or more organizations in the BPA service.</p> |
93 | | - <p><strong>User:</strong> {registration.fullname} ({registration.email})</p> |
94 | | - <p><strong>Requested access to:</strong></p> |
95 | | - <ul>{org_list_html}</ul> |
96 | | - <p>Please log into the AAI Admin Portal to review and approve access.</p> |
97 | | - """ |
98 | | - |
99 | | - email_service.send(approver_email, subject, body_html) |
100 | | - |
101 | 116 | return {"message": "User registered successfully", "user": response.json()} |
102 | 117 |
|
103 | | - |
104 | 118 | except HTTPException: |
105 | 119 | raise |
106 | 120 | except Exception as e: |
|
0 commit comments