Skip to content

Dynamic Dashboard: Integrate new endpoint for product stock in Networking layer #12756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 17, 2024
Merged
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
15 changes: 15 additions & 0 deletions Fakes/Fakes/Networking.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,21 @@ extension Networking.ProductStatus {
.published
}
}
extension Networking.ProductStock {
/// Returns a "ready to use" type filled with fake values.
///
public static func fake() -> Networking.ProductStock {
.init(
siteID: .fake(),
productID: .fake(),
name: .fake(),
sku: .fake(),
manageStock: .fake(),
stockQuantity: .fake(),
stockStatusKey: .fake()
)
}
}
extension Networking.ProductStockStatus {
/// Returns a "ready to use" type filled with fake values.
///
Expand Down
32 changes: 32 additions & 0 deletions Networking/Networking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,14 @@
DE02ABB32B563E61008E0AC4 /* blaze-payment-info.json in Resources */ = {isa = PBXBuildFile; fileRef = DE02ABB22B563E61008E0AC4 /* blaze-payment-info.json */; };
DE02ABB52B563E96008E0AC4 /* BlazePaymentInfoMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE02ABB42B563E96008E0AC4 /* BlazePaymentInfoMapper.swift */; };
DE02ABB72B563F3A008E0AC4 /* BlazePaymentInfoMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE02ABB62B563F3A008E0AC4 /* BlazePaymentInfoMapperTests.swift */; };
DE2004552BF5BD1A00660A72 /* ProductStock.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2004542BF5BD1A00660A72 /* ProductStock.swift */; };
DE2004572BF5C22600660A72 /* ProductStockListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2004562BF5C22600660A72 /* ProductStockListMapper.swift */; };
DE2004592BF6142200660A72 /* product-stock.json in Resources */ = {isa = PBXBuildFile; fileRef = DE2004582BF6142200660A72 /* product-stock.json */; };
DE20045B2BF6146700660A72 /* product-stock-without-data-envelope.json in Resources */ = {isa = PBXBuildFile; fileRef = DE20045A2BF6146700660A72 /* product-stock-without-data-envelope.json */; };
DE20045D2BF614A300660A72 /* ProductStockListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE20045C2BF614A300660A72 /* ProductStockListMapperTests.swift */; };
DE2004662BF7499600660A72 /* product-stock-decimal-sku.json in Resources */ = {isa = PBXBuildFile; fileRef = DE2004652BF7499500660A72 /* product-stock-decimal-sku.json */; };
DE2004682BF74A2500660A72 /* product-stock-parent-manage-stock.json in Resources */ = {isa = PBXBuildFile; fileRef = DE2004672BF74A2500660A72 /* product-stock-parent-manage-stock.json */; };
DE20046A2BF74B1E00660A72 /* product-stock-string-quantity.json in Resources */ = {isa = PBXBuildFile; fileRef = DE2004692BF74B1E00660A72 /* product-stock-string-quantity.json */; };
DE2095BD27956D7900171F1C /* CouponReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2095BC27956D7900171F1C /* CouponReport.swift */; };
DE2095BF279583A100171F1C /* CouponReportListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2095BE279583A100171F1C /* CouponReportListMapper.swift */; };
DE2095C127966EC800171F1C /* coupon-reports.json in Resources */ = {isa = PBXBuildFile; fileRef = DE2095C027966EC800171F1C /* coupon-reports.json */; };
Expand Down Expand Up @@ -1911,6 +1919,14 @@
DE02ABB22B563E61008E0AC4 /* blaze-payment-info.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "blaze-payment-info.json"; sourceTree = "<group>"; };
DE02ABB42B563E96008E0AC4 /* BlazePaymentInfoMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlazePaymentInfoMapper.swift; sourceTree = "<group>"; };
DE02ABB62B563F3A008E0AC4 /* BlazePaymentInfoMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlazePaymentInfoMapperTests.swift; sourceTree = "<group>"; };
DE2004542BF5BD1A00660A72 /* ProductStock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductStock.swift; sourceTree = "<group>"; };
DE2004562BF5C22600660A72 /* ProductStockListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductStockListMapper.swift; sourceTree = "<group>"; };
DE2004582BF6142200660A72 /* product-stock.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-stock.json"; sourceTree = "<group>"; };
DE20045A2BF6146700660A72 /* product-stock-without-data-envelope.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-stock-without-data-envelope.json"; sourceTree = "<group>"; };
DE20045C2BF614A300660A72 /* ProductStockListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductStockListMapperTests.swift; sourceTree = "<group>"; };
DE2004652BF7499500660A72 /* product-stock-decimal-sku.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-stock-decimal-sku.json"; sourceTree = "<group>"; };
DE2004672BF74A2500660A72 /* product-stock-parent-manage-stock.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-stock-parent-manage-stock.json"; sourceTree = "<group>"; };
DE2004692BF74B1E00660A72 /* product-stock-string-quantity.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-stock-string-quantity.json"; sourceTree = "<group>"; };
DE2095BC27956D7900171F1C /* CouponReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponReport.swift; sourceTree = "<group>"; };
DE2095BE279583A100171F1C /* CouponReportListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponReportListMapper.swift; sourceTree = "<group>"; };
DE2095C027966EC800171F1C /* coupon-reports.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "coupon-reports.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2762,6 +2778,11 @@
B559EBA820A0B5B100836CD4 /* Responses */ = {
isa = PBXGroup;
children = (
DE2004582BF6142200660A72 /* product-stock.json */,
DE20045A2BF6146700660A72 /* product-stock-without-data-envelope.json */,
DE2004652BF7499500660A72 /* product-stock-decimal-sku.json */,
DE2004672BF74A2500660A72 /* product-stock-parent-manage-stock.json */,
DE2004692BF74B1E00660A72 /* product-stock-string-quantity.json */,
EE1CB9152B4BCFA800AD24D5 /* blaze-ai-suggestions.json */,
8617EDA72B3C0E3800BD9D1B /* blaze-impressions.json */,
DE02ABB22B563E61008E0AC4 /* blaze-payment-info.json */,
Expand Down Expand Up @@ -3247,6 +3268,7 @@
0219B03823964BB3007DCD5E /* ProductShippingClassMapper.swift */,
025CA2C1238EBBAA00B05C81 /* ProductShippingClassListMapper.swift */,
45D685F723D0BC78005F87D0 /* ProductSkuMapper.swift */,
DE2004562BF5C22600660A72 /* ProductStockListMapper.swift */,
CE71E2302A4C3DDA00DB5376 /* ProductsReportMapper.swift */,
CE430673234BA6AD0073CBFF /* RefundMapper.swift */,
CE430675234BA7920073CBFF /* RefundListMapper.swift */,
Expand Down Expand Up @@ -3484,6 +3506,7 @@
EE1CB9172B4BD22800AD24D5 /* BlazeAISuggestionListMapperTests.swift */,
DE02ABB62B563F3A008E0AC4 /* BlazePaymentInfoMapperTests.swift */,
CEB9BF442BB19EDE0007978A /* ProductBundleStatsMapperTests.swift */,
DE20045C2BF614A300660A72 /* ProductStockListMapperTests.swift */,
);
path = Mapper;
sourceTree = "<group>";
Expand Down Expand Up @@ -3520,6 +3543,7 @@
CE0A0F12223942D90075ED8D /* ProductImage.swift */,
CCFEB8F529E8648C00537456 /* ProductMetadataExtractor.swift */,
CE227092228DD44C00C0626C /* ProductStatus.swift */,
DE2004542BF5BD1A00660A72 /* ProductStock.swift */,
CE17C6B0229462C000AACE1C /* ProductStockStatus.swift */,
CCAD671229E6D83C00ADC064 /* ProductSubscription.swift */,
CE6BFEE92236E191005C79FB /* ProductType.swift */,
Expand Down Expand Up @@ -3828,13 +3852,15 @@
45D685FA23D0C3CF005F87D0 /* product-search-sku.json in Resources */,
DEEF8E6529C832B500D47411 /* wordpress-site-info-with-auth-url.json in Resources */,
D865CE5F278CA183002C8520 /* stripe-payment-intent-requires-action.json in Resources */,
DE2004662BF7499600660A72 /* product-stock-decimal-sku.json in Resources */,
CCF48B2C2628AE160034EA83 /* shipping-label-account-settings.json in Resources */,
31A451D927863A2E00FE81AA /* stripe-account-live-test.json in Resources */,
DE42F9602967C88400D514C2 /* report-orders-total-without-data.json in Resources */,
DE9DEEF5291CF1B40070AD7C /* site-plugin-without-envelope.json in Resources */,
EEFC4C972A5E996C004C5A83 /* jwt-token-success.json in Resources */,
0261F5A928D4641500B7AC72 /* products-sku-search.json in Resources */,
EE57C14A2980CE4B00BC31E7 /* taxes-classes-without-data.json in Resources */,
DE20045B2BF6146700660A72 /* product-stock-without-data-envelope.json in Resources */,
EE80A24829547F8B003591E4 /* coupon-without-data.json in Resources */,
EE57C137297F98DB00BC31E7 /* product-variations-load-all-without-data.json in Resources */,
09885C8027C3FFD200910A62 /* product-variations-bulk-update.json in Resources */,
Expand Down Expand Up @@ -4097,6 +4123,7 @@
DE66C5672977CEB800DAA978 /* shipping-label-status-success-without-data.json in Resources */,
D8FBFF2922D52AFB006E3336 /* order-stats-v4-year.json in Resources */,
D865CE65278CA202002C8520 /* stripe-payment-intent-canceled.json in Resources */,
DE20046A2BF74B1E00660A72 /* product-stock-string-quantity.json in Resources */,
DE42F9672967F61D00D514C2 /* refunds-all-without-data.json in Resources */,
AED8AEBA272A97B400663FCC /* null-data.json in Resources */,
CE13680F29FA74A400EBF43C /* product-min-max-quantities.json in Resources */,
Expand Down Expand Up @@ -4132,13 +4159,15 @@
02935AEE29DFFA74001B793E /* site-enable-trial-success.json in Resources */,
8617EDA82B3C0E3800BD9D1B /* blaze-impressions.json in Resources */,
D8A284F425FBB48D0019A84B /* product-attribute-terms.json in Resources */,
DE2004682BF74A2500660A72 /* product-stock-parent-manage-stock.json in Resources */,
74E30951216E8DCE00ABCE4C /* site-visits-alt.json in Resources */,
74ABA1C5213F17AA00FFAD30 /* top-performers-day.json in Resources */,
02050F57296FE90A00710E63 /* domain-suggestions-paid.json in Resources */,
31B8D6B626583970008E3DB2 /* wcpay-account-implicitly-not-eligible.json in Resources */,
EEFC4C9B2A5ED1F6004C5A83 /* identify-language-invalid-token.json in Resources */,
EE8A30322B72A521001D7C66 /* order-with-attribution-info.json in Resources */,
EE57C125297EB4E300BC31E7 /* product-attributes-all-without-data.json in Resources */,
DE2004592BF6142200660A72 /* product-stock.json in Resources */,
31054714262E2F3B00C5C02B /* wcpay-payment-intent-requires-payment-method.json in Resources */,
EE57C134297F985A00BC31E7 /* product-variations-bulk-update-without-data.json in Resources */,
CE865A9F2A41E42F0049B03C /* date-modified-gmt.json in Resources */,
Expand Down Expand Up @@ -4425,6 +4454,7 @@
DE74F29A27E08F5A0002FE59 /* SiteSettingMapper.swift in Sources */,
CC33754E29C884000006A538 /* ProductCompositeComponent.swift in Sources */,
450106852399A7CB00E24722 /* TaxClass.swift in Sources */,
DE2004572BF5C22600660A72 /* ProductStockListMapper.swift in Sources */,
68BFF8FA2B67679700B15FF2 /* ReceiptRemote.swift in Sources */,
CC0786612677B2DA00BA9AC1 /* ShippingLabelPurchaseMapper.swift in Sources */,
456930AB264EB85A009ED69D /* ShippingLabelCarriersAndRatesMapper.swift in Sources */,
Expand Down Expand Up @@ -4615,6 +4645,7 @@
021EAA5625493B3600AA8CCD /* OrderItemAttribute.swift in Sources */,
77CE40602514CB3E003FF69D /* ProductDownloadDragAndDrop.swift in Sources */,
2685C0FE263B5D8900D9EE97 /* AddOnGroupRemote.swift in Sources */,
DE2004552BF5BD1A00660A72 /* ProductStock.swift in Sources */,
EE078D8F2AD2E65400C1199E /* JWToken.swift in Sources */,
AE1950F3296DB2C2004D37D2 /* ProductsBulkUpdateMapper.swift in Sources */,
450106912399B2C800E24722 /* TaxClassListMapper.swift in Sources */,
Expand Down Expand Up @@ -4927,6 +4958,7 @@
EEA1E2022AC18CD700A37ADD /* AIProductMapperTests.swift in Sources */,
02AED9DC2AA04716006DC460 /* KeyedDecodingContainer+WooTests.swift in Sources */,
45ED4F14239E8F2E004F1BE3 /* TaxRemoteTests.swift in Sources */,
DE20045D2BF614A300660A72 /* ProductStockListMapperTests.swift in Sources */,
D88D5A4D230BD010007B6E01 /* ProductReviewsRemoteTests.swift in Sources */,
6812FC012A6B27E100D7C625 /* InAppPurchasesTransactionMapperTests.swift in Sources */,
261CF1BC255AEE290090D8D3 /* PaymentsGatewayRemoteTests.swift in Sources */,
Expand Down
38 changes: 38 additions & 0 deletions Networking/Networking/Mapper/ProductStockListMapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

/// Mapper: `[ProductStock]`
///
struct ProductStockListMapper: Mapper {
/// Site Identifier associated to the products that will be parsed.
///
/// We're injecting this field via `JSONDecoder.userInfo` because SiteID is not returned in any of the Product Endpoints.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 TIL about getting siteID this way.

///
let siteID: Int64

/// (Attempts) to convert a dictionary into `[ProductStock]`.
///
func map(response: Data) throws -> [ProductStock] {
let decoder = JSONDecoder()
decoder.userInfo = [
.siteID: siteID
]
if hasDataEnvelope(in: response) {
return try decoder.decode(ProductStockListEnvelope.self, from: response).items
} else {
return try decoder.decode([ProductStock].self, from: response)
}
}
}


/// ProductStockListEnvelope Disposable Entity:
/// Load Product stock list endpoint returns the coupon in the `data` key.
/// This entity allows us to parse all the things with JSONDecoder.
///
private struct ProductStockListEnvelope: Decodable {
let items: [ProductStock]

private enum CodingKeys: String, CodingKey {
case items = "data"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,36 @@ extension Networking.ProductReview {
}
}

extension Networking.ProductStock {
public func copy(
siteID: CopiableProp<Int64> = .copy,
productID: CopiableProp<Int64> = .copy,
name: CopiableProp<String> = .copy,
sku: NullableCopiableProp<String> = .copy,
manageStock: CopiableProp<Bool> = .copy,
stockQuantity: NullableCopiableProp<Decimal> = .copy,
stockStatusKey: CopiableProp<String> = .copy
) -> Networking.ProductStock {
let siteID = siteID ?? self.siteID
let productID = productID ?? self.productID
let name = name ?? self.name
let sku = sku ?? self.sku
let manageStock = manageStock ?? self.manageStock
let stockQuantity = stockQuantity ?? self.stockQuantity
let stockStatusKey = stockStatusKey ?? self.stockStatusKey

return Networking.ProductStock(
siteID: siteID,
productID: productID,
name: name,
sku: sku,
manageStock: manageStock,
stockQuantity: stockQuantity,
stockStatusKey: stockStatusKey
)
}
}

extension Networking.ProductSubscription {
public func copy(
length: CopiableProp<String> = .copy,
Expand Down
108 changes: 108 additions & 0 deletions Networking/Networking/Model/Product/ProductStock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import Foundation
import Codegen

/// Represents stock details for a Product.
///
public struct ProductStock: Decodable, GeneratedCopiable, Equatable, GeneratedFakeable {

public let siteID: Int64
public let productID: Int64
public let name: String

public let sku: String?
public let manageStock: Bool
public let stockQuantity: Decimal? // Core API reports Int or null; some extensions allow decimal values as well
public let stockStatusKey: String // instock, outofstock, backorder

public var productStockStatus: ProductStockStatus {
return ProductStockStatus(rawValue: stockStatusKey)
}

public init(siteID: Int64,
productID: Int64,
name: String,
sku: String?,
manageStock: Bool,
stockQuantity: Decimal?,
stockStatusKey: String) {
self.siteID = siteID
self.productID = productID
self.name = name
self.sku = sku
self.manageStock = manageStock
self.stockQuantity = stockQuantity
self.stockStatusKey = stockStatusKey
}

/// The public initializer.
///
public init(from decoder: Decoder) throws {
guard let siteID = decoder.userInfo[.siteID] as? Int64 else {
throw ProductDecodingError.missingSiteID
}

let container = try decoder.container(keyedBy: CodingKeys.self)
let productID = try container.decode(Int64.self, forKey: .productID)
let name = try container.decode(String.self, forKey: .name)

// Even though a plain install of WooCommerce Core provides String values,
// some plugins alter the field value from String to Int or Decimal.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great research to find how plugins might alter this value.

let sku = container.failsafeDecodeIfPresent(targetType: String.self,
forKey: .sku,
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])

// Even though the API docs claim `manageStock` is a bool, it's possible that `"parent"`
// could be returned as well (typically with variations) — we need to account for this.
// A "parent" value means that stock mgmt is turned on + managed at the parent product-level, therefore
// we need to set this var as `true` in this situation.
// See: https://github.com/woocommerce/woocommerce-ios/issues/884 for more deets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great research here as well.

let manageStock = container.failsafeDecodeIfPresent(targetType: Bool.self,
forKey: .manageStock,
alternativeTypes: [
.string(transform: { value in
// A bool could not be parsed — check if "parent" is set, and if so, set manageStock to
// `true`
value.lowercased() == Values.manageStockParent ? true : false
})
]) ?? false

// Even though WooCommerce Core returns Int or null values,
// some plugins alter the field value from Int to Decimal or String.
// We handle this as an optional Decimal value.
let stockQuantity = container.failsafeDecodeIfPresent(decimalForKey: .stockQuantity)

let stockStatusKey = try container.decode(String.self, forKey: .stockStatusKey)

self.init(siteID: siteID,
productID: productID,
name: name,
sku: sku,
manageStock: manageStock,
stockQuantity: stockQuantity,
stockStatusKey: stockStatusKey)
}
}

/// Defines all of the ProductStock CodingKeys
///
private extension ProductStock {

enum CodingKeys: String, CodingKey {
case productID = "id"
case name
case sku = "sku"

case manageStock = "manage_stock"
case stockQuantity = "stock_quantity"
case stockStatusKey = "stock_status"
}

// Decoding Errors
enum DecodingError: Error {
case missingSiteID
}

enum Values {
static let manageStockParent = "parent"
}
}
Loading