Skip to content

Commit e2004da

Browse files
authored
Merge pull request #5898 from woocommerce/issue/5896-coupon-parsing
Coupons: Fix mapping for coupons to filter non-default discount types
2 parents bbe5e2a + bdc11d9 commit e2004da

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
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,

Networking/NetworkingTests/Responses/coupons-all.json

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"date_created_gmt": "2017-03-21T18:23:00",
5050
"date_modified": "2017-03-21T15:23:00",
5151
"date_modified_gmt": "2017-03-21T18:23:00",
52-
"discount_type": "percent",
52+
"discount_type": "fixed_cart",
5353
"description": "",
5454
"date_expires": null,
5555
"date_expires_gmt": null,
@@ -90,7 +90,7 @@
9090
"date_created_gmt": "2021-04-13T08:26:25",
9191
"date_modified": "2021-04-13T08:26:25",
9292
"date_modified_gmt": "2021-04-13T08:26:25",
93-
"discount_type": "fixed_cart",
93+
"discount_type": "percent",
9494
"description": "",
9595
"date_expires": null,
9696
"date_expires_gmt": null,
@@ -122,6 +122,47 @@
122122
}
123123
]
124124
}
125+
},
126+
{
127+
"id": 581,
128+
"code": "usrufb6q",
129+
"amount": "30.00",
130+
"date_created": "2022-01-07T10:59:47",
131+
"date_created_gmt": "2022-01-07T03:59:47",
132+
"date_modified": "2022-01-07T10:59:56",
133+
"date_modified_gmt": "2022-01-07T03:59:56",
134+
"discount_type": "sign_up_fee_percent",
135+
"description": "",
136+
"date_expires": null,
137+
"date_expires_gmt": null,
138+
"usage_count": 0,
139+
"individual_use": false,
140+
"product_ids": [],
141+
"excluded_product_ids": [],
142+
"usage_limit": null,
143+
"usage_limit_per_user": null,
144+
"limit_usage_to_x_items": null,
145+
"free_shipping": false,
146+
"product_categories": [],
147+
"excluded_product_categories": [],
148+
"exclude_sale_items": false,
149+
"minimum_amount": "0.00",
150+
"maximum_amount": "0.00",
151+
"email_restrictions": [],
152+
"used_by": [],
153+
"meta_data": [],
154+
"_links": {
155+
"self": [
156+
{
157+
"href": "https://huongdotests1.blog/wp-json/wc/v3/coupons/581"
158+
}
159+
],
160+
"collection": [
161+
{
162+
"href": "https://huongdotests1.blog/wp-json/wc/v3/coupons"
163+
}
164+
]
165+
}
125166
}
126167
]
127168
}

0 commit comments

Comments
 (0)