Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(BSR)[API] feat: public api: apply atomic to event routes #14940

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion api/src/pcapi/core/bookings/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,11 @@ def _execute_cancel_booking(
stock.dnBookedQuantity -= booking.quantity
if booking.activationCode and stock.quantity:
stock.quantity -= 1
repository.save(booking, stock)

if is_managed_transaction():
db.session.flush()
else:
repository.save(booking, stock)
return True


Expand Down
70 changes: 50 additions & 20 deletions api/src/pcapi/core/offers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,16 @@ def update_offer(

logger.info("Offer has been updated", extra={"offer_id": offer.id}, technical_message_id="offer.updated")

db.session.flush()

withdrawal_fields = {"bookingContact", "withdrawalDelay", "withdrawalDetails", "withdrawalType"}
withdrawal_updated = updates_set & withdrawal_fields
oa_updated = "offererAddress" in updates
if should_send_mail and (withdrawal_updated or oa_updated):
transactional_mails.send_email_for_each_ongoing_booking(offer)
on_commit(partial(transactional_mails.send_email_for_each_ongoing_booking, offer))

reason = search.IndexationReason.OFFER_UPDATE
search.async_index_offer_ids([offer.id], reason=reason, log_extra={"changes": updates_set})
on_commit(partial(search.async_index_offer_ids, [offer.id], reason=reason, log_extra={"changes": updates_set}))

return offer

Expand Down Expand Up @@ -737,9 +739,13 @@ def create_stock(
offer.lastValidationPrice = price
repository.add_to_session(created_stock, *created_activation_codes, offer)
db.session.flush()
search.async_index_offer_ids(
[offer.id],
reason=search.IndexationReason.STOCK_CREATION,

on_commit(
partial(
search.async_index_offer_ids,
[offer.id],
reason=search.IndexationReason.STOCK_CREATION,
)
)

return created_stock
Expand Down Expand Up @@ -888,10 +894,15 @@ def publish_offer(
else:
if offer.publicationDate:
offers_repository.delete_future_offer(offer.id)
search.async_index_offer_ids(
[offer.id],
reason=search.IndexationReason.OFFER_PUBLICATION,

on_commit(
partial(
search.async_index_offer_ids,
[offer.id],
reason=search.IndexationReason.OFFER_PUBLICATION,
)
)

logger.info(
"Offer has been published",
extra={"offer_id": offer.id, "venue_id": offer.venueId, "offer_status": offer.status},
Expand Down Expand Up @@ -925,7 +936,12 @@ def update_offer_fraud_information(offer: AnyOffer, user: users_models.User | No
and not venue_already_has_validated_offer
and isinstance(offer, models.Offer)
):
transactional_mails.send_first_venue_approved_offer_email_to_pro(offer)
on_commit(
partial(
transactional_mails.send_first_venue_approved_offer_email_to_pro,
offer,
)
)


def _invalidate_bookings(bookings: list[bookings_models.Booking]) -> list[bookings_models.Booking]:
Expand All @@ -948,13 +964,24 @@ def _delete_stock(stock: models.Stock, author_id: int | None = None, user_connec
)
if cancelled_bookings:
for booking in cancelled_bookings:
transactional_mails.send_booking_cancellation_by_pro_to_beneficiary_email(booking)
transactional_mails.send_booking_cancellation_confirmation_by_pro_email(cancelled_bookings)
on_commit(partial(transactional_mails.send_booking_cancellation_by_pro_to_beneficiary_email, booking))

on_commit(partial(transactional_mails.send_booking_cancellation_confirmation_by_pro_email, cancelled_bookings))

if not FeatureToggle.WIP_DISABLE_CANCEL_BOOKING_NOTIFICATION.is_active():
push_notification_job.send_cancel_booking_notification.delay([booking.id for booking in cancelled_bookings])
search.async_index_offer_ids(
[stock.offerId],
reason=search.IndexationReason.STOCK_DELETION,
on_commit(
partial(
push_notification_job.send_cancel_booking_notification.delay,
[booking.id for booking in cancelled_bookings],
)
)

on_commit(
partial(
search.async_index_offer_ids,
[stock.offerId],
reason=search.IndexationReason.STOCK_DELETION,
)
)


Expand Down Expand Up @@ -1009,9 +1036,12 @@ def create_mediation(
)
_delete_mediations_and_thumbs(previous_mediations)

search.async_index_offer_ids(
[offer.id],
reason=search.IndexationReason.MEDIATION_CREATION,
on_commit(
partial(
search.async_index_offer_ids,
[offer.id],
reason=search.IndexationReason.MEDIATION_CREATION,
)
)

return mediation
Expand Down Expand Up @@ -1284,10 +1314,10 @@ def set_offer_status_based_on_fraud_criteria(offer: AnyOffer) -> models.OfferVal
status = models.OfferValidationStatus.PENDING
offer.flaggingValidationRules = flagging_rules
if isinstance(offer, models.Offer):
compliance.update_offer_compliance_score(offer, is_primary=True)
on_commit(partial(compliance.update_offer_compliance_score, offer, is_primary=True))
else:
if isinstance(offer, models.Offer):
compliance.update_offer_compliance_score(offer, is_primary=False)
on_commit(partial(compliance.update_offer_compliance_score, offer, is_primary=False))

logger.info("Computed offer validation", extra={"offer": offer.id, "status": status.value})
return status
Expand Down
Loading
Loading