Skip to content

Commit 03bed3e

Browse files
authored
Merge pull request #8662 from woocommerce/feat/8661-jitm-migration
2 parents 6ef1b05 + 517a0f1 commit 03bed3e

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@
717717
DE50296328C609DE00551736 /* jetpack-user-not-connected.json in Resources */ = {isa = PBXBuildFile; fileRef = DE50296228C609DE00551736 /* jetpack-user-not-connected.json */; };
718718
DE50296528C60A8000551736 /* JetpackUserMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50296428C60A8000551736 /* JetpackUserMapperTests.swift */; };
719719
DE5CA111288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json in Resources */ = {isa = PBXBuildFile; fileRef = DE5CA110288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json */; };
720+
DE66C5552976662700DAA978 /* just-in-time-message-list-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE66C5542976662700DAA978 /* just-in-time-message-list-without-data.json */; };
720721
DE66C5572976913C00DAA978 /* wcpay-charge-card-present-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE66C5562976913C00DAA978 /* wcpay-charge-card-present-without-data.json */; };
721722
DE66C5532976508300DAA978 /* CookieNonceAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE66C5522976508300DAA978 /* CookieNonceAuthenticator.swift */; };
722723
DE6F308727966FEF004E1C9A /* CouponReportListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE6F308627966FEF004E1C9A /* CouponReportListMapperTests.swift */; };
@@ -1556,6 +1557,7 @@
15561557
DE50296228C609DE00551736 /* jetpack-user-not-connected.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "jetpack-user-not-connected.json"; sourceTree = "<group>"; };
15571558
DE50296428C60A8000551736 /* JetpackUserMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUserMapperTests.swift; sourceTree = "<group>"; };
15581559
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>"; };
1560+
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>"; };
15591561
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>"; };
15601562
DE66C5522976508300DAA978 /* CookieNonceAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookieNonceAuthenticator.swift; sourceTree = "<group>"; };
15611563
DE6F308627966FEF004E1C9A /* CouponReportListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponReportListMapperTests.swift; sourceTree = "<group>"; };
@@ -2187,6 +2189,7 @@
21872189
B559EBA820A0B5B100836CD4 /* Responses */ = {
21882190
isa = PBXGroup;
21892191
children = (
2192+
DE66C5542976662700DAA978 /* just-in-time-message-list-without-data.json */,
21902193
DE66C5562976913C00DAA978 /* wcpay-charge-card-present-without-data.json */,
21912194
DE4F2A432975684900B0701C /* site-api-without-data.json */,
21922195
DE4F2A3D29750EF400B0701C /* inbox-note-list-without-data.json */,
@@ -3179,6 +3182,7 @@
31793182
93D8BBFF226BC1DA00AD2EB3 /* me-settings.json in Resources */,
31803183
74C947842193A6C70024CB60 /* comment-moderate-approved.json in Resources */,
31813184
DEC51AED2768A0AD009F3DF4 /* systemStatusWithPluginsOnly.json in Resources */,
3185+
DE66C5552976662700DAA978 /* just-in-time-message-list-without-data.json in Resources */,
31823186
D8C11A5C22DFCF8100D4A88D /* order-stats-v4-year-alt.json in Resources */,
31833187
3158FE6426129B1300E566B9 /* wcpay-account-complete.json in Resources */,
31843188
B56C1EBA20EA7D2C00D749F9 /* sites.json in Resources */,

Networking/Networking/Mapper/DataBoolMapper.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import Foundation
22

3-
/// Mapper: Bool Result Wrapped in `data` Key
3+
/// Mapper: Bool Result, Wrapped in `data` Key or not
44
///
55
struct DataBoolMapper: Mapper {
66

77
/// (Attempts) to extract the boolean flag from a given JSON Encoded response.
88
///
99
func map(response: Data) throws -> Bool {
10-
try JSONDecoder().decode(DataBool.self, from: response).data
10+
do {
11+
return try JSONDecoder().decode(DataBool.self, from: response).data
12+
} catch {
13+
return try JSONDecoder().decode(Bool.self, from: response)
14+
}
1115
}
1216
}
1317

Networking/Networking/Mapper/JustInTimeMessageListMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ struct JustInTimeMessageListMapper: Mapper {
1616
decoder.userInfo = [
1717
.siteID: siteID
1818
]
19-
return try decoder.decode(JustInTimeMessageListEnvelope.self, from: response).data
19+
do {
20+
return try decoder.decode(JustInTimeMessageListEnvelope.self, from: response).data
21+
} catch {
22+
return try decoder.decode([JustInTimeMessage].self, from: response)
23+
}
2024
}
2125
}
2226

Networking/Networking/Remote/JustInTimeMessagesRemote.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public final class JustInTimeMessagesRemote: Remote, JustInTimeMessagesRemotePro
3535
locale: locale,
3636
path: Path.jitm,
3737
parameters: getParameters(messagePath: messagePath,
38-
query: query))
38+
query: query),
39+
availableAsRESTRequest: true)
3940

4041
let mapper = JustInTimeMessageListMapper(siteID: siteID)
4142

@@ -97,7 +98,8 @@ public final class JustInTimeMessagesRemote: Remote, JustInTimeMessagesRemotePro
9798
method: .post,
9899
siteID: siteID,
99100
path: Path.jitm,
100-
parameters: parameters)
101+
parameters: parameters,
102+
availableAsRESTRequest: true)
101103

102104
return try await enqueue(request, mapper: DataBoolMapper())
103105
}

Networking/NetworkingTests/Mapper/JustInTimeMessageListMapperTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ final class JustInTimeMessageListMapperTests: XCTestCase {
1515
assertEqual(1, justInTimeMessages?.count)
1616
}
1717

18+
/// Verifies that the message is parsed.
19+
///
20+
func test_JustInTimeMessageListMapper_parses_the_JustInTimeMessage_in_response_without_data_envelope() throws {
21+
let justInTimeMessages = try mapLoadJustInTimeMessageListResponseWithoutDataEnvelope()
22+
XCTAssertNotNil(justInTimeMessages)
23+
assertEqual(1, justInTimeMessages?.count)
24+
}
25+
1826
/// Verifies that the fields are all parsed correctly.
1927
///
2028
func test_JustInTimeMessageListMapper_parses_all_fields_in_result() throws {
@@ -55,4 +63,10 @@ private extension JustInTimeMessageListMapperTests {
5563
func mapLoadJustInTimeMessageListResponse() throws -> [JustInTimeMessage]? {
5664
return try mapJustInTimeMessageList(from: "just-in-time-message-list")
5765
}
66+
67+
/// Returns the JustInTimeMessageListMapper output from `just-in-time-message-list-without-data.json`
68+
///
69+
func mapLoadJustInTimeMessageListResponseWithoutDataEnvelope() throws -> [JustInTimeMessage]? {
70+
return try mapJustInTimeMessageList(from: "just-in-time-message-list-without-data")
71+
}
5872
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"content": {
4+
"message": "In-person card payments",
5+
"icon": "",
6+
"iconPath": null,
7+
"list": [],
8+
"description": "Sell anywhere, and take card payments using a mobile card reader.",
9+
"classes": "",
10+
"title": "",
11+
"disclaimer": []
12+
},
13+
"CTA": {
14+
"message": "Purchase Card Reader",
15+
"hook": "",
16+
"newWindow": true,
17+
"primary": true,
18+
"link": "https:\/\/woocommerce.com\/products\/hardware\/US"
19+
},
20+
"template": "default",
21+
"ttl": 300,
22+
"id": "woomobile_ipp_barcode_users",
23+
"feature_class": "woomobile_ipp",
24+
"expires": 3628800,
25+
"max_dismissal": 2,
26+
"activate_module": null,
27+
"is_dismissible": true,
28+
"is_user_created_by_partner": null,
29+
"message_expiration": null,
30+
"url": "https:\/\/jetpack.com\/redirect\/?source=jitm-woomobile_ipp_barcode_users&#038;site=store.example.com&#038;u=1",
31+
"jitm_stats_url": "https:\/\/pixel.wp.com\/b.gif?v=wpcom2&rand=b8ec47eefac4b8f64b70cd379dc573dd&x_jetpack-jitm=woomobile_ipp_barcode_users"
32+
}
33+
]

0 commit comments

Comments
 (0)