Skip to content

Commit

Permalink
[MBL-1736] Include Shipping In Pledge Total When It Exists (#2158)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottkicks authored Sep 18, 2024
1 parent 85624f8 commit ed0637d
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class NoShippingPledgeViewControllerTests: TestCase {
let data = PledgeViewData(
project: project,
rewards: [reward],
selectedShippingRule: nil,
selectedShippingRule: .template,
selectedQuantities: [reward.id: 1],
selectedLocationId: nil,
refTag: nil,
Expand Down Expand Up @@ -144,7 +144,7 @@ final class NoShippingPledgeViewControllerTests: TestCase {
let data = PledgeViewData(
project: .template,
rewards: [reward, .noReward],
selectedShippingRule: nil,
selectedShippingRule: .template,
selectedQuantities: [reward.id: 1, Reward.noReward.id: 1],
selectedLocationId: nil,
refTag: nil,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 37 additions & 11 deletions Library/ViewModels/NoShippingPledgeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ public class NoShippingPledgeViewModel: NoShippingPledgeViewModelType, NoShippin
)
.map(calculateAllRewardsTotal)

let calculatedShippingTotal = Signal.combineLatest(
selectedShippingRule.skipNil(),
rewards,
selectedQuantities
)
.map(calculateShippingTotal)

let allRewardsShippingTotal = Signal.merge(
selectedShippingRule.filter(isNil).mapConst(0.0),
calculatedShippingTotal
)

// Initial pledge amount is zero if not backed and not set previously in the flow.
let initialAdditionalPledgeAmount = initialData.map {
if let bonusSupport = $0.bonusSupport {
Expand Down Expand Up @@ -215,7 +227,7 @@ public class NoShippingPledgeViewModel: NoShippingPledgeViewModelType, NoShippin
*/
let calculatedPledgeTotal = Signal.combineLatest(
additionalPledgeAmount,
additionalPledgeAmount.mapConst(0),
allRewardsShippingTotal,
allRewardsTotal
)
.map(calculatePledgeTotal)
Expand All @@ -230,18 +242,29 @@ public class NoShippingPledgeViewModel: NoShippingPledgeViewModelType, NoShippin
context.map { $0.confirmationLabelHidden }
)

let shippingSummaryViewData = Signal.combineLatest(
selectedShippingRule.skipNil().map(\.location.localizedName),
project.map(\.stats.omitUSCurrencyCode),
project.map { project in
projectCountry(forCurrency: project.stats.currency) ?? project.country
},
allRewardsShippingTotal
)
.map(PledgeShippingSummaryViewData.init)

self.configurePledgeRewardsSummaryViewWithData = Signal.combineLatest(
initialData,
pledgeTotal,
additionalPledgeAmount
additionalPledgeAmount,
shippingSummaryViewData
)
.compactMap { data, pledgeTotal, additionalPledgeAmount in
.compactMap { data, pledgeTotal, additionalPledgeAmount, shipping in
let rewardsData = PostCampaignRewardsSummaryViewData(
rewards: data.rewards,
selectedQuantities: data.selectedQuantities,
projectCountry: data.project.country,
omitCurrencyCode: data.project.stats.omitUSCurrencyCode,
shipping: nil
shipping: shipping
)
let pledgeData = PledgeSummaryViewData(
project: data.project,
Expand Down Expand Up @@ -729,29 +752,32 @@ public class NoShippingPledgeViewModel: NoShippingPledgeViewModelType, NoShippin
createBackingDataAndIsApplePay,
checkoutIdProperty.signal,
baseReward,
additionalPledgeAmount
additionalPledgeAmount,
allRewardsShippingTotal
)
.map { dataAndIsApplePay, checkoutId, baseReward, additionalPledgeAmount
-> (CreateBackingData, Bool, String?, Reward, Double) in
.map { dataAndIsApplePay, checkoutId, baseReward, additionalPledgeAmount, shippingTotal
-> (CreateBackingData, Bool, String?, Reward, Double, Double) in
let (data, isApplePay) = dataAndIsApplePay
guard let checkoutId = checkoutId else {
return (
data,
isApplePay,
nil,
baseReward,
additionalPledgeAmount
additionalPledgeAmount,
shippingTotal
)
}
return (
data,
isApplePay,
String(checkoutId),
baseReward,
additionalPledgeAmount
additionalPledgeAmount,
shippingTotal
)
}
.map { data, isApplePay, checkoutId, baseReward, additionalPledgeAmount
.map { data, isApplePay, checkoutId, baseReward, additionalPledgeAmount, shippingTotal
-> ThanksPageData? in
let checkoutPropsData = checkoutProperties(
from: data.project,
Expand All @@ -760,7 +786,7 @@ public class NoShippingPledgeViewModel: NoShippingPledgeViewModelType, NoShippin
selectedQuantities: data.selectedQuantities,
additionalPledgeAmount: additionalPledgeAmount,
pledgeTotal: data.pledgeTotal,
shippingTotal: 0,
shippingTotal: shippingTotal,
checkoutId: checkoutId,
isApplePay: isApplePay
)
Expand Down
14 changes: 7 additions & 7 deletions Library/ViewModels/NoShippingPledgeViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1453,12 +1453,12 @@ final class NoShippingPledgeViewModelTests: TestCase {
checkoutId: "1",
estimatedDelivery: 1_506_897_315.0,
paymentType: "apple_pay",
revenueInUsd: 5,
revenueInUsd: 10,
rewardId: "1",
rewardMinimumUsd: 5.00,
rewardTitle: "My Reward",
shippingEnabled: true,
shippingAmountUsd: 0,
shippingAmountUsd: 5.0,
userHasStoredApplePayCard: true
)

Expand Down Expand Up @@ -1671,12 +1671,12 @@ final class NoShippingPledgeViewModelTests: TestCase {
checkoutId: "1",
estimatedDelivery: reward.estimatedDeliveryOn,
paymentType: "credit_card",
revenueInUsd: 43.00,
revenueInUsd: 58.00,
rewardId: String(reward.id),
rewardMinimumUsd: 10.00,
rewardTitle: reward.title,
shippingEnabled: true,
shippingAmountUsd: 0,
shippingAmountUsd: 15.0,
userHasStoredApplePayCard: true
)

Expand Down Expand Up @@ -3857,7 +3857,7 @@ final class NoShippingPledgeViewModelTests: TestCase {
|> Project.lens.rewardData.rewards .~ [reward]

let defaultShippingRule = ShippingRule(
cost: 10,
cost: 0,
id: 1,
location: .brooklyn,
estimatedMin: Money(amount: 1.0),
Expand All @@ -3867,7 +3867,7 @@ final class NoShippingPledgeViewModelTests: TestCase {
let data = PledgeViewData(
project: project,
rewards: [reward],
selectedShippingRule: shippingRule,
selectedShippingRule: defaultShippingRule,
selectedQuantities: [reward.id: 1],
selectedLocationId: defaultShippingRule.location.id,
refTag: .activity,
Expand Down Expand Up @@ -5086,7 +5086,7 @@ final class NoShippingPledgeViewModelTests: TestCase {

XCTAssertEqual("credit_card", segmentClientProps?["checkout_payment_type"] as? String)
XCTAssertEqual("1", segmentClientProps?["checkout_reward_id"] as? String)
XCTAssertEqual(50.00, segmentClientProps?["checkout_amount_total_usd"] as? Decimal)
XCTAssertEqual(55.00, segmentClientProps?["checkout_amount_total_usd"] as? Decimal)
XCTAssertEqual(true, segmentClientProps?["checkout_reward_is_limited_quantity"] as? Bool)
XCTAssertEqual(true, segmentClientProps?["checkout_reward_shipping_enabled"] as? Bool)
XCTAssertEqual(true, segmentClientProps?["checkout_user_has_eligible_stored_apple_pay_card"] as? Bool)
Expand Down

0 comments on commit ed0637d

Please sign in to comment.