Skip to content

Commit 8bc29b0

Browse files
authored
Shipping Labels: Update mark order completed toggle (#15917)
2 parents 25a2033 + 678aa7a commit 8bc29b0

File tree

23 files changed

+96
-25
lines changed

23 files changed

+96
-25
lines changed

Modules/Sources/Fakes/Networking.generated.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@ extension Networking.ShippingLabelAccountSettings {
14511451
isEmailReceiptsEnabled: .fake(),
14521452
paperSize: .fake(),
14531453
lastSelectedPackageID: .fake(),
1454+
lastOrderCompleted: .fake(),
14541455
addPaymentMethodURL: .fake()
14551456
)
14561457
}

Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,7 @@ extension Networking.ShippingLabelAccountSettings {
24242424
isEmailReceiptsEnabled: CopiableProp<Bool> = .copy,
24252425
paperSize: CopiableProp<ShippingLabelPaperSize> = .copy,
24262426
lastSelectedPackageID: CopiableProp<String> = .copy,
2427+
lastOrderCompleted: CopiableProp<Bool> = .copy,
24272428
addPaymentMethodURL: NullableCopiableProp<URL> = .copy
24282429
) -> Networking.ShippingLabelAccountSettings {
24292430
let siteID = siteID ?? self.siteID
@@ -2438,6 +2439,7 @@ extension Networking.ShippingLabelAccountSettings {
24382439
let isEmailReceiptsEnabled = isEmailReceiptsEnabled ?? self.isEmailReceiptsEnabled
24392440
let paperSize = paperSize ?? self.paperSize
24402441
let lastSelectedPackageID = lastSelectedPackageID ?? self.lastSelectedPackageID
2442+
let lastOrderCompleted = lastOrderCompleted ?? self.lastOrderCompleted
24412443
let addPaymentMethodURL = addPaymentMethodURL ?? self.addPaymentMethodURL
24422444

24432445
return Networking.ShippingLabelAccountSettings(
@@ -2453,6 +2455,7 @@ extension Networking.ShippingLabelAccountSettings {
24532455
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
24542456
paperSize: paperSize,
24552457
lastSelectedPackageID: lastSelectedPackageID,
2458+
lastOrderCompleted: lastOrderCompleted,
24562459
addPaymentMethodURL: addPaymentMethodURL
24572460
)
24582461
}

Modules/Sources/Networking/Model/ShippingLabel/ShippingLabelAccountSettings.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
4343
/// Uses the `id` for predefined packages or `name` for custom packages.
4444
public let lastSelectedPackageID: String
4545

46+
/// Whether to mark order as completed after last label purchase
47+
/// This is available only in the new Woo Shipping plugin.
48+
public let lastOrderCompleted: Bool
49+
4650
/// URL to open the web view for adding new payment methods
4751
public let addPaymentMethodURL: URL?
4852

@@ -58,6 +62,7 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
5862
isEmailReceiptsEnabled: Bool,
5963
paperSize: ShippingLabelPaperSize,
6064
lastSelectedPackageID: String,
65+
lastOrderCompleted: Bool,
6166
addPaymentMethodURL: URL?) {
6267
self.siteID = siteID
6368
self.canManagePayments = canManagePayments
@@ -71,6 +76,7 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
7176
self.isEmailReceiptsEnabled = isEmailReceiptsEnabled
7277
self.paperSize = paperSize
7378
self.lastSelectedPackageID = lastSelectedPackageID
79+
self.lastOrderCompleted = lastOrderCompleted
7480
self.addPaymentMethodURL = addPaymentMethodURL
7581
}
7682
}
@@ -105,6 +111,7 @@ extension ShippingLabelAccountSettings: Decodable {
105111

106112
let userMetaContainer = try container.nestedContainer(keyedBy: UserMetaKeys.self, forKey: .userMeta)
107113
let lastSelectedPackageID = try userMetaContainer.decode(String.self, forKey: .lastSelectedPackageID)
114+
let lastOrderCompleted = (try? userMetaContainer.decodeIfPresent(Bool.self, forKey: .lastOrderCompleted)) ?? false
108115

109116
self.init(siteID: siteID,
110117
canManagePayments: canManagePayments,
@@ -118,6 +125,7 @@ extension ShippingLabelAccountSettings: Decodable {
118125
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
119126
paperSize: paperSize,
120127
lastSelectedPackageID: lastSelectedPackageID,
128+
lastOrderCompleted: lastOrderCompleted,
121129
addPaymentMethodURL: addPaymentMethodURL)
122130
}
123131
}
@@ -150,6 +158,7 @@ private extension ShippingLabelAccountSettings {
150158

151159
private enum UserMetaKeys: String, CodingKey {
152160
case lastSelectedPackageID = "last_box_id"
161+
case lastOrderCompleted = "last_order_completed"
153162
}
154163
}
155164

Modules/Sources/Networking/Remote/WooShippingRemote.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public protocol WooShippingRemoteProtocol {
3737
originAddress: WooShippingAddress,
3838
destinationAddress: WooShippingAddress,
3939
package: WooShippingPackagePurchase,
40+
markOrderComplete: Bool?,
4041
completion: @escaping (Result<[ShippingLabelPurchase], Error>) -> Void)
4142
func checkLabelStatus(siteID: Int64,
4243
orderID: Int64,
@@ -284,7 +285,14 @@ public final class WooShippingRemote: Remote, WooShippingRemoteProtocol {
284285
originAddress: WooShippingAddress,
285286
destinationAddress: WooShippingAddress,
286287
package: WooShippingPackagePurchase,
288+
markOrderComplete: Bool?,
287289
completion: @escaping (Result<[ShippingLabelPurchase], Error>) -> Void) {
290+
let userMeta: [String: Any]? = {
291+
guard let markOrderComplete else {
292+
return nil
293+
}
294+
return [ParameterKey.lastOrderCompleted: markOrderComplete]
295+
}()
288296
do {
289297
let parameters: [String: Any] = [
290298
ParameterKey.async: true,
@@ -296,7 +304,8 @@ public final class WooShippingRemote: Remote, WooShippingRemoteProtocol {
296304
ParameterKey.featuresSupported: [Values.upsdap],
297305
ParameterKey.hazmat: package.encodedHazmat(),
298306
ParameterKey.customs: try package.encodedCustomsForm(),
299-
]
307+
ParameterKey.userMeta: userMeta,
308+
].compactMapValues { $0 }
300309
let path = "\(Path.purchase)/\(orderID)"
301310
let request = JetpackRequest(wooApiVersion: .wooShipping,
302311
method: .post,
@@ -635,6 +644,8 @@ private extension WooShippingRemote {
635644
static let enabled = "enabled"
636645
static let featuresSupported = "features_supported_by_client"
637646
static let confirmed = "confirmed"
647+
static let userMeta = "user_meta"
648+
static let lastOrderCompleted = "last_order_completed"
638649
}
639650

640651
enum Values {

Modules/Sources/Yosemite/Actions/WooShippingAction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public enum WooShippingAction: Action {
5656
originAddress: WooShippingAddress,
5757
destinationAddress: WooShippingAddress,
5858
package: WooShippingPackagePurchase,
59+
markOrderComplete: Bool?,
5960
backendProcessingDelay: TimeInterval = 2.0,
6061
pollingDelay: TimeInterval = 1.0,
6162
pollingMaximumRetries: Int64 = 3,

Modules/Sources/Yosemite/Model/Storage/ShippingLabelAccountSettings+ReadOnlyConvertible.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ extension Storage.ShippingLabelAccountSettings: ReadOnlyConvertible {
2626
let paymentMethodItems = paymentMethods?.map { $0.toReadOnly() } ?? []
2727

2828
/// Since account settings are not persisted for the new shipping label flow,
29-
/// the conversion for the new property `addPaymentMethodURL` is ignored.
30-
/// This avoids the complication of unnecessary Core Data migration for the new property.
29+
/// the conversion for the new properties `addPaymentMethodURL` & `lastOrderCompleted` is ignored.
30+
/// This avoids the complication of unnecessary Core Data migration for the new properties.
3131
return ShippingLabelAccountSettings(siteID: siteID,
3232
canManagePayments: canManagePayments,
3333
canEditSettings: canEditSettings,
@@ -40,6 +40,7 @@ extension Storage.ShippingLabelAccountSettings: ReadOnlyConvertible {
4040
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
4141
paperSize: .init(rawValue: paperSize ?? ""),
4242
lastSelectedPackageID: lastSelectedPackageID ?? "",
43+
lastOrderCompleted: false,
4344
addPaymentMethodURL: nil)
4445
}
4546
}

Modules/Sources/Yosemite/Stores/WooShippingStore.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public final class WooShippingStore: Store {
5757
originAddress,
5858
destinationAddress,
5959
package,
60+
markOrderComplete,
6061
backendProcessingDelay,
6162
pollingDelay,
6263
pollingMaximumRetries,
@@ -66,6 +67,7 @@ public final class WooShippingStore: Store {
6667
originAddress: originAddress,
6768
destinationAddress: destinationAddress,
6869
package: package,
70+
markOrderComplete: markOrderComplete,
6971
backendProcessingDelay: backendProcessingDelay,
7072
pollingDelay: pollingDelay,
7173
pollingMaximumRetries: pollingMaximumRetries,
@@ -216,6 +218,7 @@ private extension WooShippingStore {
216218
originAddress: WooShippingAddress,
217219
destinationAddress: WooShippingAddress,
218220
package: WooShippingPackagePurchase,
221+
markOrderComplete: Bool?,
219222
backendProcessingDelay: TimeInterval,
220223
pollingDelay: TimeInterval,
221224
pollingMaximumRetries: Int64,
@@ -225,7 +228,8 @@ private extension WooShippingStore {
225228
orderID: orderID,
226229
originAddress: originAddress,
227230
destinationAddress: destinationAddress,
228-
package: package) { result in
231+
package: package,
232+
markOrderComplete: markOrderComplete) { result in
229233
switch result {
230234
case .success(let labelPurchases):
231235
// Purchase endpoint returns an array of labels, but the polling endpoint only takes a single label at a time.

Modules/Tests/NetworkingTests/Mapper/ShippingLabelAccountSettingsMapperTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import XCTest
55

66
/// Unit Tests for `ShippingLabelAccountSettingsMapper`
77
///
8-
class ShippingLabelAccountSettingsMapperTests: XCTestCase {
8+
final class ShippingLabelAccountSettingsMapperTests: XCTestCase {
99

1010
/// Sample Site ID
1111
private let sampleSiteID: Int64 = 123456
@@ -31,6 +31,7 @@ class ShippingLabelAccountSettingsMapperTests: XCTestCase {
3131
XCTAssertEqual(settings.storeOwnerWpcomEmail, "[email protected]")
3232
XCTAssertEqual(settings.storeOwnerWpcomUsername, "apiexamples")
3333
XCTAssertEqual(settings.addPaymentMethodURL, URL(string: "https://wordpress.com/me/purchases/add-credit-card"))
34+
XCTAssertTrue(settings.lastOrderCompleted)
3435
}
3536

3637
/// Verifies that the Shipping Label Account Settings are parsed correctly.
@@ -54,6 +55,7 @@ class ShippingLabelAccountSettingsMapperTests: XCTestCase {
5455
XCTAssertEqual(settings.storeOwnerWpcomEmail, "[email protected]")
5556
XCTAssertEqual(settings.storeOwnerWpcomUsername, "apiexamples")
5657
XCTAssertEqual(settings.addPaymentMethodURL, URL(string: "https://wordpress.com/me/purchases/add-credit-card"))
58+
XCTAssertTrue(settings.lastOrderCompleted)
5759
}
5860

5961
/// Verifies that the Shipping Label Account Settings without any payment methods are parsed correctly.
@@ -77,6 +79,7 @@ class ShippingLabelAccountSettingsMapperTests: XCTestCase {
7779
XCTAssertEqual(settings.storeOwnerWpcomEmail, "[email protected]")
7880
XCTAssertEqual(settings.storeOwnerWpcomUsername, "apiexamples")
7981
XCTAssertEqual(settings.addPaymentMethodURL, URL(string: "https://wordpress.com/me/purchases/add-credit-card"))
82+
XCTAssertFalse(settings.lastOrderCompleted)
8083
}
8184

8285
}

Modules/Tests/NetworkingTests/Remote/WooShippingRemoteTests.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,16 @@ final class WooShippingRemoteTests: XCTestCase {
391391
additionalHandlingRate: ShippingLabelCarrierRate.fake().copy(rate: 20.01)
392392
)
393393
)
394+
let markOrderComplete = true
394395

395396
// When
396397
let result: Result<[ShippingLabelPurchase], Error> = waitFor { promise in
397398
remote.purchaseShippingLabel(siteID: self.sampleSiteID,
398399
orderID: self.sampleOrderID,
399400
originAddress: WooShippingAddress.fake(),
400401
destinationAddress: WooShippingAddress.fake(),
401-
package: package) { result in
402+
package: package,
403+
markOrderComplete: markOrderComplete) { result in
402404
promise(result)
403405
}
404406
}
@@ -456,6 +458,9 @@ final class WooShippingRemoteTests: XCTestCase {
456458
let customsShipment = try XCTUnwrap(customsValue["shipment_" + shipmentID] as? [String: Any])
457459
XCTAssertEqual((try XCTUnwrap(customsShipment["items"] as? [Any])).count, 2)
458460

461+
let userMetaValue = try XCTUnwrap(request.parameters["user_meta"] as? [String: Any])
462+
XCTAssertEqual(userMetaValue["last_order_completed"] as? Bool, true)
463+
459464
let labels = try XCTUnwrap(result.get())
460465
XCTAssertEqual(labels.count, 1)
461466
}
@@ -471,7 +476,8 @@ final class WooShippingRemoteTests: XCTestCase {
471476
orderID: self.sampleOrderID,
472477
originAddress: WooShippingAddress.fake(),
473478
destinationAddress: WooShippingAddress.fake(),
474-
package: WooShippingPackagePurchase.fake()) { result in
479+
package: WooShippingPackagePurchase.fake(),
480+
markOrderComplete: false) { result in
475481
promise(result)
476482
}
477483
}
@@ -492,7 +498,8 @@ final class WooShippingRemoteTests: XCTestCase {
492498
orderID: self.sampleOrderID,
493499
originAddress: WooShippingAddress.fake(),
494500
destinationAddress: WooShippingAddress.fake(),
495-
package: WooShippingPackagePurchase.fake()) { result in
501+
package: WooShippingPackagePurchase.fake(),
502+
markOrderComplete: false) { result in
496503
promise(result)
497504
}
498505
}

Modules/Tests/NetworkingTests/Responses/shipping-label-account-settings-without-data.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
}
3535
},
3636
"userMeta": {
37-
"last_box_id": "small_flat_box"
37+
"last_box_id": "small_flat_box",
38+
"last_order_completed": true
3839
}
3940
}

0 commit comments

Comments
 (0)