Showing
1 changed file
with
127 additions
and
0 deletions
... | @@ -619,6 +619,35 @@ public class swiftApi { | ... | @@ -619,6 +619,35 @@ public class swiftApi { |
619 | } | 619 | } |
620 | 620 | ||
621 | 621 | ||
622 | + public class RedeemedSMHistoryModel: Codable { | ||
623 | + private var totalRedeemedValue: Float | ||
624 | + private var redeemedCouponList: Array<CouponItemModel> | ||
625 | + | ||
626 | + init() { | ||
627 | + self.totalRedeemedValue = 0.0 | ||
628 | + self.redeemedCouponList = [] | ||
629 | + } | ||
630 | + | ||
631 | + public var _totalRedeemedValue: Float { | ||
632 | + get { // getter | ||
633 | + return self.totalRedeemedValue | ||
634 | + } | ||
635 | + set(newValue) { //setter | ||
636 | + self.totalRedeemedValue = newValue | ||
637 | + } | ||
638 | + } | ||
639 | + | ||
640 | + public var _redeemedCouponList: Array<CouponItemModel> { | ||
641 | + get { // getter | ||
642 | + return self.redeemedCouponList | ||
643 | + } | ||
644 | + set(newValue) { //setter | ||
645 | + self.redeemedCouponList = newValue | ||
646 | + } | ||
647 | + } | ||
648 | + } | ||
649 | + | ||
650 | + | ||
622 | public func getCouponsAsync(_ getCouponsCallback: @escaping (_ couponsData: Array<CouponItemModel>?) -> Void, failureCallback: @escaping (_ errorCode: Int) -> Void) -> Void { | 651 | public func getCouponsAsync(_ getCouponsCallback: @escaping (_ couponsData: Array<CouponItemModel>?) -> Void, failureCallback: @escaping (_ errorCode: Int) -> Void) -> Void { |
623 | let instanceOfMyApi = MyApi() | 652 | let instanceOfMyApi = MyApi() |
624 | 653 | ||
... | @@ -4280,4 +4309,102 @@ public class swiftApi { | ... | @@ -4280,4 +4309,102 @@ public class swiftApi { |
4280 | } | 4309 | } |
4281 | } | 4310 | } |
4282 | 4311 | ||
4312 | + | ||
4313 | + public func getRedeemedSMHistoryAsync(language: String, _ successCallback: @escaping (_ data: RedeemedSMHistoryModel?) -> Void, failureCallback: @escaping (_ errorCode: Int) -> Void) -> Void { | ||
4314 | + | ||
4315 | + let instanceOfMyApi = MyApi() | ||
4316 | + instanceOfMyApi.getCouponsUniversalAsync(language, "supermarket", getCouponsUniversalCallback, failureBlock: getCouponsUniversalFailureCallback) | ||
4317 | + | ||
4318 | + func getCouponsUniversalCallback(_ responseData: [AnyHashable: Any]?) -> Void { | ||
4319 | + var couponsArray:Array<CouponItemModel> = [] | ||
4320 | + | ||
4321 | + if let responseDataDictionary = responseData as? [String: AnyObject] { | ||
4322 | + | ||
4323 | + if (responseDataDictionary["status"] as? Int == 1) { | ||
4324 | + let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel() | ||
4325 | + dynatraceEvent._eventName = "custom_success_user_sm_coupons_loyalty" | ||
4326 | + dynatraceEvent._parameters = nil | ||
4327 | + SwiftEventBus.post("dynatrace", sender: dynatraceEvent) | ||
4328 | + | ||
4329 | + if let responseDataResult = responseDataDictionary["result"] as? [[String : Any]?] { | ||
4330 | + for coupon in responseDataResult { | ||
4331 | + if let couponDictionary = coupon { | ||
4332 | + let tempCoupon = CouponItemModel(dictionary: couponDictionary) | ||
4333 | + couponsArray.append(tempCoupon) | ||
4334 | + } | ||
4335 | + } | ||
4336 | + | ||
4337 | + var oldCoupons = couponsArray.filter({ return $0.status == 0 }) | ||
4338 | + | ||
4339 | + // sort oldCoupons by redeemed date | ||
4340 | + oldCoupons.sort(by: { | ||
4341 | + let date1 = $0.redeemed_date | ||
4342 | + let date2 = $1.redeemed_date | ||
4343 | + | ||
4344 | + if ((date1 != nil) && (date2 != nil)) { | ||
4345 | + return date1!.compare(date2!) == .orderedDescending | ||
4346 | + } else { | ||
4347 | + return false | ||
4348 | + } | ||
4349 | + | ||
4350 | + }) | ||
4351 | + | ||
4352 | + let totalCouponDiscount = oldCoupons.reduce(0) { $0 + ($1.couponset_data?.final_price ?? 0.0) } | ||
4353 | + | ||
4354 | + // RedeemedSMHistoryModel | ||
4355 | + let redeemedSMHistory = RedeemedSMHistoryModel() | ||
4356 | + redeemedSMHistory._totalRedeemedValue = totalCouponDiscount | ||
4357 | + redeemedSMHistory._redeemedCouponList = oldCoupons | ||
4358 | + | ||
4359 | + successCallback(redeemedSMHistory) | ||
4360 | + | ||
4361 | + } else { | ||
4362 | + successCallback(nil) | ||
4363 | + } | ||
4364 | + | ||
4365 | + } else { | ||
4366 | + let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel() | ||
4367 | + dynatraceEvent._eventName = "custom_error_user_sm_coupons_loyalty" | ||
4368 | + dynatraceEvent._parameters = nil | ||
4369 | + SwiftEventBus.post("dynatrace", sender: dynatraceEvent) | ||
4370 | + | ||
4371 | + successCallback(nil) | ||
4372 | + } | ||
4373 | + | ||
4374 | + } else { | ||
4375 | + let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel() | ||
4376 | + dynatraceEvent._eventName = "custom_error_user_sm_coupons_loyalty" | ||
4377 | + dynatraceEvent._parameters = nil | ||
4378 | + SwiftEventBus.post("dynatrace", sender: dynatraceEvent) | ||
4379 | + | ||
4380 | + successCallback(nil) | ||
4381 | + } | ||
4382 | + | ||
4383 | + } | ||
4384 | + | ||
4385 | + func getCouponsUniversalFailureCallback(_ error: Error?) -> Void { | ||
4386 | + print("getRedeemedSMHistoryAsync error: ") | ||
4387 | + print(error) | ||
4388 | + print("====================") | ||
4389 | + | ||
4390 | + let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel() | ||
4391 | + dynatraceEvent._eventName = "custom_error_user_sm_coupons_loyalty" | ||
4392 | + dynatraceEvent._parameters = nil | ||
4393 | + SwiftEventBus.post("dynatrace", sender: dynatraceEvent) | ||
4394 | + | ||
4395 | + // successCallback(nil) | ||
4396 | + if let error = error as? NSError { | ||
4397 | +// if (error.code == 401) { | ||
4398 | +// let sessionEvent = swiftApi.LoyaltySDKSessionExpiredEventModel() | ||
4399 | +// sessionEvent._sessionExpired = true | ||
4400 | +// SwiftEventBus.post("sdk_session_expired", sender: sessionEvent) | ||
4401 | +// } | ||
4402 | + | ||
4403 | + failureCallback(error.code) | ||
4404 | + } else { | ||
4405 | + failureCallback(-1) | ||
4406 | + } | ||
4407 | + } | ||
4408 | + } | ||
4409 | + | ||
4283 | } | 4410 | } | ... | ... |
-
Please register or login to post a comment