Skip to content

Commit 4dd41e5

Browse files
committed
Merge branch 'trunk' into woomob-1095-woo-poslocal-catalog-bg-app-refresh-task-dispatching
2 parents 7abfa86 + 17d34bf commit 4dd41e5

File tree

41 files changed

+3642
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3642
-285
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
/// Mapper: Booking
4+
///
5+
struct BookingMapper: Mapper {
6+
let siteID: Int64
7+
8+
func map(response: Data) throws -> Booking {
9+
let decoder = JSONDecoder()
10+
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)
11+
decoder.userInfo = [
12+
.siteID: siteID
13+
]
14+
if hasDataEnvelope(in: response) {
15+
return try decoder.decode(BookingEnvelope.self, from: response).booking
16+
} else {
17+
return try decoder.decode(Booking.self, from: response)
18+
}
19+
}
20+
}
21+
22+
private struct BookingEnvelope: Decodable {
23+
let booking: Booking
24+
25+
private enum CodingKeys: String, CodingKey {
26+
case booking = "data"
27+
}
28+
}

Modules/Sources/Networking/Remote/BookingsRemote.swift

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ public protocol BookingsRemoteProtocol {
1010
pageNumber: Int,
1111
pageSize: Int,
1212
startDateBefore: String?,
13-
startDateAfter: String?) async throws -> [Booking]
13+
startDateAfter: String?,
14+
searchQuery: String?,
15+
order: BookingsRemote.Order) async throws -> [Booking]
16+
17+
func loadBooking(bookingID: Int64,
18+
siteID: Int64) async throws -> Booking?
1419
}
1520

1621
/// Booking: Remote Endpoints
@@ -27,15 +32,20 @@ public final class BookingsRemote: Remote, BookingsRemoteProtocol {
2732
/// - pageSize: Number of bookings to be retrieved per page.
2833
/// - startDateBefore: Filter bookings with start date before this timestamp.
2934
/// - startDateAfter: Filter bookings with start date after this timestamp.
35+
/// - searchQuery: Search query to filter bookings.
36+
/// - order: Sort order for bookings (ascending or descending).
3037
///
3138
public func loadAllBookings(for siteID: Int64,
3239
pageNumber: Int = Default.pageNumber,
3340
pageSize: Int = Default.pageSize,
3441
startDateBefore: String? = nil,
35-
startDateAfter: String? = nil) async throws -> [Booking] {
42+
startDateAfter: String? = nil,
43+
searchQuery: String? = nil,
44+
order: Order) async throws -> [Booking] {
3645
var parameters = [
3746
ParameterKey.page: String(pageNumber),
38-
ParameterKey.perPage: String(pageSize)
47+
ParameterKey.perPage: String(pageSize),
48+
ParameterKey.order: order.rawValue
3949
]
4050

4151
if let startDateBefore = startDateBefore {
@@ -46,12 +56,34 @@ public final class BookingsRemote: Remote, BookingsRemoteProtocol {
4656
parameters[ParameterKey.startDateAfter] = startDateAfter
4757
}
4858

59+
if let searchQuery = searchQuery, !searchQuery.isEmpty {
60+
parameters[ParameterKey.search] = searchQuery
61+
}
62+
4963
let path = Path.bookings
5064
let request = JetpackRequest(wooApiVersion: .wcBookings, method: .get, siteID: siteID, path: path, parameters: parameters, availableAsRESTRequest: true)
5165
let mapper = ListMapper<Booking>(siteID: siteID)
5266

5367
return try await enqueue(request, mapper: mapper)
5468
}
69+
70+
public func loadBooking(
71+
bookingID: Int64,
72+
siteID: Int64
73+
) async throws -> Booking? {
74+
let path = "\(Path.bookings)/\(bookingID)"
75+
let request = JetpackRequest(
76+
wooApiVersion: .wcBookings,
77+
method: .get,
78+
siteID: siteID,
79+
path: path,
80+
availableAsRESTRequest: true
81+
)
82+
83+
let mapper = BookingMapper(siteID: siteID)
84+
85+
return try await enqueue(request, mapper: mapper)
86+
}
5587
}
5688

5789
// MARK: - Constants
@@ -62,6 +94,11 @@ public extension BookingsRemote {
6294
public static let pageNumber: Int = Remote.Default.firstPageNumber
6395
}
6496

97+
enum Order: String {
98+
case ascending = "asc"
99+
case descending = "desc"
100+
}
101+
65102
private enum Path {
66103
static let bookings = "bookings"
67104
}
@@ -71,5 +108,7 @@ public extension BookingsRemote {
71108
static let perPage: String = "per_page"
72109
static let startDateBefore: String = "start_date_before"
73110
static let startDateAfter: String = "start_date_after"
111+
static let search: String = "search"
112+
static let order: String = "order"
74113
}
75114
}

Modules/Sources/NetworkingCore/Remote/OrdersRemote.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import Foundation
22

3+
public protocol OrdersRemoteProtocol {
4+
func loadOrders(
5+
for siteID: Int64,
6+
orderIDs: [Int64]
7+
) async throws -> [Order]
8+
}
9+
310
/// Order: Remote Endpoints
411
///
5-
public class OrdersRemote: Remote {
12+
public class OrdersRemote: Remote, OrdersRemoteProtocol {
613
/// The source of the order creation.
714
public enum OrderCreationSource {
815
case storeManagement
@@ -111,6 +118,41 @@ public class OrdersRemote: Remote {
111118
enqueue(request, mapper: mapper, completion: completion)
112119
}
113120

121+
/// Retrieves specific `Order`s.
122+
///
123+
/// - Parameters:
124+
/// - siteID: Site for which we'll fetch remote orders.
125+
/// - orderIDs: The IDs of the orders to fetch.
126+
/// - Returns: Array of orders.
127+
/// - Throws: Network or parsing errors.
128+
///
129+
public func loadOrders(
130+
for siteID: Int64,
131+
orderIDs: [Int64]
132+
) async throws -> [Order] {
133+
guard !orderIDs.isEmpty else {
134+
return []
135+
}
136+
137+
let parameters: [String: Any] = [
138+
ParameterKeys.include: Set(orderIDs).map(String.init).joined(separator: ","),
139+
ParameterKeys.fields: ParameterValues.fieldValues
140+
]
141+
142+
let path = Constants.ordersPath
143+
let request = JetpackRequest(
144+
wooApiVersion: .mark3,
145+
method: .get,
146+
siteID: siteID,
147+
path: path,
148+
parameters: parameters,
149+
availableAsRESTRequest: true
150+
)
151+
let mapper = OrderListMapper(siteID: siteID)
152+
153+
return try await enqueue(request, mapper: mapper)
154+
}
155+
114156
/// Retrieves the notes for a specific `Order`
115157
///
116158
/// - Parameters:
@@ -534,6 +576,7 @@ public extension OrdersRemote {
534576
static let addedByUser: String = "added_by_user"
535577
static let customerNote: String = "customer_note"
536578
static let keyword: String = "search"
579+
static let include: String = "include"
537580
static let note: String = "note"
538581
static let page: String = "page"
539582
static let perPage: String = "per_page"

Modules/Sources/Storage/Model/Booking/Booking+CoreDataProperties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ extension Booking {
2121
@NSManaged public var statusKey: String?
2222
@NSManaged public var localTimezone: String?
2323
@NSManaged public var currency: String?
24-
24+
@NSManaged public var orderInfo: BookingOrderInfo?
2525
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
import CoreData
3+
4+
@objc(BookingCustomerInfo)
5+
public class BookingCustomerInfo: NSManagedObject {
6+
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
import CoreData
3+
4+
extension BookingCustomerInfo {
5+
@NSManaged public var billingAddress1: String?
6+
@NSManaged public var billingAddress2: String?
7+
@NSManaged public var billingCity: String?
8+
@NSManaged public var billingCompany: String?
9+
@NSManaged public var billingCountry: String?
10+
@NSManaged public var billingEmail: String?
11+
@NSManaged public var billingFirstName: String?
12+
@NSManaged public var billingLastName: String?
13+
@NSManaged public var billingPhone: String?
14+
@NSManaged public var billingPostcode: String?
15+
@NSManaged public var billingState: String?
16+
@NSManaged public var orderInfo: BookingOrderInfo?
17+
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
import CoreData
3+
4+
@objc(BookingOrderInfo)
5+
public class BookingOrderInfo: NSManagedObject {
6+
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
import CoreData
3+
4+
extension BookingOrderInfo {
5+
@NSManaged public var statusKey: String?
6+
@NSManaged public var paymentInfo: BookingPaymentInfo?
7+
@NSManaged public var customerInfo: BookingCustomerInfo?
8+
@NSManaged public var productInfo: BookingProductInfo?
9+
@NSManaged public var booking: Booking?
10+
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
import CoreData
3+
4+
@objc(BookingPaymentInfo)
5+
public class BookingPaymentInfo: NSManagedObject {
6+
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Foundation
2+
import CoreData
3+
4+
extension BookingPaymentInfo {
5+
@NSManaged public var paymentMethodID: String?
6+
@NSManaged public var paymentMethodTitle: String?
7+
@NSManaged public var subtotal: String?
8+
@NSManaged public var subtotalTax: String?
9+
@NSManaged public var total: String?
10+
@NSManaged public var totalTax: String?
11+
@NSManaged public var orderInfo: BookingOrderInfo?
12+
}

0 commit comments

Comments
 (0)