|
| 1 | +import Foundation |
| 2 | +import protocol Networking.POSCatalogSyncRemoteProtocol |
| 3 | +import class Networking.AlamofireNetwork |
| 4 | +import class Networking.POSCatalogSyncRemote |
| 5 | +import CocoaLumberjackSwift |
| 6 | + |
| 7 | +// TODO - remove the periphery ignore comment when the catalog is integrated with POS. |
| 8 | +// periphery:ignore |
| 9 | +public protocol POSCatalogFullSyncServiceProtocol { |
| 10 | + /// Starts a full catalog sync process |
| 11 | + /// - Parameter siteID: The site ID to sync catalog for |
| 12 | + /// - Returns: The synced catalog containing products and variations |
| 13 | + func startFullSync(for siteID: Int64) async throws -> POSCatalog |
| 14 | +} |
| 15 | + |
| 16 | +/// POS catalog from full sync. |
| 17 | +// TODO - remove the periphery ignore comment when the catalog is integrated with POS. |
| 18 | +// periphery:ignore |
| 19 | +public struct POSCatalog { |
| 20 | + public let products: [POSProduct] |
| 21 | + public let variations: [POSProductVariation] |
| 22 | +} |
| 23 | + |
| 24 | +// TODO - remove the periphery ignore comment when the service is integrated with POS. |
| 25 | +// periphery:ignore |
| 26 | +public final class POSCatalogFullSyncService: POSCatalogFullSyncServiceProtocol { |
| 27 | + private let syncRemote: POSCatalogSyncRemoteProtocol |
| 28 | + private let batchSize: Int |
| 29 | + |
| 30 | + public convenience init?(credentials: Credentials?, batchSize: Int = 2) { |
| 31 | + guard let credentials else { |
| 32 | + DDLogError("⛔️ Could not create POSCatalogFullSyncService due missing credentials") |
| 33 | + return nil |
| 34 | + } |
| 35 | + let network = AlamofireNetwork(credentials: credentials, ensuresSessionManagerIsInitialized: true) |
| 36 | + let syncRemote = POSCatalogSyncRemote(network: network) |
| 37 | + self.init(syncRemote: syncRemote, batchSize: batchSize) |
| 38 | + } |
| 39 | + |
| 40 | + init(syncRemote: POSCatalogSyncRemoteProtocol, batchSize: Int) { |
| 41 | + self.syncRemote = syncRemote |
| 42 | + self.batchSize = batchSize |
| 43 | + } |
| 44 | + |
| 45 | + // MARK: - Protocol Conformance |
| 46 | + |
| 47 | + public func startFullSync(for siteID: Int64) async throws -> POSCatalog { |
| 48 | + DDLogInfo("🔄 Starting full catalog sync for site ID: \(siteID)") |
| 49 | + |
| 50 | + do { |
| 51 | + let catalog = try await loadCatalog(for: siteID, syncRemote: syncRemote) |
| 52 | + DDLogInfo("✅ Loaded \(catalog.products.count) products and \(catalog.variations.count) variations for siteID \(siteID)") |
| 53 | + return catalog |
| 54 | + } catch { |
| 55 | + throw error |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// MARK: - Remote Loading |
| 61 | + |
| 62 | +private extension POSCatalogFullSyncService { |
| 63 | + func loadCatalog(for siteID: Int64, syncRemote: POSCatalogSyncRemoteProtocol) async throws -> POSCatalog { |
| 64 | + // Loads products and variations in batches in parallel. |
| 65 | + async let productsTask = loadAllProducts(for: siteID, syncRemote: syncRemote) |
| 66 | + async let variationsTask = loadAllProductVariations(for: siteID, syncRemote: syncRemote) |
| 67 | + |
| 68 | + let (products, variations) = try await (productsTask, variationsTask) |
| 69 | + return POSCatalog(products: products, variations: variations) |
| 70 | + } |
| 71 | + |
| 72 | + func loadAllProducts(for siteID: Int64, syncRemote: POSCatalogSyncRemoteProtocol) async throws -> [POSProduct] { |
| 73 | + DDLogInfo("🔄 Starting products sync for site ID: \(siteID)") |
| 74 | + |
| 75 | + var allProducts: [POSProduct] = [] |
| 76 | + var currentPage = 1 |
| 77 | + var hasMorePages = true |
| 78 | + |
| 79 | + while hasMorePages { |
| 80 | + let pagesToFetch = Array(currentPage..<(currentPage + batchSize)) |
| 81 | + |
| 82 | + let batchResults = try await withThrowingTaskGroup(of: PageResult<POSProduct>.self) { group in |
| 83 | + for pageNumber in pagesToFetch { |
| 84 | + group.addTask { |
| 85 | + let result = try await syncRemote.loadProducts(siteID: siteID, pageNumber: pageNumber) |
| 86 | + return PageResult(pageNumber: pageNumber, items: result) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + var results: [PageResult<POSProduct>] = [] |
| 91 | + for try await result in group { |
| 92 | + results.append(result) |
| 93 | + } |
| 94 | + return results.sorted(by: { $0.pageNumber < $1.pageNumber }) |
| 95 | + } |
| 96 | + |
| 97 | + // Processes results in order and checks if there are more pages. |
| 98 | + let newProducts = batchResults.flatMap { $0.items.items } |
| 99 | + allProducts.append(contentsOf: newProducts) |
| 100 | + |
| 101 | + let highestPageResult = batchResults.last?.items |
| 102 | + hasMorePages = (highestPageResult?.hasMorePages ?? false) && !newProducts.isEmpty |
| 103 | + currentPage += batchSize |
| 104 | + |
| 105 | + DDLogInfo("📥 Loaded batch: \(batchResults.count) pages, total products: \(allProducts.count), hasMorePages: \(hasMorePages)") |
| 106 | + } |
| 107 | + |
| 108 | + DDLogInfo("✅ Products sync complete: \(allProducts.count) products loaded") |
| 109 | + return allProducts |
| 110 | + } |
| 111 | + |
| 112 | + func loadAllProductVariations(for siteID: Int64, syncRemote: POSCatalogSyncRemoteProtocol) async throws -> [POSProductVariation] { |
| 113 | + DDLogInfo("🔄 Starting variations sync for site ID: \(siteID)") |
| 114 | + |
| 115 | + var allVariations: [POSProductVariation] = [] |
| 116 | + var currentPage = 1 |
| 117 | + var hasMorePages = true |
| 118 | + |
| 119 | + while hasMorePages { |
| 120 | + let pagesToFetch = Array(currentPage..<(currentPage + batchSize)) |
| 121 | + |
| 122 | + let batchResults = try await withThrowingTaskGroup(of: PageResult<POSProductVariation>.self) { group in |
| 123 | + for pageNumber in pagesToFetch { |
| 124 | + group.addTask { |
| 125 | + let result = try await syncRemote.loadProductVariations(siteID: siteID, pageNumber: pageNumber) |
| 126 | + return PageResult(pageNumber: pageNumber, items: result) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + var results: [PageResult<POSProductVariation>] = [] |
| 131 | + for try await result in group { |
| 132 | + results.append(result) |
| 133 | + } |
| 134 | + return results.sorted(by: { $0.pageNumber < $1.pageNumber }) |
| 135 | + } |
| 136 | + |
| 137 | + // Processes results in order and checks if there are more pages. |
| 138 | + let newVariations = batchResults.flatMap { $0.items.items } |
| 139 | + allVariations.append(contentsOf: newVariations) |
| 140 | + |
| 141 | + let highestPageResult = batchResults.last?.items |
| 142 | + hasMorePages = (highestPageResult?.hasMorePages ?? false) && !newVariations.isEmpty |
| 143 | + currentPage += batchSize |
| 144 | + |
| 145 | + DDLogInfo("📥 Loaded batch: \(batchResults.count) pages, total variations: \(allVariations.count), hasMorePages: \(hasMorePages)") |
| 146 | + } |
| 147 | + |
| 148 | + DDLogInfo("✅ Variations sync complete: \(allVariations.count) variations loaded") |
| 149 | + return allVariations |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +private struct PageResult<T> { |
| 154 | + let pageNumber: Int |
| 155 | + let items: PagedItems<T> |
| 156 | +} |
0 commit comments