Skip to content

Commit

Permalink
(BSR)[API] feat: public api: apply atomic to event routes
Browse files Browse the repository at this point in the history
Add `atomic` decorator to post/patch/delete event routes.
  • Loading branch information
jbaudet-pass committed Nov 6, 2024
1 parent 5e22b59 commit 1db6f2b
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 134 deletions.
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
15 changes: 9 additions & 6 deletions api/src/pcapi/core/offerers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
from pcapi.models import pc_object
from pcapi.models.feature import FeatureToggle
from pcapi.models.validation_status_mixin import ValidationStatus
from pcapi.repository import atomic
from pcapi.repository import mark_transaction_as_invalid
from pcapi.repository import on_commit
from pcapi.repository import repository
from pcapi.routes.serialization import offerers_serialize
Expand Down Expand Up @@ -2819,12 +2821,13 @@ def get_or_create_address(location_data: LocationData, is_manual_edition: bool =


def get_or_create_offerer_address(offerer_id: int, address_id: int, label: str | None = None) -> models.OffererAddress:
try:
offerer_address = models.OffererAddress(offererId=offerer_id, addressId=address_id, label=label)
db.session.add(offerer_address)
db.session.flush()
except sa.exc.IntegrityError:
db.session.rollback()
with atomic():
try:
offerer_address = models.OffererAddress(offererId=offerer_id, addressId=address_id, label=label)
db.session.add(offerer_address)
db.session.flush()
except sa.exc.IntegrityError:
mark_transaction_as_invalid()

offerer_address = (
models.OffererAddress.query.filter(
Expand Down
4 changes: 4 additions & 0 deletions api/src/pcapi/core/offers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ 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
Expand Down Expand Up @@ -888,10 +890,12 @@ 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,
)

logger.info(
"Offer has been published",
extra={"offer_id": offer.id, "venue_id": offer.venueId, "offer_status": offer.status},
Expand Down
Loading

0 comments on commit 1db6f2b

Please sign in to comment.