Skip to content

Commit

Permalink
source-braintree-native: fix disputes bug
Browse files Browse the repository at this point in the history
The `disputes` stream is slightly different from the other incremental
streams. One of these differences is that Braintree doesn't allow us to
search disputes based on the `created_at` field. Instead, we use
`receive_date` as a proxy for `created_at`. This means we recieve
results for entire days and have to filter out results we've already
seen.

I fixed a few bugs with the `disputes` stream:
- The start date does not need pushed back a day. I initially thought
    `DisputeSearch.recevied_date.between(start, end)` was exclusive of
    the `start` date, but that was incorrect.
- `most_recent_created_at` is now updated whenever there's a
    `created_at` value more recent that `most_recent_created_at`.
  • Loading branch information
Alex-Bair committed Dec 17, 2024
1 parent 981a135 commit a72d6ac
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions source-braintree-native/source_braintree_native/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ async def fetch_disputes(
) -> AsyncGenerator[IncrementalResource | LogCursor, None]:
assert isinstance(log_cursor, datetime)
most_recent_created_at = log_cursor
# The start date must be shifted back 1 day since we have to query Braintree using the received_date field,
# which is less granular than the created_at cursor field (date vs. datetime).
start = log_cursor - timedelta(days=1)
window_end = log_cursor + timedelta(hours=window_size)
end = min(window_end, datetime.now(tz=UTC))

# Braintree does not let us search disputes based on the created_at field. I assume received_at is an adequate proxy
# for created_at, although received_at is less granular than created_at (date vs. datetime). We'll always receive
# results we've already seen in this search, but we filter those out client-side.
search_result = await asyncio.to_thread(
braintree_gateway.dispute.search,
DisputeSearch.received_date.between(start, end),
DisputeSearch.received_date.between(log_cursor, end),
)

count = 0
Expand All @@ -403,12 +403,14 @@ async def fetch_disputes(

if doc.created_at > log_cursor:
yield doc
most_recent_created_at = doc.created_at

if doc.created_at > most_recent_created_at:
most_recent_created_at = doc.created_at

if count >= SEARCH_LIMIT:
raise RuntimeError(_search_limit_error_message(count, "disputes"))

if end == window_end:
yield window_end
elif most_recent_created_at > log_cursor:
if most_recent_created_at > log_cursor:
yield most_recent_created_at
else:
yield end

0 comments on commit a72d6ac

Please sign in to comment.