diff --git a/src/pcapi/core/offers/factories.py b/src/pcapi/core/offers/factories.py index 58b181d1d1..ca818c102c 100644 --- a/src/pcapi/core/offers/factories.py +++ b/src/pcapi/core/offers/factories.py @@ -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): diff --git a/src/pcapi/core/users/external/__init__.py b/src/pcapi/core/users/external/__init__.py index 34412a7f3a..87d99a7ec4 100644 --- a/src/pcapi/core/users/external/__init__.py +++ b/src/pcapi/core/users/external/__init__.py @@ -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 @@ -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 diff --git a/tests/core/users/external/external_users_test.py b/tests/core/users/external/external_users_test.py index 9624e5bd56..737ca63335 100644 --- a/tests/core/users/external/external_users_test.py +++ b/tests/core/users/external/external_users_test.py @@ -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 @@ -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