Skip to content

Commit 09bbf3d

Browse files
authored
Merge pull request #7117 from woocommerce/issue/7114-coupon-encoder-bug
Coupon: Fix issue encoding coupons
2 parents a99b74e + b01c11e commit 09bbf3d

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@
623623
DE74F29C27E0A1D00002FE59 /* setting-coupon.json in Resources */ = {isa = PBXBuildFile; fileRef = DE74F29B27E0A1D00002FE59 /* setting-coupon.json */; };
624624
DE74F29E27E0A6800002FE59 /* SiteSettingMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE74F29D27E0A6800002FE59 /* SiteSettingMapperTests.swift */; };
625625
DE74F2A027E3137F0002FE59 /* setting-analytics.json in Resources */ = {isa = PBXBuildFile; fileRef = DE74F29F27E3137F0002FE59 /* setting-analytics.json */; };
626+
DE97C3922861B8E20042E973 /* CouponEncoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE97C3912861B8E20042E973 /* CouponEncoderTests.swift */; };
626627
DE9D6BCC270D769C00BA6562 /* shipping-label-address-without-name-validation-success.json in Resources */ = {isa = PBXBuildFile; fileRef = DE9D6BCB270D769B00BA6562 /* shipping-label-address-without-name-validation-success.json */; };
627628
DEC2961C26BBE764005A056B /* ShippingLabelCustomsForm.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC2961B26BBE764005A056B /* ShippingLabelCustomsForm.swift */; };
628629
DEC51A95274CDA52009F3DF4 /* SitePluginMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51A94274CDA52009F3DF4 /* SitePluginMapper.swift */; };
@@ -1304,6 +1305,7 @@
13041305
DE74F29B27E0A1D00002FE59 /* setting-coupon.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "setting-coupon.json"; sourceTree = "<group>"; };
13051306
DE74F29D27E0A6800002FE59 /* SiteSettingMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteSettingMapperTests.swift; sourceTree = "<group>"; };
13061307
DE74F29F27E3137F0002FE59 /* setting-analytics.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "setting-analytics.json"; sourceTree = "<group>"; };
1308+
DE97C3912861B8E20042E973 /* CouponEncoderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponEncoderTests.swift; sourceTree = "<group>"; };
13071309
DE9D6BCB270D769B00BA6562 /* shipping-label-address-without-name-validation-success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "shipping-label-address-without-name-validation-success.json"; sourceTree = "<group>"; };
13081310
DEC2961B26BBE764005A056B /* ShippingLabelCustomsForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShippingLabelCustomsForm.swift; sourceTree = "<group>"; };
13091311
DEC51A94274CDA52009F3DF4 /* SitePluginMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SitePluginMapper.swift; sourceTree = "<group>"; };
@@ -1646,6 +1648,7 @@
16461648
B557D9F0209753AA005962F4 /* NetworkingTests */ = {
16471649
isa = PBXGroup;
16481650
children = (
1651+
DE97C3902861B8CD0042E973 /* Encoder */,
16491652
5726F7322460A8E30031CAAC /* Copiable */,
16501653
B559EBA820A0B5B100836CD4 /* Responses */,
16511654
B518663220A0A2E800037A38 /* Settings */,
@@ -2291,6 +2294,14 @@
22912294
path = Product;
22922295
sourceTree = "<group>";
22932296
};
2297+
DE97C3902861B8CD0042E973 /* Encoder */ = {
2298+
isa = PBXGroup;
2299+
children = (
2300+
DE97C3912861B8E20042E973 /* CouponEncoderTests.swift */,
2301+
);
2302+
path = Encoder;
2303+
sourceTree = "<group>";
2304+
};
22942305
DEC51AE527684717009F3DF4 /* SystemStatusDetails */ = {
22952306
isa = PBXGroup;
22962307
children = (
@@ -3091,6 +3102,7 @@
30913102
74AB5B5121AF426D00859C12 /* SiteAPIRemoteTests.swift in Sources */,
30923103
077F39E626A5D15800ABEADC /* SystemPluginMapperTests.swift in Sources */,
30933104
4599FC6624A633A10056157A /* ProductTagsRemoteTests.swift in Sources */,
3105+
DE97C3922861B8E20042E973 /* CouponEncoderTests.swift in Sources */,
30943106
B567AF2F20A0FB8F00AB6C62 /* AuthenticatedRequestTests.swift in Sources */,
30953107
B5C6FCCD20A34B8300A4F8E4 /* OrderListMapperTests.swift in Sources */,
30963108
B518663520A0A2E800037A38 /* Constants.swift in Sources */,

Networking/Networking/Model/Coupon.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public struct Coupon {
175175
try container.encode(amount, forKey: .amount)
176176
try container.encode(discountType, forKey: .discountType)
177177
try container.encode(description, forKey: .description)
178+
try container.encode(individualUse, forKey: .individualUse)
178179
try container.encode(productIds, forKey: .productIds)
179180
try container.encode(excludedProductIds, forKey: .excludedProductIds)
180181
try container.encode(usageLimit, forKey: .usageLimit)
@@ -183,6 +184,7 @@ public struct Coupon {
183184
try container.encode(freeShipping, forKey: .freeShipping)
184185
try container.encode(productCategories, forKey: .productCategories)
185186
try container.encode(excludedProductCategories, forKey: .excludedProductCategories)
187+
try container.encode(excludeSaleItems, forKey: .excludeSaleItems)
186188
try container.encode(minimumAmount, forKey: .minimumAmount)
187189
try container.encode(maximumAmount, forKey: .maximumAmount)
188190
try container.encode(emailRestrictions, forKey: .emailRestrictions)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import XCTest
2+
@testable import Networking
3+
4+
final class CouponEncoderTests: XCTestCase {
5+
6+
func test_coupon_encoder_encodes_necessary_fields_correctly() throws {
7+
// Given
8+
let dateFormatter = DateFormatter.Defaults.dateTimeFormatter
9+
let coupon = Coupon(
10+
couponID: 720,
11+
code: "free shipping",
12+
amount: "10.00",
13+
dateCreated: dateFormatter.date(from: "2017-03-21T18:25:02")!,
14+
dateModified: dateFormatter.date(from: "2017-03-21T18:25:02")!,
15+
discountType: .fixedCart,
16+
description: "Coupon description",
17+
dateExpires: dateFormatter.date(from: "2017-03-31T15:25:02"),
18+
usageCount: 10,
19+
individualUse: true,
20+
productIds: [12893712, 12389],
21+
excludedProductIds: [12213],
22+
usageLimit: 1200,
23+
usageLimitPerUser: 3,
24+
limitUsageToXItems: 10,
25+
freeShipping: true,
26+
productCategories: [123, 435, 232],
27+
excludedProductCategories: [908],
28+
excludeSaleItems: false,
29+
minimumAmount: "5.00",
30+
maximumAmount: "500.00",
31+
emailRestrictions: ["*@a8c.com", "[email protected]"],
32+
33+
)
34+
35+
// When
36+
let parameters = try coupon.toDictionary(keyEncodingStrategy: .convertToSnakeCase, dateFormatter: dateFormatter)
37+
38+
// Then
39+
XCTAssertEqual(parameters["code"] as? String, "free shipping")
40+
XCTAssertEqual(parameters["amount"] as? String, "10.00")
41+
XCTAssertEqual(parameters["discount_type"] as? String, "fixed_cart")
42+
XCTAssertEqual(parameters["description"] as? String, "Coupon description")
43+
XCTAssertEqual(parameters["date_expires"] as? String, "2017-03-31T15:25:02")
44+
XCTAssertEqual(parameters["individual_use"] as? Bool, true)
45+
XCTAssertEqual(parameters["product_ids"] as? [Int], [12893712, 12389])
46+
XCTAssertEqual(parameters["excluded_product_ids"] as? [Int], [12213])
47+
XCTAssertEqual(parameters["usage_limit"] as? Int, 1200)
48+
XCTAssertEqual(parameters["usage_limit_per_user"] as? Int, 3)
49+
XCTAssertEqual(parameters["limit_usage_to_x_items"] as? Int, 10)
50+
XCTAssertEqual(parameters["free_shipping"] as? Bool, true)
51+
XCTAssertEqual(parameters["product_categories"] as? [Int], [123, 435, 232])
52+
XCTAssertEqual(parameters["excluded_product_categories"] as? [Int], [908])
53+
XCTAssertEqual(parameters["exclude_sale_items"] as? Bool, false)
54+
XCTAssertEqual(parameters["minimum_amount"] as? String, "5.00")
55+
XCTAssertEqual(parameters["maximum_amount"] as? String, "500.00")
56+
XCTAssertEqual(parameters["email_restrictions"] as? [String], ["*@a8c.com", "[email protected]"])
57+
58+
// These fields cannot be updated so they are not encoded
59+
XCTAssertNil(parameters["coupon_id"])
60+
XCTAssertNil(parameters["date_created"])
61+
XCTAssertNil(parameters["date_modified"])
62+
XCTAssertNil(parameters["usage_count"])
63+
XCTAssertNil(parameters["used_by"])
64+
}
65+
}

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
9.5
44
-----
5+
- [*] Coupons: Fixed issue saving "Individual Use" and "Exclude Sale Items" fields. [https://github.com/woocommerce/woocommerce-ios/pull/7117]
56
- [*] Orders: The customer shipping/billing address form now navigates back automatically after selecting a country or state. [https://github.com/woocommerce/woocommerce-ios/pull/7119]
67

78
9.4

0 commit comments

Comments
 (0)