Skip to content

Commit bc568ef

Browse files
authored
Merge pull request #8008 from woocommerce/issue/7994-add-zd-ipp-statuses
Add IPP plugin status tags to ZenDesk
2 parents fa0a8cd + d341fc5 commit bc568ef

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

WooCommerce/Classes/Authentication/Epilogue/SwitchStoreUseCase.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class SwitchStoreUseCase: SwitchStoreUseCaseProtocol {
1212

1313
private let stores: StoresManager
1414
private let storageManager: StorageManagerType
15+
private let zendeskShared: ZendeskManagerProtocol = ZendeskProvider.shared
1516

1617
private lazy var resultsController: ResultsController<StorageSite> = {
1718
return ResultsController(storageManager: storageManager, sortedBy: [])
@@ -72,7 +73,9 @@ final class SwitchStoreUseCase: SwitchStoreUseCaseProtocol {
7273
// https://github.com/woocommerce/woocommerce-ios/pull/2013#discussion_r454620804
7374
logOutOfCurrentStore {
7475
self.finalizeStoreSelection(storeID)
75-
76+
// Inform the Zendesk shared instance that the Store has been switched
77+
// https://github.com/woocommerce/woocommerce-ios/pull/8008/
78+
self.zendeskShared.observeStoreSwitch()
7679
onCompletion(true)
7780
}
7881
}

WooCommerce/Classes/Tools/Zendesk/ZendeskManager.swift

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import CoreTelephony
99
import SafariServices
1010
import Yosemite
1111

12-
1312
extension NSNotification.Name {
1413
static let ZDPNReceived = NSNotification.Name(rawValue: "ZDPNReceived")
1514
static let ZDPNCleared = NSNotification.Name(rawValue: "ZDPNCleared")
@@ -23,6 +22,8 @@ extension NSNotification.Name {
2322
protocol ZendeskManagerProtocol: SupportManagerAdapter {
2423
typealias onUserInformationCompletion = (_ success: Bool, _ email: String?) -> Void
2524

25+
func observeStoreSwitch()
26+
2627
/// Displays the Zendesk New Request view from the given controller, for users to submit new tickets.
2728
///
2829
func showNewRequestIfPossible(from controller: UIViewController, with sourceTag: String?)
@@ -45,6 +46,10 @@ protocol ZendeskManagerProtocol: SupportManagerAdapter {
4546
}
4647

4748
struct NoZendeskManager: ZendeskManagerProtocol {
49+
func observeStoreSwitch() {
50+
// no-op
51+
}
52+
4853
func showNewRequestIfPossible(from controller: UIViewController) {
4954
// no-op
5055
}
@@ -138,6 +143,65 @@ struct ZendeskProvider {
138143
///
139144
#if !targetEnvironment(macCatalyst)
140145
final class ZendeskManager: NSObject, ZendeskManagerProtocol {
146+
private let stores = ServiceLocator.stores
147+
private let storageManager = ServiceLocator.storageManager
148+
149+
/// Controller for fetching site plugins from Storage
150+
///
151+
private lazy var pluginResultsController: ResultsController<StorageSitePlugin> = createPluginResultsController()
152+
153+
/// Returns a `pluginResultsController` using the latest selected site ID for predicate
154+
///
155+
private func createPluginResultsController() -> ResultsController<StorageSitePlugin> {
156+
var sitePredicate: NSPredicate? = nil
157+
if let siteID = stores.sessionManager.defaultSite?.siteID {
158+
sitePredicate = NSPredicate(format: "siteID == %lld", siteID)
159+
} else {
160+
DDLogError("ZendeskManager: No siteID found when attempting to initialize Plugins Results predicate.")
161+
}
162+
163+
let pluginStatusDescriptor = [NSSortDescriptor(keyPath: \StorageSitePlugin.status, ascending: true)]
164+
165+
return ResultsController(storageManager: storageManager,
166+
matching: sitePredicate,
167+
sortedBy: pluginStatusDescriptor)
168+
}
169+
170+
func observeStoreSwitch() {
171+
pluginResultsController = createPluginResultsController()
172+
do {
173+
try pluginResultsController.performFetch()
174+
} catch {
175+
DDLogError("ZendeskManager: Unable to update plugin results")
176+
}
177+
}
178+
179+
/// List of tags that reflect Stripe and WCPay plugin statuses
180+
///
181+
private var ippPluginStatuses: [String] {
182+
var ippTags = [PluginStatus]()
183+
if let stripe = pluginResultsController.fetchedObjects.first(where: { $0.plugin == PluginSlug.stripe }) {
184+
if stripe.status == .active {
185+
ippTags.append(.stripeInstalledAndActivated)
186+
} else if stripe.status == .inactive {
187+
ippTags.append(.stripeInstalledButNotActivated)
188+
}
189+
} else {
190+
ippTags.append(.stripeNotInstalled)
191+
}
192+
if let wcpay = pluginResultsController.fetchedObjects.first(where: { $0.plugin == PluginSlug.wcpay }) {
193+
if wcpay.status == .active {
194+
ippTags.append(.wcpayInstalledAndActivated)
195+
} else if wcpay.status == .inactive {
196+
ippTags.append(.wcpayInstalledButNotActivated)
197+
}
198+
}
199+
else {
200+
ippTags.append(.wcpayNotInstalled)
201+
}
202+
return ippTags.map { $0.rawValue }
203+
}
204+
141205
func showNewRequestIfPossible(from controller: UIViewController) {
142206
showNewRequestIfPossible(from: controller, with: nil)
143207
}
@@ -185,11 +249,11 @@ final class ZendeskManager: NSObject, ZendeskManagerProtocol {
185249
return ZDKPushProvider(zendesk: zendesk)
186250
}
187251

188-
189252
/// Designated Initialier
190253
///
191254
fileprivate override init() {
192255
super.init()
256+
try? pluginResultsController.performFetch()
193257
observeZendeskNotifications()
194258
}
195259

@@ -324,8 +388,7 @@ final class ZendeskManager: NSObject, ZendeskManagerProtocol {
324388
/// The SDK tag is used in a trigger and displays tickets in Woo > Mobile Apps New.
325389
///
326390
func getTags(supportSourceTag: String?) -> [String] {
327-
let tags = [Constants.platformTag, Constants.sdkTag, Constants.jetpackTag]
328-
391+
let tags = [Constants.platformTag, Constants.sdkTag, Constants.jetpackTag] + ippPluginStatuses
329392
return decorateTags(tags: tags, supportSourceTag: supportSourceTag)
330393
}
331394

@@ -1066,6 +1129,20 @@ private extension ZendeskManager {
10661129
}
10671130
}
10681131

1132+
private extension ZendeskManager {
1133+
enum PluginSlug {
1134+
static let stripe = "woocommerce-gateway-stripe/woocommerce-gateway-stripe"
1135+
static let wcpay = "woocommerce-payments/woocommerce-payments"
1136+
}
1137+
enum PluginStatus: String {
1138+
case stripeNotInstalled = "woo_mobile_stripe_not_installed"
1139+
case stripeInstalledAndActivated = "woo_mobile_stripe_installed_and_activated"
1140+
case stripeInstalledButNotActivated = "woo_mobile_stripe_installed_and_not_activated"
1141+
case wcpayNotInstalled = "woo_mobile_wcpay_not_installed"
1142+
case wcpayInstalledAndActivated = "woo_mobile_wcpay_installed_and_activated"
1143+
case wcpayInstalledButNotActivated = "woo_mobile_wcpay_installed_and_not_activated"
1144+
}
1145+
}
10691146

10701147
// MARK: - UITextFieldDelegate
10711148
//

WooCommerce/WooCommerceTests/Testing/MockZendeskManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ final class MockZendeskManager: ZendeskManagerProtocol {
6767
func reset() {
6868
// no-op
6969
}
70+
71+
func observeStoreSwitch() {
72+
// no-op
73+
}
7074
}
7175

7276
extension MockZendeskManager: SupportManagerAdapter {

0 commit comments

Comments
 (0)