Manos Chorianopoulos

optional language parameter added to requests, fixed getCouponSets, getCouponsUniversal

This diff is collapsed. Click to expand it.
...@@ -1582,10 +1582,13 @@ public final class WarplySDK { ...@@ -1582,10 +1582,13 @@ public final class WarplySDK {
1582 // MARK: - Campaigns 1582 // MARK: - Campaigns
1583 1583
1584 /// Get campaigns with filters 1584 /// Get campaigns with filters
1585 - public func getCampaigns(language: String, filters: [String: Any] = [:], completion: @escaping ([CampaignItemModel]?) -> Void, failureCallback: @escaping (Int) -> Void) { 1585 + public func getCampaigns(language: String? = nil, filters: [String: Any] = [:], completion: @escaping ([CampaignItemModel]?) -> Void, failureCallback: @escaping (Int) -> Void) {
1586 + // Handle language default inside the method
1587 + let finalLanguage = language ?? self.applicationLocale
1588 +
1586 Task { 1589 Task {
1587 do { 1590 do {
1588 - let endpoint = Endpoint.getCampaigns(language: language, filters: filters) 1591 + let endpoint = Endpoint.getCampaigns(language: finalLanguage, filters: filters)
1589 let response = try await networkService.requestRaw(endpoint) 1592 let response = try await networkService.requestRaw(endpoint)
1590 1593
1591 var campaignsArray: [CampaignItemModel] = [] 1594 var campaignsArray: [CampaignItemModel] = []
...@@ -1687,10 +1690,13 @@ public final class WarplySDK { ...@@ -1687,10 +1690,13 @@ public final class WarplySDK {
1687 } 1690 }
1688 1691
1689 /// Get personalized campaigns 1692 /// Get personalized campaigns
1690 - public func getCampaignsPersonalized(language: String, filters: [String: Any] = [:], completion: @escaping ([CampaignItemModel]?) -> Void, failureCallback: @escaping (Int) -> Void) { 1693 + public func getCampaignsPersonalized(language: String? = nil, filters: [String: Any] = [:], completion: @escaping ([CampaignItemModel]?) -> Void, failureCallback: @escaping (Int) -> Void) {
1694 + // Handle language default inside the method
1695 + let finalLanguage = language ?? self.applicationLocale
1696 +
1691 Task { 1697 Task {
1692 do { 1698 do {
1693 - let endpoint = Endpoint.getCampaignsPersonalized(language: language, filters: filters) 1699 + let endpoint = Endpoint.getCampaignsPersonalized(language: finalLanguage, filters: filters)
1694 let response = try await networkService.requestRaw(endpoint) 1700 let response = try await networkService.requestRaw(endpoint)
1695 1701
1696 var campaignsArray: [CampaignItemModel] = [] 1702 var campaignsArray: [CampaignItemModel] = []
...@@ -1913,7 +1919,9 @@ public final class WarplySDK { ...@@ -1913,7 +1919,9 @@ public final class WarplySDK {
1913 // MARK: - Coupons 1919 // MARK: - Coupons
1914 1920
1915 /// Get coupons 1921 /// Get coupons
1916 - public func getCoupons(language: String, completion: @escaping ([CouponItemModel]?) -> Void, failureCallback: @escaping (Int) -> Void) { 1922 + public func getCoupons(language: String? = nil, completion: @escaping ([CouponItemModel]?) -> Void, failureCallback: @escaping (Int) -> Void) {
1923 + // Handle language default inside the method
1924 + let finalLanguage = language ?? self.applicationLocale
1917 // First get merchants 1925 // First get merchants
1918 // getMultilingualMerchants(categories: [], defaultShown: false, center: 0.0, tags: [], uuid: "", distance: 0, parentUuids: []) { merchantsData in 1926 // getMultilingualMerchants(categories: [], defaultShown: false, center: 0.0, tags: [], uuid: "", distance: 0, parentUuids: []) { merchantsData in
1919 // if let merchantsData = merchantsData { 1927 // if let merchantsData = merchantsData {
...@@ -1963,12 +1971,17 @@ public final class WarplySDK { ...@@ -1963,12 +1971,17 @@ public final class WarplySDK {
1963 self.setAllOldCouponList(couponsArray) 1971 self.setAllOldCouponList(couponsArray)
1964 1972
1965 // Filter out supermarket coupons 1973 // Filter out supermarket coupons
1966 - let noSMCoupons = couponsArray.filter { $0.couponset_data?.couponset_type != "supermarket" } 1974 + // let noSMCoupons = couponsArray.filter { $0.couponset_data?.couponset_type != "supermarket" }
1975 +
1976 + // self.setCouponList(noSMCoupons)
1977 + // self.setOldCouponList(noSMCoupons)
1978 +
1979 + // var activeCoupons = noSMCoupons.filter { $0.status == 1 }
1967 1980
1968 - self.setCouponList(noSMCoupons) 1981 + self.setCouponList(couponsArray)
1969 - self.setOldCouponList(noSMCoupons) 1982 + self.setOldCouponList(couponsArray)
1970 1983
1971 - var activeCoupons = noSMCoupons.filter { $0.status == 1 } 1984 + var activeCoupons = couponsArray.filter { $0.status == 1 }
1972 1985
1973 // Sort active coupons by expiration date 1986 // Sort active coupons by expiration date
1974 let dateFormatter = DateFormatter() 1987 let dateFormatter = DateFormatter()
...@@ -2013,10 +2026,17 @@ public final class WarplySDK { ...@@ -2013,10 +2026,17 @@ public final class WarplySDK {
2013 } 2026 }
2014 2027
2015 /// Get coupon sets 2028 /// Get coupon sets
2016 - public func getCouponSets(completion: @escaping ([CouponSetItemModel]?) -> Void) { 2029 + public func getCouponSets(
2030 + language: String? = nil,
2031 + completion: @escaping ([CouponSetItemModel]?) -> Void,
2032 + failureCallback: @escaping (Int) -> Void
2033 + ) {
2034 + // Handle language default inside the method
2035 + let finalLanguage = language ?? self.applicationLocale
2036 +
2017 Task { 2037 Task {
2018 do { 2038 do {
2019 - let endpoint = Endpoint.getCouponSets(active: true, visible: true, uuids: nil) 2039 + let endpoint = Endpoint.getCouponSets(language: finalLanguage, active: true, visible: true, uuids: nil)
2020 let response = try await networkService.requestRaw(endpoint) 2040 let response = try await networkService.requestRaw(endpoint)
2021 2041
2022 var couponSetsArray: [CouponSetItemModel] = [] 2042 var couponSetsArray: [CouponSetItemModel] = []
...@@ -2044,9 +2064,13 @@ public final class WarplySDK { ...@@ -2044,9 +2064,13 @@ public final class WarplySDK {
2044 let dynatraceEvent = LoyaltySDKDynatraceEventModel() 2064 let dynatraceEvent = LoyaltySDKDynatraceEventModel()
2045 dynatraceEvent._eventName = "custom_error_couponset_loyalty" 2065 dynatraceEvent._eventName = "custom_error_couponset_loyalty"
2046 dynatraceEvent._parameters = nil 2066 dynatraceEvent._parameters = nil
2047 - SwiftEventBus.post("dynatrace", sender: dynatraceEvent) 2067 + self.postFrameworkEvent("dynatrace", sender: dynatraceEvent)
2048 2068
2049 - completion(nil) 2069 + if let networkError = error as? NetworkError {
2070 + failureCallback(networkError.code)
2071 + } else {
2072 + failureCallback(-1)
2073 + }
2050 } 2074 }
2051 } 2075 }
2052 } 2076 }
...@@ -2116,17 +2140,20 @@ public final class WarplySDK { ...@@ -2116,17 +2140,20 @@ public final class WarplySDK {
2116 } 2140 }
2117 2141
2118 /// Get coupon sets (async/await variant) 2142 /// Get coupon sets (async/await variant)
2143 + /// - Parameter language: Language code for localized content (optional, defaults to applicationLocale)
2119 /// - Returns: Array of coupon set models 2144 /// - Returns: Array of coupon set models
2120 /// - Throws: WarplyError if the request fails 2145 /// - Throws: WarplyError if the request fails
2121 - public func getCouponSets() async throws -> [CouponSetItemModel] { 2146 + public func getCouponSets(language: String? = nil) async throws -> [CouponSetItemModel] {
2122 return try await withCheckedThrowingContinuation { continuation in 2147 return try await withCheckedThrowingContinuation { continuation in
2123 - getCouponSets { couponSets in 2148 + getCouponSets(language: language, completion: { couponSets in
2124 if let couponSets = couponSets { 2149 if let couponSets = couponSets {
2125 continuation.resume(returning: couponSets) 2150 continuation.resume(returning: couponSets)
2126 } else { 2151 } else {
2127 continuation.resume(throwing: WarplyError.networkError) 2152 continuation.resume(throwing: WarplyError.networkError)
2128 } 2153 }
2129 - } 2154 + }, failureCallback: { errorCode in
2155 + continuation.resume(throwing: WarplyError.unknownError(errorCode))
2156 + })
2130 } 2157 }
2131 } 2158 }
2132 2159
......
...@@ -65,7 +65,7 @@ public enum Endpoint { ...@@ -65,7 +65,7 @@ public enum Endpoint {
65 65
66 // Coupons 66 // Coupons
67 case getCoupons(language: String, couponsetType: String) 67 case getCoupons(language: String, couponsetType: String)
68 - case getCouponSets(active: Bool, visible: Bool, uuids: [String]?) 68 + case getCouponSets(language: String, active: Bool, visible: Bool, uuids: [String]?)
69 case getAvailableCoupons 69 case getAvailableCoupons
70 70
71 // Market & Merchants 71 // Market & Merchants
...@@ -255,12 +255,12 @@ public enum Endpoint { ...@@ -255,12 +255,12 @@ public enum Endpoint {
255 ] 255 ]
256 ] 256 ]
257 257
258 - case .getCouponSets(let active, let visible, let uuids): 258 + case .getCouponSets(let language, let active, let visible, let uuids):
259 var couponParams: [String: Any] = [ 259 var couponParams: [String: Any] = [
260 "action": "retrieve_multilingual", 260 "action": "retrieve_multilingual",
261 "active": active, 261 "active": active,
262 "visible": visible, 262 "visible": visible,
263 - "language": "LANG", // TODO: Make this configurable 263 + "language": language,
264 "exclude": [ 264 "exclude": [
265 [ 265 [
266 "field": "couponset_type", 266 "field": "couponset_type",
......