ecommerce icon indicating copy to clipboard operation
ecommerce copied to clipboard

Fix future prices incorrectly affecting lowest recent price calculation

Open Copilot opened this issue 2 months ago • 2 comments

Problem

The PublicOffer::Product lowest recent price calculation was not properly handling future prices. The find_border_event method in RegisterLowestPrice could potentially select future price events as "border events", which would then be included in the lowest price comparison despite being future prices that shouldn't affect current pricing.

Root Cause

The issue was in the find_border_event method:

def find_border_event(price_changes)
  price_changes.reverse.find { |price_change| !recent_event?(price_change) }
end

This method looks for events that are not in the recent period (last 30 days) to use as a baseline for price comparison. However, it only checked !recent_event? without explicitly excluding future events. While future events are typically considered "recent" by the current logic, edge cases or configuration changes could make future events qualify as non-recent, causing them to be incorrectly selected as border events and included in the lowest price calculation.

Solution

Updated the find_border_event method to explicitly exclude future events:

def find_border_event(price_changes)
  price_changes.reverse.find { |price_change| !recent_event?(price_change) && !future_event?(price_change) }
end

This ensures that:

  1. Border events must be from the past (not future)
  2. Border events must be older than the recent period (30+ days ago)
  3. Future prices are completely excluded from affecting current lowest price calculations

Testing

Added a comprehensive test case test_future_price_not_selected_as_border_event_even_when_lowest that verifies future prices are never selected as border events, even when they represent the lowest price value.

The existing tests test_ignores_future_prices and test_takes_last_event_when_no_events_in_last_30_days continue to pass, confirming the fix doesn't break existing functionality.

Impact

  • Robustness: Makes the code more explicit and resistant to edge cases
  • Correctness: Ensures future pricing events never affect current price calculations
  • Minimal: Single line change with no breaking changes to existing behavior
  • Future-proof: Prevents potential bugs if the recent_event? logic changes

Fixes RailsEventStore/ecommerce#435


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Sep 24 '25 19:09 Copilot

Deploy Preview for ecommerce-events ready!

Name Link
Latest commit 89380f038ae78fb9c7e0b62cdf899034e04b87b8
Latest deploy log https://app.netlify.com/projects/ecommerce-events/deploys/68d44c69434a0b0007791aa3
Deploy Preview https://deploy-preview-458--ecommerce-events.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Sep 24 '25 19:09 netlify[bot]

We'd need an integration test for this issue too.

andrzejkrzywda avatar Sep 24 '25 20:09 andrzejkrzywda