Skip to content

Commit

Permalink
(PC-33424)[API] feat: set headline offer to inactive when it has no i…
Browse files Browse the repository at this point in the history
…mage
  • Loading branch information
ogeber-pass committed Jan 13, 2025
1 parent 50551a6 commit 38eba16
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/src/pcapi/core/offers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,18 @@ def get_offerers_active_headline_offer(offerer_id: int) -> models.HeadlineOffer
def get_inactive_headline_offers() -> list[models.HeadlineOffer]:
return (
models.HeadlineOffer.query.join(models.Offer, models.HeadlineOffer.offerId == models.Offer.id)
.filter(models.Offer.status != offer_mixin.OfferStatus.ACTIVE)
.outerjoin(models.Mediation, models.Mediation.offerId == models.Offer.id)
.outerjoin(models.Product, models.Offer.productId == models.Product.id)
.outerjoin(models.ProductMediation, models.ProductMediation.productId == models.Product.id)
.filter(
sa.or_(
models.Offer.status != offer_mixin.OfferStatus.ACTIVE,
sa.and_( # type: ignore
models.ProductMediation.id.is_(None),
models.Mediation.id.is_(None),
),
)
)
.filter(
# We don't want to fetch HeadlineOffers that have already been marked as finished
sa.or_(
Expand Down
16 changes: 16 additions & 0 deletions api/tests/core/offers/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2252,12 +2252,15 @@ def test_set_upper_timespan_of_inactive_headline_offers(self):
venue_1 = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer_1 = factories.OfferFactory(isActive=True, venue=venue_1)
factories.StockFactory(offer=offer_1)
factories.MediationFactory(offer=offer_1)
venue_2 = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
stock = factories.StockFactory(quantity=1)
offer_2 = factories.OfferFactory(isActive=True, venue=venue_2, stocks=[stock])
factories.MediationFactory(offer=offer_2)
venue_3 = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer_3 = factories.OfferFactory(isActive=True, venue=venue_3)
factories.StockFactory(offer=offer_3)
factories.MediationFactory(offer=offer_3)

headline_offer_1 = factories.HeadlineOfferFactory(offer=offer_1)
headline_offer_2 = factories.HeadlineOfferFactory(offer=offer_2)
Expand All @@ -2284,13 +2287,26 @@ def test_do_not_update_upper_timespan_of_already_inactive_headline_offers(self):
venue = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer = factories.OfferFactory(isActive=True, venue=venue)
factories.StockFactory(offer=offer)
factories.MediationFactory(offer=offer)
creation_time = datetime.utcnow() - timedelta(days=20)
finished_timespan = (creation_time, creation_time + timedelta(days=10))
old_headline_offer = factories.HeadlineOfferFactory(offer=offer, timespan=finished_timespan)
api.set_upper_timespan_of_inactive_headline_offers()
assert old_headline_offer.timespan.lower.date() == creation_time.date()
assert old_headline_offer.timespan.upper.date() == (creation_time + timedelta(days=10)).date()

def test_set_upper_timespan_of_inactive_headline_offers_without_image(self):
offer = factories.OfferFactory(isActive=True)
factories.StockFactory(offer=offer)

headline_offer = factories.HeadlineOfferFactory(offer=offer)
assert headline_offer.isActive

api.set_upper_timespan_of_inactive_headline_offers()

assert not headline_offer.isActive
assert headline_offer.timespan.upper is not None


@pytest.mark.usefixtures("db_session")
class OfferExpenseDomainsTest:
Expand Down

0 comments on commit 38eba16

Please sign in to comment.