Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[NT-1586] Fix shipping costs on Add On selection #1321

Merged
merged 15 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions KsApi/GraphSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ public enum Query {
}

public enum Reward {
public enum ShippingRulesExpandedConnection {
public enum Argument {
case locationId(String)
}
}

case amount(NonEmptySet<Money>)
case backersCount
case convertedAmount(NonEmptySet<Money>)
Expand All @@ -250,6 +256,10 @@ public enum Query {
case remainingQuantity
case shippingPreference
case shippingRules(NonEmptySet<ShippingRule>)
case shippingRulesExpanded(
Set<QueryArg<ShippingRulesExpandedConnection.Argument>>,
NonEmptySet<Connection<ShippingRule>>
)
case startsAt

public enum Item: String {
Expand Down Expand Up @@ -671,6 +681,7 @@ extension Query.Reward: QueryType {
case .remainingQuantity: return "remainingQuantity"
case .shippingPreference: return "shippingPreference"
case let .shippingRules(fields): return "shippingRules { \(join(fields)) }"
case let .shippingRulesExpanded(args, fields): return "shippingRulesExpanded" + connection(args, fields)
case .startsAt: return "startsAt"
}
}
Expand All @@ -682,6 +693,14 @@ extension Query.Reward.Item: QueryType {
}
}

extension Query.Reward.ShippingRulesExpandedConnection.Argument: CustomStringConvertible {
public var description: String {
switch self {
case let .locationId(id): return "forLocation: \"\(id)\""
}
}
}

extension Query.User.LaunchedProjects: QueryType {
public var description: String {
switch self {
Expand Down
6 changes: 6 additions & 0 deletions KsApi/models/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ extension Location: Equatable {}
public func == (lhs: Location, rhs: Location) -> Bool {
return lhs.id == rhs.id
}

extension Location: GraphIDBridging {
public static var modelName: String {
return "Location"
}
}
8 changes: 4 additions & 4 deletions KsApi/models/Money.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Prelude

public struct Money: Swift.Decodable, Equatable {
public var amount: Double
public var currency: CurrencyCode
public var symbol: String
public var currency: CurrencyCode?
public var symbol: String?

public enum CurrencyCode: String, CaseIterable, Swift.Decodable, Equatable {
case aud = "AUD"
Expand Down Expand Up @@ -42,8 +42,8 @@ extension Money {
}

self.amount = amount
self.currency = try values.decode(CurrencyCode.self, forKey: .currency)
self.symbol = try values.decode(String.self, forKey: .symbol)
self.currency = try values.decodeIfPresent(CurrencyCode.self, forKey: .currency)
self.symbol = try values.decodeIfPresent(String.self, forKey: .symbol)
}
}

Expand Down
10 changes: 7 additions & 3 deletions KsApi/models/Reward.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public struct Reward {
public let rewardsItems: [RewardsItem]
public let shipping: Shipping // only v1
public let shippingRules: [ShippingRule]? // only GraphQL
public let shippingRulesExpanded: [ShippingRule]? // only GraphQL
public let startsAt: TimeInterval?
public let title: String?

Expand All @@ -29,11 +30,13 @@ public struct Reward {
/**
Returns the closest matching `ShippingRule` for this `Reward` to `otherShippingRule`.
If no match is found `otherShippingRule` is returned, this is to be backward-compatible
with v1 Rewards that do not include the `shippingRules` array.
with v1 Rewards that do not include the `shippingRulesExpanded` array.
*/
public func shippingRule(matching otherShippingRule: ShippingRule?) -> ShippingRule? {
return self.shippingRules?
.first { shippingRule in shippingRule.location.id == otherShippingRule?.location.id }
return self.shippingRulesExpanded?
.first { shippingRule in
shippingRule.location.id == otherShippingRule?.location.id
}
?? otherShippingRule
}

Expand Down Expand Up @@ -100,6 +103,7 @@ extension Reward: Argo.Decodable {
<*> ((json <|| "rewards_items") <|> .success([]))
<*> tryDecodable(json)
<*> json <||? "shipping_rules"
<*> json <||? "shipping_rules_expanded"
<*> json <|? "starts_at"
<*> json <|? "title"
}
Expand Down
19 changes: 18 additions & 1 deletion KsApi/models/RewardTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ final class RewardTests: XCTestCase {
XCTAssertEqual(reward.shippingRule(matching: match), shippingRule1)
}

func testRewardShippingRuleExpanded_Match() {
let shippingRule1 = ShippingRule.template
|> ShippingRule.lens.cost .~ 5.0
|> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 1)
let shippingRule2 = ShippingRule.template
|> ShippingRule.lens.cost .~ 1.0
|> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 2)
let reward = Reward.template
|> Reward.lens.shippingRulesExpanded .~ [shippingRule1, shippingRule2]

let match = ShippingRule.template
|> ShippingRule.lens.cost .~ 500.0
|> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 1)

XCTAssertEqual(reward.shippingRule(matching: match), shippingRule1)
}

func testRewardShippingRule_NoMatch() {
let shippingRule1 = ShippingRule.template
|> ShippingRule.lens.cost .~ 5.0
Expand All @@ -212,7 +229,7 @@ final class RewardTests: XCTestCase {
|> ShippingRule.lens.cost .~ 1.0
|> ShippingRule.lens.location .~ (.template |> Location.lens.id .~ 2)
let reward = Reward.template
|> Reward.lens.shippingRules .~ [shippingRule1, shippingRule2]
|> Reward.lens.shippingRulesExpanded .~ [shippingRule1, shippingRule2]

let match = ShippingRule.template
|> ShippingRule.lens.cost .~ 500.0
Expand Down
5 changes: 5 additions & 0 deletions KsApi/models/graphql/GraphReward.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct GraphReward: Swift.Decodable {
var remainingQuantity: Int?
var shippingPreference: ShippingPreference?
var shippingRules: [ShippingRule]?
var shippingRulesExpanded: ShippingRuleExpanded?
var startsAt: TimeInterval?

struct Items: Swift.Decodable {
Expand Down Expand Up @@ -50,6 +51,10 @@ struct GraphReward: Swift.Decodable {
var name: String
}
}

struct ShippingRuleExpanded: Swift.Decodable {
let nodes: [ShippingRule]
}
}

extension GraphReward {
Expand Down
22 changes: 22 additions & 0 deletions KsApi/models/graphql/adapters/Reward+GraphReward.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extension Reward {
rewardsItems: rewardItemsData(from: graphReward, with: projectId),
shipping: shippingData(from: graphReward),
shippingRules: shippingRulesData(from: graphReward),
shippingRulesExpanded: shippingRulesExpandedData(from: graphReward),
startsAt: graphReward.startsAt,
title: graphReward.name
)
Expand Down Expand Up @@ -109,3 +110,24 @@ private func shippingRulesData(
)
}
}

private func shippingRulesExpandedData(
from graphReward: GraphReward
) -> [ShippingRule]? {
guard let shippingRules = graphReward.shippingRulesExpanded?.nodes else { return nil }

return shippingRules.compactMap { shippingRule -> ShippingRule? in
guard let locationId = decompose(id: shippingRule.location.id) else { return nil }
return ShippingRule(
cost: shippingRule.cost.amount,
id: decompose(id: shippingRule.id),
location: Location(
country: shippingRule.location.country,
displayableName: shippingRule.location.displayableName,
id: locationId,
localizedName: shippingRule.location.name,
name: shippingRule.location.name
)
)
}
}
39 changes: 39 additions & 0 deletions KsApi/models/lenses/RewardLenses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -41,6 +42,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -63,6 +65,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -85,6 +88,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -107,6 +111,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -129,6 +134,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -151,6 +157,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -173,6 +180,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -195,6 +203,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -217,6 +226,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -239,6 +249,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -261,6 +272,7 @@ extension Reward {
rewardsItems: $0,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -283,6 +295,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $0,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -305,6 +318,30 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $0,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title
) }
)

public static let shippingRulesExpanded = Lens<Reward, [ShippingRule]?>(
view: { $0.shippingRulesExpanded },
set: { Reward(
backersCount: $1.backersCount,
convertedMinimum: $1.convertedMinimum,
description: $1.description,
endsAt: $1.endsAt,
estimatedDeliveryOn: $1.estimatedDeliveryOn,
hasAddOns: $1.hasAddOns,
id: $1.id,
limit: $1.limit,
limitPerBacker: $1.limitPerBacker,
minimum: $1.minimum,
remaining: $1.remaining,
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $0,
startsAt: $1.startsAt,
title: $1.title
) }
Expand All @@ -327,6 +364,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $0,
title: $1.title
) }
Expand All @@ -349,6 +387,7 @@ extension Reward {
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $0
) }
Expand Down
3 changes: 3 additions & 0 deletions KsApi/models/templates/RewardTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extension Reward {
type: nil
),
shippingRules: nil,
shippingRulesExpanded: nil,
startsAt: nil,
title: "My Reward"
)
Expand All @@ -49,6 +50,7 @@ extension Reward {
type: nil
),
shippingRules: nil,
shippingRulesExpanded: nil,
startsAt: nil,
title: nil
)
Expand All @@ -74,6 +76,7 @@ extension Reward {
type: nil
),
shippingRules: nil,
shippingRulesExpanded: nil,
startsAt: nil,
title: nil
)
Expand Down
1 change: 1 addition & 0 deletions KsApi/models/templates/graphql/GraphRewardTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension GraphReward {
remainingQuantity: 10,
shippingPreference: .restricted,
shippingRules: [.template],
shippingRulesExpanded: ShippingRuleExpanded(nodes: [.template]),
startsAt: 1_487_502_131
)
}
Expand Down
Loading