Skip to content

Commit a3282dc

Browse files
committed
Merge branch 'trunk' into task/WOOMOB-1221-pos-settings-a11y-improvements
# Conflicts: # WooCommerce/Classes/POS/Presentation/Settings/PointOfSaleSettingsHardwareDetailView.swift
2 parents dbf9a59 + 5df8f98 commit a3282dc

File tree

5 files changed

+144
-350
lines changed

5 files changed

+144
-350
lines changed

Modules/Sources/Networking/Remote/POSCatalogSyncRemote.swift

Lines changed: 39 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import Foundation
55
public class POSCatalogSyncRemote: Remote {
66
private let dateFormatter = ISO8601DateFormatter()
77

8+
// MARK: - Incremental Sync Endpoints
9+
810
/// Loads POS products modified after the specified date.
911
///
1012
/// - Parameters:
@@ -16,7 +18,7 @@ public class POSCatalogSyncRemote: Remote {
1618
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
1719
public func loadProducts(modifiedAfter: Date, siteID: Int64, pageNumber: Int)
1820
async throws -> PagedItems<POSProduct> {
19-
let path = "products"
21+
let path = Path.products
2022
let parameters = [
2123
ParameterKey.modifiedAfter: dateFormatter.string(from: modifiedAfter),
2224
ParameterKey.page: String(pageNumber),
@@ -41,7 +43,7 @@ public class POSCatalogSyncRemote: Remote {
4143
///
4244
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
4345
public func loadProductVariations(modifiedAfter: Date, siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation> {
44-
let path = "variations"
46+
let path = Path.variations
4547
let parameters = [
4648
ParameterKey.modifiedAfter: dateFormatter.string(from: modifiedAfter),
4749
ParameterKey.page: String(pageNumber),
@@ -56,62 +58,52 @@ public class POSCatalogSyncRemote: Remote {
5658
return createPagedItems(items: variations, responseHeaders: responseHeaders, currentPageNumber: pageNumber)
5759
}
5860

59-
/// Generates a POS catalog. The catalog is generated asynchronously and a download URL is returned in the
60-
/// status response endpoint associated with a job ID.
61+
// MARK: - Full Sync Endpoints
62+
63+
/// Loads POS products for full sync.
6164
///
6265
/// - Parameters:
63-
/// - siteID: Site ID to generate catalog for.
64-
/// - fields: Optional array of fields to include in catalog.
65-
/// - forceGenerate: Whether to force generation of a new catalog.
66-
/// - Returns: Catalog job response with job ID.
66+
/// - siteID: Site ID to load products from.
67+
/// - pageNumber: Page number for pagination.
68+
/// - Returns: Paginated list of POS products.
6769
///
6870
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
69-
public func generateCatalog(for siteID: Int64, forceGenerate: Bool = false) async throws -> POSCatalogGenerationResponse {
70-
let path = "catalog"
71-
let parameters: [String: Any] = [
72-
ParameterKey.fullSyncFields: POSProduct.requestFields
71+
public func loadProducts(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProduct> {
72+
let path = Path.products
73+
let parameters = [
74+
ParameterKey.page: String(pageNumber),
75+
ParameterKey.perPage: String(Constants.defaultPageSize),
76+
ParameterKey.fields: POSProduct.requestFields.joined(separator: ",")
7377
]
7478

75-
let request = JetpackRequest(wooApiVersion: .mark3, method: .post, siteID: siteID, path: path, parameters: parameters, availableAsRESTRequest: true)
76-
let mapper = SingleItemMapper<POSCatalogGenerationResponse>(siteID: siteID)
77-
return try await enqueue(request, mapper: mapper)
79+
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: parameters)
80+
let mapper = ListMapper<POSProduct>(siteID: siteID)
81+
let (products, responseHeaders) = try await enqueueWithResponseHeaders(request, mapper: mapper)
82+
83+
return createPagedItems(items: products, responseHeaders: responseHeaders, currentPageNumber: pageNumber)
7884
}
7985

80-
/// Checks the status of a catalog generation job. A download URL is returned when the job is complete.
86+
/// Loads POS product variations for full sync.
8187
///
8288
/// - Parameters:
83-
/// - siteID: Site ID for the catalog job.
84-
/// - jobID: Job ID to check status for.
85-
/// - Returns: Catalog status response.
89+
/// - siteID: Site ID to load variations from.
90+
/// - pageNumber: Page number for pagination.
91+
/// - Returns: Paginated list of POS product variations.
8692
///
8793
// periphery:ignore - TODO - remove this periphery ignore comment when this endpoint is integrated with catalog sync
88-
public func checkCatalogStatus(for siteID: Int64, jobID: String) async throws -> POSCatalogStatusResponse {
89-
let path = "catalog/status/\(jobID)"
94+
public func loadProductVariations(siteID: Int64, pageNumber: Int) async throws -> PagedItems<POSProductVariation> {
95+
let path = Path.variations
96+
let parameters = [
97+
ParameterKey.page: String(pageNumber),
98+
ParameterKey.perPage: String(Constants.defaultPageSize),
99+
ParameterKey.fields: POSProductVariation.requestFields.joined(separator: ",")
100+
]
90101

91-
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, availableAsRESTRequest: true)
92-
let mapper = SingleItemMapper<POSCatalogStatusResponse>(siteID: siteID)
93-
return try await enqueue(request, mapper: mapper)
94-
}
102+
let request = JetpackRequest(wooApiVersion: .wcAnalytics, method: .get, siteID: siteID, path: path, parameters: parameters)
103+
let mapper = ListMapper<POSProductVariation>(siteID: siteID)
104+
let (variations, responseHeaders) = try await enqueueWithResponseHeaders(request, mapper: mapper)
95105

96-
/// Downloads the generated catalog at the specified download URL.
97-
/// - Parameters:
98-
/// - siteID: Site ID to download catalog for.
99-
/// - downloadURL: Download URL of the catalog file.
100-
/// - Returns: List of products and variations in the POS catalog.
101-
// periphery:ignore - TODO - remove this periphery ignore comment when this method is integrated with catalog sync
102-
public func downloadCatalog(for siteID: Int64, downloadURL: String) async throws -> POSCatalog {
103-
// TODO: WOOMOB-1173 - move download task to the background using `URLSessionConfiguration.background`
104-
guard let url = URL(string: downloadURL) else {
105-
throw NetworkError.invalidURL
106-
}
107-
let request = URLRequest(url: url)
108-
let mapper = ListMapper<POSProduct>(siteID: siteID)
109-
let items = try await enqueue(request, mapper: mapper)
110-
let variationProductTypeKey = "variation"
111-
let products = items.filter { $0.productTypeKey != variationProductTypeKey }
112-
let variations = items.filter { $0.productTypeKey == variationProductTypeKey }
113-
.map { $0.toVariation }
114-
return POSCatalog(products: products, variations: variations)
106+
return createPagedItems(items: variations, responseHeaders: responseHeaders, currentPageNumber: pageNumber)
115107
}
116108
}
117109

@@ -127,78 +119,10 @@ private extension POSCatalogSyncRemote {
127119
static let page = "page"
128120
static let perPage = "per_page"
129121
static let fields = "_fields"
130-
static let fullSyncFields = "fields"
131122
}
132-
}
133-
134-
// MARK: - Response Models
135-
136-
/// Response from catalog generation request.
137-
// periphery:ignore - TODO - remove this periphery ignore comment when the corresponding endpoint is integrated with catalog sync
138-
public struct POSCatalogGenerationResponse: Decodable {
139-
/// Unique identifier for tracking the catalog generation job.
140-
public let jobID: String
141-
142-
private enum CodingKeys: String, CodingKey {
143-
case jobID = "job_id"
144-
}
145-
}
146-
147-
/// Response from catalog status check.
148-
// periphery:ignore - TODO - remove this periphery ignore comment when the corresponding endpoint is integrated with catalog sync
149-
public struct POSCatalogStatusResponse: Decodable {
150-
/// Current status of the catalog generation job.
151-
public let status: POSCatalogStatus
152-
/// Download URL for the completed catalog (available when status is complete).
153-
public let downloadURL: String?
154-
/// Progress percentage of the catalog generation (0.0 to 100.0).
155-
public let progress: Double
156-
157-
private enum CodingKeys: String, CodingKey {
158-
case status
159-
case downloadURL = "download_url"
160-
case progress
161-
}
162-
}
163-
164-
/// Catalog generation status.
165-
public enum POSCatalogStatus: String, Decodable {
166-
case pending
167-
case processing
168-
case complete
169-
}
170-
171-
/// POS catalog from download.
172-
// periphery:ignore - TODO - remove this periphery ignore comment when the corresponding endpoint is integrated with catalog sync
173-
public struct POSCatalog {
174-
public let products: [POSProduct]
175-
public let variations: [POSProductVariation]
176-
}
177123

178-
private extension POSProduct {
179-
var toVariation: POSProductVariation {
180-
let variationAttributes = attributes.compactMap { attribute in
181-
try? attribute.toProductVariationAttribute()
182-
}
183-
184-
let firstImage = images.first
185-
186-
return .init(
187-
siteID: siteID,
188-
productID: parentID,
189-
productVariationID: productID,
190-
attributes: variationAttributes,
191-
image: firstImage,
192-
sku: sku,
193-
globalUniqueID: globalUniqueID,
194-
price: price,
195-
regularPrice: regularPrice,
196-
salePrice: salePrice,
197-
onSale: onSale,
198-
downloadable: downloadable,
199-
manageStock: manageStock,
200-
stockQuantity: stockQuantity,
201-
stockStatusKey: stockStatusKey
202-
)
124+
enum Path {
125+
static let products = "products"
126+
static let variations = "variations"
203127
}
204128
}

0 commit comments

Comments
 (0)