Skip to content

Commit bdc11d9

Browse files
committed
Add custom decoder for discount type
1 parent ebd8463 commit bdc11d9

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

Networking/Networking/Mapper/CouponListMapper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct CouponListMapper: Mapper {
1414
let coupons = try Coupon.decoder.decode(CouponListEnvelope.self, from: response).coupons
1515
return coupons
1616
.map { $0.copy(siteID: siteID) }
17-
.filter { $0.mappedDiscountType != nil }
17+
.filter { $0.discountType != .other }
1818
}
1919
}
2020

Networking/Networking/Model/Coupon.swift

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,7 @@ 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 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?
29+
public let discountType: DiscountType
4530

4631
public let description: String
4732

@@ -97,9 +82,10 @@ public struct Coupon {
9782
/// There are other types supported by other plugins, but those are not supported for now.
9883
///
9984
public enum DiscountType: String {
100-
case percent = "percent"
85+
case percent
10186
case fixedCart = "fixed_cart"
10287
case fixedProduct = "fixed_product"
88+
case other
10389
}
10490

10591
public init(siteID: Int64 = 0,
@@ -132,7 +118,7 @@ public struct Coupon {
132118
self.amount = amount
133119
self.dateCreated = dateCreated
134120
self.dateModified = dateModified
135-
self.mappedDiscountType = discountType
121+
self.discountType = discountType
136122
self.description = description
137123
self.dateExpires = dateExpires
138124
self.usageCount = usageCount
@@ -166,7 +152,7 @@ extension Coupon: Codable {
166152
case amount
167153
case dateCreated = "dateCreatedGmt"
168154
case dateModified = "dateModifiedGmt"
169-
case mappedDiscountType
155+
case discountType
170156
case description
171157
case dateExpires = "dateExpiresGmt"
172158
case usageCount
@@ -187,7 +173,12 @@ extension Coupon: Codable {
187173
}
188174
}
189175

190-
extension Coupon.DiscountType: Codable {}
176+
extension Coupon.DiscountType: Codable {
177+
public init(from decoder: Decoder) throws {
178+
let rawValue = try decoder.singleValueContainer().decode(String.self)
179+
self = Coupon.DiscountType(rawValue: rawValue) ?? .other
180+
}
181+
}
191182

192183

193184
// MARK: - Other Conformances

Networking/NetworkingTests/Mapper/CouponListMapperTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CouponListMapperTests: XCTestCase {
77
///
88
private let dummySiteID: Int64 = 12983476
99

10-
/// Verifies that the whole list is parsed.
10+
/// Verifies that the whole list is parsed, minus the items with non-default discount type.
1111
///
1212
func test_CouponsList_map_parses_all_coupons_in_response() throws {
1313
let coupons = try mapLoadAllCouponsResponse()
@@ -73,7 +73,7 @@ class CouponListMapperTests: XCTestCase {
7373
amount: "0.00",
7474
dateCreated: dateFormatter.date(from: "2021-04-13T08:26:25")!,
7575
dateModified: dateFormatter.date(from: "2021-04-13T08:26:25")!,
76-
discountType: .fixedCart,
76+
discountType: .percent,
7777
description: "",
7878
dateExpires: nil,
7979
usageCount: 0,

0 commit comments

Comments
 (0)