From 3c615fd9512e9801fbe5562ebea88e15552b5414 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 10 Oct 2023 15:11:10 -0300 Subject: [PATCH] Ensures the max end date if user has more than 1 sku 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. --- storeapi/base/impl/StoreContext.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/storeapi/base/impl/StoreContext.cpp b/storeapi/base/impl/StoreContext.cpp index d3f0954e8..0d155b4f4 100644 --- a/storeapi/base/impl/StoreContext.cpp +++ b/storeapi/base/impl/StoreContext.cpp @@ -10,9 +10,11 @@ #include #include +#include #include #include #include +#include #include #include "../Exception.hpp" @@ -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(