Skip to content

Commit 9c2ae63

Browse files
committed
Return shipments when syncing labels for woo shipping
1 parent 5feb817 commit 9c2ae63

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

Modules/Sources/Yosemite/Actions/WooShippingAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public enum WooShippingAction: Action {
111111
///
112112
case syncShippingLabels(siteID: Int64,
113113
orderID: Int64,
114-
completion: (Result<[ShippingLabel], Error>) -> Void)
114+
completion: (Result<ShippingLabelSyncResult, Error>) -> Void)
115115

116116
/// Updates shipments for given order
117117
///

Modules/Sources/Yosemite/Stores/WooShippingStore.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import Foundation
22
import Networking
33
import Storage
44

5+
public struct ShippingLabelSyncResult {
6+
public let labels: [ShippingLabel]
7+
public let shipments: WooShippingShipments
8+
9+
public init(labels: [ShippingLabel], shipments: WooShippingShipments) {
10+
self.labels = labels
11+
self.shipments = shipments
12+
}
13+
14+
public static let none = ShippingLabelSyncResult(labels: [], shipments: [:])
15+
}
16+
517
/// Implements `WooShippingAction` actions
618
///
719
public final class WooShippingStore: Store {
@@ -292,21 +304,22 @@ private extension WooShippingStore {
292304

293305
func syncShippingLabels(siteID: Int64,
294306
orderID: Int64,
295-
completion: @escaping (Result<[ShippingLabel], Error>) -> Void) {
307+
completion: @escaping (Result<ShippingLabelSyncResult, Error>) -> Void) {
296308
remote.loadConfig(siteID: siteID, orderID: orderID, completion: { [weak self] result in
297309
guard let self else { return }
298310

299311
switch result {
300312
case .failure(let error):
301313
completion(.failure(error))
302314
case .success(let config):
315+
let shipments = config.shipments
303316
guard let labels = config.shippingLabelData?.currentOrderLabels else {
304-
return completion(.success([]))
317+
return completion(.success(ShippingLabelSyncResult(labels: [], shipments: shipments)))
305318
}
306319
upsertShippingLabelsInBackground(siteID: siteID,
307320
orderID: orderID,
308321
shippingLabels: labels) {
309-
completion(.success(labels))
322+
completion(.success(ShippingLabelSyncResult(labels: labels, shipments: shipments)))
310323
}
311324
}
312325
})

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsDataSource.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ final class OrderDetailsDataSource: NSObject {
6161
return true
6262
}
6363

64+
/// Shipments created for the order
65+
///
66+
private var wooShippingShipments: [Shipment] = []
67+
6468
/// Whether the button to create shipping labels should be visible.
6569
///
6670
var shouldShowShippingLabelCreation: Bool {
@@ -1696,6 +1700,36 @@ extension OrderDetailsDataSource {
16961700
}
16971701
}
16981702

1703+
// MARK: - Woo Shipping Shipments
1704+
extension OrderDetailsDataSource {
1705+
struct Shipment {
1706+
let index: String
1707+
let shippingLabel: ShippingLabel?
1708+
}
1709+
1710+
func populateShipments(labels: [ShippingLabel], shipments: WooShippingShipments) {
1711+
var contents = [Shipment]()
1712+
1713+
for key in shipments.keys.sorted(by: { $0.localizedStandardCompare($1) == .orderedAscending }) {
1714+
1715+
let label: ShippingLabel? = {
1716+
let purchasedLabels = labels.filter {
1717+
$0.shipmentID == key && $0.status == .purchased
1718+
}
1719+
let sortedLabels = purchasedLabels.sorted { $0.dateCreated > $1.dateCreated }
1720+
if let completedLabel = sortedLabels.first(where: { $0.refund == nil }) {
1721+
return completedLabel
1722+
} else {
1723+
return sortedLabels.first
1724+
}
1725+
}()
1726+
1727+
let shipment = Shipment(index: key, shippingLabel: label)
1728+
contents.append(shipment)
1729+
}
1730+
self.wooShippingShipments = contents
1731+
}
1732+
}
16991733

17001734
// MARK: - Constants
17011735
extension OrderDetailsDataSource {

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsViewModel.swift

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,12 @@ extension OrderDetailsViewModel {
283283
taskGroup.addTask { [weak self] in
284284
guard let self else { return }
285285
// Sync shipping labels and update order with the result if available
286-
let shippingLabels = await syncShippingLabels()
286+
let syncResult = await syncShippingLabels()
287+
dataSource.populateShipments(labels: syncResult.labels,
288+
shipments: syncResult.shipments)
289+
287290
// Update the order with the newly synced shipping labels
288-
let updatedOrder = order.copy(shippingLabels: shippingLabels)
291+
let updatedOrder = order.copy(shippingLabels: syncResult.labels)
289292
update(order: updatedOrder)
290293
}
291294
}
@@ -703,26 +706,26 @@ extension OrderDetailsViewModel {
703706
}
704707

705708
@discardableResult
706-
@MainActor func syncShippingLabels() async -> [ShippingLabel] {
709+
@MainActor func syncShippingLabels() async -> ShippingLabelSyncResult {
707710
let isRevampedFlow = featureFlagService.isFeatureFlagEnabled(.revampedShippingLabelCreation)
708711
guard isRevampedFlow else {
709712
/// old logic for syncing labels
710713
if await localRequirementsForShippingLabelsAreFulfilled() {
711714
return await syncShippingLabelsForLegacyPlugin(isRevampedFlow: isRevampedFlow)
712715
}
713-
return []
716+
return .none
714717
}
715718

716719
guard !orderContainsOnlyVirtualProducts else {
717-
return []
720+
return .none
718721
}
719722

720723
if await isPluginActive(pluginPath: SitePlugin.SupportedPluginPath.WooShipping) {
721724
return await syncShippingLabelsForWooShipping()
722725
} else if await isPluginActive(pluginPath: SitePlugin.SupportedPluginPath.LegacyWCShip) {
723726
return await syncShippingLabelsForLegacyPlugin(isRevampedFlow: isRevampedFlow)
724727
} else {
725-
return []
728+
return .none
726729
}
727730
}
728731

@@ -990,33 +993,45 @@ private extension OrderDetailsViewModel {
990993
}
991994
}
992995

993-
@MainActor func syncShippingLabelsForWooShipping() async -> [ShippingLabel] {
996+
@MainActor func syncShippingLabelsForWooShipping() async -> ShippingLabelSyncResult {
994997
await withCheckedContinuation { continuation in
995998
stores.dispatch(WooShippingAction.syncShippingLabels(siteID: order.siteID, orderID: order.orderID) { [weak self] result in
996-
let labels = self?.handleShippingLabelSyncingResult(result: result, isRevampedFlow: true) ?? []
997-
continuation.resume(returning: labels)
999+
switch result {
1000+
case let .success(result):
1001+
self?.trackShippingLabelSyncingResult(result: .success, isRevampedFlow: true)
1002+
continuation.resume(returning: result)
1003+
case let .failure(error):
1004+
self?.trackShippingLabelSyncingResult(result: .failed(error: error), isRevampedFlow: true)
1005+
continuation.resume(returning: .none)
1006+
}
9981007
})
9991008
}
10001009
}
10011010

1002-
@MainActor func syncShippingLabelsForLegacyPlugin(isRevampedFlow: Bool) async -> [ShippingLabel] {
1011+
@MainActor func syncShippingLabelsForLegacyPlugin(isRevampedFlow: Bool) async -> ShippingLabelSyncResult {
10031012
await withCheckedContinuation { continuation in
10041013
stores.dispatch(ShippingLabelAction.synchronizeShippingLabels(siteID: order.siteID, orderID: order.orderID) { [weak self] result in
1005-
let labels = self?.handleShippingLabelSyncingResult(result: result, isRevampedFlow: isRevampedFlow) ?? []
1006-
continuation.resume(returning: labels)
1014+
switch result {
1015+
case .success(let labels):
1016+
self?.trackShippingLabelSyncingResult(result: .success, isRevampedFlow: isRevampedFlow)
1017+
continuation.resume(returning: ShippingLabelSyncResult(labels: labels, shipments: [:]))
1018+
case .failure(let error):
1019+
self?.trackShippingLabelSyncingResult(result: .failed(error: error), isRevampedFlow: isRevampedFlow)
1020+
continuation.resume(returning: .none)
1021+
}
10071022
})
10081023
}
10091024
}
10101025

1011-
func handleShippingLabelSyncingResult(result: Result<[ShippingLabel], Error>, isRevampedFlow: Bool) -> [ShippingLabel] {
1026+
func trackShippingLabelSyncingResult(result: WooAnalyticsEvent.ShippingLabelsAPIRequestResult,
1027+
isRevampedFlow: Bool) {
10121028
switch result {
1013-
case .success(let shippingLabels):
1029+
case .success:
10141030
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
10151031
result: .success,
10161032
isRevampedFlow: isRevampedFlow
10171033
))
1018-
return shippingLabels
1019-
case .failure(let error):
1034+
case .failed(let error):
10201035
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
10211036
result: .failed(error: error),
10221037
isRevampedFlow: isRevampedFlow
@@ -1026,7 +1041,6 @@ private extension OrderDetailsViewModel {
10261041
} else {
10271042
DDLogError("⛔️ Error synchronizing shipping labels: \(error)")
10281043
}
1029-
return []
10301044
}
10311045
}
10321046

0 commit comments

Comments
 (0)