Manos Chorianopoulos

fix redeemed coupons and unified coupons sorting

...@@ -605,6 +605,7 @@ public class swiftApi { ...@@ -605,6 +605,7 @@ public class swiftApi {
605 public let barcode: String? 605 public let barcode: String?
606 public let status: Int? 606 public let status: Int?
607 public let redeemed: String? 607 public let redeemed: String?
608 + public let redeemed_date: Date?
608 public var couponset_data: CouponSetItemModel? 609 public var couponset_data: CouponSetItemModel?
609 610
610 // Market Coupon 611 // Market Coupon
...@@ -673,15 +674,18 @@ public class swiftApi { ...@@ -673,15 +674,18 @@ public class swiftApi {
673 let dateFormatter3 = DateFormatter() 674 let dateFormatter3 = DateFormatter()
674 dateFormatter3.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS" 675 dateFormatter3.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS"
675 if let date = dateFormatter3.date(from: redeemedString ?? "") { 676 if let date = dateFormatter3.date(from: redeemedString ?? "") {
677 + self.redeemed_date = date
676 dateFormatter3.dateFormat = "dd/MM/yyyy" 678 dateFormatter3.dateFormat = "dd/MM/yyyy"
677 let resultString = dateFormatter3.string(from: date) 679 let resultString = dateFormatter3.string(from: date)
678 self.redeemed = resultString 680 self.redeemed = resultString
679 } else { 681 } else {
680 self.redeemed = "" 682 self.redeemed = ""
683 + self.redeemed_date = Date()
681 } 684 }
682 685
683 } else { 686 } else {
684 self.redeemed = "" 687 self.redeemed = ""
688 + self.redeemed_date = Date()
685 } 689 }
686 690
687 } 691 }
...@@ -4410,7 +4414,26 @@ public class swiftApi { ...@@ -4410,7 +4414,26 @@ public class swiftApi {
4410 4414
4411 public func setOldCouponList(_ coupons: Array<CouponItemModel>) { 4415 public func setOldCouponList(_ coupons: Array<CouponItemModel>) {
4412 // let filteredCoupons = coupons.filter { CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: $0.discount ?? "")) } 4416 // let filteredCoupons = coupons.filter { CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: $0.discount ?? "")) }
4413 - let oldCoupons = coupons.filter({ return $0.status == 0 }) 4417 + var oldCoupons = coupons.filter({ return $0.status == 0 })
4418 +
4419 + // sort oldCoupons by redeemed date
4420 +// let dateFormatter = DateFormatter()
4421 +// dateFormatter.dateFormat = "dd/MM/yyyy"
4422 +
4423 + oldCoupons.sort(by: {
4424 +// let date1 = dateFormatter.date(from: $0.redeemed ?? "")
4425 +// let date2 = dateFormatter.date(from: $1.redeemed ?? "")
4426 +
4427 + let date1 = $0.redeemed_date
4428 + let date2 = $1.redeemed_date
4429 +
4430 + if ((date1 != nil) && (date2 != nil)) {
4431 + return date1!.compare(date2!) == .orderedDescending
4432 + } else {
4433 + return false
4434 + }
4435 +
4436 + })
4414 4437
4415 GlobalVariables.oldCouponList = oldCoupons 4438 GlobalVariables.oldCouponList = oldCoupons
4416 } 4439 }
...@@ -6940,8 +6963,21 @@ public class swiftApi { ...@@ -6940,8 +6963,21 @@ public class swiftApi {
6940 // dateFormatter.dateFormat = "dd/MM/yyyy" 6963 // dateFormatter.dateFormat = "dd/MM/yyyy"
6941 dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS" 6964 dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS"
6942 let sortedUnifiedCouponsArray = unifiedCouponsArray.sorted(by: { dateFormatter.date(from:$0._created)?.compare(dateFormatter.date(from:$1._created)!) == .orderedDescending }) 6965 let sortedUnifiedCouponsArray = unifiedCouponsArray.sorted(by: { dateFormatter.date(from:$0._created)?.compare(dateFormatter.date(from:$1._created)!) == .orderedDescending })
6966 +// let sortedOldUnifiedCouponsArray = oldUnifiedCouponsArray.sorted(by: { $0.redeemed_date?.compare($1.redeemed_date!) == .orderedDescending })
6943 6967
6944 swiftApi().setUnifiedCouponList(sortedUnifiedCouponsArray) 6968 swiftApi().setUnifiedCouponList(sortedUnifiedCouponsArray)
6969 +
6970 + oldUnifiedCouponsArray.sort(by: {
6971 + let date1 = $0.redeemed_date
6972 + let date2 = $1.redeemed_date
6973 +
6974 + if ((date1 != nil) && (date2 != nil)) {
6975 + return date1!.compare(date2!) == .orderedDescending
6976 + } else {
6977 + return false
6978 + }
6979 +
6980 + })
6945 swiftApi().setOldUnifiedCouponList(oldUnifiedCouponsArray) 6981 swiftApi().setOldUnifiedCouponList(oldUnifiedCouponsArray)
6946 successCallback(sortedUnifiedCouponsArray) 6982 successCallback(sortedUnifiedCouponsArray)
6947 } 6983 }
......