Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Modules/Sources/Fakes/Networking.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ extension Networking.Customer {
public static func fake() -> Networking.Customer {
.init(
siteID: .fake(),
userID: .fake(),
customerID: .fake(),
email: .fake(),
username: .fake(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ extension Networking.CreateProductVariation {
extension Networking.Customer {
public func copy(
siteID: CopiableProp<Int64> = .copy,
userID: CopiableProp<Int64> = .copy,
customerID: CopiableProp<Int64> = .copy,
email: CopiableProp<String> = .copy,
username: NullableCopiableProp<String> = .copy,
Expand All @@ -646,6 +647,7 @@ extension Networking.Customer {
shipping: NullableCopiableProp<Address> = .copy
) -> Networking.Customer {
let siteID = siteID ?? self.siteID
let userID = userID ?? self.userID
let customerID = customerID ?? self.customerID
let email = email ?? self.email
let username = username ?? self.username
Expand All @@ -656,6 +658,7 @@ extension Networking.Customer {

return Networking.Customer(
siteID: siteID,
userID: userID,
customerID: customerID,
email: email,
username: username,
Expand Down
27 changes: 21 additions & 6 deletions Modules/Sources/Networking/Model/Customer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ import Codegen
/// Represents a Customer entity:
/// https://woocommerce.github.io/woocommerce-rest-api-docs/#customer-properties
///
/// This model is used in TWO different contexts:
/// 1. When fetching from `/wc/v3/customers/{id}` endpoint:
/// - `userID` = WordPress user ID (mapped from API "id" field)
/// - `customerID` = 0 (not available from this endpoint)
/// 2. When converting from Storage.Customer via toReadOnly():
/// - `userID` = WordPress user ID (if registered)
/// - `customerID` = Analytics customer ID (from WCAnalyticsCustomer)
///
public struct Customer: Codable, GeneratedCopiable, GeneratedFakeable, Equatable {
/// The siteID for the customer
public let siteID: Int64

/// Unique identifier for the customer
/// WordPress user ID (mapped from API "id" field)
/// This is the WordPress user account identifier, not the analytics customer ID
public let userID: Int64

/// Analytics customer ID (only set when converting from Storage.Customer taking value from WCAnalyticsCustomer)
/// This field is not mapped to any API response
public let customerID: Int64

/// The email address for the customer
Expand All @@ -31,20 +44,22 @@ public struct Customer: Codable, GeneratedCopiable, GeneratedFakeable, Equatable

/// Computed property to check if the customer is a guest
public var isGuest: Bool {
customerID == 0
userID == 0
}

/// Customer struct initializer
///
public init(siteID: Int64,
customerID: Int64,
userID: Int64,
customerID: Int64 = 0,
email: String,
username: String?,
firstName: String?,
lastName: String?,
billing: Address?,
shipping: Address?) {
self.siteID = siteID
self.userID = userID
self.customerID = customerID
self.email = email
self.username = username
Expand All @@ -63,7 +78,7 @@ public struct Customer: Codable, GeneratedCopiable, GeneratedFakeable, Equatable

let container = try decoder.container(keyedBy: CodingKeys.self)

let customerID = try container.decode(Int64.self, forKey: .customerID)
let userID = try container.decode(Int64.self, forKey: .userID)
let email = try container.decode(String.self, forKey: .email)
let username = try container.decode(String.self, forKey: .username)
let firstName = try container.decodeIfPresent(String.self, forKey: .firstName)
Expand All @@ -72,7 +87,7 @@ public struct Customer: Codable, GeneratedCopiable, GeneratedFakeable, Equatable
let shipping = try? container.decode(Address.self, forKey: .shipping)

self.init(siteID: siteID,
customerID: customerID,
userID: userID,
email: email,
username: username,
firstName: firstName,
Expand All @@ -87,7 +102,7 @@ public struct Customer: Codable, GeneratedCopiable, GeneratedFakeable, Equatable
///
extension Customer {
enum CodingKeys: String, CodingKey {
case customerID = "id"
case userID = "id"
case email
case username
case firstName = "first_name"
Expand Down
6 changes: 3 additions & 3 deletions Modules/Sources/Networking/Remote/CustomerRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ public class CustomerRemote: Remote {
/// Retrieves a `Customer`
///
/// - Parameters:
/// - customerID: ID of the customer that will be retrieved
/// - userID: ID of the registered WordPress user (customer) that will be retrieved.
/// - siteID: Site for which we'll fetch the customer.
/// - completion: Closure to be executed upon completion.
///
public func retrieveCustomer(for siteID: Int64, with customerID: Int64, completion: @escaping (Result<Customer, Error>) -> Void) {
let path = "customers/\(customerID)"
public func retrieveCustomer(for siteID: Int64, with userID: Int64, completion: @escaping (Result<Customer, Error>) -> Void) {
let path = "customers/\(userID)"
let request = JetpackRequest(wooApiVersion: .mark3,
method: .get,
siteID: siteID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension Customer {
@NSManaged public var billingPostcode: String?
@NSManaged public var billingState: String?
@NSManaged public var customerID: Int64
@NSManaged public var userID: Int64
@NSManaged public var email: String?
@NSManaged public var username: String?
@NSManaged public var firstName: String?
Expand Down
4 changes: 4 additions & 0 deletions Modules/Sources/Storage/Model/MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This file documents changes in the WCiOS Storage data model. Please explain any changes to the data model as well as any custom migrations.

## Model 125 (Release 23.2.0.0)
- @povilasstaskus 2025-08-28
- Added `userID` attribute to `Customer` entity to link customers with WordPress user accounts.

## Model 124 (Release 22.9.0.0)
- @itsmeichigo 2025-07-11
- Added `WooShippingShipment` entity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>Model 124.xcdatamodel</string>
<string>Model 125.xcdatamodel</string>
</dict>
</plist>
Loading