Skip to content

Commit

Permalink
[Woo POS] Remove itemlist dependency for order sync (#14507)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshheald authored Nov 26, 2024
2 parents 89b3adc + a8472ef commit 8356224
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 54 deletions.
6 changes: 6 additions & 0 deletions Networking/Networking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
209AD3C52AC19E7500825D76 /* WooPaymentsDepositsOverviewMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 209AD3C42AC19E7500825D76 /* WooPaymentsDepositsOverviewMapper.swift */; };
20D210C32B1780CE0099E517 /* deposits-overview-all.json in Resources */ = {isa = PBXBuildFile; fileRef = 20D210C22B1780CE0099E517 /* deposits-overview-all.json */; };
20D210C52B1788E60099E517 /* deposits-overview-all-no-default-currency.json in Resources */ = {isa = PBXBuildFile; fileRef = 20D210C42B1788E60099E517 /* deposits-overview-all-no-default-currency.json */; };
20F616482CF4B74600F9FA2A /* POSOrdersRemoteProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20F616472CF4B74600F9FA2A /* POSOrdersRemoteProtocol.swift */; };
20F616492CF4B8CD00F9FA2A /* POSOrdersRemoteProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20F616472CF4B74600F9FA2A /* POSOrdersRemoteProtocol.swift */; };
21DB5B99C4107CF69C0A57EC /* Pods_NetworkingTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 69314EDE650855CAF927057E /* Pods_NetworkingTests.framework */; };
24F98C522502E79800F49B68 /* FeatureFlagRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24F98C512502E79800F49B68 /* FeatureFlagRemote.swift */; };
24F98C5E2502EDCF00F49B68 /* BundleWooTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24F98C5D2502EDCF00F49B68 /* BundleWooTests.swift */; };
Expand Down Expand Up @@ -1470,6 +1472,7 @@
209AD3C42AC19E7500825D76 /* WooPaymentsDepositsOverviewMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WooPaymentsDepositsOverviewMapper.swift; sourceTree = "<group>"; };
20D210C22B1780CE0099E517 /* deposits-overview-all.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "deposits-overview-all.json"; sourceTree = "<group>"; };
20D210C42B1788E60099E517 /* deposits-overview-all-no-default-currency.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "deposits-overview-all-no-default-currency.json"; sourceTree = "<group>"; };
20F616472CF4B74600F9FA2A /* POSOrdersRemoteProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = POSOrdersRemoteProtocol.swift; sourceTree = "<group>"; };
24F98C512502E79800F49B68 /* FeatureFlagRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureFlagRemote.swift; sourceTree = "<group>"; };
24F98C5D2502EDCF00F49B68 /* BundleWooTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BundleWooTests.swift; sourceTree = "<group>"; };
24F98C5F2502EF8200F49B68 /* FeatureFlagRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureFlagRemoteTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2891,6 +2894,7 @@
03EB99872906A78400F06A39 /* JustInTimeMessagesRemote.swift */,
B5DAEFEF2180DD5A0002356A /* NotificationsRemote.swift */,
B557DA0120975500005962F4 /* OrdersRemote.swift */,
20F616472CF4B74600F9FA2A /* POSOrdersRemoteProtocol.swift */,
D8FBFF0A22D3ADB1006E3336 /* OrderStatsRemoteV4.swift */,
261CF1B7255AE62D0090D8D3 /* PaymentGatewayRemote.swift */,
45152808257A7C6E0076B03C /* ProductAttributesRemote.swift */,
Expand Down Expand Up @@ -4825,6 +4829,7 @@
263A6DBE2BEAA4A900C292B2 /* MIContainer.swift in Sources */,
263A6DBB2BEAA49B00C292B2 /* AnyCodable.swift in Sources */,
26373E0D2BFCEAFB008E6735 /* Address.swift in Sources */,
20F616492CF4B8CD00F9FA2A /* POSOrdersRemoteProtocol.swift in Sources */,
26373E2A2BFD08AF008E6735 /* WatchOS-Models+Copiable.swift in Sources */,
263A6DBC2BEAA49B00C292B2 /* AnyDecodable.swift in Sources */,
263A6DBD2BEAA49B00C292B2 /* AnyEncodable.swift in Sources */,
Expand Down Expand Up @@ -5084,6 +5089,7 @@
933A2732222234F800C2143A /* Logging.swift in Sources */,
CE50346021B5799F007573C6 /* SitePlan.swift in Sources */,
EE1CB9092B4BC8C500AD24D5 /* BlazeTargetOptionMappers.swift in Sources */,
20F616482CF4B74600F9FA2A /* POSOrdersRemoteProtocol.swift in Sources */,
DE2E8E9F295310C5002E4B14 /* WordPressSiteMapper.swift in Sources */,
B59325CC217E2B4C000B0E8E /* NoteListMapper.swift in Sources */,
B505F6CD20BEE37E00BB1B69 /* AccountMapper.swift in Sources */,
Expand Down
54 changes: 28 additions & 26 deletions Networking/Networking/Remote/OrdersRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,6 @@ public class OrdersRemote: Remote {
}
}

public func createPOSOrder(siteID: Int64, order: Order, fields: [CreateOrderField]) async throws -> Order {
return try await withCheckedThrowingContinuation { continuation in
createOrder(siteID: siteID, order: order, giftCard: nil, fields: fields) { result in
switch result {
case let .success(order):
continuation.resume(returning: order)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}

/// Updates the `OrderStatus` of a given Order.
///
/// - Parameters:
Expand Down Expand Up @@ -325,19 +312,6 @@ public class OrdersRemote: Remote {
}
}

public func updatePOSOrder(siteID: Int64, order: Order, fields: [UpdateOrderField]) async throws -> Order {
return try await withCheckedThrowingContinuation { continuation in
updateOrder(from: siteID, order: order, giftCard: nil, fields: fields) { result in
switch result {
case let .success(order):
continuation.resume(returning: order)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}

/// Adds an order note to a specific Order.
///
/// - Parameters:
Expand Down Expand Up @@ -411,6 +385,34 @@ public class OrdersRemote: Remote {
}
}

extension OrdersRemote: POSOrdersRemoteProtocol {
public func createPOSOrder(siteID: Int64, order: Order, fields: [CreateOrderField]) async throws -> Order {
return try await withCheckedThrowingContinuation { continuation in
createOrder(siteID: siteID, order: order, giftCard: nil, fields: fields) { result in
switch result {
case let .success(order):
continuation.resume(returning: order)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}

public func updatePOSOrder(siteID: Int64, order: Order, fields: [UpdateOrderField]) async throws -> Order {
return try await withCheckedThrowingContinuation { continuation in
updateOrder(from: siteID, order: order, giftCard: nil, fields: fields) { result in
switch result {
case let .success(order):
continuation.resume(returning: order)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}
}


// MARK: - Constants!
//
Expand Down
11 changes: 11 additions & 0 deletions Networking/Networking/Remote/POSOrdersRemoteProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

public protocol POSOrdersRemoteProtocol {
func updatePOSOrder(siteID: Int64,
order: Order,
fields: [OrdersRemote.UpdateOrderField]) async throws -> Order

func createPOSOrder(siteID: Int64,
order: Order,
fields: [OrdersRemote.CreateOrderField]) async throws -> Order
}
10 changes: 5 additions & 5 deletions WooCommerce/Classes/POS/Models/PointOfSaleAggregateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,21 @@ extension PointOfSaleAggregateModel {
return
}
// calculate totals and sync order if there was a change in the cart
await syncOrder(for: cart, allItems: itemsService.allItems)
await syncOrder(for: cart)
}

@MainActor
private func syncOrder(for cartProducts: [CartItem], allItems: [POSItem]) async {
private func syncOrder(for cartProducts: [CartItem]) async {
guard orderState.isSyncing == false else {
return
}
orderState = .syncing
let cart = cartProducts.map {
POSCartItem(itemID: nil, product: $0.item, quantity: Decimal($0.quantity))
POSCartItem(product: $0.item, quantity: Decimal($0.quantity))
}

do {
let syncedOrder = try await orderService.syncOrder(cart: cart, order: order, allProducts: allItems)
let syncedOrder = try await orderService.syncOrder(cart: cart, order: order)
self.order = syncedOrder
orderState = .loaded(totals(for: syncedOrder))
await startPaymentWhenCardReaderConnected()
Expand All @@ -386,7 +386,7 @@ extension PointOfSaleAggregateModel {
// Consider removing error or handle specific errors with our own formatting and localization
orderState = .error(.init(message: error.localizedDescription, handler: { [weak self] in
Task {
await self?.syncOrder(for: cartProducts, allItems: allItems)
await self?.syncOrder(for: cartProducts)
}
}))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import enum Yosemite.POSProductProviderError

protocol PointOfSaleItemsServiceProtocol {
var itemListStatePublisher: any Publisher<ItemListState, Never> { get }
@available(*, deprecated, message: "allItems will be removed in a future release. Use itemListState's associated value instead")
var allItems: [POSItem] { get }
func loadInitialItems() async
func loadNextItems() async
func reload() async
Expand All @@ -16,7 +14,7 @@ protocol PointOfSaleItemsServiceProtocol {
class PointOfSaleItemsService: PointOfSaleItemsServiceProtocol {
private(set) var itemListStatePublisher: any Publisher<ItemListState, Never>
private var itemListStateSubject: PassthroughSubject<ItemListState, Never> = .init()
private(set) var allItems: [POSItem] = []
private var allItems: [POSItem] = []
private var currentPage: Int = Constants.initialPage
private var mightHaveMorePages: Bool = true
private let itemProvider: POSItemProvider
Expand Down
2 changes: 1 addition & 1 deletion WooCommerce/Classes/POS/Utils/POSOrderPreviewService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import enum Yosemite.OrderFactory
import struct Yosemite.Order

class POSOrderPreviewService: POSOrderServiceProtocol {
func syncOrder(cart: [POSCartItem], order: Order?, allProducts: [any POSItem]) async throws -> Order {
func syncOrder(cart: [POSCartItem], order: Order?) async throws -> Order {
OrderFactory.emptyNewOrder
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
class MockPOSOrderService: POSOrderServiceProtocol {
var simulateSyncing = false
var orderToReturn: Order?
func syncOrder(cart: [Yosemite.POSCartItem], order: Yosemite.Order?, allProducts: [any Yosemite.POSItem]) async throws -> Yosemite.Order {
func syncOrder(cart: [Yosemite.POSCartItem], order: Yosemite.Order?) async throws -> Yosemite.Order {
if simulateSyncing {
try await Task.sleep(nanoseconds: UInt64(1 * Double(NSEC_PER_SEC)))
}
Expand Down
16 changes: 16 additions & 0 deletions Yosemite/Yosemite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@
209AD3CE2AC1A9C200825D76 /* WooPaymentsPayoutsOverview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 209AD3CD2AC1A9C200825D76 /* WooPaymentsPayoutsOverview.swift */; };
20BCF6F22B0E554500954840 /* SystemStatusService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20BCF6F12B0E554500954840 /* SystemStatusService.swift */; };
20BCF6F52B0E57AB00954840 /* MockSystemStatusService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20BCF6F42B0E57AB00954840 /* MockSystemStatusService.swift */; };
20CF75B82CF4C3B900ACCF4A /* MockPOSOrdersRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20CF75B72CF4C3AD00ACCF4A /* MockPOSOrdersRemote.swift */; };
20D035002CDBBD6400C0F901 /* CommonReaderConfigProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D034FF2CDBBD6400C0F901 /* CommonReaderConfigProviderTests.swift */; };
20D035022CDBBE2A00C0F901 /* MockCardReaderCapableRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D035012CDBBE2A00C0F901 /* MockCardReaderCapableRemote.swift */; };
20D210C12B177EEF0099E517 /* WooPaymentsPayoutServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D210C02B177EEF0099E517 /* WooPaymentsPayoutServiceTests.swift */; };
20F616462CF4B4E500F9FA2A /* POSOrderServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20F616452CF4B4DE00F9FA2A /* POSOrderServiceTests.swift */; };
24163B9E257F41A600F94EC3 /* StoresManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24163B9D257F41A600F94EC3 /* StoresManager.swift */; };
24163BA8257F41C500F94EC3 /* SessionManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24163BA7257F41C500F94EC3 /* SessionManagerProtocol.swift */; };
247CE7AB2582DB9300F9D9D1 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 247CE7AA2582DB9300F9D9D1 /* String+Extensions.swift */; };
Expand Down Expand Up @@ -667,9 +669,11 @@
209AD3CD2AC1A9C200825D76 /* WooPaymentsPayoutsOverview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WooPaymentsPayoutsOverview.swift; sourceTree = "<group>"; };
20BCF6F12B0E554500954840 /* SystemStatusService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemStatusService.swift; sourceTree = "<group>"; };
20BCF6F42B0E57AB00954840 /* MockSystemStatusService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MockSystemStatusService.swift; path = ../../../WooCommerce/WooCommerceTests/Mocks/MockSystemStatusService.swift; sourceTree = "<group>"; };
20CF75B72CF4C3AD00ACCF4A /* MockPOSOrdersRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPOSOrdersRemote.swift; sourceTree = "<group>"; };
20D034FF2CDBBD6400C0F901 /* CommonReaderConfigProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommonReaderConfigProviderTests.swift; sourceTree = "<group>"; };
20D035012CDBBE2A00C0F901 /* MockCardReaderCapableRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCardReaderCapableRemote.swift; sourceTree = "<group>"; };
20D210C02B177EEF0099E517 /* WooPaymentsPayoutServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WooPaymentsPayoutServiceTests.swift; sourceTree = "<group>"; };
20F616452CF4B4DE00F9FA2A /* POSOrderServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = POSOrderServiceTests.swift; sourceTree = "<group>"; };
24163B9D257F41A600F94EC3 /* StoresManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoresManager.swift; sourceTree = "<group>"; };
24163BA7257F41C500F94EC3 /* SessionManagerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionManagerProtocol.swift; sourceTree = "<group>"; };
247CE7AA2582DB9300F9D9D1 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1257,6 +1261,14 @@
path = Payments;
sourceTree = "<group>";
};
20F616442CF4B4D800F9FA2A /* POS */ = {
isa = PBXGroup;
children = (
20F616452CF4B4DE00F9FA2A /* POSOrderServiceTests.swift */,
);
path = POS;
sourceTree = "<group>";
};
247CE7AF2582DBD000F9D9D1 /* Mocks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1904,6 +1916,7 @@
B5C9DE1D2087FF20006B910A /* Mocks */ = {
isa = PBXGroup;
children = (
20CF75B72CF4C3AD00ACCF4A /* MockPOSOrdersRemote.swift */,
D88303EE25E45E5500C877F9 /* CardPresentPayments */,
578CE7892475E78C00492EBF /* Networking */,
D8BD6A4B229D07C8007CAD6C /* custom-shipment-provider.plist */,
Expand Down Expand Up @@ -1993,6 +2006,7 @@
B5F2AE9320EBAD5200FEDC59 /* Tools */ = {
isa = PBXGroup;
children = (
20F616442CF4B4D800F9FA2A /* POS */,
20D210BF2B177EDB0099E517 /* Payments */,
E18FDAFF28F98360008519BA /* InAppPurchases */,
02FF055723D984500058E6E7 /* Media */,
Expand Down Expand Up @@ -2659,6 +2673,7 @@
0391C13D29CC5E14008A3E86 /* OrderCardPresentPaymentEligibilityStoreTests.swift in Sources */,
B5C9DE222087FF20006B910A /* DispatcherTests.swift in Sources */,
DEDA8DB32B184B950076BF0F /* WordPressThemeStoreTests.swift in Sources */,
20CF75B82CF4C3B900ACCF4A /* MockPOSOrdersRemote.swift in Sources */,
45182D2327B55F9C00B4C05C /* InboxNotesStoreTests.swift in Sources */,
03FBDA2A263296C400ACE257 /* CouponStoreTests.swift in Sources */,
DAD988C42C4A5147009DE9E3 /* LocalIDStoreTests.swift in Sources */,
Expand Down Expand Up @@ -2710,6 +2725,7 @@
02DAE7FA291A9F36009342B7 /* MockDomainRemote.swift in Sources */,
741F34842195F752005F5BD9 /* CommentStoreTests.swift in Sources */,
B5C9DE282087FF20006B910A /* MockSiteStore.swift in Sources */,
20F616462CF4B4E500F9FA2A /* POSOrderServiceTests.swift in Sources */,
FEEB2F5F268A1C5E0075A6E0 /* User+RolesTests.swift in Sources */,
DEB387882C2C09FC0025256E /* MockGoogleListingsAndAdsRemote.swift in Sources */,
B5BC736820D1AA8F00B5B6FA /* AccountStoreTests.swift in Sources */,
Expand Down
Loading

0 comments on commit 8356224

Please sign in to comment.