-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from HackAtUCI/feature/decision-release
Create decision release endpoint for directors
- Loading branch information
Showing
4 changed files
with
99 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import itertools | ||
from typing import Iterable, Iterator, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def batched(iterable: Iterable[T], n: int) -> Iterator[tuple[T, ...]]: | ||
"""batched('ABCDEFG', 3) --> ABC DEF G""" | ||
# from https://docs.python.org/3/library/itertools.html#itertools.batched | ||
if n < 1: | ||
raise ValueError("n must be at least one") | ||
it = iter(iterable) | ||
while batch := tuple(itertools.islice(it, n)): | ||
yield batch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from typing import Protocol | ||
from typing import Iterable, Protocol | ||
|
||
from pydantic import EmailStr | ||
|
||
|
@@ -9,6 +9,12 @@ | |
IH_SENDER = ("[email protected]", "IrvineHacks 2024 Applications") | ||
REPLY_TO_HACK_AT_UCI = ("[email protected]", "Hack at UCI") | ||
|
||
DECISION_TEMPLATES = { | ||
Decision.ACCEPTED: Template.ACCEPTED_EMAIL, | ||
Decision.REJECTED: Template.REJECTED_EMAIL, | ||
Decision.WAITLISTED: Template.WAITLISTED_EMAIL, | ||
} | ||
|
||
|
||
class ContactInfo(Protocol): | ||
first_name: str | ||
|
@@ -46,27 +52,13 @@ async def send_guest_login_email(email: EmailStr, passphrase: str) -> None: | |
|
||
|
||
async def send_decision_email( | ||
sender_email: tuple[str, str], applicant_batch: dict[tuple[str, EmailStr], Decision] | ||
applicant_batch: Iterable[tuple[str, EmailStr]], decision: Decision | ||
) -> None: | ||
personalization_dict: dict[Decision, list[ApplicationUpdatePersonalization]] = { | ||
Decision.ACCEPTED: [], | ||
Decision.REJECTED: [], | ||
Decision.WAITLISTED: [], | ||
} | ||
|
||
for (first_name, email), status in applicant_batch.items(): | ||
personalization = ApplicationUpdatePersonalization( | ||
email=email, first_name=first_name | ||
) | ||
if status in (Decision.ACCEPTED, Decision.REJECTED, Decision.WAITLISTED): | ||
personalization_dict[status].append(personalization) | ||
|
||
template_data = { | ||
Template.ACCEPTED_EMAIL: personalization_dict[Decision.ACCEPTED], | ||
Template.REJECTED_EMAIL: personalization_dict[Decision.REJECTED], | ||
Template.WAITLISTED_EMAIL: personalization_dict[Decision.WAITLISTED], | ||
} | ||
"""Send a specific decision email to a group of applicants.""" | ||
personalizations = [ | ||
ApplicationUpdatePersonalization(email=email, first_name=first_name) | ||
for first_name, email in applicant_batch | ||
] | ||
|
||
for template, data in template_data.items(): | ||
if data: | ||
await sendgrid_handler.send_email(template, sender_email, data, True) | ||
template = DECISION_TEMPLATES[decision] | ||
await sendgrid_handler.send_email(template, IH_SENDER, personalizations, True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,26 @@ | ||
from unittest.mock import AsyncMock, call, patch | ||
|
||
from test_sendgrid_handler import SAMPLE_SENDER | ||
from unittest.mock import AsyncMock, patch | ||
|
||
from models.ApplicationData import Decision | ||
from services.sendgrid_handler import ApplicationUpdatePersonalization, Template | ||
from utils import email_handler | ||
from utils.email_handler import IH_SENDER | ||
|
||
|
||
@patch("services.sendgrid_handler.send_email") | ||
async def test_send_decision_email(mock_sendgrid_handler_send_email: AsyncMock) -> None: | ||
applicants = { | ||
("test1", "[email protected]"): Decision.ACCEPTED, | ||
("test2", "[email protected]"): Decision.REJECTED, | ||
("test3", "[email protected]"): Decision.WAITLISTED, | ||
} | ||
|
||
accepted = [ | ||
ApplicationUpdatePersonalization(first_name="test1", email="[email protected]") | ||
users = [ | ||
("test1", "[email protected]"), | ||
("test2", "[email protected]"), | ||
("test3", "[email protected]"), | ||
] | ||
rejected = [ | ||
ApplicationUpdatePersonalization(first_name="test2", email="[email protected]") | ||
] | ||
waitlisted = [ | ||
ApplicationUpdatePersonalization(first_name="test3", email="[email protected]") | ||
|
||
expected_personalizations = [ | ||
ApplicationUpdatePersonalization(first_name=name, email=email) | ||
for name, email in users | ||
] | ||
|
||
await email_handler.send_decision_email(SAMPLE_SENDER, applicants) | ||
await email_handler.send_decision_email(users, Decision.ACCEPTED) | ||
|
||
mock_sendgrid_handler_send_email.assert_has_calls( | ||
[ | ||
call(Template.ACCEPTED_EMAIL, SAMPLE_SENDER, accepted, True), | ||
call(Template.REJECTED_EMAIL, SAMPLE_SENDER, rejected, True), | ||
call(Template.WAITLISTED_EMAIL, SAMPLE_SENDER, waitlisted, True), | ||
] | ||
mock_sendgrid_handler_send_email.assert_called_once_with( | ||
Template.ACCEPTED_EMAIL, IH_SENDER, expected_personalizations, True | ||
) |