def find_favorites_domain_by_beneficiary(
        beneficiary_identifier: int) -> list[FavoriteDomain]:
    favorite_sql_entities = (models.Favorite.query.filter(
        models.Favorite.userId == beneficiary_identifier).options(
            joinedload(models.Favorite.offer).joinedload(
                Offer.venue).joinedload(Venue.managingOfferer)).options(
                    joinedload(models.Favorite.mediation)).options(
                        joinedload(models.Favorite.offer).joinedload(
                            Offer.stocks)).options(
                                joinedload(models.Favorite.offer).joinedload(
                                    Offer.product)).options(
                                        joinedload(
                                            models.Favorite.offer).joinedload(
                                                Offer.mediations)).all())

    offer_ids = [
        favorite_sql_entity.offer.id
        for favorite_sql_entity in favorite_sql_entities
    ]

    bookings = (Offer.query.filter(Offer.id.in_(offer_ids)).join(Stock).join(
        Booking).join(IndividualBooking).filter(
            IndividualBooking.userId == beneficiary_identifier).filter(
                Booking.status != BookingStatus.CANCELLED).with_entities(
                    Booking.id.label("booking_id"),
                    Booking.quantity,
                    Offer.id.label("offer_id"),
                    Stock.id.label("stock_id"),
                ).all())

    bookings_by_offer_id = {
        booking.offer_id: {
            "id": booking.booking_id,
            "stock_id": booking.stock_id,
            "quantity": booking.quantity
        }
        for booking in bookings
    }

    return [
        favorite_domain_converter.to_domain(
            favorite_sql_entity,
            bookings_by_offer_id.get(favorite_sql_entity.offerId, None))
        for favorite_sql_entity in favorite_sql_entities
    ]
    def find_by_beneficiary(self,
                            beneficiary_identifier: int) -> List[Favorite]:
        favorite_sql_entities = (FavoriteSQLEntity.query.filter(
            FavoriteSQLEntity.userId == beneficiary_identifier).options(
                joinedload(FavoriteSQLEntity.offer).joinedload(
                    Offer.venue).joinedload(Venue.managingOfferer)
            ).options(joinedload(FavoriteSQLEntity.mediation)).options(
                joinedload(FavoriteSQLEntity.offer).joinedload(
                    Offer.stocks)).options(
                        joinedload(FavoriteSQLEntity.offer).joinedload(
                            Offer.product)).options(
                                joinedload(FavoriteSQLEntity.offer).joinedload(
                                    Offer.mediations)).all())

        offer_ids = [
            favorite_sql_entity.offer.id
            for favorite_sql_entity in favorite_sql_entities
        ]

        bookings = (Offer.query.filter(
            Offer.id.in_(offer_ids)).join(Stock).join(Booking).filter(
                Booking.userId == beneficiary_identifier).filter(
                    Booking.isCancelled == False).with_entities(
                        Booking.id.label("booking_id"),
                        Booking.quantity,
                        Offer.id.label("offer_id"),
                        Stock.id.label("stock_id"),
                    ).all())

        bookings_by_offer_id = {
            booking.offer_id: {
                "id": booking.booking_id,
                "stock_id": booking.stock_id,
                "quantity": booking.quantity
            }
            for booking in bookings
        }

        return [
            favorite_domain_converter.to_domain(
                favorite_sql_entity,
                bookings_by_offer_id.get(favorite_sql_entity.offerId, None))
            for favorite_sql_entity in favorite_sql_entities
        ]
def add_to_favorite():
    mediation = None
    offer_id = request.json.get("offerId")
    mediation_id = request.json.get("mediationId")
    check_offer_id_is_present_in_request(offer_id)

    offer = load_or_404(Offer, offer_id)
    if mediation_id is not None:
        mediation = load_or_404(Mediation, mediation_id)

    favorite_sql_entity = FavoriteSQLEntity()
    favorite_sql_entity.mediation = mediation
    favorite_sql_entity.offer = offer
    favorite_sql_entity.user = current_user
    repository.save(favorite_sql_entity)

    favorite = favorite_domain_converter.to_domain(favorite_sql_entity)

    return jsonify(serialize_favorite(favorite)), 201