@@ -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