@@ -84,8 +84,8 @@ public final class WooShippingStore: Store {
8484 updateDestinationAddress ( siteID: siteID, orderID: orderID, address: address, completion: completion)
8585 case let . loadConfig( siteID, orderID, completion) :
8686 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)
8989 case let . updateShipment( siteID, orderID, shipmentToUpdate, completion) :
9090 updateShipment ( siteID: siteID,
9191 orderID: orderID,
@@ -290,26 +290,23 @@ private extension WooShippingStore {
290290 remote. acceptUPSTermsOfService ( siteID: siteID, originAddress: originAddress, completion: completion)
291291 }
292292
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
297297 guard let self else { return }
298-
299298 switch result {
300299 case . failure( let error) :
301300 completion ( . failure( error) )
302301 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) )
310307 }
311308 }
312- } )
309+ }
313310 }
314311}
315312
@@ -679,35 +676,60 @@ private extension WooShippingStore {
679676 } , completion: nil , on: . main)
680677 }
681678
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*.
683680 /// `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 ) {
692685 storageManager. performAndSave ( { [ weak self] storage in
693686 guard let self else { return }
694687 guard let order = storage. loadOrder ( siteID: siteID, orderID: orderID) else {
695688 return
696689 }
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)
698695 } , completion: onCompletion, on: . main)
699696 }
700697
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)
711733 storageShippingLabel. update ( with: shippingLabel)
712734 storageShippingLabel. order = storageOrder
713735
@@ -720,14 +742,39 @@ private extension WooShippingStore {
720742 let destinationAddress = storageShippingLabel. destinationAddress ?? storage. insertNewObject ( ofType: Storage . ShippingLabelAddress. self)
721743 destinationAddress. update ( with: shippingLabel. destinationAddress)
722744 storageShippingLabel. destinationAddress = destinationAddress
745+
746+ /// Set the shipping label to the shipment's relationship
747+ storageShipment. shippingLabel = storageShippingLabel
748+ } else {
749+ storageShipment. shippingLabel = nil
723750 }
751+ }
724752
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+ }
731778 }
732779 }
733780
0 commit comments