Skip to content

Commit b9a0a92

Browse files
committed
Add parent to AggregateOrderItem
1 parent b012818 commit b9a0a92

File tree

10 files changed

+90
-26
lines changed

10 files changed

+90
-26
lines changed

WooCommerce/Classes/Copiable/Models+Copiable.generated.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ extension WooCommerce.AggregateOrderItem {
1616
sku: NullableCopiableProp<String> = .copy,
1717
total: NullableCopiableProp<NSDecimalNumber> = .copy,
1818
imageURL: NullableCopiableProp<URL> = .copy,
19-
attributes: CopiableProp<[OrderItemAttribute]> = .copy
19+
attributes: CopiableProp<[OrderItemAttribute]> = .copy,
20+
parent: NullableCopiableProp<Int64> = .copy
2021
) -> WooCommerce.AggregateOrderItem {
2122
let itemID = itemID ?? self.itemID
2223
let productID = productID ?? self.productID
@@ -28,6 +29,7 @@ extension WooCommerce.AggregateOrderItem {
2829
let total = total ?? self.total
2930
let imageURL = imageURL ?? self.imageURL
3031
let attributes = attributes ?? self.attributes
32+
let parent = parent ?? self.parent
3133

3234
return WooCommerce.AggregateOrderItem(
3335
itemID: itemID,
@@ -39,7 +41,8 @@ extension WooCommerce.AggregateOrderItem {
3941
sku: sku,
4042
total: total,
4143
imageURL: imageURL,
42-
attributes: attributes
44+
attributes: attributes,
45+
parent: parent
4346
)
4447
}
4548
}

WooCommerce/Classes/Tools/AggregateData/AggregateDataHelper.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ final class AggregateDataHelper {
4848
.compactMap { currency.convertToDecimal($0.total) }
4949
.reduce(NSDecimalNumber(value: 0), { $0.adding($1) })
5050

51-
let attributes = orderItems.first(where: {
51+
// Find the order item matching this refund, to get its properties
52+
let matchingOrderItem = orderItems.first(where: {
5253
guard let refundedItemID = item.refundedItemID else {
5354
return false
5455
}
5556

5657
return $0.itemID == Int64(refundedItemID)
57-
})?.attributes ?? []
58+
})
59+
let attributes = matchingOrderItem?.attributes ?? []
60+
let parent = matchingOrderItem?.parent
5861

5962
return AggregateOrderItem(
6063
itemID: key,
@@ -65,7 +68,8 @@ final class AggregateDataHelper {
6568
quantity: totalQuantity,
6669
sku: item.sku,
6770
total: total,
68-
attributes: attributes
71+
attributes: attributes,
72+
parent: parent
6973
)
7074
}
7175

@@ -93,7 +97,8 @@ final class AggregateDataHelper {
9397
quantity: item.quantity,
9498
sku: item.sku,
9599
total: total,
96-
attributes: item.attributes
100+
attributes: item.attributes,
101+
parent: item.parent
97102
)
98103
}
99104

@@ -128,7 +133,8 @@ final class AggregateDataHelper {
128133
quantity: totalQuantity,
129134
sku: item.sku,
130135
total: total,
131-
attributes: item.attributes
136+
attributes: item.attributes,
137+
parent: item.parent
132138
)
133139
}
134140

WooCommerce/Classes/ViewModels/Order Details/Aggregate Order Items/AggregateOrderItem.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ struct AggregateOrderItem: Equatable, GeneratedCopiable {
2626

2727
let attributes: [OrderItemAttribute]
2828

29+
/// Item ID of the parent order item, if any.
30+
///
31+
let parent: Int64?
32+
2933
/// Designated initializer.
3034
///
3135
init(itemID: String,
@@ -37,7 +41,8 @@ struct AggregateOrderItem: Equatable, GeneratedCopiable {
3741
sku: String?,
3842
total: NSDecimalNumber?,
3943
imageURL: URL? = nil,
40-
attributes: [OrderItemAttribute]) {
44+
attributes: [OrderItemAttribute],
45+
parent: Int64?) {
4146
self.itemID = itemID
4247
self.productID = productID
4348
self.variationID = variationID
@@ -48,6 +53,7 @@ struct AggregateOrderItem: Equatable, GeneratedCopiable {
4853
self.total = total
4954
self.imageURL = imageURL
5055
self.attributes = attributes
56+
self.parent = parent
5157
}
5258
}
5359

WooCommerce/Classes/ViewModels/Order Details/Shipping Labels/AggregatedShippingLabelOrderItems.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private extension AggregatedShippingLabelOrderItems {
109109
func orderItem(from model: OrderItemModel, quantity: Int) -> AggregateOrderItem {
110110
switch model {
111111
case .productName(let name):
112-
return .init(itemID: "0", productID: 0, variationID: 0, name: name, price: nil, quantity: 0, sku: nil, total: nil, attributes: [])
112+
return .init(itemID: "0", productID: 0, variationID: 0, name: name, price: nil, quantity: 0, sku: nil, total: nil, attributes: [], parent: nil)
113113
case .product(let product, let orderItem, let name):
114114
let itemID = orderItem?.itemID.description ?? "0"
115115
let productName = orderItem?.name ?? name
@@ -131,7 +131,8 @@ private extension AggregatedShippingLabelOrderItems {
131131
sku: orderItem?.sku ?? product.sku,
132132
total: totalPrice,
133133
imageURL: imageURL,
134-
attributes: orderItem?.attributes ?? [])
134+
attributes: orderItem?.attributes ?? [],
135+
parent: orderItem?.parent)
135136
case .productVariation(let variation, let orderItem, let name):
136137
let itemID = orderItem?.itemID.description ?? "0"
137138
let productName = orderItem?.name ?? name
@@ -153,7 +154,8 @@ private extension AggregatedShippingLabelOrderItems {
153154
sku: orderItem?.sku ?? variation.sku,
154155
total: totalPrice,
155156
imageURL: imageURL,
156-
attributes: orderItem?.attributes ?? [])
157+
attributes: orderItem?.attributes ?? [],
158+
parent: orderItem?.parent)
157159
}
158160
}
159161

WooCommerce/WooCommerceTests/Tools/AggregateDataHelperTests.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ final class AggregateDataHelperTests: XCTestCase {
9898
XCTAssertEqual(aggregatedOrderItems[0].attributes, testOrderItemAttributes)
9999
}
100100

101+
func test_refunded_AggregateOrderItem_has_parent_from_OrderItem() throws {
102+
// Given
103+
let productID: Int64 = 1
104+
let parentID: Int64 = 61
105+
let orderItems = [MockOrderItem.sampleItem(itemID: parentID, productID: productID, quantity: 1),
106+
MockOrderItem.sampleItem(itemID: 62, productID: productID, quantity: 1, parent: parentID)]
107+
let refundItems = [MockRefunds.sampleRefundItem(productID: productID, refundedItemID: "62", quantity: 1)]
108+
let refunds = [MockRefunds.sampleRefund(items: refundItems)]
109+
110+
// When
111+
let aggregatedRefundItems = AggregateDataHelper.combineRefundedProducts(from: refunds, orderItems: orderItems)
112+
113+
// Then
114+
let actualParentID = try XCTUnwrap(aggregatedRefundItems?.first).parent
115+
XCTAssertEqual(actualParentID, parentID)
116+
}
117+
101118
func test_two_order_items_with_same_productID_and_different_itemIDs_create_two_AggregateOrderItems() {
102119
// Given
103120
let productID: Int64 = 1
@@ -183,7 +200,8 @@ private extension AggregateDataHelperTests {
183200
quantity: -1,
184201
sku: "HOODIE-HAPPY-NINJA",
185202
total: currencyFormatter.convertToDecimal("-31.50") ?? NSDecimalNumber.zero,
186-
attributes: []
203+
attributes: [],
204+
parent: nil
187205
)
188206

189207
let item1 = AggregateOrderItem(
@@ -195,7 +213,8 @@ private extension AggregateDataHelperTests {
195213
quantity: -1,
196214
sku: "T-SHIRT-NINJA-SILHOUETTE",
197215
total: currencyFormatter.convertToDecimal("-18.00") ?? NSDecimalNumber.zero,
198-
attributes: []
216+
attributes: [],
217+
parent: nil
199218
)
200219

201220
let item2 = AggregateOrderItem(
@@ -207,7 +226,8 @@ private extension AggregateDataHelperTests {
207226
quantity: -1,
208227
sku: "HOODIE-SHIP-YOUR-IDEA-BLACK-L",
209228
total: currencyFormatter.convertToDecimal("-31.50") ?? NSDecimalNumber.zero,
210-
attributes: testOrderItemAttributes
229+
attributes: testOrderItemAttributes,
230+
parent: nil
211231
)
212232

213233
let item3 = AggregateOrderItem(
@@ -219,7 +239,8 @@ private extension AggregateDataHelperTests {
219239
quantity: -2,
220240
sku: "HOODIE-WOO-LOGO",
221241
total: currencyFormatter.convertToDecimal("-63.00") ?? NSDecimalNumber.zero,
222-
attributes: []
242+
attributes: [],
243+
parent: nil
223244
)
224245

225246
let item4 = AggregateOrderItem(
@@ -231,7 +252,8 @@ private extension AggregateDataHelperTests {
231252
quantity: -3,
232253
sku: "HOODIE-SHIP-YOUR-IDEA-BLUE-XL",
233254
total: currencyFormatter.convertToDecimal("-81.00") ?? NSDecimalNumber.zero,
234-
attributes: []
255+
attributes: [],
256+
parent: nil
235257
)
236258

237259
return [item0, item1, item2, item3, item4]

WooCommerce/WooCommerceTests/Tools/MockAggregateOrderItem.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public struct MockAggregateOrderItem {
1414
quantity: 0,
1515
sku: nil,
1616
total: nil,
17-
attributes: [])
17+
attributes: [],
18+
parent: nil)
1819
}
1920
}

WooCommerce/WooCommerceTests/Tools/MockOrderItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public struct MockOrderItem {
3333
total: total,
3434
totalTax: totalTax,
3535
attributes: attributes,
36-
parent: nil)
36+
parent: parent)
3737
}
3838
}

WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/ProductDetailsCellViewModelTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ private extension ProductDetailsCellViewModelTests {
225225
total: NSDecimalNumber?,
226226
sku: String = "",
227227
imageURL: URL? = nil,
228-
attributes: [OrderItemAttribute] = []) -> AggregateOrderItem {
228+
attributes: [OrderItemAttribute] = [],
229+
parent: Int64? = nil) -> AggregateOrderItem {
229230
AggregateOrderItem(itemID: "2",
230231
productID: 1,
231232
variationID: 6,
@@ -235,7 +236,8 @@ private extension ProductDetailsCellViewModelTests {
235236
sku: sku,
236237
total: total,
237238
imageURL: imageURL,
238-
attributes: attributes)
239+
attributes: attributes,
240+
parent: parent)
239241
}
240242

241243
func makeOrderItemRefund(quantity: Decimal,

WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/Shipping Labels/AggregatedShippingLabelOrderItemsTests.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ final class AggregatedShippingLabelOrderItemsTests: XCTestCase {
1717

1818
// Then
1919
XCTAssertEqual(shippingLabelOrderItems, [
20-
.init(itemID: "0", productID: 0, variationID: 0, name: "Password protected!", price: nil, quantity: 0, sku: nil, total: nil, attributes: [])
20+
.init(itemID: "0",
21+
productID: 0,
22+
variationID: 0,
23+
name: "Password protected!",
24+
price: nil,
25+
quantity: 0,
26+
sku: nil,
27+
total: nil,
28+
attributes: [],
29+
parent: nil)
2130
])
2231
XCTAssertEqual(shippingLabelOrderItems[0], aggregatedOrderItems.orderItem(of: shippingLabel, at: 0))
2332
}
@@ -50,10 +59,20 @@ final class AggregatedShippingLabelOrderItemsTests: XCTestCase {
5059
sku: orderItem1.sku,
5160
total: 59.2,
5261
imageURL: imageURL1,
53-
attributes: orderItem1.attributes),
62+
attributes: orderItem1.attributes,
63+
parent: nil),
5464
// Product with ID 3013 does not have a matching OrderItem so the price and SKU come from the Product.
5565
// Since a Product's name could change, the name falls back to the name in shipping label's `productNames`.
56-
.init(itemID: "0", productID: 3013, variationID: 0, name: "PW", price: 25.9, quantity: 3, sku: product2.sku, total: 77.7, attributes: [])
66+
.init(itemID: "0",
67+
productID: 3013,
68+
variationID: 0,
69+
name: "PW",
70+
price: 25.9,
71+
quantity: 3,
72+
sku: product2.sku,
73+
total: 77.7,
74+
attributes: [],
75+
parent: nil)
5776
])
5877
XCTAssertEqual(shippingLabelOrderItems[0], aggregatedOrderItems.orderItem(of: shippingLabel, at: 0))
5978
XCTAssertEqual(shippingLabelOrderItems[1], aggregatedOrderItems.orderItem(of: shippingLabel, at: 1))
@@ -97,7 +116,8 @@ final class AggregatedShippingLabelOrderItemsTests: XCTestCase {
97116
sku: orderItem.sku,
98117
total: 25.9,
99118
imageURL: imageURL,
100-
attributes: orderItem.attributes)
119+
attributes: orderItem.attributes,
120+
parent: nil)
101121
])
102122
XCTAssertEqual(shippingLabelOrderItems[0], aggregatedOrderItems.orderItem(of: shippingLabel, at: 0))
103123
}
@@ -131,7 +151,8 @@ final class AggregatedShippingLabelOrderItemsTests: XCTestCase {
131151
sku: variation.sku,
132152
total: 62,
133153
imageURL: imageURL,
134-
attributes: [])
154+
attributes: [],
155+
parent: nil)
135156
])
136157
XCTAssertEqual(shippingLabelOrderItems[0], aggregatedOrderItems.orderItem(of: shippingLabel, at: 0))
137158
}

WooCommerce/WooCommerceTests/ViewRelated/Products/Product Loader/ProductLoaderViewControllerModelTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ private extension ProductLoaderViewControllerModelTests {
134134
quantity: 3,
135135
sku: nil,
136136
total: 3.6,
137-
attributes: [])
137+
attributes: [],
138+
parent: nil)
138139
}
139140

140141
func makeTopEarnerStatsItem(productID: Int64) -> TopEarnerStatsItem {

0 commit comments

Comments
 (0)