Skip to content

Commit a25fb79

Browse files
authored
Merge branch 'trunk' into woomob-1210-remove-unused-properties-from-pos-entities
2 parents 906316b + 00a34fb commit a25fb79

File tree

18 files changed

+679
-430
lines changed

18 files changed

+679
-430
lines changed

Modules/Sources/Networking/Remote/POSCatalogSyncRemote.swift

Lines changed: 40 additions & 88 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,54 +119,14 @@ private extension POSCatalogSyncRemote {
127119
static let page = "page"
128120
static let perPage = "per_page"
129121
static let fields = "_fields"
130-
static let fullSyncFields = "fields"
131-
}
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"
144122
}
145-
}
146123

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
124+
enum Path {
125+
static let products = "products"
126+
static let variations = "variations"
161127
}
162128
}
163129

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-
}
177-
178130
private extension POSProduct {
179131
var toVariation: POSProductVariation {
180132
let variationAttributes = attributes.compactMap { attribute in
@@ -199,4 +151,4 @@ private extension POSProduct {
199151
stockStatusKey: stockStatusKey
200152
)
201153
}
202-
}
154+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
public struct POSReceiptInformation: Equatable {
3+
public let storeName: String?
4+
public let storeAddress: String?
5+
public let phone: String?
6+
public let email: String?
7+
public let refundReturnsPolicy: String?
8+
9+
public init(storeName: String?, storeAddress: String?, phone: String?, email: String?, refundReturnsPolicy: String?) {
10+
self.storeName = storeName
11+
self.storeAddress = storeAddress
12+
self.phone = phone
13+
self.email = email
14+
self.refundReturnsPolicy = refundReturnsPolicy
15+
}
16+
17+
public static let empty = POSReceiptInformation(
18+
storeName: nil,
19+
storeAddress: nil,
20+
phone: nil,
21+
email: nil,
22+
refundReturnsPolicy: nil
23+
)
24+
}

Modules/Sources/Yosemite/Tools/POS/PointOfSaleSettingsService.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import Networking
33
import Storage
44

55
public protocol PointOfSaleSettingsServiceProtocol {
6-
var siteID: Int64 { get }
7-
func retrievePointOfSaleSettings() async throws -> [SiteSetting]
6+
func retrievePointOfSaleSettings() async throws -> POSReceiptInformation
87
}
98

109
public final class PointOfSaleSettingsService: PointOfSaleSettingsServiceProtocol {
@@ -25,7 +24,19 @@ public final class PointOfSaleSettingsService: PointOfSaleSettingsServiceProtoco
2524
network: network))
2625
}
2726

28-
public func retrievePointOfSaleSettings() async throws -> [SiteSetting] {
29-
return try await settingStoreMethods.retrievePointOfSaleSettings(siteID: siteID)
27+
public func retrievePointOfSaleSettings() async throws -> POSReceiptInformation {
28+
let siteSettings = try await settingStoreMethods.retrievePointOfSaleSettings(siteID: siteID)
29+
return POSReceiptInformation(
30+
storeName: settingValue(from: siteSettings, settingID: "woocommerce_pos_store_name"),
31+
storeAddress: settingValue(from: siteSettings, settingID: "woocommerce_pos_store_address"),
32+
phone: settingValue(from: siteSettings, settingID: "woocommerce_pos_store_phone"),
33+
email: settingValue(from: siteSettings, settingID: "woocommerce_pos_store_email"),
34+
refundReturnsPolicy: settingValue(from: siteSettings, settingID: "woocommerce_pos_refund_returns_policy")
35+
)
36+
}
37+
38+
private func settingValue(from siteSettings: [SiteSetting], settingID: String) -> String? {
39+
let value = siteSettings.first { $0.settingID == settingID }?.value
40+
return value?.isEmpty == true ? nil : value
3041
}
3142
}

0 commit comments

Comments
 (0)