|
| 1 | +import Foundation |
| 2 | +import Yosemite |
| 3 | + |
| 4 | +/// Generates and creates all variations needed for a product. |
| 5 | +/// |
| 6 | +final class GenerateAllVariationsUseCase { |
| 7 | + |
| 8 | + /// Stores dependency. Needed to generate variations |
| 9 | + /// |
| 10 | + private let stores: StoresManager |
| 11 | + |
| 12 | + init(stores: StoresManager) { |
| 13 | + self.stores = stores |
| 14 | + } |
| 15 | + |
| 16 | + /// Generates all missing variations for a product. Up to 100 variations. |
| 17 | + /// Parameters: |
| 18 | + /// - `Product`: Product on which we will be creating the variations |
| 19 | + /// - `onStateChanged`: Closure invoked every time there is a significant state change in the generation process. |
| 20 | + /// |
| 21 | + func generateAllVariations(for product: Product, onStateChanged: @escaping (State) -> Void) { |
| 22 | + |
| 23 | + // Fetch Previous variations |
| 24 | + onStateChanged(.fetching) |
| 25 | + fetchAllVariations(of: product) { result in |
| 26 | + switch result { |
| 27 | + case .success(let existingVariations): |
| 28 | + |
| 29 | + // Generate variations locally |
| 30 | + let variationsToGenerate = ProductVariationGenerator.generateVariations(for: product, excluding: existingVariations) |
| 31 | + |
| 32 | + // Guard for 100 variation limit |
| 33 | + guard variationsToGenerate.count <= 100 else { |
| 34 | + return onStateChanged(.error(.tooManyVariations(variationCount: variationsToGenerate.count))) |
| 35 | + } |
| 36 | + |
| 37 | + // Guard for no variations to generate |
| 38 | + guard variationsToGenerate.count > 0 else { |
| 39 | + return onStateChanged(.finished(false, product)) |
| 40 | + } |
| 41 | + |
| 42 | + // Confirm generation with merchant |
| 43 | + onStateChanged(.confirmation(variationsToGenerate.count, { confirmed in |
| 44 | + |
| 45 | + guard confirmed else { |
| 46 | + return onStateChanged(.canceled) |
| 47 | + } |
| 48 | + |
| 49 | + // Create variations remotely |
| 50 | + onStateChanged(.creating) |
| 51 | + self.createVariationsRemotely(for: product, variations: variationsToGenerate) { result in |
| 52 | + switch result { |
| 53 | + case .success(let generatedVariations): |
| 54 | + |
| 55 | + // Updates the current product with the up-to-date list of variations IDs. |
| 56 | + // This is needed in order to reflect variations count changes back to other screens. |
| 57 | + let updatedProduct = product.copy(variations: product.variations + generatedVariations.map { $0.productVariationID }) |
| 58 | + onStateChanged(.finished(true, updatedProduct)) |
| 59 | + |
| 60 | + case .failure(let error): |
| 61 | + onStateChanged(.error(error)) |
| 62 | + } |
| 63 | + } |
| 64 | + })) |
| 65 | + |
| 66 | + case .failure(let error): |
| 67 | + onStateChanged(.error(.unableToFetchVariations)) |
| 68 | + DDLogError("⛔️ Failed to create variations: \(error)") |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// MARK: Helper Methods |
| 75 | +// |
| 76 | +private extension GenerateAllVariationsUseCase { |
| 77 | + /// Fetches all remote variations. |
| 78 | + /// |
| 79 | + private func fetchAllVariations(of product: Product, onCompletion: @escaping (Result<[ProductVariation], Error>) -> Void) { |
| 80 | + let action = ProductVariationAction.synchronizeAllProductVariations(siteID: product.siteID, productID: product.productID, onCompletion: onCompletion) |
| 81 | + stores.dispatch(action) |
| 82 | + } |
| 83 | + |
| 84 | + /// Creates the provided variations remotely. |
| 85 | + /// |
| 86 | + private func createVariationsRemotely(for product: Product, |
| 87 | + variations: [CreateProductVariation], |
| 88 | + onCompletion: @escaping (Result<[ProductVariation], GenerationError>) -> Void) { |
| 89 | + let action = ProductVariationAction.createProductVariations(siteID: product.siteID, |
| 90 | + productID: product.productID, |
| 91 | + productVariations: variations, onCompletion: { result in |
| 92 | + switch result { |
| 93 | + case .success(let variations): |
| 94 | + onCompletion(.success(variations)) |
| 95 | + case .failure(let error): |
| 96 | + onCompletion(.failure(.unableToCreateVariations)) |
| 97 | + DDLogError("⛔️ Failed to create variations: \(error)") |
| 98 | + } |
| 99 | + }) |
| 100 | + stores.dispatch(action) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// MARK: Definitions |
| 105 | +/// |
| 106 | +extension GenerateAllVariationsUseCase { |
| 107 | + /// Type that represents the possible states while all variations are being created. |
| 108 | + /// |
| 109 | + enum State { |
| 110 | + /// State while previous variations are being fetched |
| 111 | + /// |
| 112 | + case fetching |
| 113 | + |
| 114 | + /// State to allow merchant to confirm the variation generation |
| 115 | + /// |
| 116 | + case confirmation(_ numberOfVariations: Int, _ onCompletion: (_ confirmed: Bool) -> Void) |
| 117 | + |
| 118 | + /// State while the variations are being created remotely |
| 119 | + /// |
| 120 | + case creating |
| 121 | + |
| 122 | + ///State when the merchant decides to not continue with the generation process. |
| 123 | + /// |
| 124 | + case canceled |
| 125 | + |
| 126 | + /// State when the the process is finished. `variationsCreated` indicates if variations were created or not. |
| 127 | + /// `updatedProduct` contains the original product with the new generated variation ids in it's variations array. |
| 128 | + /// |
| 129 | + case finished(_ variationsCreated: Bool, _ updatedProduct: Product) |
| 130 | + |
| 131 | + /// Error state in any part of the process. |
| 132 | + /// |
| 133 | + case error(GenerationError) |
| 134 | + } |
| 135 | + |
| 136 | + /// Type to represent known generation errors |
| 137 | + /// |
| 138 | + enum GenerationError: LocalizedError, Equatable { |
| 139 | + case unableToFetchVariations |
| 140 | + case unableToCreateVariations |
| 141 | + case tooManyVariations(variationCount: Int) |
| 142 | + |
| 143 | + var errorTitle: String { |
| 144 | + switch self { |
| 145 | + case .unableToFetchVariations: |
| 146 | + return NSLocalizedString("Unable to fetch variations", comment: "Error title for when we can't fetch existing variations.") |
| 147 | + case .unableToCreateVariations: |
| 148 | + return NSLocalizedString("Unable to create variations", comment: "Error title for when we can't create variations remotely.") |
| 149 | + case .tooManyVariations: |
| 150 | + return NSLocalizedString("Generation limit exceeded", comment: "Error title for when there are too many variations to generate.") |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + var errorDescription: String? { |
| 155 | + switch self { |
| 156 | + case .unableToFetchVariations: |
| 157 | + return NSLocalizedString("Something went wrong, please try again later.", comment: "Error message for when we can't fetch existing variations.") |
| 158 | + case .unableToCreateVariations: |
| 159 | + return NSLocalizedString("Something went wrong, please try again later.", comment: "Error message for when we can't create variations remotely") |
| 160 | + case .tooManyVariations(let variationCount): |
| 161 | + let format = NSLocalizedString( |
| 162 | + "Currently creation is supported for 100 variations maximum. Generating variations for this product would create %d variations.", |
| 163 | + comment: "Error description for when there are too many variations to generate." |
| 164 | + ) |
| 165 | + return String.localizedStringWithFormat(format, variationCount) |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments