11import protocol Networking. Network
22import protocol Networking. ProductVariationsRemoteProtocol
3- import class Networking. ProductsRemote
4- import class Networking. ProductVariationsRemote
3+ import class Networking. CouponsRemote
54import class Networking. AlamofireNetwork
65import class WooFoundation. CurrencyFormatter
76import class WooFoundation. CurrencySettings
@@ -10,8 +9,7 @@ import Storage
109public final class PointOfSaleCouponService : PointOfSaleItemServiceProtocol {
1110 private var siteID : Int64
1211 private let currencyFormatter : CurrencyFormatter
13- private let productsRemote : ProductsRemote
14- private let variationRemote : ProductVariationsRemoteProtocol
12+ private let couponsRemote : CouponsRemote
1513 private let storage : StorageManagerType ?
1614
1715 public init ( siteID: Int64 ,
@@ -20,8 +18,7 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
2018 storage: StorageManagerType ? = nil ) {
2119 self . siteID = siteID
2220 self . currencyFormatter = CurrencyFormatter ( currencySettings: currencySettings)
23- self . productsRemote = ProductsRemote ( network: network)
24- self . variationRemote = ProductVariationsRemote ( network: network)
21+ self . couponsRemote = CouponsRemote ( network: network)
2522 self . storage = storage
2623 }
2724
@@ -38,26 +35,36 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
3835 // TODO:
3936 // gh-15326 - Return PagedItems<POSItem> instead.
4037 @MainActor
41- public func providePointOfSaleCoupons( ) -> [ POSItem ] {
38+ public func providePointOfSaleCoupons( ) async -> [ POSItem ] {
4239 guard let storage = storage else {
4340 return [ ]
4441 }
45- let predicate = NSPredicate ( format: " siteID == %lld " , siteID)
46- let descriptor = NSSortDescriptor ( keyPath: \StorageCoupon . dateCreated,
47- ascending: false )
48-
49- let resultsController = ResultsController < StorageCoupon > ( storageManager: storage,
50- matching: predicate,
51- sortedBy: [ descriptor] )
52-
42+ // #2 Remote
5343 do {
54- try resultsController. performFetch ( )
55- let storeCoupons = resultsController. fetchedObjects
56- return mapCouponsToPOSItems ( coupons: storeCoupons)
44+ let remoteCoupons = try await couponsRemote. loadAllCoupons ( for: siteID)
45+ return mapCouponsToPOSItems ( coupons: remoteCoupons)
5746 } catch {
5847 debugPrint ( error)
5948 return [ ]
6049 }
50+
51+ // #1 Storage
52+ // let predicate = NSPredicate(format: "siteID == %lld", siteID)
53+ // let descriptor = NSSortDescriptor(keyPath: \StorageCoupon.dateCreated,
54+ // ascending: false)
55+ //
56+ // let resultsController = ResultsController<StorageCoupon>(storageManager: storage,
57+ // matching: predicate,
58+ // sortedBy: [descriptor])
59+ //
60+ // do {
61+ // try resultsController.performFetch()
62+ // let storeCoupons = resultsController.fetchedObjects
63+ // return mapCouponsToPOSItems(coupons: storeCoupons)
64+ // } catch {
65+ // debugPrint(error)
66+ // return []
67+ // }
6168 }
6269
6370 private func mapCouponsToPOSItems( coupons: [ Coupon ] ) -> [ POSItem ] {
@@ -73,7 +80,7 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
7380
7481 @MainActor
7582 public func providePointOfSaleItems( pageNumber: Int ) async throws -> PagedItems < POSItem > {
76- let coupons = providePointOfSaleCoupons ( )
83+ let coupons = await providePointOfSaleCoupons ( )
7784 return . init( items: coupons, hasMorePages: false )
7885 }
7986}
0 commit comments