Skip to content

Commit a150143

Browse files
[Shipping Labels] Delete redundant hstariff class validation logic (#15990)
2 parents 805a719 + b3b97d5 commit a150143

File tree

4 files changed

+35
-153
lines changed

4 files changed

+35
-153
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModel.swift

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,34 +120,28 @@ private extension WooShippingCustomsFormViewModel {
120120
}
121121
.assign(to: &$isMissingITN)
122122

123-
let hsTariffNumberTotalValueDictionary = $itemsViewModels
124-
.map { childViewModels in
125-
childViewModels.map { $0.$hsTariffNumberTotalValue.eraseToAnyPublisher() }
126-
}
127-
.flatMap { childPublishers in
128-
childPublishers.combineLatest()
129-
}
130-
.map { values in
131-
var hsTariffNumberTotalValueDictionary: [String: Decimal] = [:]
132-
for (hsTariffNumber, totalValuePerItem) in values.compacted() {
133-
hsTariffNumberTotalValueDictionary[hsTariffNumber, default: 0] += totalValuePerItem
134-
}
135-
return hsTariffNumberTotalValueDictionary
136-
}
123+
let totalItemValue = $itemsViewModels
124+
.map { childViewModels in
125+
childViewModels.map { $0.$totalValue.eraseToAnyPublisher() }
126+
}
127+
.flatMap { childPublishers in
128+
childPublishers.combineLatest()
129+
}
130+
.map { values in
131+
return values.reduce(0) { partialResult, value in
132+
return partialResult + value
133+
}
134+
}
137135

138136
$internationalTransactionNumber.combineLatest(
139-
$itemsViewModels,
140-
hsTariffNumberTotalValueDictionary,
137+
totalItemValue,
141138
$destinationCountryCode)
142139
.map { input -> ITNValidationError? in
143-
let (itn, items, hsTariffNumberTotalValueDictionary, countryCode) = input
140+
let (itn, totalItemValue, countryCode) = input
144141
guard itn.isEmpty else {
145142
return ITNNumberValidator.isValid(itn) ? nil : .invalidFormat
146143
}
147144

148-
let totalItemValue = items.reduce(0, { sum, item in
149-
sum + item.totalValue
150-
})
151145
if totalItemValue > Constants.minimumValueForRequiredITN {
152146
return .missingForTotalShipmentValue
153147
}
@@ -160,11 +154,6 @@ private extension WooShippingCustomsFormViewModel {
160154
return .missingForRequiredDestination
161155
}
162156

163-
for (_, value) in hsTariffNumberTotalValueDictionary {
164-
if value > Constants.minimumValueForRequiredITN {
165-
return .missingForTariffClass
166-
}
167-
}
168157
return nil
169158
}
170159
.assign(to: &$itnValidationError)

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModel.swift

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ final class WooShippingCustomsItemViewModel: ObservableObject {
99
@Published var hsTariffNumber: String = ""
1010
@Published var valuePerUnit: String = ""
1111
@Published var weightPerUnit: String = ""
12-
// Useful to determine externally if the shipping requires an ITN
13-
@Published var hsTariffNumberTotalValue: (String, Decimal)?
1412

1513
@Published private var originCountryCode: String?
1614

@@ -31,13 +29,7 @@ final class WooShippingCustomsItemViewModel: ObservableObject {
3129

3230
@Published private(set) var countries: [Country] = []
3331

34-
var totalValue: Decimal {
35-
guard currencySymbol == "$",
36-
let valuePerUnitDecimal = Decimal(string: valuePerUnit) else {
37-
return 0
38-
}
39-
return valuePerUnitDecimal * itemQuantity
40-
}
32+
@Published private(set) var totalValue: Decimal = 0
4133

4234
/// View model for selecting a country from a list.
4335
var countrySelectorVM: CountrySelectorViewModel {
@@ -104,7 +96,7 @@ final class WooShippingCustomsItemViewModel: ObservableObject {
10496

10597
combineToPreselectCountry()
10698
combineRequiredInformationIsEntered()
107-
combineHSTariffNumberTotalValue()
99+
combineTotalItemValue()
108100
}
109101
}
110102

@@ -162,22 +154,19 @@ private extension WooShippingCustomsItemViewModel {
162154
.store(in: &cancellables)
163155
}
164156

165-
func combineHSTariffNumberTotalValue() {
166-
Publishers.CombineLatest($valuePerUnit, $hsTariffNumber)
167-
.sink { [weak self] valuePerUnit, hsTariffNumber in
168-
guard let self = self else { return }
169-
170-
guard self.currencySymbol == "$",
171-
let valuePerUnitDecimal = Decimal(string: valuePerUnit),
172-
hsTariffNumber.isNotEmpty,
173-
isValidTariffNumber else {
174-
self.hsTariffNumberTotalValue = nil
175-
return
176-
}
177-
178-
self.hsTariffNumberTotalValue = (hsTariffNumber, valuePerUnitDecimal * itemQuantity)
157+
func combineTotalItemValue() {
158+
$valuePerUnit.map { [weak self] valuePerUnit in
159+
guard
160+
let self,
161+
currencySymbol == "$",
162+
let valuePerUnitDecimal = Decimal(string: valuePerUnit)
163+
else {
164+
return 0
179165
}
180-
.store(in: &cancellables)
166+
167+
return valuePerUnitDecimal * itemQuantity
168+
}
169+
.assign(to: &$totalValue)
181170
}
182171

183172
/// Specifically introduced to check for a `0` value

WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModelTests.swift

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ final class WooShippingCustomsFormViewModelTests: XCTestCase {
150150
}
151151
}
152152

153-
func test_itnValidationError_when_item_view_models_hsTariffNumberTotalValue_is_nil() {
153+
func test_itnValidationError_when_item_view_models_total_value_exceeds_threshold() {
154154
// Given
155155
viewModel = WooShippingCustomsFormViewModel(order: Order.fake().copy(currency: "USD"),
156156
shipment: sampleShipment,
@@ -173,45 +173,6 @@ final class WooShippingCustomsFormViewModelTests: XCTestCase {
173173
XCTAssertEqual(viewModel.itnValidationError, .missingForTotalShipmentValue)
174174
}
175175

176-
func test_itnValidationError_when_item_view_models_hsTariffNumberTotalValue_is_less_than_2500() {
177-
// Given
178-
viewModel = WooShippingCustomsFormViewModel(order: Order.fake(),
179-
shipment: sampleShipment,
180-
onFormReady: { _ in })
181-
182-
// When
183-
viewModel.itemsViewModels.first?.hsTariffNumberTotalValue = ("123456", 1000)
184-
185-
// Then
186-
XCTAssertNil(viewModel.itnValidationError)
187-
}
188-
189-
func test_itnValidationError_when_item_view_models_hsTariffNumberTotalValue_is_more_than_2500() {
190-
// Given
191-
viewModel = WooShippingCustomsFormViewModel(order: Order.fake(),
192-
shipment: sampleShipment,
193-
onFormReady: { _ in })
194-
195-
// When
196-
viewModel.itemsViewModels[0].requiredInformationIsEntered = true
197-
viewModel.itemsViewModels[0].hsTariffNumberTotalValue = ("123456", 1000)
198-
viewModel.itemsViewModels[1].hsTariffNumberTotalValue = ("123456", 2000)
199-
viewModel.itemsViewModels[1].requiredInformationIsEntered = true
200-
viewModel.internationalTransactionNumber = ""
201-
viewModel.updateDestinationCountry(code: "CA") // ITN is not required for Canada
202-
203-
// Then
204-
XCTAssertTrue(viewModel.requiredInformationIsEntered)
205-
XCTAssertNil(viewModel.itnValidationError)
206-
207-
// When
208-
viewModel.updateDestinationCountry(code: "UK")
209-
210-
// Then
211-
XCTAssertFalse(viewModel.requiredInformationIsEntered)
212-
XCTAssertEqual(viewModel.itnValidationError, .missingForTariffClass)
213-
}
214-
215176
func test_itnValidationError_when_destination_country_requires_ITN() {
216177
// Given
217178
let requiredDestinations = ["IR", "SY", "KP", "CU", "SD"]
@@ -481,12 +442,16 @@ final class WooShippingCustomsFormViewModelTests: XCTestCase {
481442

482443
private extension WooShippingCustomsFormViewModelTests {
483444
var sampleShipment: Shipment {
445+
return sampleShipment()
446+
}
447+
448+
func sampleShipment(_ manualValuePerUnit: Double? = nil) -> Shipment {
484449
let item1 = ShippingLabelPackageItem(productOrVariationID: 1,
485450
orderItemID: 123,
486451
name: "Shirt",
487452
weight: 0.5,
488453
quantity: 2,
489-
value: 9.99,
454+
value: manualValuePerUnit ?? 9.99,
490455
dimensions: ProductDimensions.fake(),
491456
attributes: [],
492457
imageURL: nil)
@@ -495,7 +460,7 @@ private extension WooShippingCustomsFormViewModelTests {
495460
name: "Pants",
496461
weight: 0.5,
497462
quantity: 1,
498-
value: 11,
463+
value: manualValuePerUnit ?? 11,
499464
dimensions: ProductDimensions.fake(),
500465
attributes: [],
501466
imageURL: nil)

WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModelTests.swift

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -66,67 +66,6 @@ final class WooShippingCustomsItemViewModelTests: XCTestCase {
6666
XCTAssertFalse(viewModel.isValidWeight, "isValidWeight should be false when the weight is zero")
6767
}
6868

69-
func test_hsTariffNumberTotalValue_when_currency_symbol_is_not_$_returns_nil() {
70-
// Given
71-
viewModel = WooShippingCustomsItemViewModel(itemName: "Test",
72-
itemProductID: 22,
73-
itemQuantity: 1,
74-
itemValue: 0,
75-
itemWeight: 0,
76-
currencySymbol: "$")
77-
78-
// Then
79-
XCTAssertNil(viewModel.hsTariffNumberTotalValue)
80-
}
81-
82-
func test_hsTariffNumberTotalValue_when_currency_symbol_is_$_but_hsTariffNumber_is_empty_returns_nil() {
83-
//Given
84-
viewModel = WooShippingCustomsItemViewModel(itemName: "Test",
85-
itemProductID: 22,
86-
itemQuantity: 1,
87-
itemValue: 0,
88-
itemWeight: 0,
89-
currencySymbol: "$")
90-
91-
// When
92-
viewModel.valuePerUnit = "1000"
93-
viewModel.hsTariffNumber = ""
94-
95-
// Then
96-
XCTAssertNil(viewModel.hsTariffNumberTotalValue)
97-
}
98-
99-
func test_hsTariffNumberTotalValue_when_currency_symbol_is_$_but_invalid_hs_tariff_number_returns_nil() {
100-
// Given
101-
viewModel = WooShippingCustomsItemViewModel(itemName: "Test",
102-
itemProductID: 22,
103-
itemQuantity: 1,
104-
itemValue: 0,
105-
itemWeight: 0,
106-
currencySymbol: "$")
107-
viewModel.hsTariffNumber = "123"
108-
viewModel.valuePerUnit = "1000"
109-
110-
// Then
111-
XCTAssertNil(viewModel.hsTariffNumberTotalValue)
112-
}
113-
114-
func test_hsTariffNumberTotalValue_when_currency_symbol_is_$_and_value_is_more_than_2500_and_valid_hs_tariff_number_returns_values() {
115-
// Given
116-
viewModel = WooShippingCustomsItemViewModel(itemName: "Test",
117-
itemProductID: 22,
118-
itemQuantity: 2,
119-
itemValue: 0,
120-
itemWeight: 0,
121-
currencySymbol: "$")
122-
viewModel.valuePerUnit = "3000"
123-
viewModel.hsTariffNumber = "123456"
124-
125-
// Then
126-
XCTAssertEqual(viewModel.hsTariffNumberTotalValue?.0, viewModel.hsTariffNumber)
127-
XCTAssertEqual(viewModel.hsTariffNumberTotalValue?.1, Decimal(string: viewModel.valuePerUnit)! * 2)
128-
}
129-
13069
func test_isNumberValid_whenGivenValidTariffNumbers_shouldReturnTrue() {
13170
XCTAssertTrue(HSTariffNumberValidator.isNumberValid("123456"))
13271
XCTAssertTrue(HSTariffNumberValidator.isNumberValid("12.34.56"))

0 commit comments

Comments
 (0)