Skip to content

Commit 300a2f9

Browse files
authored
[POS Orders] Move sales channel representation to Networking (#15863)
2 parents 90d9ddf + 7df616b commit 300a2f9

File tree

6 files changed

+42
-32
lines changed

6 files changed

+42
-32
lines changed

Modules/Sources/NetworkingCore/Model/Order.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public struct Order: Decodable, Sendable, GeneratedCopiable, GeneratedFakeable {
6969
///
7070
public let createdVia: String?
7171

72+
public var salesChannel: SalesChannel? {
73+
guard let createdVia else { return nil }
74+
return SalesChannel(rawValue: createdVia)
75+
}
76+
7277
/// Order struct initializer.
7378
///
7479
public init(siteID: Int64,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Foundation
2+
3+
public enum SalesChannel {
4+
case pointOfSale
5+
}
6+
7+
extension SalesChannel: RawRepresentable {
8+
public init?(rawValue: String) {
9+
switch rawValue {
10+
case "pos-rest-api":
11+
self = .pointOfSale
12+
default:
13+
return nil
14+
}
15+
}
16+
17+
public var rawValue: String {
18+
switch self {
19+
case .pointOfSale:
20+
return description
21+
}
22+
}
23+
24+
public var description: String {
25+
switch self {
26+
case .pointOfSale:
27+
return NSLocalizedString("salesChannel.pos.description",
28+
value: "POS",
29+
comment: "The acronym for 'Point of Sale'.")
30+
}
31+
}
32+
}

WooCommerce/Classes/ViewRelated/Orders/Cells/OrderListCellViewModel.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,7 @@ struct OrderListCellViewModel {
7979
/// Textual representation of the sales channel
8080
///
8181
var salesChannel: String? {
82-
guard let createdVia = order.createdVia else {
83-
return nil
84-
}
85-
86-
switch createdVia {
87-
case "pos-rest-api":
88-
return Localization.salesChannelPOSText
89-
default:
90-
return nil
91-
}
82+
order.salesChannel?.description
9283
}
9384

9485
/// The localized unabbreviated total for a given order item, which includes the currency.
@@ -128,9 +119,5 @@ extension OrderListCellViewModel {
128119
"orderlistcellviewmodel.customerName.guestName",
129120
value: "Guest",
130121
comment: "In Order List, the name of the billed person when there are no first and last name.")
131-
static let salesChannelPOSText = NSLocalizedString(
132-
"orderlistcellviewmodel.salesChannel.salesChannelPOSText",
133-
value: "POS",
134-
comment: "In Order List, the acronym for 'Point of Sale' that is shown as a badge for certain orders.")
135122
}
136123
}

WooCommerce/Classes/ViewRelated/Orders/Order Details/Order Summary Section/SummaryTableViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class SummaryTableViewCell: UITableViewCell {
3131
func configure(_ viewModel: SummaryTableViewCellViewModel) {
3232
titleLabel.text = viewModel.billedPersonName
3333
subtitleLabel.text = viewModel.subtitle
34-
salesChannelLabel.text = viewModel.formattedSalesChannel
34+
salesChannelLabel.text = viewModel.salesChannel
3535
salesChannelLabel.isHidden = (salesChannelLabel.text == nil)
3636
display(presentation: viewModel.presentation)
3737
}

WooCommerce/Classes/ViewRelated/Orders/Order Details/Order Summary Section/SummaryTableViewCellViewModel.swift

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct SummaryTableViewCellViewModel {
99

1010
private let billingAddress: Address?
1111
private let dateCreated: Date
12-
private let salesChannel: String?
12+
private(set) var salesChannel: String?
1313

1414
let presentation: OrderStatusPresentation
1515

@@ -21,7 +21,7 @@ struct SummaryTableViewCellViewModel {
2121

2222
billingAddress = order.billingAddress
2323
dateCreated = order.dateCreated
24-
salesChannel = order.createdVia
24+
salesChannel = order.salesChannel?.description
2525

2626
presentation = OrderStatusPresentation(
2727
style: status?.status ?? order.status,
@@ -48,20 +48,6 @@ struct SummaryTableViewCellViewModel {
4848
formatter.timeZone = .siteTimezone
4949
return formatter.string(from: dateCreated)
5050
}
51-
52-
/// Textual representation of the sales channel
53-
///
54-
var formattedSalesChannel: String? {
55-
guard let salesChannel = salesChannel else {
56-
return nil
57-
}
58-
switch salesChannel {
59-
case "pos-rest-api":
60-
return "POS"
61-
default:
62-
return nil
63-
}
64-
}
6551
}
6652

6753
private extension SummaryTableViewCellViewModel {

WooCommerce/WooCommerceTests/ViewRelated/Orders/OrderListCellViewModelTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ final class OrderListCellViewModelTests: XCTestCase {
7070
let viewModel = OrderListCellViewModel(order: order, currencySettings: ServiceLocator.currencySettings)
7171

7272
// Then
73-
XCTAssertEqual(viewModel.salesChannel, OrderListCellViewModel.Localization.salesChannelPOSText)
73+
XCTAssertEqual(viewModel.salesChannel, "POS")
7474
}
7575

7676
func test_salesChannel_when_createdVia_is_nil_then_returns_nil() {

0 commit comments

Comments
 (0)