Skip to content

Commit 3c2c665

Browse files
committed
rejection email
1 parent d89846e commit 3c2c665

File tree

2 files changed

+122
-21
lines changed

2 files changed

+122
-21
lines changed

services/web/server/src/simcore_service_webserver/users/_accounts_service.py

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -307,34 +307,16 @@ async def reject_user_account(
307307
return pre_registration_id
308308

309309

310-
async def send_approval_email_to_user(
310+
def _create_product_and_user_data(
311311
app: web.Application,
312312
*,
313313
product_name: ProductName,
314-
invitation_link: HttpUrl,
315314
user_email: LowerCaseEmailStr,
316315
first_name: str,
317316
last_name: str,
318-
) -> None:
319-
"""Send approval email to user with invitation link.
320-
321-
Args:
322-
app: The web application instance
323-
product_name: Product name for which the user was approved
324-
invitation_link: URL link for the invitation
325-
user_email: Email of the user to send approval to
326-
user_name: Name of the user
327-
"""
328-
from notifications_library._email import compose_email, create_email_session
329-
from notifications_library._email_render import (
330-
get_support_address,
331-
get_user_address,
332-
render_email_parts,
333-
)
317+
):
318+
"""Create ProductData and UserData objects for email rendering."""
334319
from notifications_library._models import ProductData, ProductUIData, UserData
335-
from notifications_library._render import (
336-
create_render_environment_from_notifications_library,
337-
)
338320

339321
# Get product data from the app
340322
product = get_product(app, product_name=product_name)
@@ -381,6 +363,37 @@ async def send_approval_email_to_user(
381363
last_name=last_name,
382364
)
383365

366+
return product_data, user_data
367+
368+
369+
async def send_approval_email_to_user(
370+
app: web.Application,
371+
*,
372+
product_name: ProductName,
373+
invitation_link: HttpUrl,
374+
user_email: LowerCaseEmailStr,
375+
first_name: str,
376+
last_name: str,
377+
) -> None:
378+
from notifications_library._email import compose_email, create_email_session
379+
from notifications_library._email_render import (
380+
get_support_address,
381+
get_user_address,
382+
render_email_parts,
383+
)
384+
from notifications_library._render import (
385+
create_render_environment_from_notifications_library,
386+
)
387+
388+
# Create product and user data
389+
product_data, user_data = _create_product_and_user_data(
390+
app,
391+
product_name=product_name,
392+
user_email=user_email,
393+
first_name=first_name,
394+
last_name=last_name,
395+
)
396+
384397
# Prepare event data
385398
event_extra_data = {
386399
"host": str(invitation_link).split("?")[0],
@@ -408,3 +421,59 @@ async def send_approval_email_to_user(
408421
# Send email
409422
async with create_email_session(settings=SMTPSettings.create_from_envs()) as smtp:
410423
await smtp.send_message(msg)
424+
425+
426+
async def send_rejection_email_to_user(
427+
app: web.Application,
428+
*,
429+
product_name: ProductName,
430+
user_email: LowerCaseEmailStr,
431+
first_name: str,
432+
last_name: str,
433+
host: str,
434+
) -> None:
435+
from notifications_library._email import compose_email, create_email_session
436+
from notifications_library._email_render import (
437+
get_support_address,
438+
get_user_address,
439+
render_email_parts,
440+
)
441+
from notifications_library._render import (
442+
create_render_environment_from_notifications_library,
443+
)
444+
445+
# Create product and user data
446+
product_data, user_data = _create_product_and_user_data(
447+
app,
448+
product_name=product_name,
449+
user_email=user_email,
450+
first_name=first_name,
451+
last_name=last_name,
452+
)
453+
454+
# Prepare event data (based on test_email_events.py)
455+
event_extra_data = {
456+
"host": host,
457+
}
458+
459+
# Render email parts
460+
parts = render_email_parts(
461+
env=create_render_environment_from_notifications_library(),
462+
event_name="on_account_rejected",
463+
user=user_data,
464+
product=product_data,
465+
**event_extra_data,
466+
)
467+
468+
# Compose email
469+
msg = compose_email(
470+
from_=get_support_address(product_data),
471+
to=get_user_address(user_data),
472+
subject=parts.subject,
473+
content_text=parts.text_content,
474+
content_html=parts.html_content,
475+
)
476+
477+
# Send email
478+
async with create_email_session(settings=SMTPSettings.create_from_envs()) as smtp:
479+
await smtp.send_message(msg)

services/web/server/src/simcore_service_webserver/users/_controller/rest/accounts_rest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,36 @@ async def reject_user_account(request: web.Request) -> web.Response:
250250
)
251251
assert pre_registration_id # nosec
252252

253+
# Send rejection email to user
254+
with log_context(
255+
_logger,
256+
logging.INFO,
257+
"Sending rejection email to %s ...",
258+
rejection_data.email,
259+
):
260+
# get pre-registration data
261+
found = await _accounts_service.search_users_accounts(
262+
request.app,
263+
email_glob=rejection_data.email,
264+
product_name=req_ctx.product_name,
265+
include_products=False,
266+
)
267+
user_account = found[0]
268+
assert user_account.pre_registration_id == pre_registration_id # nosec
269+
assert user_account.email == rejection_data.email # nosec
270+
271+
# send email to user
272+
fire_and_forget_task(
273+
_accounts_service.send_rejection_email_to_user(
274+
request.app,
275+
product_name=req_ctx.product_name,
276+
user_email=rejection_data.email,
277+
first_name=user_account.first_name or "User",
278+
last_name=user_account.last_name or "",
279+
host=request.host,
280+
),
281+
task_suffix_name=f"{__name__}.send_rejection_email_to_user.{rejection_data.email}",
282+
fire_and_forget_tasks_collection=request.app[APP_FIRE_AND_FORGET_TASKS_KEY],
283+
)
284+
253285
return web.json_response(status=status.HTTP_204_NO_CONTENT)

0 commit comments

Comments
 (0)