|
| 1 | +import Foundation |
| 2 | +import GRDB |
| 3 | +import protocol Storage.GRDBManagerProtocol |
| 4 | + |
| 5 | +public protocol POSCatalogSettingsServiceProtocol { |
| 6 | + /// Gets catalog statistics for the specified site. |
| 7 | + /// - Parameter siteID: The site ID to get catalog statistics for. |
| 8 | + /// - Returns: Catalog statistics including product/variation counts. |
| 9 | + func loadCatalogStatistics(for siteID: Int64) async throws -> POSCatalogStatistics |
| 10 | + |
| 11 | + /// Gets the last sync dates for the specified site. |
| 12 | + /// - Parameter siteID: The site ID to get sync dates for. |
| 13 | + /// - Returns: Sync dates information. |
| 14 | + func loadSyncDates(for siteID: Int64) async throws -> POSSyncDates |
| 15 | +} |
| 16 | + |
| 17 | +public struct POSCatalogStatistics { |
| 18 | + public let productCount: Int |
| 19 | + public let variationCount: Int |
| 20 | + |
| 21 | + public init(productCount: Int, variationCount: Int) { |
| 22 | + self.productCount = productCount |
| 23 | + self.variationCount = variationCount |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +public struct POSSyncDates { |
| 28 | + public let lastFullSyncDate: Date? |
| 29 | + public let lastIncrementalSyncDate: Date? |
| 30 | + |
| 31 | + public init(lastFullSyncDate: Date?, lastIncrementalSyncDate: Date?) { |
| 32 | + self.lastFullSyncDate = lastFullSyncDate |
| 33 | + self.lastIncrementalSyncDate = lastIncrementalSyncDate |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +public class POSCatalogSettingsService: POSCatalogSettingsServiceProtocol { |
| 38 | + private let grdbManager: GRDBManagerProtocol |
| 39 | + |
| 40 | + public init(grdbManager: GRDBManagerProtocol) { |
| 41 | + self.grdbManager = grdbManager |
| 42 | + } |
| 43 | + |
| 44 | + public func loadCatalogStatistics(for siteID: Int64) async throws -> POSCatalogStatistics { |
| 45 | + try await grdbManager.databaseConnection.read { db in |
| 46 | + let productCount = try PersistedProduct.filter { $0.siteID == siteID }.fetchCount(db) |
| 47 | + let variationCount = try PersistedProductVariation.filter { $0.siteID == siteID }.fetchCount(db) |
| 48 | + return POSCatalogStatistics(productCount: productCount, variationCount: variationCount) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public func loadSyncDates(for siteID: Int64) async throws -> POSSyncDates { |
| 53 | + try await grdbManager.databaseConnection.read { db in |
| 54 | + let site = try PersistedSite.filter(key: siteID).fetchOne(db) |
| 55 | + return POSSyncDates( |
| 56 | + lastFullSyncDate: site?.lastCatalogFullSyncDate, |
| 57 | + lastIncrementalSyncDate: site?.lastCatalogIncrementalSyncDate |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments