Skip to content

Shipping Labels: Show shipment details on order details screen #15889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a320b96
Register hosting cell for shipment details
itsmeichigo Jul 9, 2025
04be29a
Make refund completion handler reusable
itsmeichigo Jul 9, 2025
b2de8cd
Display new cells for woo shipping shipments
itsmeichigo Jul 9, 2025
2bb36e4
Add cancel button for customs form printing view
itsmeichigo Jul 9, 2025
b266b5b
Update title mode for customs form printing view
itsmeichigo Jul 9, 2025
a1370da
Navigate to the correct shipment when tapping Create shipping labels
itsmeichigo Jul 10, 2025
4069d95
Register hosting cell for shipment details
itsmeichigo Jul 9, 2025
e016d65
Make refund completion handler reusable
itsmeichigo Jul 9, 2025
53a1dad
Display new cells for woo shipping shipments
itsmeichigo Jul 9, 2025
2a985b7
Add cancel button for customs form printing view
itsmeichigo Jul 9, 2025
574cbf0
Update title mode for customs form printing view
itsmeichigo Jul 9, 2025
1229d89
Navigate to the correct shipment when tapping Create shipping labels
itsmeichigo Jul 10, 2025
8a62624
Fix build failures
itsmeichigo Jul 14, 2025
83bcf79
Merge branch 'woomob-754-ui-updates' of github.com:woocommerce/woocom…
itsmeichigo Jul 14, 2025
d288124
Hide label creation CTA if shipments are available
itsmeichigo Jul 14, 2025
a71288c
Show checkmark on fulfilled shipments
itsmeichigo Jul 14, 2025
2e62051
Ensure UI is updated after requesting refund
itsmeichigo Jul 14, 2025
7febdf6
Update shipment and order after purchasing label
itsmeichigo Jul 14, 2025
99df170
Remove modals from shipment cells
itsmeichigo Jul 14, 2025
05a5fb4
Remove unused variable
itsmeichigo Jul 15, 2025
031e6fc
Simplify layout for shipment details cell
itsmeichigo Jul 15, 2025
0c31b47
Handle refund action
itsmeichigo Jul 15, 2025
5453538
Handle printing customs form
itsmeichigo Jul 15, 2025
3dcfee8
Handle viewing shipment items
itsmeichigo Jul 15, 2025
90aecfa
Remove unused itemsDataSource
itsmeichigo Jul 15, 2025
9aa0612
Merge branch 'trunk' into woomob-754-ui-updates
itsmeichigo Jul 15, 2025
fc36d4f
Update background color for refund text
itsmeichigo Jul 15, 2025
c1d02d8
Upsert shipments after updating shipments
itsmeichigo Jul 15, 2025
5994c8e
Update unit tests for WooShippingStore to verify persistence logic
itsmeichigo Jul 15, 2025
5200d88
Update release notes
itsmeichigo Jul 15, 2025
fb0d3d0
Add test for checking shipment section
itsmeichigo Jul 15, 2025
7414f16
Fix incorrect section category
itsmeichigo Jul 16, 2025
6fa79d4
Update preselection logic for create label form to avoid ambiguity
itsmeichigo Jul 16, 2025
504e7b3
Merge branch 'trunk' into woomob-754-ui-updates
itsmeichigo Jul 16, 2025
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 @@ -49,3 +49,10 @@ public struct WooShippingShipmentItem: Codable, Equatable, GeneratedFakeable, Ge
}

public typealias WooShippingShipments = [String: [WooShippingShipmentItem]]

public extension WooShippingShipmentItem {
var quantity: Decimal {
guard let subItems else { return 0 }
return subItems.count > 0 ? Decimal(subItems.count) : 1
}
}
52 changes: 50 additions & 2 deletions Modules/Sources/Yosemite/Stores/WooShippingStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,12 @@ private extension WooShippingStore {
// If label has PURCHASED status, stop polling
if labelStatusResponse.status == .purchased,
let label = labelStatusResponse.getPurchasedLabel() {
completion(.success(label))
guard let self else {
return completion(.success(label))
}
insertPurchasedLabelInBackground(siteID: siteID, orderID: orderID, shippingLabel: label) {
completion(.success(label))
}
}

// If label has PURCHASE_ERROR status, return error and stop polling
Expand Down Expand Up @@ -410,7 +415,23 @@ private extension WooShippingStore {
orderID: Int64,
shipmentToUpdate: WooShippingUpdateShipment,
completion: @escaping (Result<WooShippingShipments, Error>) -> Void) {
remote.updateShipment(siteID: siteID, orderID: orderID, shipmentToUpdate: shipmentToUpdate, completion: completion)
remote.updateShipment(siteID: siteID, orderID: orderID, shipmentToUpdate: shipmentToUpdate) { [weak self] result in
guard let self, let contents = try? result.get() else {
return completion(result)
}
let shipments = contents.map { (index, items) in
WooShippingShipment(siteID: siteID,
orderID: orderID,
index: index,
items: items,
shippingLabel: nil)
}
upsertShipmentsInBackground(siteID: siteID,
orderID: orderID,
shipments: shipments) {
completion(.success(contents))
}
}
}
}

Expand Down Expand Up @@ -644,10 +665,15 @@ private extension WooShippingStore {
DDLogWarn("⚠️ No shipping label found in storage when updating refund")
return shippingLabel.copy(refund: refund)
}
let storageShipment = storageShippingLabel.shipment

let storageRefund = storageShippingLabel.refund ?? storage.insertNewObject(ofType: Storage.ShippingLabelRefund.self)
storageRefund.update(with: refund)
storageShippingLabel.refund = storageRefund

// update stored shipment to trigger onDidChangeContent notification
storageShipment?.shippingLabel = storageShippingLabel

return storageShippingLabel.toReadOnly()

}, completion: { result in
Expand Down Expand Up @@ -676,6 +702,28 @@ private extension WooShippingStore {
}, completion: nil, on: .main)
}

/// Inserts the specified readonly shipping label entity *in a background thread*.
/// `onCompletion` will be called on the main thread!
func insertPurchasedLabelInBackground(siteID: Int64,
orderID: Int64,
shippingLabel: ShippingLabel,
onCompletion: @escaping () -> Void) {
storageManager.performAndSave({ [weak self] storage in
guard let self else { return }

let storageOrder = storage.loadOrder(siteID: siteID, orderID: orderID)
let storageShipment = storage.loadAllShipments(siteID: siteID, orderID: orderID)
.first(where: { $0.index == shippingLabel.shipmentID })

guard let storageOrder, let storageShipment else { return }

update(storageShipment: storageShipment,
storageOrder: storageOrder,
shippingLabel: shippingLabel,
using: storage)
}, completion: onCompletion, on: .main)
}

/// Updates/inserts the specified readonly shipments entities *in a background thread*.
/// `onCompletion` will be called on the main thread!
func upsertShipmentsInBackground(siteID: Int64,
Expand Down
55 changes: 47 additions & 8 deletions Modules/Tests/YosemiteTests/Stores/WooShippingStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,22 @@ final class WooShippingStoreTests: XCTestCase {

// MARK: `purchaseShippingLabel`

func test_purchaseShippingLabel_returns_shipping_label_on_success() throws {
func test_purchaseShippingLabel_returns_shipping_label_on_success_and_persists_label_in_storage() throws {
// Given
let expectedLabel = ShippingLabel.fake().copy(shippingLabelID: 13579)
let expectedLabel = ShippingLabel.fake().copy(siteID: sampleSiteID, orderID: sampleOrderID, shippingLabelID: 13579, shipmentID: "0")
let labelStatusResponse = ShippingLabelStatusPollingResponse.purchased(expectedLabel)
let remote = MockWooShippingRemote()
remote.whenPurchaseShippingLabel(siteID: sampleSiteID, thenReturn: .success([ShippingLabelPurchase.fake().copy(shippingLabelID: 13579)]))
remote.whenCheckLabelStatus(siteID: sampleSiteID, thenReturn: .success(labelStatusResponse))

let order = insertOrder(siteID: sampleSiteID, orderID: sampleOrderID)
let shipment = insertShipment(siteID: sampleSiteID, orderID: sampleOrderID, index: "0")
shipment.order = order

let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

// When
let result: Result<ShippingLabel, Error> = waitFor { promise in
let result: Result<ShippingLabel, Error> = waitFor(timeout: 10) { promise in
let action = WooShippingAction.purchaseShippingLabel(siteID: self.sampleSiteID,
orderID: self.sampleOrderID,
originAddress: .fake(),
Expand All @@ -558,6 +563,15 @@ final class WooShippingStoreTests: XCTestCase {
XCTAssertTrue(result.isSuccess)
let actualLabel = try XCTUnwrap(result.get())
XCTAssertEqual(actualLabel, expectedLabel)

// label is persisted
let storedLabels = storageManager.viewStorage.loadAllShippingLabels(siteID: sampleSiteID, orderID: sampleOrderID)
XCTAssertEqual(storedLabels.count, 1)
XCTAssertEqual(storedLabels.first?.shippingLabelID, expectedLabel.shippingLabelID)

let storedShipments = storageManager.viewStorage.loadAllShipments(siteID: sampleSiteID, orderID: sampleOrderID)
XCTAssertEqual(storedShipments.count, 1)
XCTAssertEqual(storedShipments.first?.shippingLabel?.shippingLabelID, expectedLabel.shippingLabelID)
}

func test_purchaseShippingLabel_returns_error_on_purchaseShippingLabel_request_failure() throws {
Expand Down Expand Up @@ -1037,11 +1051,13 @@ final class WooShippingStoreTests: XCTestCase {

// MARK: `updateShipment`

func test_updateShipment_returns_success_response() throws {
func test_updateShipment_returns_success_response_and_persists_shipments() throws {
// Given
let remote = MockWooShippingRemote()
let expected = ["0": [WooShippingShipmentItem.fake()]]
remote.whenUpdatingShipment(siteID: sampleSiteID, thenReturn: .success(expected))

insertOrder(siteID: sampleSiteID, orderID: sampleOrderID)
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

// When
Expand All @@ -1057,6 +1073,10 @@ final class WooShippingStoreTests: XCTestCase {
// Then
let actual = try XCTUnwrap(result.get())
XCTAssertEqual(actual, expected)

let storedShipments = storageManager.viewStorage.loadAllShipments(siteID: sampleSiteID, orderID: sampleOrderID)
XCTAssertEqual(storedShipments.count, 1)
XCTAssertEqual(storedShipments.first?.index, "0")
}

func test_updateShipment_returns_error_on_failure() throws {
Expand Down Expand Up @@ -1088,16 +1108,18 @@ final class WooShippingStoreTests: XCTestCase {
let sampleOrderID: Int64 = 134
let remote = MockWooShippingRemote()
let expectedRefund = Yosemite.ShippingLabelRefund(dateRequested: Date(), status: .pending)
let shippingLabel = MockShippingLabel.emptyLabel().copy(siteID: sampleSiteID, orderID: sampleOrderID, shippingLabelID: 123)
let shippingLabel = MockShippingLabel.emptyLabel().copy(siteID: sampleSiteID, orderID: sampleOrderID, shippingLabelID: 123, shipmentID: "0")

remote.whenRefundingShippingLabel(siteID: shippingLabel.siteID,
orderID: shippingLabel.orderID,
shippingLabelID: shippingLabel.shippingLabelID,
thenReturn: .success(expectedRefund))
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

let shipment = insertShipment(siteID: sampleSiteID, orderID: sampleOrderID, index: "0")
// Inserts a shipping label without a refund.
insertShippingLabel(shippingLabel)
let storedLabel = insertShippingLabel(shippingLabel)
shipment.shippingLabel = storedLabel

XCTAssertEqual(viewStorage.countObjects(ofType: StorageShippingLabel.self), 1)
XCTAssertEqual(viewStorage.countObjects(ofType: StorageShippingLabelRefund.self), 0)
Expand All @@ -1121,6 +1143,10 @@ final class WooShippingStoreTests: XCTestCase {

XCTAssertEqual(viewStorage.countObjects(ofType: StorageShippingLabel.self), 1)
XCTAssertEqual(viewStorage.countObjects(ofType: StorageShippingLabelRefund.self), 1)

let storedShipments = viewStorage.loadAllShipments(siteID: sampleSiteID, orderID: sampleOrderID)
XCTAssertEqual(storedShipments.first?.shippingLabel?.shippingLabelID, shippingLabel.shippingLabelID)
XCTAssertNotNil(storedShipments.first?.shippingLabel?.refund)
}

func test_refundShippingLabel_returns_error_on_failure() throws {
Expand Down Expand Up @@ -1390,15 +1416,28 @@ private extension WooShippingStoreTests {
groupId: "")])])
}

func insertShippingLabel(_ readOnlyShippingLabel: Yosemite.ShippingLabel) {
@discardableResult
func insertShippingLabel(_ readOnlyShippingLabel: Yosemite.ShippingLabel) -> StorageShippingLabel {
let shippingLabel = viewStorage.insertNewObject(ofType: StorageShippingLabel.self)
shippingLabel.update(with: readOnlyShippingLabel)
return shippingLabel
}

func insertOrder(siteID: Int64, orderID: Int64) {
@discardableResult
func insertOrder(siteID: Int64, orderID: Int64) -> StorageOrder {
let order = viewStorage.insertNewObject(ofType: StorageOrder.self)
order.siteID = siteID
order.orderID = orderID
order.statusKey = ""
return order
}

@discardableResult
func insertShipment(siteID: Int64, orderID: Int64, index: String) -> StorageWooShippingShipment {
let shipment = viewStorage.insertNewObject(ofType: StorageWooShippingShipment.self)
shipment.siteID = siteID
shipment.orderID = orderID
shipment.index = index
return shipment
}
}
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

22.9
-----
- [**] Order Details: Update Shipping Labels section for stores with Woo Shipping extension [https://github.com/woocommerce/woocommerce-ios/pull/15889]
- [*] Order List: New orders made through Point of Sale are now filterable via the Order List filters menu [https://github.com/woocommerce/woocommerce-ios/pull/15910]

22.8
Expand Down
Loading