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

Commit

Permalink
(PC-9959) Send booking dateUsed to batch
Browse files Browse the repository at this point in the history
  • Loading branch information
viconnex committed Jul 13, 2021
1 parent 17e5671 commit 723ca0c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/pcapi/notifications/push/user_attributes_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ def get_user_booking_attributes(user: User) -> dict:

user_bookings = (
Booking.query.options(
joinedload(Booking.stock)
.joinedload(Stock.offer)
.load_only(
Offer.type,
Offer.url,
)
joinedload(Booking.stock).joinedload(Stock.offer).load_only(Offer.type, Offer.url, Offer.productId)
)
.filter_by(userId=user.id)
.order_by(db.desc(Booking.dateCreated))
Expand All @@ -62,6 +57,12 @@ def get_user_booking_attributes(user: User) -> dict:
"u.credit": int(credit.all.remaining * 100) if credit else 0,
}

for booking in user_bookings:
if booking.dateUsed:
attributes[f"date(u.booked_product_{booking.stock.offer.productId}_date_used)"] = _format_date(
booking.dateUsed
)

# A Batch tag can't be an empty list, otherwise the API returns an error
if booking_categories:
attributes["ut.booking_categories"] = booking_categories
Expand Down
12 changes: 10 additions & 2 deletions tests/notifications/push/user_attributes_updates_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

import pytest

from pcapi.core.bookings.factories import BookingFactory
Expand All @@ -14,8 +16,8 @@ class GetUserBookingAttributesTest:
def test_get_attributes(self):
user = UserFactory()
b1 = BookingFactory(user=user, amount=10)
b2 = BookingFactory(user=user, amount=10)
b3 = BookingFactory(user=user, amount=10)
b2 = BookingFactory(user=user, amount=10, dateUsed=datetime(2021, 5, 6))
b3 = BookingFactory(user=user, amount=10, dateUsed=datetime(2021, 7, 8))
BookingFactory(user=user, amount=100, isCancelled=True)

n_query_get_user = 1
Expand All @@ -28,6 +30,12 @@ def test_get_attributes(self):
last_date_created = max(booking.dateCreated for booking in [b1, b2, b3])

assert attributes == {
f"date(u.booked_product_{b2.stock.offer.product.id}_date_used)": b2.dateUsed.strftime(
BATCH_DATETIME_FORMAT
),
f"date(u.booked_product_{b3.stock.offer.product.id}_date_used)": b3.dateUsed.strftime(
BATCH_DATETIME_FORMAT
),
"date(u.last_booking_date)": last_date_created.strftime(BATCH_DATETIME_FORMAT),
"u.credit": 47000,
"ut.booking_categories": ["ThingType.AUDIOVISUEL"],
Expand Down

0 comments on commit 723ca0c

Please sign in to comment.