Skip to content

Commit

Permalink
(PC-32247)[API] feat: add OFFER_ADDRESS in OFFER_WITHDRAWAL_UPDATED_B…
Browse files Browse the repository at this point in the history
…Y_PRO mail
  • Loading branch information
anougbele-pass committed Oct 15, 2024
1 parent b49d0e7 commit a47e912
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pcapi.core.bookings.models as bookings_models
from pcapi.core.mails import models
from pcapi.core.mails.transactional.sendinblue_template_ids import TransactionalEmail
from pcapi.core.offerers.models import OffererAddress
from pcapi.core.offerers.models import Venue
from pcapi.core.offers.models import ActivationCode
from pcapi.core.offers.models import Offer
Expand All @@ -29,9 +30,16 @@ def send_email_for_each_ongoing_booking(offer: Offer) -> None:
sqla.orm.joinedload(bookings_models.Booking.user).load_only(User.firstName, User.email),
sqla.orm.joinedload(bookings_models.Booking.stock)
.joinedload(Stock.offer)
.load_only(Offer.id, Offer.withdrawalDelay, Offer.withdrawalDetails, Offer.withdrawalType)
.joinedload(Offer.venue)
.load_only(Venue.street),
sqla.orm.joinedload(bookings_models.Booking.activationCode).load_only(ActivationCode.code),
sqla.orm.joinedload(bookings_models.Booking.stock)
.load_only(Stock.id)
.joinedload(Stock.offer)
.load_only(Offer.id)
.joinedload(Offer.offererAddress)
.joinedload(OffererAddress.address),
)
)

Expand All @@ -53,6 +61,7 @@ def send_email_for_each_ongoing_booking(offer: Offer) -> None:
user_first_name=booking.user.firstName,
offer_name=offer.name,
offer_token=booking.activationCode.code if booking.activationCode else booking.token,
offer_address=booking.stock.offer.offererAddress.fullAddress,
)
)
send_withdrawal_detail_changed_emails.delay(mails_request)
Expand All @@ -69,6 +78,7 @@ def send_booking_withdrawal_updated(
offer_withdrawal_type: str | None,
offerer_name: str,
venue_address: str,
offer_address: str | None,
) -> None:
data = get_booking_withdrawal_updated_email_data(
user_first_name=user_first_name,
Expand All @@ -79,6 +89,7 @@ def send_booking_withdrawal_updated(
offer_withdrawal_type=offer_withdrawal_type,
offerer_name=offerer_name,
venue_address=venue_address,
offer_address=offer_address,
)
mails.send(recipients=recipients, data=data)

Expand All @@ -93,6 +104,7 @@ def get_booking_withdrawal_updated_email_data(
offer_withdrawal_type: str | None,
offerer_name: str,
venue_address: str,
offer_address: str | None,
) -> models.TransactionalEmailData:
return models.TransactionalEmailData(
template=TransactionalEmail.OFFER_WITHDRAWAL_UPDATED_BY_PRO.value,
Expand All @@ -105,5 +117,6 @@ def get_booking_withdrawal_updated_email_data(
"OFFERER_NAME": offerer_name,
"USER_FIRST_NAME": user_first_name,
"VENUE_ADDRESS": venue_address,
"OFFER_ADDRESS": offer_address,
},
)
1 change: 1 addition & 0 deletions api/src/pcapi/tasks/mails_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ def send_withdrawal_detail_changed_emails(payload: WithdrawalChangedMailRequest)
offer_withdrawal_type=payload.offer_withdrawal_type,
offerer_name=payload.offerer_name,
venue_address=payload.venue_address,
offer_address=booker.offer_address,
)
1 change: 1 addition & 0 deletions api/src/pcapi/tasks/serialization/mails_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class WithdrawalChangedMailBookingDetail(BaseModel):
user_first_name: str
offer_name: str
offer_token: str
offer_address: str | None


class WithdrawalChangedMailRequest(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,57 @@

import pytest

from pcapi.core import testing as core_testing
from pcapi.core.bookings.factories import BookingFactory
import pcapi.core.mails.testing as mails_testing
from pcapi.core.mails.transactional.bookings.booking_withdrawal_updated import send_booking_withdrawal_updated
from pcapi.core.mails.transactional.bookings.booking_withdrawal_updated import send_email_for_each_ongoing_booking
from pcapi.core.mails.transactional.sendinblue_template_ids import TransactionalEmail
from pcapi.core.offers import factories as offers_factories
from pcapi.core.testing import assert_no_duplicated_queries


pytestmark = pytest.mark.usefixtures("db_session")
@pytest.mark.usefixtures("db_session")
class SendBookingWithdrawalUpdatedTest:
def test_send_withdrawalchanged_email(self):
with assert_no_duplicated_queries():
send_booking_withdrawal_updated(
recipients=["georges@example.com"],
user_first_name="Georges",
offer_name="my awesome offer",
offer_token="XXXXXX",
offer_withdrawal_delay="2 semaines",
offer_withdrawal_details="my withdrawal details",
offer_withdrawal_type="no_ticket",
offerer_name="my offerer name",
venue_address="my awesome address",
offer_address="my awesome offer address",
)

assert len(mails_testing.outbox) == 1
email = mails_testing.outbox[0]
assert email["To"] == "georges@example.com"
assert email["template"] == dataclasses.asdict(TransactionalEmail.OFFER_WITHDRAWAL_UPDATED_BY_PRO.value)
assert email["params"]["OFFER_NAME"] == "my awesome offer"
assert email["params"]["OFFER_TOKEN"] == "XXXXXX"
assert email["params"]["OFFER_WITHDRAWAL_DELAY"] == "2 semaines"
assert email["params"]["OFFER_WITHDRAWAL_DETAILS"] == "my withdrawal details"
assert email["params"]["OFFER_WITHDRAWAL_TYPE"] == "no_ticket"
assert email["params"]["OFFERER_NAME"] == "my offerer name"
assert email["params"]["USER_FIRST_NAME"] == "Georges"
assert email["params"]["VENUE_ADDRESS"] == "my awesome address"
assert email["params"]["OFFER_ADDRESS"] == "my awesome offer address"

def test_send_withdrawalchanged_email():
with assert_no_duplicated_queries():
send_booking_withdrawal_updated(
recipients=["georges@example.com"],
user_first_name="Georges",
offer_name="my awesome offer",
offer_token="XXXXXX",
offer_withdrawal_delay="2 semaines",
offer_withdrawal_details="my withdrawal details",
offer_withdrawal_type="no_ticket",
offerer_name="my offerer name",
venue_address="my awesome address",
)
def test_send_email_for_each_ongoing_booking(
self,
):

assert len(mails_testing.outbox) == 1
email = mails_testing.outbox[0]
assert email["To"] == "georges@example.com"
assert email["template"] == dataclasses.asdict(TransactionalEmail.OFFER_WITHDRAWAL_UPDATED_BY_PRO.value)
assert email["params"]["OFFER_NAME"] == "my awesome offer"
assert email["params"]["OFFER_TOKEN"] == "XXXXXX"
assert email["params"]["OFFER_WITHDRAWAL_DELAY"] == "2 semaines"
assert email["params"]["OFFER_WITHDRAWAL_DETAILS"] == "my withdrawal details"
assert email["params"]["OFFER_WITHDRAWAL_TYPE"] == "no_ticket"
assert email["params"]["OFFERER_NAME"] == "my offerer name"
assert email["params"]["USER_FIRST_NAME"] == "Georges"
assert email["params"]["VENUE_ADDRESS"] == "my awesome address"
booking = BookingFactory(stock=offers_factories.EventStockFactory())
offer = booking.stock.offer
# load offer
# load venue for venue_address
# load offerer for offerer_name
# load booking
BookingFactory(stock=offers_factories.EventStockFactory(offer=offer))
with core_testing.assert_num_queries(4):
send_email_for_each_ongoing_booking(offer)

0 comments on commit a47e912

Please sign in to comment.