Skip to content

Commit 2b8fdb8

Browse files
[Shipping Labels] Fix missing origin address for previously purchased shipping labels (#15872)
2 parents 2ad8619 + 548904a commit 2b8fdb8

File tree

9 files changed

+72
-27
lines changed

9 files changed

+72
-27
lines changed

Modules/Sources/Networking/Mapper/WooShippingConfigMapper.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,10 @@ private struct WooShippingConfigMapperEnvelope: Decodable {
4444
extension WooShippingConfigMapper {
4545
/// Load only the relevant fields from remote
4646
///
47-
static let fieldsToLoad = "config.shipments, config.shippingLabelData.currentOrderLabels, config.shippingLabelData.storedData.selected_destination"
47+
static let fieldsToLoad = [
48+
"config.shipments",
49+
"config.shippingLabelData.currentOrderLabels",
50+
"config.shippingLabelData.storedData.selected_destination",
51+
"config.shippingLabelData.storedData.selected_origin"
52+
].joined(separator: ", ")
4853
}

Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingConfigResponse.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,16 @@ public struct WooShippingLabelData: Decodable, Equatable {
7979

8080
/// Inject destination addresses into labels if present
8181
let orderLabels: [ShippingLabel]
82-
if let destinations = storedData?.selectedDestinations, !destinations.isEmpty {
83-
orderLabels = WooShippingLabelData.mapDestinations(destinations, into: decodedOrderLabels)
82+
83+
let destinations = storedData?.selectedDestinations
84+
let origins = storedData?.selectedOrigins
85+
86+
if destinations?.isEmpty == false || origins?.isEmpty == false {
87+
orderLabels = WooShippingLabelData.mapAddresses(
88+
origins: origins,
89+
destinations: destinations,
90+
into: decodedOrderLabels
91+
)
8492
} else {
8593
orderLabels = decodedOrderLabels
8694
}
@@ -98,21 +106,27 @@ public struct WooShippingLabelData: Decodable, Equatable {
98106
}
99107

100108
public extension WooShippingLabelData {
101-
typealias WooShippingLabelDestinations = [String: WooShippingAddress]
109+
typealias WooShippingLabelAddressMap = [String: WooShippingAddress]
102110

103111
struct StoredData: Decodable, Equatable {
104-
let selectedDestinations: WooShippingLabelDestinations?
112+
let selectedDestinations: WooShippingLabelAddressMap?
113+
let selectedOrigins: WooShippingLabelAddressMap?
105114

106115
public enum CodingKeys: String, CodingKey {
107116
case selectedDestination = "selected_destination"
117+
case selectedOrigin = "selected_origin"
108118
}
109119

110120
public init(from decoder: any Decoder) throws {
111121
let container = try decoder.container(keyedBy: CodingKeys.self)
112-
self.selectedDestinations = try? container.decodeIfPresent(
113-
WooShippingLabelDestinations.self,
122+
selectedDestinations = try? container.decodeIfPresent(
123+
WooShippingLabelAddressMap.self,
114124
forKey: CodingKeys.selectedDestination
115125
)
126+
selectedOrigins = try? container.decodeIfPresent(
127+
WooShippingLabelAddressMap.self,
128+
forKey: CodingKeys.selectedOrigin
129+
)
116130
}
117131
}
118132
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import Foundation
22

33
extension WooShippingLabelData {
4-
static func mapDestinations(
5-
_ destinations: WooShippingLabelDestinations,
4+
static func mapAddresses(
5+
origins: WooShippingLabelAddressMap?,
6+
destinations: WooShippingLabelAddressMap?,
67
into labels: [ShippingLabel]
78
) -> [ShippingLabel] {
89
return labels.map { label in
9-
guard
10-
let shipmentID = label.shipmentID,
11-
let destinationAddress = destinations[
12-
WooShippingShipmentIDFormatter.formattedShipmentID(shipmentID)
13-
] ?? destinations[shipmentID] /// Fallback for ids previously submitted without `shipment_<id>` formatting
14-
else {
10+
guard let shipmentID = label.shipmentID else {
1511
return label
1612
}
1713

18-
return label.copy(destinationAddress: destinationAddress.toShippingLabelAddress())
14+
let formattedID = WooShippingShipmentIDFormatter.formattedShipmentID(shipmentID)
15+
let originAddress = origins?[formattedID] ?? origins?[shipmentID]
16+
let destinationAddress = destinations?[formattedID] ?? destinations?[shipmentID]
17+
18+
return label.copy(
19+
originAddress: originAddress?.toShippingLabelAddress() ?? .copy,
20+
destinationAddress: destinationAddress?.toShippingLabelAddress() ?? .copy
21+
)
1922
}
2023
}
2124
}

Modules/Tests/NetworkingTests/Remote/WooShippingRemoteTests.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,10 @@ final class WooShippingRemoteTests: XCTestCase {
857857

858858
// MARK: - Load Config With Destinations
859859

860-
func test_loadConfig_withDestinations_parses_success_response() throws {
860+
func test_load_config_with_addresses_parses_success_response() throws {
861861
// Given
862862
let remote = WooShippingRemote(network: network)
863-
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-with-destinations")
863+
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-with-addresses")
864864

865865
// When
866866
let result: Result<WooShippingConfig, Error> = waitFor { promise in
@@ -874,12 +874,14 @@ final class WooShippingRemoteTests: XCTestCase {
874874
let label = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels.first)
875875
XCTAssertNotNil(label.destinationAddress)
876876
XCTAssertEqual(label.destinationAddress.address1, "200 N SPRING ST")
877+
XCTAssertNotNil(label.originAddress)
878+
XCTAssertEqual(label.originAddress.address1, "Test origin address line")
877879
}
878880

879-
func test_loadConfig_withoutDestinations_parses_success_response() throws {
881+
func test_load_config_without_addresses_parses_success_response() throws {
880882
// Given
881883
let remote = WooShippingRemote(network: network)
882-
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-without-destinations")
884+
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-without-addresses")
883885

884886
// When
885887
let result: Result<WooShippingConfig, Error> = waitFor { promise in
@@ -892,12 +894,13 @@ final class WooShippingRemoteTests: XCTestCase {
892894
let config = try XCTUnwrap(result.get())
893895
let label = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels.first)
894896
XCTAssertTrue(label.destinationAddress.isEmpty)
897+
XCTAssertTrue(label.originAddress.isEmpty)
895898
}
896899

897-
func test_loadConfig_withInvalidDestinations_parses_success_response() throws {
900+
func test_load_config_with_invalid_addresses_parses_success_response() throws {
898901
// Given
899902
let remote = WooShippingRemote(network: network)
900-
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-with-invalid-destinations")
903+
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-with-invalid-addresses")
901904

902905
// When
903906
let result: Result<WooShippingConfig, Error> = waitFor { promise in
@@ -910,6 +913,7 @@ final class WooShippingRemoteTests: XCTestCase {
910913
let config = try XCTUnwrap(result.get())
911914
let label = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels.first)
912915
XCTAssertTrue(label.destinationAddress.isEmpty)
916+
XCTAssertTrue(label.originAddress.isEmpty)
913917
}
914918

915919
// MARK: Update shipment

Modules/Tests/NetworkingTests/Responses/wooshipping-config-with-destinations.json renamed to Modules/Tests/NetworkingTests/Responses/wooshipping-config-with-addresses.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
"postcode": "90012-4801",
1212
"country": "US"
1313
}
14+
},
15+
"selected_origin": {
16+
"shipment_1": {
17+
"address_1": "Test origin address line",
18+
"city": "Test origin city",
19+
"state": "CA",
20+
"postcode": "90012-4801",
21+
"country": "US"
22+
}
1423
}
1524
},
1625
"currentOrderLabels": [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
{
88
"address_1": "200 N SPRING ST"
99
}
10+
],
11+
"selected_origin": [
12+
{
13+
"address_1": "Test origin address line"
14+
}
1015
]
1116
},
1217
"currentOrderLabels": [

RELEASE-NOTES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
22.7
99
-----
10-
- [*] Shipping Labels: Fixed an issue where the destination address was missing for previously purchased shipping labels. [https://github.com/woocommerce/woocommerce-ios/pull/15866]
10+
- [*] Shipping Labels: Fixed an issue where the origin and destination addresses were missing for previously purchased shipping labels. [https://github.com/woocommerce/woocommerce-ios/pull/15866]
1111
- [*] Shipping Labels: Fixed an issue where the purchase button would display a stale price after changing the origin address. [https://github.com/woocommerce/woocommerce-ios/pull/15795]
1212
- [*] Order Details: Fix crash when reloading data [https://github.com/woocommerce/woocommerce-ios/pull/15764]
1313
- [*] Shipping Labels: Improved shipment management UI by hiding remove/merge options instead of disabling them, hiding merge option for orders with 2 or fewer unfulfilled shipments, and hiding the ellipsis menu when no remove/merge actions are available [https://github.com/woocommerce/woocommerce-ios/pull/15760]

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,15 @@ private extension WooShippingCreateLabelsView {
374374
Text(Localization.BottomSheet.shipFrom)
375375
.trackSize(size: $shipmentDetailsShipFromSize)
376376

377-
if viewModel.canViewLabel,
378-
let addressLines = viewModel.originAddressLines {
379-
AddressLinesView(addressLines: addressLines)
380-
.frame(maxWidth: .infinity, alignment: .leading)
377+
if viewModel.canViewLabel {
378+
Group {
379+
if let addressLines = viewModel.originAddressLines {
380+
AddressLinesView(addressLines: addressLines)
381+
} else {
382+
Spacer()
383+
}
384+
}
385+
.frame(maxWidth: .infinity, alignment: .leading)
381386
} else {
382387
VStack(alignment: .leading) {
383388
Button {

0 commit comments

Comments
 (0)