Skip to content

Commit 09a52be

Browse files
authored
Merge pull request #8547 from woocommerce/feat/8544-migrate-order-stats
REST API: Migrate order stats
2 parents c07fa39 + 7871073 commit 09a52be

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@
724724
DEF13C5029629EEA0024A02B /* user-complete-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */; };
725725
DEF13C562965689F0024A02B /* LeaderboardListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */; };
726726
DEF13C5A296571150024A02B /* leaderboards-year-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C59296571150024A02B /* leaderboards-year-without-data.json */; };
727+
DEF13C5C2965812D0024A02B /* order-stats-v4-year-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C5B2965812D0024A02B /* order-stats-v4-year-without-data.json */; };
727728
DEFBA74E29485A7600C35BA9 /* RESTRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBA74D29485A7600C35BA9 /* RESTRequest.swift */; };
728729
DEFBA7542949CE6600C35BA9 /* RequestProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBA7532949CE6600C35BA9 /* RequestProcessor.swift */; };
729730
DEFBA7562949D17400C35BA9 /* DefaultRequestAuthenticatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBA7552949D17300C35BA9 /* DefaultRequestAuthenticatorTests.swift */; };
@@ -1519,6 +1520,7 @@
15191520
DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "user-complete-without-data.json"; sourceTree = "<group>"; };
15201521
DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeaderboardListMapperTests.swift; sourceTree = "<group>"; };
15211522
DEF13C59296571150024A02B /* leaderboards-year-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "leaderboards-year-without-data.json"; sourceTree = "<group>"; };
1523+
DEF13C5B2965812D0024A02B /* order-stats-v4-year-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "order-stats-v4-year-without-data.json"; sourceTree = "<group>"; };
15221524
DEFBA74D29485A7600C35BA9 /* RESTRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RESTRequest.swift; sourceTree = "<group>"; };
15231525
DEFBA7532949CE6600C35BA9 /* RequestProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestProcessor.swift; sourceTree = "<group>"; };
15241526
DEFBA7552949D17300C35BA9 /* DefaultRequestAuthenticatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultRequestAuthenticatorTests.swift; sourceTree = "<group>"; };
@@ -2195,6 +2197,7 @@
21952197
D8FBFF2322D52815006E3336 /* order-stats-v4-daily.json */,
21962198
D8FBFF2622D529F2006E3336 /* order-stats-v4-month.json */,
21972199
D8FBFF2822D52AFA006E3336 /* order-stats-v4-year.json */,
2200+
DEF13C5B2965812D0024A02B /* order-stats-v4-year-without-data.json */,
21982201
D8FBFF1422D3BE09006E3336 /* order-stats-v4-defaults.json */,
21992202
D8FBFF1722D4DDB9006E3336 /* order-stats-v4-hour.json */,
22002203
D8C11A5B22DFCF8100D4A88D /* order-stats-v4-year-alt.json */,
@@ -3099,6 +3102,7 @@
30993102
31A451D127863A2E00FE81AA /* stripe-account-rejected-other.json in Resources */,
31003103
DEF13C5029629EEA0024A02B /* user-complete-without-data.json in Resources */,
31013104
74ABA1CA213F19FE00FFAD30 /* top-performers-year.json in Resources */,
3105+
DEF13C5C2965812D0024A02B /* order-stats-v4-year-without-data.json in Resources */,
31023106
31A451CF27863A2E00FE81AA /* stripe-account-rejected-terms-of-service.json in Resources */,
31033107
DE2E8EA9295416C9002E4B14 /* wordpress-site-info.json in Resources */,
31043108
268EC45C26C169F600716F5C /* order-with-faulty-attributes.json in Resources */,

Networking/Networking/Mapper/OrderStatsV4Mapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ struct OrderStatsV4Mapper: Mapper {
2424
.siteID: siteID,
2525
.granularity: granularity
2626
]
27-
return try decoder.decode(OrderStatsV4Envelope.self, from: response).orderStats
27+
do {
28+
return try decoder.decode(OrderStatsV4Envelope.self, from: response).orderStats
29+
} catch {
30+
return try decoder.decode(OrderStatsV4.self, from: response)
31+
}
2832
}
2933
}
3034

Networking/Networking/Remote/OrderStatsRemoteV4.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public final class OrderStatsRemoteV4: Remote {
3333
ParameterKeys.forceRefresh: forceRefresh
3434
]
3535

36-
let request = JetpackRequest(wooApiVersion: .wcAnalytics, method: .get, siteID: siteID, path: Constants.orderStatsPath, parameters: parameters)
36+
let request = JetpackRequest(wooApiVersion: .wcAnalytics,
37+
method: .get,
38+
siteID: siteID,
39+
path: Constants.orderStatsPath,
40+
parameters: parameters,
41+
availableAsRESTRequest: true)
3742
let mapper = OrderStatsV4Mapper(siteID: siteID, granularity: unit)
3843
enqueue(request, mapper: mapper, completion: completion)
3944
}

Networking/NetworkingTests/Mapper/OrderStatsMapperV4Tests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,49 @@ final class OrderStatsV4MapperTests: XCTestCase {
218218
XCTAssertNil(nonZeroYearTotals.totalProducts)
219219
XCTAssertEqual(nonZeroYearTotals.averageOrderValue, 266)
220220
}
221+
222+
/// Verifies that all of the yearly unit OrderStatsV4 fields are parsed correctly
223+
/// if the response contains no data envelope.
224+
///
225+
func test_yearly_unit_stat_fields_are_properly_parsed_without_data_envelope() {
226+
guard let yearlyStats = mapOrderStatsWithYearlyUnitResponseWithoutDataEnvelope() else {
227+
XCTFail()
228+
return
229+
}
230+
231+
XCTAssertEqual(yearlyStats.siteID, Constants.siteID)
232+
XCTAssertEqual(yearlyStats.granularity, .yearly)
233+
234+
XCTAssertEqual(yearlyStats.totals.totalOrders, 3)
235+
XCTAssertEqual(yearlyStats.totals.totalItemsSold, 5)
236+
XCTAssertEqual(yearlyStats.totals.grossRevenue, 800)
237+
XCTAssertEqual(yearlyStats.totals.totalCoupons, 0)
238+
XCTAssertEqual(yearlyStats.totals.couponDiscount, 0)
239+
XCTAssertEqual(yearlyStats.totals.refunds, 0)
240+
XCTAssertEqual(yearlyStats.totals.taxes, 0)
241+
XCTAssertEqual(yearlyStats.totals.shipping, 0)
242+
XCTAssertEqual(yearlyStats.totals.netRevenue, 800)
243+
XCTAssertEqual(yearlyStats.totals.totalProducts, 2)
244+
XCTAssertEqual(yearlyStats.totals.averageOrderValue, 266)
245+
246+
XCTAssertEqual(yearlyStats.intervals.count, 1)
247+
248+
let nonZeroYear = yearlyStats.intervals[0]
249+
let nonZeroYearTotals = nonZeroYear.subtotals
250+
251+
XCTAssertEqual(nonZeroYear.interval, "2019")
252+
253+
XCTAssertEqual(nonZeroYearTotals.totalOrders, 3)
254+
XCTAssertEqual(nonZeroYearTotals.grossRevenue, 800)
255+
XCTAssertEqual(nonZeroYearTotals.totalCoupons, 0)
256+
XCTAssertEqual(nonZeroYearTotals.couponDiscount, 0)
257+
XCTAssertEqual(nonZeroYearTotals.refunds, 0)
258+
XCTAssertEqual(nonZeroYearTotals.taxes, 0)
259+
XCTAssertEqual(nonZeroYearTotals.shipping, 0)
260+
XCTAssertEqual(nonZeroYearTotals.netRevenue, 800)
261+
XCTAssertNil(nonZeroYearTotals.totalProducts)
262+
XCTAssertEqual(nonZeroYearTotals.averageOrderValue, 266)
263+
}
221264
}
222265

223266
private extension OrderStatsV4MapperTests {
@@ -251,6 +294,12 @@ private extension OrderStatsV4MapperTests {
251294
return mapStatItems(from: "order-stats-v4-year", granularity: .yearly)
252295
}
253296

297+
/// Returns the OrderStatsV4Mapper output upon receiving `order-stats-v4-year-without-data`
298+
///
299+
func mapOrderStatsWithYearlyUnitResponseWithoutDataEnvelope() -> OrderStatsV4? {
300+
return mapStatItems(from: "order-stats-v4-year-without-data", granularity: .yearly)
301+
}
302+
254303
/// Returns the OrderStatsV4Mapper output upon receiving `filename` (Data Encoded)
255304
///
256305
func mapStatItems(from filename: String, granularity: StatsGranularityV4) -> OrderStatsV4? {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"totals": {
3+
"orders_count": 3,
4+
"num_items_sold": 5,
5+
"total_sales": 800,
6+
"coupons": 0,
7+
"coupons_count": 0,
8+
"refunds": 0,
9+
"taxes": 0,
10+
"shipping": 0,
11+
"net_revenue": 800,
12+
"products": 2,
13+
"avg_order_value": 266,
14+
"segments": []
15+
},
16+
"intervals": [
17+
{
18+
"interval": "2019",
19+
"date_start": "2019-07-09 00:00:00",
20+
"date_start_gmt": "2019-07-09 00:00:00",
21+
"date_end": "2019-07-09 23:59:59",
22+
"date_end_gmt": "2019-07-09 23:59:59",
23+
"subtotals": {
24+
"orders_count": 3,
25+
"num_items_sold": 5,
26+
"total_sales": 800,
27+
"coupons": 0,
28+
"coupons_count": 0,
29+
"refunds": 0,
30+
"taxes": 0,
31+
"shipping": 0,
32+
"net_revenue": 800,
33+
"avg_order_value": 266,
34+
"segments": []
35+
}
36+
}
37+
]
38+
}

0 commit comments

Comments
 (0)