@@ -26,22 +26,7 @@ public struct Coupon {
2626 public let dateModified : Date
2727
2828 /// Determines the type of discount that will be applied. Options: `.percent` `.fixedCart` and `.fixedProduct`
29- public var discountType : DiscountType {
30- if let type = mappedDiscountType {
31- return type
32- } else {
33- // Returns default value for fallback case to avoid working with optionals.
34- // Since `CouponListMapper` filters out nil `mappedDiscountType`,
35- // this case is unlikely to happen.
36- return . fixedCart
37- }
38- }
39-
40- /// Discount type if matched with any of the ones supported by Core.
41- /// Returns nil if other types are found.
42- /// Used to filter only coupons with default types, so internal to this module only.
43- ///
44- internal let mappedDiscountType : DiscountType ?
29+ public let discountType : DiscountType
4530
4631 public let description : String
4732
@@ -97,9 +82,10 @@ public struct Coupon {
9782 /// There are other types supported by other plugins, but those are not supported for now.
9883 ///
9984 public enum DiscountType : String {
100- case percent = " percent "
85+ case percent
10186 case fixedCart = " fixed_cart "
10287 case fixedProduct = " fixed_product "
88+ case other
10389 }
10490
10591 public init ( siteID: Int64 = 0 ,
@@ -132,7 +118,7 @@ public struct Coupon {
132118 self . amount = amount
133119 self . dateCreated = dateCreated
134120 self . dateModified = dateModified
135- self . mappedDiscountType = discountType
121+ self . discountType = discountType
136122 self . description = description
137123 self . dateExpires = dateExpires
138124 self . usageCount = usageCount
@@ -166,7 +152,7 @@ extension Coupon: Codable {
166152 case amount
167153 case dateCreated = " dateCreatedGmt "
168154 case dateModified = " dateModifiedGmt "
169- case mappedDiscountType
155+ case discountType
170156 case description
171157 case dateExpires = " dateExpiresGmt "
172158 case usageCount
@@ -187,7 +173,12 @@ extension Coupon: Codable {
187173 }
188174}
189175
190- extension Coupon . DiscountType : Codable { }
176+ extension Coupon . DiscountType : Codable {
177+ public init ( from decoder: Decoder ) throws {
178+ let rawValue = try decoder. singleValueContainer ( ) . decode ( String . self)
179+ self = Coupon . DiscountType ( rawValue: rawValue) ?? . other
180+ }
181+ }
191182
192183
193184// MARK: - Other Conformances
0 commit comments