Skip to content

Commit

Permalink
Ensures the max end date if user has more than 1 sku
Browse files Browse the repository at this point in the history
The previous fix raised a concern on whether the user will
endup having multiple SKUs of the same product.
If so, multiple end dates would be found,
but the iteration stopped on the first collected SKU.
This ensures we go over them all,
returning the last end date found.
  • Loading branch information
CarlosNihelton committed Oct 10, 2023
1 parent c9add5e commit 3c615fd
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions storeapi/base/impl/StoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <winrt/Windows.System.h>
#include <winrt/base.h>

#include <algorithm>
#include <format>
#include <functional>
#include <iterator>
#include <ranges>
#include <type_traits>

#include "../Exception.hpp"
Expand Down Expand Up @@ -41,18 +43,22 @@ StoreContext::Product::CurrentExpirationDate() const {
// A single product might have more than one SKU and not all of them
// (maybe none) show both `IsSubscription` and `IsInUserCollection` properties
// simultaneously true.
for (auto sku : self.Skus()) {
if (sku.IsInUserCollection()) {
auto collected = sku.CollectionData();
return winrt::clock::to_sys(collected.EndDate());
}
auto expDates = self.Skus() |
std::views::filter(&StoreSku::IsInUserCollection) |
std::views::transform([](StoreSku const& s) {
return s.CollectionData().EndDate();
});
auto endWinDate = std::ranges::max_element(expDates);

// Should never be true if called from a product the user is subscribed to.
if (expDates.empty()) {
throw Exception{
ErrorCode::Unsubscribed,
std::format("product ID: {}", winrt::to_string(self.StoreId())),
};
}

// Should be unreachable if called from a product user is subscribed to.
throw Exception{
ErrorCode::Unsubscribed,
std::format("product ID: {}", winrt::to_string(self.StoreId())),
};
return winrt::clock::to_sys(*endWinDate);
}

void StoreContext::Product::PromptUserForPurchase(
Expand Down

0 comments on commit 3c615fd

Please sign in to comment.