Skip to content

Commit b926866

Browse files
authored
Merge pull request #8670 from woocommerce/feat/8668-product-add-on-migration
REST API: Migrate product add-ons
2 parents afa9730 + dae210f commit b926866

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@
719719
DE5CA111288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json in Resources */ = {isa = PBXBuildFile; fileRef = DE5CA110288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json */; };
720720
DE66C5552976662700DAA978 /* just-in-time-message-list-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE66C5542976662700DAA978 /* just-in-time-message-list-without-data.json */; };
721721
DE66C5572976913C00DAA978 /* wcpay-charge-card-present-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE66C5562976913C00DAA978 /* wcpay-charge-card-present-without-data.json */; };
722+
DE66C559297799D000DAA978 /* add-on-groups-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE66C558297799CF00DAA978 /* add-on-groups-without-data.json */; };
722723
DE66C5532976508300DAA978 /* CookieNonceAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE66C5522976508300DAA978 /* CookieNonceAuthenticator.swift */; };
723724
DE6F308727966FEF004E1C9A /* CouponReportListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE6F308627966FEF004E1C9A /* CouponReportListMapperTests.swift */; };
724725
DE74F29A27E08F5A0002FE59 /* SiteSettingMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE74F29927E08F5A0002FE59 /* SiteSettingMapper.swift */; };
@@ -1559,6 +1560,7 @@
15591560
DE5CA110288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-malformed-variations-and-image-alt.json"; sourceTree = "<group>"; };
15601561
DE66C5542976662700DAA978 /* just-in-time-message-list-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "just-in-time-message-list-without-data.json"; sourceTree = "<group>"; };
15611562
DE66C5562976913C00DAA978 /* wcpay-charge-card-present-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "wcpay-charge-card-present-without-data.json"; sourceTree = "<group>"; };
1563+
DE66C558297799CF00DAA978 /* add-on-groups-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "add-on-groups-without-data.json"; sourceTree = "<group>"; };
15621564
DE66C5522976508300DAA978 /* CookieNonceAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookieNonceAuthenticator.swift; sourceTree = "<group>"; };
15631565
DE6F308627966FEF004E1C9A /* CouponReportListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponReportListMapperTests.swift; sourceTree = "<group>"; };
15641566
DE74F29927E08F5A0002FE59 /* SiteSettingMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteSettingMapper.swift; sourceTree = "<group>"; };
@@ -2189,6 +2191,7 @@
21892191
B559EBA820A0B5B100836CD4 /* Responses */ = {
21902192
isa = PBXGroup;
21912193
children = (
2194+
DE66C558297799CF00DAA978 /* add-on-groups-without-data.json */,
21922195
DE66C5542976662700DAA978 /* just-in-time-message-list-without-data.json */,
21932196
DE66C5562976913C00DAA978 /* wcpay-charge-card-present-without-data.json */,
21942197
DE4F2A432975684900B0701C /* site-api-without-data.json */,
@@ -3113,6 +3116,7 @@
31133116
3158FE8026129EC300E566B9 /* wcpay-account-restricted-overdue.json in Resources */,
31143117
45AF57A924AB42CD0088E2F7 /* product-tags-extra.json in Resources */,
31153118
74AB5B4F21AF3F0E00859C12 /* site-api-no-woo.json in Resources */,
3119+
DE66C559297799D000DAA978 /* add-on-groups-without-data.json in Resources */,
31163120
265BCA02243056E3004E53EE /* categories-all.json in Resources */,
31173121
D8FBFF2422D52815006E3336 /* order-stats-v4-daily.json in Resources */,
31183122
CEC4BF91234E40B5008D9195 /* refund-single.json in Resources */,

Networking/Networking/Mapper/AddOnGroupMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ struct AddOnGroupMapper: Mapper {
1111
func map(response: Data) throws -> [AddOnGroup] {
1212
let decoder = JSONDecoder()
1313
decoder.userInfo = [.siteID: siteID]
14-
return try decoder.decode(AddOnGroupEnvelope.self, from: response).data
14+
do {
15+
return try decoder.decode(AddOnGroupEnvelope.self, from: response).data
16+
} catch {
17+
return try decoder.decode([AddOnGroup].self, from: response)
18+
}
1519
}
1620
}
1721

Networking/Networking/Remote/AddOnGroupRemote.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ public final class AddOnGroupRemote: Remote {
77
/// Retrieves all the `AddOnGroups` available for a given `siteID`
88
///
99
public func loadAddOnGroups(siteID: Int64, onCompletion: @escaping (Result<[AddOnGroup], Error>) -> ()) {
10-
let request = JetpackRequest(wooApiVersion: .addOnsV1, method: .get, siteID: siteID, path: Path.addOnGroups)
10+
let request = JetpackRequest(wooApiVersion: .addOnsV1,
11+
method: .get,
12+
siteID: siteID,
13+
path: Path.addOnGroups,
14+
availableAsRESTRequest: true)
1115
let mapper = AddOnGroupMapper(siteID: siteID)
1216
enqueue(request, mapper: mapper, completion: onCompletion)
1317
}

Networking/NetworkingTests/Mapper/AddOnGroupMapperTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ class AddOnGroupMapperTests: XCTestCase {
2424
XCTAssertEqual(secondGroup.name, "Music")
2525
XCTAssertEqual(secondGroup.addOns.count, 1)
2626
}
27+
28+
func test_addOnGroups_field_are_properly_parsed_when_response_has_no_data_envelope() throws {
29+
// Given & When
30+
let addOnGroups = try XCTUnwrap(mapLoadGroupAddOnsResponseWithoutDataEnvelope())
31+
32+
// Then
33+
XCTAssertEqual(addOnGroups.count, 2)
34+
35+
let firstGroup = addOnGroups[0]
36+
XCTAssertEqual(firstGroup.siteID, dummySiteID)
37+
XCTAssertEqual(firstGroup.groupID, 422)
38+
XCTAssertEqual(firstGroup.name, "Gifts")
39+
XCTAssertEqual(firstGroup.addOns.count, 2)
40+
41+
let secondGroup = addOnGroups[1]
42+
XCTAssertEqual(secondGroup.siteID, dummySiteID)
43+
XCTAssertEqual(secondGroup.groupID, 427)
44+
XCTAssertEqual(secondGroup.name, "Music")
45+
XCTAssertEqual(secondGroup.addOns.count, 1)
46+
}
2747
}
2848

2949
// MARK: JSON Loading
@@ -34,4 +54,11 @@ private extension AddOnGroupMapperTests {
3454
}
3555
return try? AddOnGroupMapper(siteID: dummySiteID).map(response: response)
3656
}
57+
58+
func mapLoadGroupAddOnsResponseWithoutDataEnvelope() -> [AddOnGroup]? {
59+
guard let response = Loader.contentsOf("add-on-groups-without-data") else {
60+
return nil
61+
}
62+
return try? AddOnGroupMapper(siteID: dummySiteID).map(response: response)
63+
}
3764
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
[
2+
{
3+
"id": 422,
4+
"name": "Gifts",
5+
"priority": 10,
6+
"restrict_to_categories": [],
7+
"fields": [
8+
{
9+
"name": "Gift Wrapping",
10+
"title_format": "label",
11+
"description_enable": 0,
12+
"description": "",
13+
"type": "multiple_choice",
14+
"display": "select",
15+
"position": 0,
16+
"required": 0,
17+
"restrictions": 0,
18+
"restrictions_type": "any_text",
19+
"adjust_price": 0,
20+
"price_type": "flat_fee",
21+
"price": "",
22+
"min": 0,
23+
"max": 0,
24+
"options": [
25+
{
26+
"label": "Basic",
27+
"price": "5",
28+
"image": "",
29+
"price_type": "flat_fee"
30+
},
31+
{
32+
"label": "Complex",
33+
"price": "7",
34+
"image": "",
35+
"price_type": "flat_fee"
36+
}
37+
]
38+
},
39+
{
40+
"name": "Dedicatory Card",
41+
"title_format": "label",
42+
"description_enable": 0,
43+
"description": "",
44+
"type": "custom_textarea",
45+
"display": "select",
46+
"position": 1,
47+
"required": 0,
48+
"restrictions": 0,
49+
"restrictions_type": "any_text",
50+
"adjust_price": 1,
51+
"price_type": "flat_fee",
52+
"price": "1",
53+
"min": 0,
54+
"max": 0,
55+
"options": [
56+
{
57+
"label": "",
58+
"price": "",
59+
"image": "",
60+
"price_type": "flat_fee"
61+
}
62+
]
63+
}
64+
]
65+
},
66+
{
67+
"id": 427,
68+
"name": "Music",
69+
"priority": 10,
70+
"restrict_to_categories": {
71+
"21": "Music"
72+
},
73+
"fields": [
74+
{
75+
"name": "Format",
76+
"title_format": "label",
77+
"description_enable": 0,
78+
"description": "",
79+
"type": "multiple_choice",
80+
"display": "select",
81+
"position": 0,
82+
"required": 1,
83+
"restrictions": 0,
84+
"restrictions_type": "any_text",
85+
"adjust_price": 0,
86+
"price_type": "flat_fee",
87+
"price": "",
88+
"min": 0,
89+
"max": 0,
90+
"options": [
91+
{
92+
"label": "MP3",
93+
"price": "",
94+
"image": "",
95+
"price_type": "flat_fee"
96+
},
97+
{
98+
"label": "ACC",
99+
"price": "",
100+
"image": "",
101+
"price_type": "flat_fee"
102+
},
103+
{
104+
"label": "WAV",
105+
"price": "",
106+
"image": "",
107+
"price_type": "flat_fee"
108+
}
109+
]
110+
}
111+
]
112+
}
113+
]

0 commit comments

Comments
 (0)