Skip to content

Commit

Permalink
(PC-33061)[API] feat: add atomic in collective_offers routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcicurel-pass committed Dec 23, 2024
1 parent 34e6dea commit d1721a6
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 207 deletions.
23 changes: 8 additions & 15 deletions api/src/pcapi/core/educational/api/national_program.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from pcapi.core.educational import exceptions
from pcapi.core.educational import models
from pcapi.models import db
from pcapi.repository import repository


AnyCollectiveOffer = models.CollectiveOffer | models.CollectiveOfferTemplate


def _link_offer(program: models.NationalProgram, offer: AnyCollectiveOffer, commit: bool) -> None:
def _link_offer(program: models.NationalProgram, offer: AnyCollectiveOffer) -> None:
offer.nationalProgramId = program.id

if isinstance(offer, models.CollectiveOffer):
Expand All @@ -17,37 +16,31 @@ def _link_offer(program: models.NationalProgram, offer: AnyCollectiveOffer, comm
nationalProgramId=program.id, collectiveOfferTemplateId=offer.id
)

if commit:
repository.save(offer, history_event)
else:
db.session.add_all([offer, history_event])
db.session.add_all([offer, history_event])


def _unlink_offer(offer: AnyCollectiveOffer, commit: bool) -> None:
def _unlink_offer(offer: AnyCollectiveOffer) -> None:
offer.nationalProgramId = None

if commit:
repository.save(offer)
else:
db.session.add(offer)
db.session.add(offer)


def link_or_unlink_offer_to_program(program_id: int | None, offer: AnyCollectiveOffer, commit: bool = True) -> None:
def link_or_unlink_offer_to_program(program_id: int | None, offer: AnyCollectiveOffer) -> None:
"""
Link offer to national program and save this change within the
offer's national programs history.
Unlink the offer if program_id is None.
"""
if not program_id:
_unlink_offer(offer, commit)
_unlink_offer(offer)
return

program = get_national_program(program_id)
if not program:
raise exceptions.NationalProgramNotFound()

_link_offer(program, offer, commit)
_link_offer(program, offer)
db.session.flush()


def get_national_program(program_id: int | None) -> models.NationalProgram | None:
Expand Down
60 changes: 36 additions & 24 deletions api/src/pcapi/core/educational/api/offer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from decimal import Decimal
from functools import partial
import logging
import typing

Expand Down Expand Up @@ -33,6 +34,8 @@
from pcapi.models import feature
from pcapi.models import offer_mixin
from pcapi.models import validation_status_mixin
from pcapi.repository import is_managed_transaction
from pcapi.repository import on_commit
from pcapi.routes.adage.v1.serialization import prebooking
from pcapi.routes.adage_iframe.serialization.offers import PostCollectiveRequestBodyModel
from pcapi.routes.public.collective.serialization import offers as public_api_collective_offers_serialize
Expand Down Expand Up @@ -228,7 +231,7 @@ def create_collective_offer_template(

collective_offer_template.bookingEmails = offer_data.booking_emails
db.session.add(collective_offer_template)
db.session.commit()
db.session.flush()

if offer_data.nationalProgramId:
national_program_api.link_or_unlink_offer_to_program(offer_data.nationalProgramId, collective_offer_template)
Expand Down Expand Up @@ -283,7 +286,7 @@ def create_collective_offer(
update_external_pro(email)

db.session.add(collective_offer)
db.session.commit()
db.session.flush()

if offer_data.nationalProgramId:
national_program_api.link_or_unlink_offer_to_program(offer_data.nationalProgramId, collective_offer)
Expand Down Expand Up @@ -324,7 +327,7 @@ def create_collective_offer_template_from_collective_offer(
)
db.session.delete(offer)
db.session.add(collective_offer_template)
db.session.commit()
db.session.flush()

logger.info(
"Collective offer template has been created and regular collective offer deleted",
Expand Down Expand Up @@ -409,10 +412,10 @@ def update_collective_offer_educational_institution(
else:
raise exceptions.EducationalRedactorNotFound()

db.session.commit()
db.session.flush()

if educational_institution_id is not None and offer.validation == offer_mixin.OfferValidationStatus.APPROVED:
adage_client.notify_institution_association(serialize_collective_offer(offer))
on_commit(partial(adage_client.notify_institution_association, serialize_collective_offer(offer)))

return offer

Expand Down Expand Up @@ -608,18 +611,27 @@ def publish_collective_offer_template(

if offer_template.validation == offer_mixin.OfferValidationStatus.DRAFT:
update_offer_fraud_information(offer_template, user)
search.async_index_collective_offer_template_ids(
[offer_template.id],
reason=search.IndexationReason.OFFER_PUBLICATION,

on_commit(
partial(
search.async_index_collective_offer_template_ids,
[offer_template.id],
reason=search.IndexationReason.OFFER_PUBLICATION,
)
)
db.session.commit()

db.session.flush()

return offer_template


def delete_image(obj: educational_models.HasImageMixin) -> None:
obj.delete_image()
db.session.commit()

if is_managed_transaction():
db.session.flush()
else:
db.session.commit()


def attach_image(
Expand All @@ -628,18 +640,18 @@ def attach_image(
crop_params: image_conversion.CropParams,
credit: str,
) -> None:
try:
obj.set_image(
image=image,
credit=credit,
crop_params=crop_params,
ratio=image_conversion.ImageRatio.PORTRAIT,
keep_original=False,
)
except:
db.session.rollback()
raise
db.session.commit()
obj.set_image(
image=image,
credit=credit,
crop_params=crop_params,
ratio=image_conversion.ImageRatio.PORTRAIT,
keep_original=False,
)

if is_managed_transaction():
db.session.flush()
else:
db.session.commit()


def _get_expired_collective_offer_template_ids(
Expand Down Expand Up @@ -707,7 +719,7 @@ def duplicate_offer_and_stock(
)

db.session.add(offer)
db.session.commit()
db.session.flush()

if original_offer.imageUrl:
image_file = get_image_from_url(original_offer.imageUrl)
Expand All @@ -721,7 +733,7 @@ def duplicate_offer_and_stock(
content_type="image/jpeg",
)

db.session.commit()
db.session.flush()
return offer


Expand Down
7 changes: 6 additions & 1 deletion api/src/pcapi/core/external/compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pcapi.domain import music_types
from pcapi.domain import show_types
from pcapi.models import db
from pcapi.repository import is_managed_transaction
from pcapi.tasks import compliance_tasks
from pcapi.tasks.serialization.compliance_tasks import GetComplianceScoreRequest

Expand Down Expand Up @@ -39,7 +40,11 @@ def make_update_offer_compliance_score(payload: GetComplianceScoreRequest) -> No
)

db.session.execute(statement)
db.session.commit()

if is_managed_transaction():
db.session.flush()
else:
db.session.commit()


def _get_payload_for_compliance_api(offer: offers_models.Offer) -> GetComplianceScoreRequest:
Expand Down
Loading

0 comments on commit d1721a6

Please sign in to comment.