|
| 1 | +import Foundation |
| 2 | +import Yosemite |
| 3 | + |
| 4 | +/// Generates all possible variations from a product attributes |
| 5 | +/// |
| 6 | +struct ProductVariationGenerator { |
| 7 | + |
| 8 | + /// Group a colection of attribute options. |
| 9 | + /// EG: [Size: Large, Color: Black, Fabric: Cotton] |
| 10 | + /// |
| 11 | + private struct Combination: Hashable { |
| 12 | + let options: [Option] |
| 13 | + } |
| 14 | + |
| 15 | + /// Represents an attribute option. |
| 16 | + /// EG: Size: Large |
| 17 | + /// |
| 18 | + private struct Option: Hashable { |
| 19 | + let attributeID: Int64 |
| 20 | + let attributeName: String |
| 21 | + let value: String |
| 22 | + } |
| 23 | + |
| 24 | + /// Generates all possible variations from a product attributes. |
| 25 | + /// Additionally it excludes variations that already exists in the `variations` parameter. |
| 26 | + /// |
| 27 | + static func generateVariations(for product: Product, excluding variations: [ProductVariation]) -> [CreateProductVariation] { |
| 28 | + let allCombinations = getCombinations(from: product) |
| 29 | + let uniqueCombinations = filterExistingCombinations(allCombinations, existing: variations) |
| 30 | + return buildVariations(from: uniqueCombinations, for: product) |
| 31 | + } |
| 32 | + |
| 33 | + /// Generates all posible combination for a product attributes. |
| 34 | + /// |
| 35 | + private static func getCombinations(from product: Product) -> [Combination] { |
| 36 | + // Iterates through attributes while eceiving the previous combinations list. |
| 37 | + product.attributes.reduce([Combination(options: [])]) { combinations, attribute in |
| 38 | + combinations.flatMap { combination in |
| 39 | + // When receiving a previous combination list, we add each attribute to each previous combination util we finish with them. |
| 40 | + attribute.options.map { option in |
| 41 | + Combination(options: combination.options + [Option(attributeID: attribute.attributeID, attributeName: attribute.name, value: option)]) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Removes the provided variations from the given combinations array. |
| 48 | + /// |
| 49 | + private static func filterExistingCombinations(_ combinations: [Combination], existing variations: [ProductVariation]) -> [Combination] { |
| 50 | + // Convert variations into combinations |
| 51 | + let existingCombinations = variations.map { existingVariation in |
| 52 | + let options = existingVariation.attributes.map { attibute in |
| 53 | + Option(attributeID: attibute.id, attributeName: attibute.name, value: attibute.option) |
| 54 | + } |
| 55 | + return Combination(options: options) |
| 56 | + } |
| 57 | + |
| 58 | + // Filter existing combinations. |
| 59 | + let existingSet = Set(existingCombinations) |
| 60 | + return combinations.filter { combination in |
| 61 | + !existingSet.contains(combination) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// Convert the provided combinations into `[CreateProductVariation]` types that are consumed by our Yosemite stores. |
| 66 | + /// |
| 67 | + private static func buildVariations(from combinations: [Combination], for product: Product) -> [CreateProductVariation] { |
| 68 | + combinations.map { combination in |
| 69 | + let attributes = combination.options.map { option in |
| 70 | + ProductVariationAttribute(id: option.attributeID, name: option.attributeName, option: option.value) |
| 71 | + } |
| 72 | + return CreateProductVariation(regularPrice: "", attributes: attributes) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments