Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public struct WooShippingLabelData: Decodable, Equatable {

public extension WooShippingLabelData {
typealias WooShippingLabelAddressMap = [String: WooShippingAddress]
typealias WooShippingLabelAddressArray = [WooShippingAddress]

struct StoredData: Decodable, Equatable {
let selectedDestinations: WooShippingLabelAddressMap?
Expand All @@ -119,15 +120,38 @@ public extension WooShippingLabelData {

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
selectedDestinations = try? container.decodeIfPresent(
WooShippingLabelAddressMap.self,
forKey: CodingKeys.selectedDestination
selectedDestinations = Self.decodeAddresses(
from: container,
for: .selectedDestination
)
selectedOrigins = try? container.decodeIfPresent(
WooShippingLabelAddressMap.self,
forKey: CodingKeys.selectedOrigin

selectedOrigins = Self.decodeAddresses(
from: container,
for: .selectedOrigin
)
}

private static func decodeAddresses(
from container: KeyedDecodingContainer<CodingKeys>,
for key: CodingKeys
) -> WooShippingLabelAddressMap? {
if let addressesMap = try? container.decodeIfPresent(
WooShippingLabelAddressMap.self,
forKey: key
) {
return addressesMap
} else if let addressesArray = try? container.decodeIfPresent(
WooShippingLabelAddressArray.self,
forKey: key
) {
return addressesArray.enumerated().reduce(into: [:]) { result, address in
let formattedIDKey = WooShippingLabelData.formattedShipmentIDFromArrayIndex(address.offset)
result[formattedIDKey] = address.element
}
}

return nil
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ extension WooShippingLabelData {
)
}
}

static func formattedShipmentIDFromArrayIndex(_ index: Int) -> String {
/// ID indexing starts from `1`. So the ID would be the index incremented by `1`.
return WooShippingShipmentIDFormatter.formattedShipmentID(
String(index + 1)
)
}
}
25 changes: 23 additions & 2 deletions Modules/Tests/NetworkingTests/Remote/WooShippingRemoteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,9 @@ final class WooShippingRemoteTests: XCTestCase {
XCTAssertNotNil(result.failure)
}

// MARK: - Load Config With Destinations
// MARK: - Load Config With Addresses

func test_load_config_with_addresses_parses_success_response() throws {
func test_load_config_with_addresses_map_parses_success_response() throws {
// Given
let remote = WooShippingRemote(network: network)
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-with-addresses")
Expand All @@ -878,6 +878,27 @@ final class WooShippingRemoteTests: XCTestCase {
XCTAssertEqual(label.originAddress.address1, "Test origin address line")
}

func test_load_config_with_addresses_array_parses_success_response() throws {
// Given
let remote = WooShippingRemote(network: network)
network.simulateResponse(requestUrlSuffix: "config/label-purchase/\(sampleOrderID)", filename: "wooshipping-config-with-addresses-array")

// When
let result: Result<WooShippingConfig, Error> = waitFor { promise in
remote.loadConfig(siteID: self.sampleSiteID, orderID: self.sampleOrderID) { result in
promise(result)
}
}

// Then
let config = try XCTUnwrap(result.get())
let label = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels.first)
XCTAssertNotNil(label.destinationAddress)
XCTAssertEqual(label.destinationAddress.address1, "200 N SPRING ST")
XCTAssertNotNil(label.originAddress)
XCTAssertEqual(label.originAddress.address1, "Test origin address line")
}

func test_load_config_without_addresses_parses_success_response() throws {
// Given
let remote = WooShippingRemote(network: network)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"data": {
"config": {
"shippingLabelData": {
"storedData": {
"selected_destination": [
{
"address_1": "200 N SPRING ST",
"city": "LOS ANGELES",
"state": "CA",
"postcode": "90012-4801",
"country": "US"
}
],
"selected_origin": [
{
"address_1": "Test origin address line",
"city": "Test origin city",
"state": "CA",
"postcode": "90012-4801",
"country": "US"
}
]
},
"currentOrderLabels": [
{
"label_id": 4871,
"tracking": null,
"refundable_amount": 0,
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASE_IN_PROGRESS",
"commercial_invoice_url": "",
"is_commercial_invoice_submitted_electronically": false,
"package_name": "Small Flat Rate Box",
"is_letter": false,
"product_names": [
"BG upload"
],
"product_ids": [
522
],
"id": "1",
"receipt_item_id": -1,
"created_date": 1742292110000
}
]
}
}
}
}
Loading