Skip to content

Commit dd60db1

Browse files
committed
Add loadOrder method for fetching a specific order
1 parent 710519d commit dd60db1

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

Modules/Sources/NetworkingCore/Remote/OrdersRemote.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,24 @@ public extension OrdersRemote {
598598
case customerID
599599
case currency
600600
}
601+
602+
/// Loads a single order asynchronously for POS
603+
/// - Parameters:
604+
/// - siteID: Site for which we'll fetch the order.
605+
/// - orderID: ID of the order to load.
606+
/// - Returns: The loaded Order.
607+
/// - Throws: Network or parsing errors.
608+
func loadPOSOrder(siteID: Int64, orderID: Int64) async throws -> Order {
609+
let path = "\(Constants.ordersPath)/\(orderID)"
610+
let request = JetpackRequest(wooApiVersion: .mark3,
611+
method: .get,
612+
siteID: siteID,
613+
path: path,
614+
availableAsRESTRequest: true)
615+
let mapper = OrderMapper(siteID: siteID)
616+
617+
return try await enqueue(request, mapper: mapper)
618+
}
601619
}
602620

603621
private extension OrdersRemote.OrderCreationSource {

Modules/Sources/NetworkingCore/Remote/POSOrdersRemoteProtocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public protocol POSOrdersRemoteProtocol {
1919
order: Order,
2020
fields: [OrdersRemote.CreateOrderField]) async throws -> Order
2121

22+
func loadPOSOrder(siteID: Int64, orderID: Int64) async throws -> Order
23+
2224
func loadPOSOrders(siteID: Int64,
2325
pageNumber: Int,
2426
pageSize: Int) async throws -> PagedItems<Order>

Modules/Sources/Yosemite/PointOfSale/OrderList/PointOfSaleOrderListFetchStrategy.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import struct NetworkingCore.PagedItems
33

44
public protocol PointOfSaleOrderListFetchStrategy {
55
func fetchOrders(pageNumber: Int) async throws -> PagedItems<POSOrder>
6+
func loadOrder(orderID: Int64) async throws -> POSOrder
67
var supportsCaching: Bool { get }
78
var showsLoadingWithItems: Bool { get }
89
var id: String { get }
@@ -26,6 +27,10 @@ struct PointOfSaleDefaultOrderListFetchStrategy: PointOfSaleOrderListFetchStrate
2627
func fetchOrders(pageNumber: Int) async throws -> PagedItems<POSOrder> {
2728
try await orderListService.providePointOfSaleOrders(pageNumber: pageNumber)
2829
}
30+
31+
func loadOrder(orderID: Int64) async throws -> POSOrder {
32+
try await orderListService.loadOrder(orderID: orderID)
33+
}
2934
}
3035

3136
struct PointOfSaleSearchOrderListFetchStrategy: PointOfSaleOrderListFetchStrategy {
@@ -43,4 +48,8 @@ struct PointOfSaleSearchOrderListFetchStrategy: PointOfSaleOrderListFetchStrateg
4348
func fetchOrders(pageNumber: Int) async throws -> PagedItems<POSOrder> {
4449
try await orderListService.searchPointOfSaleOrders(searchTerm: searchTerm, pageNumber: pageNumber)
4550
}
51+
52+
func loadOrder(orderID: Int64) async throws -> POSOrder {
53+
try await orderListService.loadOrder(orderID: orderID)
54+
}
4655
}

Modules/Sources/Yosemite/PointOfSale/OrderList/PointOfSaleOrderListService.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@ public final class PointOfSaleOrderListService: PointOfSaleOrderListServiceProto
7070
throw PointOfSaleOrderListServiceError.requestFailed
7171
}
7272
}
73+
74+
public func loadOrder(orderID: Int64) async throws -> POSOrder {
75+
do {
76+
let order = try await ordersRemote.loadPOSOrder(siteID: siteID, orderID: orderID)
77+
return mapper.map(order: order)
78+
} catch AFError.explicitlyCancelled {
79+
throw PointOfSaleOrderListServiceError.requestCancelled
80+
} catch {
81+
throw PointOfSaleOrderListServiceError.requestFailed
82+
}
83+
}
7384
}

Modules/Sources/Yosemite/PointOfSale/OrderList/PointOfSaleOrderListServiceProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public enum PointOfSaleOrderListServiceError: Error, Equatable {
99
public protocol PointOfSaleOrderListServiceProtocol {
1010
func providePointOfSaleOrders(pageNumber: Int) async throws -> PagedItems<POSOrder>
1111
func searchPointOfSaleOrders(searchTerm: String, pageNumber: Int) async throws -> PagedItems<POSOrder>
12+
func loadOrder(orderID: Int64) async throws -> POSOrder
1213
}

0 commit comments

Comments
 (0)