Skip to content
Merged
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
13 changes: 13 additions & 0 deletions Modules/Sources/Fakes/Networking.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,19 @@ extension Networking.WooShippingSelectedRate {
)
}
}
extension Networking.WooShippingShipment {
/// Returns a "ready to use" type filled with fake values.
///
public static func fake() -> Networking.WooShippingShipment {
.init(
siteID: .fake(),
orderID: .fake(),
index: .fake(),
items: .fake(),
shippingLabel: .fake()
)
}
}
extension Networking.WooShippingShipmentItem {
/// Returns a "ready to use" type filled with fake values.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3481,7 +3481,7 @@ extension Networking.WooShippingAddress {
extension Networking.WooShippingConfig {
public func copy(
siteID: CopiableProp<Int64> = .copy,
shipments: CopiableProp<[String: [WooShippingShipmentItem]]> = .copy,
shipments: CopiableProp<[WooShippingShipment]> = .copy,
shippingLabelData: NullableCopiableProp<WooShippingLabelData> = .copy
) -> Networking.WooShippingConfig {
let siteID = siteID ?? self.siteID
Expand Down Expand Up @@ -3724,6 +3724,30 @@ extension Networking.WooShippingPackagesResponse {
}
}

extension Networking.WooShippingShipment {
public func copy(
siteID: CopiableProp<Int64> = .copy,
orderID: CopiableProp<Int64> = .copy,
index: CopiableProp<String> = .copy,
items: CopiableProp<[WooShippingShipmentItem]> = .copy,
shippingLabel: NullableCopiableProp<ShippingLabel> = .copy
) -> Networking.WooShippingShipment {
let siteID = siteID ?? self.siteID
let orderID = orderID ?? self.orderID
let index = index ?? self.index
let items = items ?? self.items
let shippingLabel = shippingLabel ?? self.shippingLabel

return Networking.WooShippingShipment(
siteID: siteID,
orderID: orderID,
index: index,
items: items,
shippingLabel: shippingLabel
)
}
}

extension Networking.WooShippingShipmentItem {
public func copy(
id: CopiableProp<Int64> = .copy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public struct WooShippingConfig: Decodable, Equatable, GeneratedFakeable, Genera
/// The remote ID of the site that owns this shipping label config info.
public let siteID: Int64

/// Shipments of this order. The keys are the ids of the shipment.
public let shipments: WooShippingShipments
/// Shipments of this order.
public let shipments: [WooShippingShipment]

/// Holds info about the shipping labels
public let shippingLabelData: WooShippingLabelData?

public init(siteID: Int64,
shipments: WooShippingShipments,
shipments: [WooShippingShipment],
shippingLabelData: WooShippingLabelData?) {
self.siteID = siteID
self.shipments = shipments
Expand All @@ -39,17 +39,46 @@ public struct WooShippingConfig: Decodable, Equatable, GeneratedFakeable, Genera
throw WooShippingConfigDecodingError.missingSiteID
}

guard let orderID = decoder.userInfo[.orderID] as? Int64 else {
throw WooShippingConfigDecodingError.missingOrderID
}

let container = try decoder.container(keyedBy: CodingKeys.self)
let shipments: WooShippingShipments = {
let shippingLabelData = try container.decodeIfPresent(WooShippingLabelData.self, forKey: .shippingLabelData)

let shipments: [WooShippingShipment] = {
guard let shipmentsString = try? container.decodeIfPresent(String.self, forKey: .shipments),
let data = shipmentsString.data(using: .utf8) else {
return [:]
return []
}

return (try? JSONDecoder().decode(WooShippingShipments.self, from: data)) ?? [:]
guard let contents = (try? JSONDecoder().decode(WooShippingShipments.self, from: data)) else {
return []
}

let labels = shippingLabelData?.currentOrderLabels ?? []
var shipments = [WooShippingShipment]()
for (index, items) in contents {
let label: ShippingLabel? = {
let purchasedLabels = labels.filter {
$0.shipmentID == index && $0.status == .purchased
}
let sortedLabels = purchasedLabels.sorted { $0.dateCreated > $1.dateCreated }
if let completedLabel = sortedLabels.first(where: { $0.refund == nil }) {
return completedLabel
} else {
return sortedLabels.first
}
}()
shipments.append(WooShippingShipment(siteID: siteID,
orderID: orderID,
index: index,
items: items,
shippingLabel: label))
}
return shipments
}()

let shippingLabelData = try container.decodeIfPresent(WooShippingLabelData.self, forKey: .shippingLabelData)
self.init(siteID: siteID,
shipments: shipments,
shippingLabelData: shippingLabelData)
Expand Down Expand Up @@ -79,4 +108,5 @@ public struct WooShippingLabelData: Decodable, Equatable {
//
enum WooShippingConfigDecodingError: Error {
case missingSiteID
case missingOrderID
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
import Foundation
import Codegen
import WooFoundation

/// Represents a shipment in Shipping Labels for the WooCommerce Shipping extension.
/// Represents a shipment from the WooCommerce Shipping extension.
///
public struct WooShippingShipment: Equatable, GeneratedFakeable, GeneratedCopiable {
/// ID of the site that the shipment belongs to.
public let siteID: Int64

/// ID of the order that the shipment belongs to.
public let orderID: Int64

/// Index of the shipment.
/// The expected format is a numeric string, e.g: "0", "1", etc.
public let index: String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment clarifying the index format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in cc35a03.


/// Contents of the shipment
public let items: [WooShippingShipmentItem]

/// The latest label purchased for the shipment
public let shippingLabel: ShippingLabel?

public init(siteID: Int64,
orderID: Int64,
index: String,
items: [WooShippingShipmentItem],
shippingLabel: ShippingLabel?) {
self.siteID = siteID
self.orderID = orderID
self.index = index
self.items = items
self.shippingLabel = shippingLabel
}
}

/// Represents a shipment item from the WooCommerce Shipping extension.
///
public struct WooShippingShipmentItem: Codable, Equatable, GeneratedFakeable, GeneratedCopiable {
/// ID of the shipment
Expand Down
1 change: 1 addition & 0 deletions Modules/Sources/Yosemite/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public typealias WooShippingDestinationAddressUpdate = Networking.WooShippingDes
public typealias WooShippingConfig = Networking.WooShippingConfig
public typealias WooShippingUpdateShipment = Networking.WooShippingUpdateShipment
public typealias WooShippingShipmentItem = Networking.WooShippingShipmentItem
public typealias WooShippingShipment = Networking.WooShippingShipment
public typealias WooShippingShipments = Networking.WooShippingShipments
public typealias WooShippingSelectedRate = Networking.WooShippingSelectedRate
public typealias WPComPlan = Networking.WPComPlan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ final class WooShippingConfigMapperTests: XCTestCase {
}

XCTAssertEqual(config.shipments.count, 3)
let shipment0 = try XCTUnwrap(config.shipments.first(where: { $0.index == "0" }))
XCTAssertNil(shipment0.shippingLabel) // non-purchased status means no label found
XCTAssertEqual(shipment0.items.count, 1)

let shipment1 = try XCTUnwrap(config.shipments.first(where: { $0.index == "1" }))
XCTAssertEqual(shipment1.shippingLabel?.shippingLabelID, 4871)
XCTAssertEqual(shipment1.items.count, 1)

let shipment2 = try XCTUnwrap(config.shipments.first(where: { $0.index == "2" }))
XCTAssertEqual(shipment2.shippingLabel?.shippingLabelID, 4873)
XCTAssertNotNil(shipment2.shippingLabel?.refund)
XCTAssertEqual(shipment2.items.count, 1)

let shippingLabelData = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels)
XCTAssertEqual(shippingLabelData.count, 1)
XCTAssertEqual(shippingLabelData.count, 3)
XCTAssertEqual(shippingLabelData.first?.shipmentID, "1")
XCTAssertEqual(shippingLabelData.first?.shippingLabelID, 4871)
}
Expand All @@ -28,8 +41,22 @@ final class WooShippingConfigMapperTests: XCTestCase {
}

XCTAssertEqual(config.shipments.count, 3)
let shipment0 = try XCTUnwrap(config.shipments.first(where: { $0.index == "0" }))
XCTAssertNil(shipment0.shippingLabel) // non-purchased status means no label found
XCTAssertEqual(shipment0.items.count, 1)

let shipment1 = try XCTUnwrap(config.shipments.first(where: { $0.index == "1" }))
XCTAssertEqual(shipment1.shippingLabel?.shippingLabelID, 4871)
XCTAssertNil(shipment1.shippingLabel?.refund)
XCTAssertEqual(shipment1.items.count, 1)

let shipment2 = try XCTUnwrap(config.shipments.first(where: { $0.index == "2" }))
XCTAssertEqual(shipment2.shippingLabel?.shippingLabelID, 4873)
XCTAssertNotNil(shipment2.shippingLabel?.refund)
XCTAssertEqual(shipment2.items.count, 1)

let shippingLabelData = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels)
XCTAssertEqual(shippingLabelData.count, 1)
XCTAssertEqual(shippingLabelData.count, 3)
XCTAssertEqual(shippingLabelData.first?.shipmentID, "1")
XCTAssertEqual(shippingLabelData.first?.shippingLabelID, 4871)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASE_IN_PROGRESS",
"status": "PURCHASED",
"commercial_invoice_url": "",
"is_commercial_invoice_submitted_electronically": false,
"package_name": "Small Flat Rate Box",
Expand All @@ -23,6 +23,55 @@
"id": "1",
"receipt_item_id": -1,
"created_date": 1742292110000
},
{
"label_id": 4872,
"tracking": null,
"refundable_amount": 0,
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASE_ERROR",
"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": "0",
"receipt_item_id": -1,
"created_date": 1742292110000
},
{
"label_id": 4873,
"tracking": null,
"refundable_amount": 0,
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASED",
"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
],
"refund": {
"request_date": 1603715617000,
"status": "pending",
"refund_date": 1
},
"id": "2",
"receipt_item_id": -1,
"created_date": 1742292110000
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASE_IN_PROGRESS",
"status": "PURCHASED",
"commercial_invoice_url": "",
"is_commercial_invoice_submitted_electronically": false,
"package_name": "Small Flat Rate Box",
Expand All @@ -24,6 +24,55 @@
"id": "1",
"receipt_item_id": -1,
"created_date": 1742292110000
},
{
"label_id": 4872,
"tracking": null,
"refundable_amount": 0,
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASE_ERROR",
"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": "0",
"receipt_item_id": -1,
"created_date": 1742292110000
},
{
"label_id": 4873,
"tracking": null,
"refundable_amount": 0,
"created": 1742292110723,
"carrier_id": "usps",
"service_name": "USPS - Priority Mail",
"status": "PURCHASED",
"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
],
"refund": {
"request_date": 1603715617000,
"status": "pending",
"refund_date": 1
},
"id": "2",
"receipt_item_id": -1,
"created_date": 1742292110000
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ final class WooShippingStoreTests: XCTestCase {
func test_loadConfig_returns_success_response() throws {
// Given
let remote = MockWooShippingRemote()
let expectedConfig = WooShippingConfig.fake().copy(shipments: ["0": [WooShippingShipmentItem.fake()]])
let expectedConfig = WooShippingConfig.fake().copy(shipments: [WooShippingShipment.fake()])
remote.whenLoadingConfig(siteID: sampleSiteID, thenReturn: .success(expectedConfig))
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

Expand Down Expand Up @@ -1084,7 +1084,7 @@ final class WooShippingStoreTests: XCTestCase {
expiryDate: nil)
}()
let expectedResponse = WooShippingConfig.fake().copy(
shipments: ["0": [WooShippingShipmentItem.fake()]],
shipments: [WooShippingShipment.fake()],
shippingLabelData: WooShippingLabelData(currentOrderLabels: [expectedShippingLabel])
)
remote.whenLoadingConfig(siteID: sampleSiteID, thenReturn: .success(expectedResponse))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,6 @@ extension OrderDetailsDataSource {
}
}


// MARK: - Constants
extension OrderDetailsDataSource {
enum Localization {
Expand Down
Loading