Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
(PC-10445) Handle offers with empty subcategory
Browse files Browse the repository at this point in the history
  • Loading branch information
lixxday committed Aug 20, 2021
1 parent e2bfc57 commit 6d59132
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/pcapi/core/offers/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ def _create(cls, model_class, *args, **kwargs):
# FIXME: fseguin(2021-07-22): deprecated
@factory.post_generation
def match_type(self, create, extracted, **kwargs):
self.type = getattr(ALL_SUBCATEGORIES_DICT.get(self.subcategoryId, ""), "matching_type", None)
db.session.add(self)
db.session.commit()
if self.type == "MATCHED_FROM_SUBCATEGORY_ID_IN_POST_GENERATION":
self.type = getattr(ALL_SUBCATEGORIES_DICT.get(self.subcategoryId, ""), "matching_type", None)
db.session.add(self)
db.session.commit()


class EventOfferFactory(OfferFactory):
Expand Down
27 changes: 17 additions & 10 deletions src/pcapi/core/users/external/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pcapi.core.bookings.models import Booking
from pcapi.core.categories.subcategories import ALL_SUBCATEGORIES
from pcapi.core.categories.subcategories import ALL_SUBCATEGORIES_DICT
from pcapi.core.offers.models import Offer
from pcapi.core.offers.models import Stock
from pcapi.core.users.external.models import UserAttributes
Expand Down Expand Up @@ -70,18 +71,24 @@ def get_user_attributes(user: User) -> dict:
)


def _get_bookings_categories_and_subcategories(user_bookings: list[Booking]) -> Tuple[list[str], list[str]]:
booking_subcategories_ids = list(
set(
booking.stock.offer.subcategoryId
for booking in user_bookings
if booking.stock.offer.subcategoryId is not None
)
# FIXME: corentinn(2021-08-20): deprecated after type -> subcategory transition
def _get_offer_subcategory(offer: Offer) -> str:
"""Returns an offer subcategory, falling back on the type to find it if subcategoryId is None
Args:
offer (Offer): The offer
Returns:
str: subcategoryId
"""
return offer.subcategoryId or next(
subcategory.id for subcategory in ALL_SUBCATEGORIES if subcategory.matching_type == offer.type
)
booking_subcategories = [
subcategory for subcategory in ALL_SUBCATEGORIES if subcategory.id in booking_subcategories_ids
]


def _get_bookings_categories_and_subcategories(user_bookings: list[Booking]) -> Tuple[list[str], list[str]]:
booking_subcategories_ids = list(set(_get_offer_subcategory(booking.stock.offer) for booking in user_bookings))
booking_subcategories = [ALL_SUBCATEGORIES_DICT[subcategory_id] for subcategory_id in booking_subcategories_ids]
booking_categories = list(set(subcategory.category_id for subcategory in booking_subcategories))
return booking_categories, booking_subcategories_ids

Expand Down
28 changes: 28 additions & 0 deletions tests/core/users/external/external_users_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pcapi.core.users import testing as sendinblue_testing
from pcapi.core.users.external import TRACKED_PRODUCT_IDS
from pcapi.core.users.external import _get_bookings_categories_and_subcategories
from pcapi.core.users.external import _get_offer_subcategory
from pcapi.core.users.external import _get_user_bookings
from pcapi.core.users.external import get_user_attributes
from pcapi.core.users.external import update_external_user
Expand Down Expand Up @@ -120,3 +121,30 @@ def test_get_bookings_categories_and_subcategories():
BookingFactory(user=user, isCancelled=True)

assert _get_bookings_categories_and_subcategories(_get_user_bookings(user)) == (["FILM"], ["SUPPORT_PHYSIQUE_FILM"])

offer = OfferFactory(type="ThingType.MUSIQUE", subcategoryId=None)
BookingFactory(user=user, stock__offer=offer)
BookingFactory(user=user, stock__offer=offer)
BookingFactory(user=user, isCancelled=True)

assert set(_get_bookings_categories_and_subcategories(_get_user_bookings(user))[0]) == set(
["FILM", "MUSIQUE_ENREGISTREE"]
)
assert set(_get_bookings_categories_and_subcategories(_get_user_bookings(user))[1]) == set(
[
"CAPTATION_MUSIQUE",
"SUPPORT_PHYSIQUE_FILM",
]
)


@pytest.mark.parametrize(
"offer_type,subcategory,result",
[
("ThingType.MUSIQUE", None, "CAPTATION_MUSIQUE"),
("ThingType.JEUX", "CAPTATION_MUSIQUE", "CAPTATION_MUSIQUE"),
],
)
def test_get_offer_subcategory(offer_type, subcategory, result):
offer = OfferFactory(type=offer_type, subcategoryId=subcategory)
assert _get_offer_subcategory(offer) == result

0 comments on commit 6d59132

Please sign in to comment.