Skip to content

Commit 242b122

Browse files
authored
Merge branch 'trunk' into task/WOOMOB-534-more-asset-reduction
2 parents f593fff + 8bc29b0 commit 242b122

File tree

60 files changed

+1273
-249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1273
-249
lines changed

Modules/Sources/Fakes/Networking.generated.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@ extension Networking.ShippingLabelAccountSettings {
14511451
isEmailReceiptsEnabled: .fake(),
14521452
paperSize: .fake(),
14531453
lastSelectedPackageID: .fake(),
1454+
lastOrderCompleted: .fake(),
14541455
addPaymentMethodURL: .fake()
14551456
)
14561457
}

Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,7 @@ extension Networking.ShippingLabelAccountSettings {
24242424
isEmailReceiptsEnabled: CopiableProp<Bool> = .copy,
24252425
paperSize: CopiableProp<ShippingLabelPaperSize> = .copy,
24262426
lastSelectedPackageID: CopiableProp<String> = .copy,
2427+
lastOrderCompleted: CopiableProp<Bool> = .copy,
24272428
addPaymentMethodURL: NullableCopiableProp<URL> = .copy
24282429
) -> Networking.ShippingLabelAccountSettings {
24292430
let siteID = siteID ?? self.siteID
@@ -2438,6 +2439,7 @@ extension Networking.ShippingLabelAccountSettings {
24382439
let isEmailReceiptsEnabled = isEmailReceiptsEnabled ?? self.isEmailReceiptsEnabled
24392440
let paperSize = paperSize ?? self.paperSize
24402441
let lastSelectedPackageID = lastSelectedPackageID ?? self.lastSelectedPackageID
2442+
let lastOrderCompleted = lastOrderCompleted ?? self.lastOrderCompleted
24412443
let addPaymentMethodURL = addPaymentMethodURL ?? self.addPaymentMethodURL
24422444

24432445
return Networking.ShippingLabelAccountSettings(
@@ -2453,6 +2455,7 @@ extension Networking.ShippingLabelAccountSettings {
24532455
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
24542456
paperSize: paperSize,
24552457
lastSelectedPackageID: lastSelectedPackageID,
2458+
lastOrderCompleted: lastOrderCompleted,
24562459
addPaymentMethodURL: addPaymentMethodURL
24572460
)
24582461
}

Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingShipmentItem.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ public struct WooShippingShipmentItem: Codable, Equatable, GeneratedFakeable, Ge
4949
}
5050

5151
public typealias WooShippingShipments = [String: [WooShippingShipmentItem]]
52+
53+
public extension WooShippingShipmentItem {
54+
var quantity: Decimal {
55+
guard let subItems else { return 0 }
56+
return subItems.count > 0 ? Decimal(subItems.count) : 1
57+
}
58+
}

Modules/Sources/Networking/Model/ShippingLabel/ShippingLabelAccountSettings.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
4343
/// Uses the `id` for predefined packages or `name` for custom packages.
4444
public let lastSelectedPackageID: String
4545

46+
/// Whether to mark order as completed after last label purchase
47+
/// This is available only in the new Woo Shipping plugin.
48+
public let lastOrderCompleted: Bool
49+
4650
/// URL to open the web view for adding new payment methods
4751
public let addPaymentMethodURL: URL?
4852

@@ -58,6 +62,7 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
5862
isEmailReceiptsEnabled: Bool,
5963
paperSize: ShippingLabelPaperSize,
6064
lastSelectedPackageID: String,
65+
lastOrderCompleted: Bool,
6166
addPaymentMethodURL: URL?) {
6267
self.siteID = siteID
6368
self.canManagePayments = canManagePayments
@@ -71,6 +76,7 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
7176
self.isEmailReceiptsEnabled = isEmailReceiptsEnabled
7277
self.paperSize = paperSize
7378
self.lastSelectedPackageID = lastSelectedPackageID
79+
self.lastOrderCompleted = lastOrderCompleted
7480
self.addPaymentMethodURL = addPaymentMethodURL
7581
}
7682
}
@@ -105,6 +111,7 @@ extension ShippingLabelAccountSettings: Decodable {
105111

106112
let userMetaContainer = try container.nestedContainer(keyedBy: UserMetaKeys.self, forKey: .userMeta)
107113
let lastSelectedPackageID = try userMetaContainer.decode(String.self, forKey: .lastSelectedPackageID)
114+
let lastOrderCompleted = (try? userMetaContainer.decodeIfPresent(Bool.self, forKey: .lastOrderCompleted)) ?? false
108115

109116
self.init(siteID: siteID,
110117
canManagePayments: canManagePayments,
@@ -118,6 +125,7 @@ extension ShippingLabelAccountSettings: Decodable {
118125
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
119126
paperSize: paperSize,
120127
lastSelectedPackageID: lastSelectedPackageID,
128+
lastOrderCompleted: lastOrderCompleted,
121129
addPaymentMethodURL: addPaymentMethodURL)
122130
}
123131
}
@@ -150,6 +158,7 @@ private extension ShippingLabelAccountSettings {
150158

151159
private enum UserMetaKeys: String, CodingKey {
152160
case lastSelectedPackageID = "last_box_id"
161+
case lastOrderCompleted = "last_order_completed"
153162
}
154163
}
155164

Modules/Sources/Networking/Remote/WooShippingRemote.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public protocol WooShippingRemoteProtocol {
3737
originAddress: WooShippingAddress,
3838
destinationAddress: WooShippingAddress,
3939
package: WooShippingPackagePurchase,
40+
markOrderComplete: Bool?,
4041
completion: @escaping (Result<[ShippingLabelPurchase], Error>) -> Void)
4142
func checkLabelStatus(siteID: Int64,
4243
orderID: Int64,
@@ -284,7 +285,14 @@ public final class WooShippingRemote: Remote, WooShippingRemoteProtocol {
284285
originAddress: WooShippingAddress,
285286
destinationAddress: WooShippingAddress,
286287
package: WooShippingPackagePurchase,
288+
markOrderComplete: Bool?,
287289
completion: @escaping (Result<[ShippingLabelPurchase], Error>) -> Void) {
290+
let userMeta: [String: Any]? = {
291+
guard let markOrderComplete else {
292+
return nil
293+
}
294+
return [ParameterKey.lastOrderCompleted: markOrderComplete]
295+
}()
288296
do {
289297
let parameters: [String: Any] = [
290298
ParameterKey.async: true,
@@ -296,7 +304,8 @@ public final class WooShippingRemote: Remote, WooShippingRemoteProtocol {
296304
ParameterKey.featuresSupported: [Values.upsdap],
297305
ParameterKey.hazmat: package.encodedHazmat(),
298306
ParameterKey.customs: try package.encodedCustomsForm(),
299-
]
307+
ParameterKey.userMeta: userMeta,
308+
].compactMapValues { $0 }
300309
let path = "\(Path.purchase)/\(orderID)"
301310
let request = JetpackRequest(wooApiVersion: .wooShipping,
302311
method: .post,
@@ -635,6 +644,8 @@ private extension WooShippingRemote {
635644
static let enabled = "enabled"
636645
static let featuresSupported = "features_supported_by_client"
637646
static let confirmed = "confirmed"
647+
static let userMeta = "user_meta"
648+
static let lastOrderCompleted = "last_order_completed"
638649
}
639650

640651
enum Values {

Modules/Sources/Storage/Tools/StorageType+Deletions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ public extension StorageType {
195195
}
196196
}
197197

198-
/// Deletes all of the stored SystemPlugins for the provided siteID whose name is not included in `currentSystemPlugins` array
198+
/// Deletes stored system plugins for the provided siteID that are not in the currentSystemPluginPaths list.
199199
///
200-
func deleteStaleSystemPlugins(siteID: Int64, currentSystemPlugins: [String]) {
200+
func deleteStaleSystemPlugins(siteID: Int64, currentSystemPluginPaths: [String]) {
201201
let systemPlugins = loadSystemPlugins(siteID: siteID).filter {
202-
!currentSystemPlugins.contains($0.name)
202+
!currentSystemPluginPaths.contains($0.plugin)
203203
}
204204
systemPlugins.forEach {
205205
deleteObject($0)

Modules/Sources/Storage/Tools/StorageType+Extensions.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,28 @@ public extension StorageType {
841841

842842
/// Returns a system plugin with a specified `siteID` and `path`.
843843
///
844-
func loadSystemPlugin(siteID: Int64, path: String) -> SystemPlugin? {
845-
let predicate = \SystemPlugin.siteID == siteID && \SystemPlugin.plugin == path
844+
/// - Parameters:
845+
/// - siteID: The site ID to filter by.
846+
/// - path: The plugin path to match (e.g., "woocommerce/woocommerce.php").
847+
/// - active: Optional active state filter. If provided, only plugins with matching active state are returned. If nil, active state is ignored.
848+
/// - Returns: The matching system plugin, or nil if not found.
849+
func loadSystemPlugin(siteID: Int64, path: String, active: Bool? = nil) -> SystemPlugin? {
850+
let predicate = if let active {
851+
\SystemPlugin.siteID == siteID && \SystemPlugin.plugin == path && \SystemPlugin.active == active
852+
} else {
853+
\SystemPlugin.siteID == siteID && \SystemPlugin.plugin == path
854+
}
846855
return firstObject(ofType: SystemPlugin.self, matching: predicate)
847856
}
848857

858+
/// Returns stored system plugins for a provided `siteID` matching the given plugin `paths`.
859+
///
860+
func loadSystemPlugins(siteID: Int64, matchingPaths paths: [String]) -> [SystemPlugin] {
861+
let predicate = NSPredicate(format: "siteID == %lld && plugin in %@", siteID, paths)
862+
let descriptor = NSSortDescriptor(keyPath: \SystemPlugin.plugin, ascending: true)
863+
return allObjects(ofType: SystemPlugin.self, matching: predicate, sortedBy: [descriptor])
864+
}
865+
849866
// MARK: - Inbox Notes
850867

851868
/// Returns a single Inbox Note given a `siteID` and `id`

Modules/Sources/Yosemite/Actions/WooShippingAction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public enum WooShippingAction: Action {
5656
originAddress: WooShippingAddress,
5757
destinationAddress: WooShippingAddress,
5858
package: WooShippingPackagePurchase,
59+
markOrderComplete: Bool?,
5960
backendProcessingDelay: TimeInterval = 2.0,
6061
pollingDelay: TimeInterval = 1.0,
6162
pollingMaximumRetries: Int64 = 3,

Modules/Sources/Yosemite/Model/Storage/ShippingLabelAccountSettings+ReadOnlyConvertible.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ extension Storage.ShippingLabelAccountSettings: ReadOnlyConvertible {
2626
let paymentMethodItems = paymentMethods?.map { $0.toReadOnly() } ?? []
2727

2828
/// Since account settings are not persisted for the new shipping label flow,
29-
/// the conversion for the new property `addPaymentMethodURL` is ignored.
30-
/// This avoids the complication of unnecessary Core Data migration for the new property.
29+
/// the conversion for the new properties `addPaymentMethodURL` & `lastOrderCompleted` is ignored.
30+
/// This avoids the complication of unnecessary Core Data migration for the new properties.
3131
return ShippingLabelAccountSettings(siteID: siteID,
3232
canManagePayments: canManagePayments,
3333
canEditSettings: canEditSettings,
@@ -40,6 +40,7 @@ extension Storage.ShippingLabelAccountSettings: ReadOnlyConvertible {
4040
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
4141
paperSize: .init(rawValue: paperSize ?? ""),
4242
lastSelectedPackageID: lastSelectedPackageID ?? "",
43+
lastOrderCompleted: false,
4344
addPaymentMethodURL: nil)
4445
}
4546
}

Modules/Sources/Yosemite/PointOfSale/Eligibility/POSSystemStatusService.swift

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
4848
)
4949

5050
// Upserts all plugins in storage.
51-
await storageManager.performAndSaveAsync({ [weak self] storage in
52-
self?.upsertSystemPlugins(siteID: siteID, systemStatus: systemStatus, in: storage)
51+
await storageManager.performAndSaveAsync({ storage in
52+
let useCase = SystemPluginsUpsertUseCase(storage: storage)
53+
useCase.upsert(siteID: siteID, activePlugins: systemStatus.activePlugins, inactivePlugins: systemStatus.inactivePlugins)
5354
})
5455

5556
// Loads WooCommerce plugin from storage.
56-
guard let wcPlugin = storageManager.viewStorage.loadSystemPlugin(siteID: siteID, path: Constants.wcPluginPath)?.toReadOnly() else {
57+
guard let wcPlugin = storageManager.viewStorage.loadSystemPlugin(siteID: siteID, path: Constants.wcPluginPath, active: true)?.toReadOnly() else {
5758
return POSPluginAndFeatureInfo(wcPlugin: nil, featureValue: nil)
5859
}
5960

@@ -63,42 +64,6 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
6364
}
6465
}
6566

66-
private extension POSSystemStatusService {
67-
/// Updates or inserts system plugins in storage.
68-
func upsertSystemPlugins(siteID: Int64, systemStatus: POSPluginEligibilitySystemStatus, in storage: StorageType) {
69-
// Active and inactive plugins share identical structure, but are stored in separate parts of the remote response
70-
// (and without an active attribute in the response). So we apply the correct value for active (or not)
71-
let readonlySystemPlugins: [SystemPlugin] = {
72-
let activePlugins = systemStatus.activePlugins.map {
73-
$0.copy(active: true)
74-
}
75-
76-
let inactivePlugins = systemStatus.inactivePlugins.map {
77-
$0.copy(active: false)
78-
}
79-
80-
return activePlugins + inactivePlugins
81-
}()
82-
83-
let storedPlugins = storage.loadSystemPlugins(siteID: siteID, matching: readonlySystemPlugins.map { $0.name })
84-
readonlySystemPlugins.forEach { readonlySystemPlugin in
85-
// Loads or creates new StorageSystemPlugin matching the readonly one.
86-
let storageSystemPlugin: StorageSystemPlugin = {
87-
if let systemPlugin = storedPlugins.first(where: { $0.name == readonlySystemPlugin.name }) {
88-
return systemPlugin
89-
}
90-
return storage.insertNewObject(ofType: StorageSystemPlugin.self)
91-
}()
92-
93-
storageSystemPlugin.update(with: readonlySystemPlugin)
94-
}
95-
96-
// Removes stale system plugins.
97-
let currentSystemPlugins = readonlySystemPlugins.map(\.name)
98-
storage.deleteStaleSystemPlugins(siteID: siteID, currentSystemPlugins: currentSystemPlugins)
99-
}
100-
}
101-
10267
private extension POSSystemStatusService {
10368
enum Constants {
10469
static let wcPluginPath = "woocommerce/woocommerce.php"

0 commit comments

Comments
 (0)