Skip to content

Commit dd73d5d

Browse files
committed
Always make system status API request for plugin and feature switch to fix an issue where the plugin from storage is outdated before it is synced externally with a tradeoff of an extra API request when POS tab is tapped for the first time.
1 parent 2f26bc2 commit dd73d5d

File tree

1 file changed

+6
-58
lines changed

1 file changed

+6
-58
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/POS/POSTabEligibilityChecker.swift

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,12 @@ final class POSTabEligibilityChecker: POSEntryPointEligibilityCheckerProtocol {
8181

8282
/// Determines whether the POS entry point can be shown based on the selected store and feature gates.
8383
func checkEligibility() async -> POSEligibilityState {
84-
await checkEligibility(pluginEligibility: nil)
85-
}
86-
87-
/// Checks the eligibility for POS based on site settings and plugin eligibility.
88-
/// - Parameter pluginEligibility: An optional parameter that can provide pre-fetched plugin eligibility state.
89-
/// - Returns: The eligibility state for POS.
90-
private func checkEligibility(pluginEligibility: POSEligibilityState?) async -> POSEligibilityState {
9184
guard #available(iOS 17.0, *) else {
9285
return .ineligible(reason: .unsupportedIOSVersion)
9386
}
9487

9588
async let siteSettingsEligibility = checkSiteSettingsEligibility()
96-
async let pluginEligibility = checkPluginEligibility(pluginEligibility: pluginEligibility)
89+
async let pluginEligibility = checkPluginEligibility()
9790

9891
switch await siteSettingsEligibility {
9992
case .eligible:
@@ -144,8 +137,7 @@ final class POSTabEligibilityChecker: POSEntryPointEligibilityCheckerProtocol {
144137
throw error
145138
}
146139
case .unsupportedWooCommerceVersion, .wooCommercePluginNotFound:
147-
let pluginEligibility = await refreshPluginEligibility()
148-
return await checkEligibility(pluginEligibility: pluginEligibility)
140+
return await checkEligibility()
149141
case .featureSwitchDisabled:
150142
// TODO: WOOMOB-759 - enable feature switch via API and check eligibility again
151143
// For now, just checks eligibility again.
@@ -160,41 +152,20 @@ final class POSTabEligibilityChecker: POSEntryPointEligibilityCheckerProtocol {
160152

161153
private extension POSTabEligibilityChecker {
162154
/// Checks the eligibility of the WooCommerce plugin and plugin version based POS feature switch value.
163-
/// When a pre-fetched eligibility state is not provided, the plugin is the first matching WC plugin found in the storage for performance reason, which is
164-
/// fetched remotely outside of the eligibility checker during site initialization.
165-
/// The feature switch value is fetched remotely if the plugin version supports it.
166155
///
167156
/// - Parameter pluginEligibility: An optional parameter that can provide pre-fetched plugin eligibility state.
168157
/// - Returns: The eligibility state for POS based on the WooCommerce plugin and POS feature switch.
169-
func checkPluginEligibility(pluginEligibility: POSEligibilityState? = nil) async -> POSEligibilityState {
170-
if let pluginEligibility {
171-
return pluginEligibility
172-
}
173-
async let wcPlugin = fetchWooCommercePlugin(siteID: siteID)
174-
let wcPluginEligibility = checkWooCommercePluginEligibility(wcPlugin: await wcPlugin)
175-
switch wcPluginEligibility {
176-
case .eligible:
177-
return .eligible
178-
case .ineligible(let reason):
179-
return .ineligible(reason: reason)
180-
case .pendingFeatureSwitchCheck:
181-
return await checkFeatureSwitchRemotely(siteID: siteID)
182-
}
183-
}
184-
185-
/// Refreshes the eligibility state of the WooCommerce plugin and POS feature switch by making a system status API request.
186-
/// - Returns: The eligibility state for POS based on the WooCommerce plugin and POS feature switch from remote sync.
187-
func refreshPluginEligibility() async -> POSEligibilityState {
158+
func checkPluginEligibility() async -> POSEligibilityState {
188159
do {
189-
async let info = systemStatusService.loadWooCommercePluginAndPOSFeatureSwitch(siteID: siteID)
190-
let wcPluginEligibility = checkWooCommercePluginEligibility(wcPlugin: try await info.wcPlugin)
160+
let info = try await systemStatusService.loadWooCommercePluginAndPOSFeatureSwitch(siteID: siteID)
161+
let wcPluginEligibility = checkWooCommercePluginEligibility(wcPlugin: info.wcPlugin)
191162
switch wcPluginEligibility {
192163
case .eligible:
193164
return .eligible
194165
case .ineligible(let reason):
195166
return .ineligible(reason: reason)
196167
case .pendingFeatureSwitchCheck:
197-
let isFeatureSwitchEnabled = try await info.featureValue == true
168+
let isFeatureSwitchEnabled = info.featureValue == true
198169
return isFeatureSwitchEnabled ? .eligible : .ineligible(reason: .featureSwitchDisabled)
199170
}
200171
} catch {
@@ -229,29 +200,6 @@ private extension POSTabEligibilityChecker {
229200
// For versions that support the feature switch, checks if the feature switch is enabled separately.
230201
return .pendingFeatureSwitchCheck
231202
}
232-
233-
@MainActor
234-
func fetchWooCommercePlugin(siteID: Int64) async -> SystemPlugin {
235-
await pluginsService.waitForPluginInStorage(siteID: siteID, pluginPath: Constants.wcPlugin, isActive: true)
236-
}
237-
238-
@MainActor
239-
func checkFeatureSwitchRemotely(siteID: Int64) async -> POSEligibilityState {
240-
await withCheckedContinuation { [weak self] continuation in
241-
guard let self else {
242-
return continuation.resume(returning: .ineligible(reason: .selfDeallocated))
243-
}
244-
let action = SettingAction.isFeatureEnabled(siteID: siteID, feature: .pointOfSale) { result in
245-
switch result {
246-
case .success(let isEnabled):
247-
continuation.resume(returning: isEnabled ? .eligible : .ineligible(reason: .featureSwitchDisabled))
248-
case .failure:
249-
continuation.resume(returning: .ineligible(reason: .featureSwitchSyncFailure))
250-
}
251-
}
252-
stores.dispatch(action)
253-
}
254-
}
255203
}
256204

257205
// MARK: - Site Settings Related Eligibility Check

0 commit comments

Comments
 (0)