@@ -84,8 +84,8 @@ public final class WooShippingStore: Store {
84
84
updateDestinationAddress ( siteID: siteID, orderID: orderID, address: address, completion: completion)
85
85
case let . loadConfig( siteID, orderID, completion) :
86
86
loadConfig ( siteID: siteID, orderID: orderID, completion: completion)
87
- case let . syncShippingLabels ( siteID, orderID, completion) :
88
- syncShippingLabels ( siteID: siteID, orderID: orderID, completion: completion)
87
+ case let . syncShipments ( siteID, orderID, completion) :
88
+ syncShipments ( siteID: siteID, orderID: orderID, completion: completion)
89
89
case let . updateShipment( siteID, orderID, shipmentToUpdate, completion) :
90
90
updateShipment ( siteID: siteID,
91
91
orderID: orderID,
@@ -290,26 +290,23 @@ private extension WooShippingStore {
290
290
remote. acceptUPSTermsOfService ( siteID: siteID, originAddress: originAddress, completion: completion)
291
291
}
292
292
293
- func syncShippingLabels ( siteID: Int64 ,
294
- orderID: Int64 ,
295
- completion: @escaping ( Result < [ ShippingLabel ] , Error > ) -> Void ) {
296
- remote. loadConfig ( siteID: siteID, orderID: orderID, completion : { [ weak self] result in
293
+ func syncShipments ( siteID: Int64 ,
294
+ orderID: Int64 ,
295
+ completion: @escaping ( Result < [ WooShippingShipment ] , Error > ) -> Void ) {
296
+ remote. loadConfig ( siteID: siteID, orderID: orderID) { [ weak self] result in
297
297
guard let self else { return }
298
-
299
298
switch result {
300
299
case . failure( let error) :
301
300
completion ( . failure( error) )
302
301
case . success( let config) :
303
- guard let labels = config. shippingLabelData? . currentOrderLabels else {
304
- return completion ( . success( [ ] ) )
305
- }
306
- upsertShippingLabelsInBackground ( siteID: siteID,
307
- orderID: orderID,
308
- shippingLabels: labels) {
309
- completion ( . success( labels) )
302
+ let shipments = config. shipments
303
+ upsertShipmentsInBackground ( siteID: siteID,
304
+ orderID: orderID,
305
+ shipments: shipments) {
306
+ completion ( . success( shipments) )
310
307
}
311
308
}
312
- } )
309
+ }
313
310
}
314
311
}
315
312
@@ -679,35 +676,60 @@ private extension WooShippingStore {
679
676
} , completion: nil , on: . main)
680
677
}
681
678
682
- /// Updates/inserts the specified readonly shipping label entities *in a background thread*.
679
+ /// Updates/inserts the specified readonly shipments entities *in a background thread*.
683
680
/// `onCompletion` will be called on the main thread!
684
- func upsertShippingLabelsInBackground( siteID: Int64 ,
685
- orderID: Int64 ,
686
- shippingLabels: [ ShippingLabel ] ,
687
- onCompletion: @escaping ( ) -> Void ) {
688
- if shippingLabels. isEmpty {
689
- return onCompletion ( )
690
- }
691
-
681
+ func upsertShipmentsInBackground( siteID: Int64 ,
682
+ orderID: Int64 ,
683
+ shipments: [ WooShippingShipment ] ,
684
+ onCompletion: @escaping ( ) -> Void ) {
692
685
storageManager. performAndSave ( { [ weak self] storage in
693
686
guard let self else { return }
694
687
guard let order = storage. loadOrder ( siteID: siteID, orderID: orderID) else {
695
688
return
696
689
}
697
- upsertShippingLabels ( siteID: siteID, orderID: orderID, shippingLabels: shippingLabels, storageOrder: order, using: storage)
690
+ upsertShipments ( siteID: siteID,
691
+ orderID: orderID,
692
+ shipments: shipments,
693
+ storageOrder: order,
694
+ using: storage)
698
695
} , completion: onCompletion, on: . main)
699
696
}
700
697
701
- /// Updates/inserts the specified readonly ShippingLabel entities in the current thread.
702
- func upsertShippingLabels( siteID: Int64 ,
703
- orderID: Int64 ,
704
- shippingLabels: [ ShippingLabel ] ,
705
- storageOrder: StorageOrder ,
706
- using storage: StorageType ) {
707
- let storedLabels = storage. loadAllShippingLabels ( siteID: siteID, orderID: orderID)
708
- for shippingLabel in shippingLabels {
709
- let storageShippingLabel = storedLabels. first ( where: { $0. shippingLabelID == shippingLabel. shippingLabelID } ) ??
710
- storage. insertNewObject ( ofType: Storage . ShippingLabel. self)
698
+ /// Updates/inserts the specified readonly WooShippingShipments entities in the current thread.
699
+ func upsertShipments( siteID: Int64 ,
700
+ orderID: Int64 ,
701
+ shipments: [ WooShippingShipment ] ,
702
+ storageOrder: StorageOrder ,
703
+ using storage: StorageType ) {
704
+ let storedShipments = storage. loadAllShipments ( siteID: siteID, orderID: orderID)
705
+ for shipment in shipments {
706
+ let storageShipment = storedShipments. first ( where: { $0. index == shipment. index } ) ??
707
+ storage. insertNewObject ( ofType: Storage . WooShippingShipment. self)
708
+ storageShipment. update ( with: shipment)
709
+ storageShipment. order = storageOrder
710
+
711
+ handleShipmentItems ( shipment, storageShipment, storage)
712
+ update ( storageShipment: storageShipment,
713
+ storageOrder: storageOrder,
714
+ shippingLabel: shipment. shippingLabel,
715
+ using: storage)
716
+ }
717
+
718
+ // Now, remove any objects that exist in storage but not in shipments
719
+ let shipmentIndices = shipments. map ( \. index)
720
+ storedShipments. filter {
721
+ !shipmentIndices. contains ( $0. index)
722
+ } . forEach {
723
+ storage. deleteObject ( $0)
724
+ }
725
+ }
726
+
727
+ func update( storageShipment: StorageWooShippingShipment ,
728
+ storageOrder: StorageOrder ,
729
+ shippingLabel: ShippingLabel ? ,
730
+ using storage: StorageType ) {
731
+ if let shippingLabel {
732
+ let storageShippingLabel = storageShipment. shippingLabel ?? storage. insertNewObject ( ofType: Storage . ShippingLabel. self)
711
733
storageShippingLabel. update ( with: shippingLabel)
712
734
storageShippingLabel. order = storageOrder
713
735
@@ -720,14 +742,39 @@ private extension WooShippingStore {
720
742
let destinationAddress = storageShippingLabel. destinationAddress ?? storage. insertNewObject ( ofType: Storage . ShippingLabelAddress. self)
721
743
destinationAddress. update ( with: shippingLabel. destinationAddress)
722
744
storageShippingLabel. destinationAddress = destinationAddress
745
+
746
+ /// Set the shipping label to the shipment's relationship
747
+ storageShipment. shippingLabel = storageShippingLabel
748
+ } else {
749
+ storageShipment. shippingLabel = nil
723
750
}
751
+ }
724
752
725
- // Now, remove any objects that exist in storage but not in shippingLabels
726
- let shippingLabelIDs = shippingLabels. map ( \. shippingLabelID)
727
- storedLabels. filter {
728
- !shippingLabelIDs. contains ( $0. shippingLabelID)
729
- } . forEach {
730
- storage. deleteObject ( $0)
753
+ /// Updates, inserts, or prunes the provided StorageWooShippingShipment's items using the provided read-only WooShippingShipment's items
754
+ ///
755
+ private func handleShipmentItems( _ readOnlyShipment: Networking . WooShippingShipment ,
756
+ _ storageShipment: Storage . WooShippingShipment ,
757
+ _ storage: StorageType ) {
758
+
759
+ let storageItemsArray = Array ( storageShipment. items ?? [ ] )
760
+
761
+ // Upsert the items from the read-only shipment
762
+ for readOnlyItem in readOnlyShipment. items {
763
+ if let existingStorageItem = storageItemsArray. first ( where: { $0. id == readOnlyItem. id } ) {
764
+ existingStorageItem. update ( with: readOnlyItem)
765
+ } else {
766
+ let newStorageItem = storage. insertNewObject ( ofType: Storage . WooShippingShipmentItem. self)
767
+ newStorageItem. update ( with: readOnlyItem)
768
+ storageShipment. addToItems ( newStorageItem)
769
+ }
770
+ }
771
+
772
+ // Now, remove any objects that exist in storageShipment.items but not in readOnlyShipment.items
773
+ storageItemsArray. forEach { storageItem in
774
+ if readOnlyShipment. items. first ( where: { $0. id == storageItem. id } ) == nil {
775
+ storageShipment. removeFromItems ( storageItem)
776
+ storage. deleteObject ( storageItem)
777
+ }
731
778
}
732
779
}
733
780
0 commit comments