Skip to content

Commit fb699ac

Browse files
committed
Filter out non-default discount types when mapping coupons
1 parent 9391e45 commit fb699ac

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Networking/Networking/Mapper/CouponListMapper.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ struct CouponListMapper: Mapper {
1212
///
1313
func map(response: Data) throws -> [Coupon] {
1414
let coupons = try Coupon.decoder.decode(CouponListEnvelope.self, from: response).coupons
15-
return coupons.map { $0.copy(siteID: siteID) }
15+
return coupons
16+
.map { $0.copy(siteID: siteID) }
17+
.filter { $0.mappedDiscountType != nil }
1618
}
1719
}
1820

Networking/Networking/Model/Coupon.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ public struct Coupon {
2626
public let dateModified: Date
2727

2828
/// Determines the type of discount that will be applied. Options: `.percent` `.fixedCart` and `.fixedProduct`
29-
public let discountType: DiscountType
29+
public var discountType: DiscountType {
30+
if let type = mappedDiscountType {
31+
return type
32+
} else {
33+
// Returns default value for fallback case to avoid working with optionals.
34+
// Since `CouponListMapper` filters out nil `mappedDiscountType`,
35+
// this case is unlikely to happen.
36+
return .fixedCart
37+
}
38+
}
39+
40+
/// Discount type if matched with any of the ones supported by Core.
41+
/// Returns nil if other types are found.
42+
/// Used to filter only coupons with default types, so internal to this module only.
43+
///
44+
internal let mappedDiscountType: DiscountType?
3045

3146
public let description: String
3247

@@ -78,6 +93,9 @@ public struct Coupon {
7893
/// Email addresses of customers who have used this coupon
7994
public let usedBy: [String]
8095

96+
/// Discount types supported by Core.
97+
/// There are other types supported by other plugins, but those are not supported for now.
98+
///
8199
public enum DiscountType: String {
82100
case percent = "percent"
83101
case fixedCart = "fixed_cart"
@@ -114,7 +132,7 @@ public struct Coupon {
114132
self.amount = amount
115133
self.dateCreated = dateCreated
116134
self.dateModified = dateModified
117-
self.discountType = discountType
135+
self.mappedDiscountType = discountType
118136
self.description = description
119137
self.dateExpires = dateExpires
120138
self.usageCount = usageCount
@@ -148,7 +166,7 @@ extension Coupon: Codable {
148166
case amount
149167
case dateCreated = "dateCreatedGmt"
150168
case dateModified = "dateModifiedGmt"
151-
case discountType
169+
case mappedDiscountType
152170
case description
153171
case dateExpires = "dateExpiresGmt"
154172
case usageCount

0 commit comments

Comments
 (0)