Skip to content

Commit b634471

Browse files
authored
Merge pull request #7066 from woocommerce/issue/6977-local-fallback
[Unified Order Editing ] Adds isEditable fallback for older stores
2 parents b83b1a7 + c97b1a3 commit b634471

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
26455E2725F669EA008A1D32 /* ProductAttributeTermListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B64543259BCE0F00EF3FB3 /* ProductAttributeTermListMapper.swift */; };
134134
26455E2A25F669F0008A1D32 /* ProductAttributeTermMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B6453F259BCDFE00EF3FB3 /* ProductAttributeTermMapper.swift */; };
135135
265BCA02243056E3004E53EE /* categories-all.json in Resources */ = {isa = PBXBuildFile; fileRef = 265BCA01243056E3004E53EE /* categories-all.json */; };
136+
265EFBDC285257950033BD33 /* Order+Fallbacks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265EFBDB285257950033BD33 /* Order+Fallbacks.swift */; };
136137
26615473242D596B00A31661 /* ProductCategoriesRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26615472242D596B00A31661 /* ProductCategoriesRemote.swift */; };
137138
26615475242D7C9500A31661 /* ProductCategoryListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26615474242D7C9500A31661 /* ProductCategoryListMapper.swift */; };
138139
26615479242DA54D00A31661 /* ProductCategoyListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26615478242DA54D00A31661 /* ProductCategoyListMapperTests.swift */; };
@@ -801,6 +802,7 @@
801802
261CF2CA255C50010090D8D3 /* payment-gateway-list-half.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "payment-gateway-list-half.json"; sourceTree = "<group>"; };
802803
262E5AD4255ACD6F000B2416 /* PaymentGatewayListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentGatewayListMapperTests.swift; sourceTree = "<group>"; };
803804
265BCA01243056E3004E53EE /* categories-all.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "categories-all.json"; sourceTree = "<group>"; };
805+
265EFBDB285257950033BD33 /* Order+Fallbacks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Order+Fallbacks.swift"; sourceTree = "<group>"; };
804806
26615472242D596B00A31661 /* ProductCategoriesRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductCategoriesRemote.swift; sourceTree = "<group>"; };
805807
26615474242D7C9500A31661 /* ProductCategoryListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductCategoryListMapper.swift; sourceTree = "<group>"; };
806808
26615478242DA54D00A31661 /* ProductCategoyListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductCategoyListMapperTests.swift; sourceTree = "<group>"; };
@@ -2161,6 +2163,7 @@
21612163
02BDB83423EA98C800BCC63E /* String+HTML.swift */,
21622164
57E8FED2246616AC0057CD68 /* Result+Extensions.swift */,
21632165
24F98C532502E8DD00F49B68 /* Bundle+Woo.swift */,
2166+
265EFBDB285257950033BD33 /* Order+Fallbacks.swift */,
21642167
);
21652168
path = Extensions;
21662169
sourceTree = "<group>";
@@ -2775,6 +2778,7 @@
27752778
025CA2C2238EBBAA00B05C81 /* ProductShippingClassListMapper.swift in Sources */,
27762779
74ABA1CD213F1B6B00FFAD30 /* TopEarnerStats.swift in Sources */,
27772780
CCAAD10F2683974000909664 /* ShippingLabelPackagePurchase.swift in Sources */,
2781+
265EFBDC285257950033BD33 /* Order+Fallbacks.swift in Sources */,
27782782
B557DA0220975500005962F4 /* JetpackRequest.swift in Sources */,
27792783
D88E229025AC990A0023F3B1 /* OrderFeeLine.swift in Sources */,
27802784
74046E1F217A6B70007DD7BF /* SiteSettingsMapper.swift in Sources */,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
/// Extension to provide fallbacks to new Order properties.
4+
///
5+
internal extension Order {
6+
7+
// MARK: WC 6.6 properties
8+
9+
/// Conditions copied from:
10+
/// https://github.com/woocommerce/woocommerce/blob/3611d4643791bad87a0d3e6e73e031bb80447417/plugins/woocommerce/includes/class-wc-order.php#L1520-L1523
11+
///
12+
static func inferNeedsPayment(status: OrderStatusEnum, total: String) -> Bool {
13+
guard let total = Double(total) else {
14+
return false
15+
}
16+
return total > .zero && (status == .pending || status == .failed)
17+
}
18+
19+
/// Conditions copied from:
20+
/// https://github.com/woocommerce/woocommerce/blob/3611d4643791bad87a0d3e6e73e031bb80447417/plugins/woocommerce/includes/class-wc-order.php#L1395-L1402
21+
///
22+
static func inferIsEditable(status: OrderStatusEnum) -> Bool {
23+
return status == .pending || status == .onHold || status == .autoDraft
24+
}
25+
}

Networking/Networking/Model/Order.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,6 @@ public struct Order: Decodable, GeneratedCopiable, GeneratedFakeable {
135135
let parentID = try container.decode(Int64.self, forKey: .parentID)
136136
let customerID = try container.decode(Int64.self, forKey: .customerID)
137137
let orderKey = try container.decode(String.self, forKey: .orderKey)
138-
139-
// TODO: Update with local fallback implementation https://github.com/woocommerce/woocommerce-ios/issues/6977
140-
let isEditable = try container.decodeIfPresent(Bool.self, forKey: .isEditable) ?? false
141-
let needsPayment = try container.decodeIfPresent(Bool.self, forKey: .needsPayment) ?? false
142-
let needsProcessing = try container.decodeIfPresent(Bool.self, forKey: .needsProcessing) ?? false
143-
144138
let number = try container.decode(String.self, forKey: .number)
145139
let status = try container.decode(OrderStatusEnum.self, forKey: .status)
146140

@@ -192,6 +186,14 @@ public struct Order: Decodable, GeneratedCopiable, GeneratedFakeable {
192186

193187
let taxes = try container.decode([OrderTaxLine].self, forKey: .taxLines)
194188

189+
// Properties added on WC 6.6, we provide a local fallback for older stores.
190+
let isEditable = try container.decodeIfPresent(Bool.self, forKey: .isEditable) ?? Self.inferIsEditable(status: status)
191+
let needsPayment = try container.decodeIfPresent(Bool.self, forKey: .needsPayment) ?? Self.inferNeedsPayment(status: status, total: total)
192+
193+
// TODO: Update with local fallback when required.
194+
// https://github.com/woocommerce/woocommerce/blob/3611d4643791bad87a0d3e6e73e031bb80447417/plugins/woocommerce/includes/class-wc-order.php#L1537-L1561
195+
let needsProcessing = try container.decodeIfPresent(Bool.self, forKey: .needsProcessing) ?? false
196+
195197
self.init(siteID: siteID,
196198
orderID: orderID,
197199
parentID: parentID,

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsViewModel.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -783,16 +783,3 @@ extension OrderDetailsViewModel {
783783
}
784784
}
785785
}
786-
787-
private extension Order {
788-
/// This check is temporary, we are working on knowing if an order needs payment directly from the API.
789-
/// Conditions copied from:
790-
/// https://github.com/woocommerce/woocommerce/blob/3611d4643791bad87a0d3e6e73e031bb80447417/plugins/woocommerce/includes/class-wc-order.php#L1520-L1523
791-
///
792-
var needsPayment: Bool {
793-
guard let total = Double(total) else {
794-
return false
795-
}
796-
return total > .zero && (status == .pending || status == .failed)
797-
}
798-
}

WooCommerce/WooCommerceTests/ViewRelated/OrderDetailsViewModelTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ final class OrderDetailsViewModelTests: XCTestCase {
9595

9696
func test_there_should_not_be_share_link_action_if_order_is_not_pending_payment() {
9797
// Given
98-
let order = Order.fake().copy(status: .processing, total: "10.0", paymentURL: nil)
98+
let order = Order.fake().copy(needsPayment: false, status: .processing, total: "10.0", paymentURL: nil)
9999

100100
// When
101101
let viewModel = OrderDetailsViewModel(order: order)
@@ -108,7 +108,7 @@ final class OrderDetailsViewModelTests: XCTestCase {
108108
func test_there_should_be_share_link_action_if_order_is_pending_payment() {
109109
// Given
110110
let paymentURL = URL(string: "http://www.automattic.com")
111-
let order = Order.fake().copy(status: .pending, total: "10.0", paymentURL: paymentURL)
111+
let order = Order.fake().copy(needsPayment: true, status: .pending, total: "10.0", paymentURL: paymentURL)
112112

113113
// When
114114
let viewModel = OrderDetailsViewModel(order: order)

0 commit comments

Comments
 (0)