Skip to content

Commit 63a869c

Browse files
committed
Simplify shipping label upsertion
1 parent a887e6e commit 63a869c

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

Yosemite/Yosemite/Stores/WooShippingStore.swift

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private extension WooShippingStore {
222222
guard let self else { return }
223223
switch result {
224224
case .success(let label):
225-
upsertShippingLabelInBackground(shippingLabel: label) {
225+
upsertShippingLabelRefundInBackground(shippingLabel: label) {
226226
completion(.success(label))
227227
}
228228
case .failure(let error):
@@ -548,38 +548,28 @@ private extension WooShippingStore {
548548
storageSavedPackage.package = predefinedPackage
549549
}
550550

551-
/// Updates/inserts the specified shipping label *in a background thread*.
551+
/// Updates the specified shipping label with the given refund *in a background thread*.
552552
/// `onCompletion` will be called on the main thread!
553-
func upsertShippingLabelInBackground(shippingLabel: ShippingLabel,
554-
onCompletion: @escaping () -> Void) {
555-
storageManager.performAndSave ({ [weak self] storage in
556-
guard let self else { return }
557-
guard let storageOrder = storage.loadOrder(siteID: shippingLabel.siteID, orderID: shippingLabel.orderID) else {
558-
return
559-
}
553+
func upsertShippingLabelRefundInBackground(shippingLabel: ShippingLabel,
554+
onCompletion: @escaping () -> Void) {
555+
storageManager.performAndSave ({ storage in
560556
let storageShippingLabel = storage.loadShippingLabel(siteID: shippingLabel.siteID,
561557
orderID: shippingLabel.orderID,
562-
shippingLabelID: shippingLabel.shippingLabelID) ??
563-
storage.insertNewObject(ofType: Storage.ShippingLabel.self)
564-
storageShippingLabel.update(with: shippingLabel)
565-
storageShippingLabel.order = storageOrder
558+
shippingLabelID: shippingLabel.shippingLabelID)
559+
guard let storageShippingLabel else {
560+
return
561+
}
566562

567-
update(shippingLabel: storageShippingLabel, withRefund: shippingLabel.refund, using: storage)
563+
if let refund = shippingLabel.refund {
564+
let storageRefund = storageShippingLabel.refund ?? storage.insertNewObject(ofType: Storage.ShippingLabelRefund.self)
565+
storageRefund.update(with: refund)
566+
storageShippingLabel.refund = storageRefund
567+
} else {
568+
storageShippingLabel.refund = nil
569+
}
568570

569571
}, completion: onCompletion, on: .main)
570572
}
571-
572-
func update(shippingLabel storageShippingLabel: StorageShippingLabel,
573-
withRefund refund: ShippingLabelRefund?,
574-
using storage: StorageType) {
575-
if let refund {
576-
let storageRefund = storageShippingLabel.refund ?? storage.insertNewObject(ofType: Storage.ShippingLabelRefund.self)
577-
storageRefund.update(with: refund)
578-
storageShippingLabel.refund = storageRefund
579-
} else {
580-
storageShippingLabel.refund = nil
581-
}
582-
}
583573
}
584574

585575
/// Represents errors that can be returned when purchasing a shipping label

Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ final class WooShippingStoreTests: XCTestCase {
967967

968968
// Inserts a shipping label without a refund.
969969
insertShippingLabel(shippingLabel)
970-
insertOrder(siteID: sampleSiteID, orderID: sampleOrderID)
971970

972971
XCTAssertEqual(viewStorage.countObjects(ofType: StorageShippingLabel.self), 1)
973972
XCTAssertEqual(viewStorage.countObjects(ofType: StorageShippingLabelRefund.self), 0)
@@ -1079,13 +1078,6 @@ private extension WooShippingStoreTests {
10791078
groupId: "")])])
10801079
}
10811080

1082-
func insertOrder(siteID: Int64, orderID: Int64) {
1083-
let order = viewStorage.insertNewObject(ofType: StorageOrder.self)
1084-
order.siteID = siteID
1085-
order.orderID = orderID
1086-
order.statusKey = ""
1087-
}
1088-
10891081
func insertShippingLabel(_ readOnlyShippingLabel: Yosemite.ShippingLabel) {
10901082
let shippingLabel = viewStorage.insertNewObject(ofType: StorageShippingLabel.self)
10911083
shippingLabel.update(with: readOnlyShippingLabel)

0 commit comments

Comments
 (0)