Manos Chorianopoulos

fix redeemed coupons and unified coupons sorting

......@@ -605,6 +605,7 @@ public class swiftApi {
public let barcode: String?
public let status: Int?
public let redeemed: String?
public let redeemed_date: Date?
public var couponset_data: CouponSetItemModel?
// Market Coupon
......@@ -673,15 +674,18 @@ public class swiftApi {
let dateFormatter3 = DateFormatter()
dateFormatter3.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS"
if let date = dateFormatter3.date(from: redeemedString ?? "") {
self.redeemed_date = date
dateFormatter3.dateFormat = "dd/MM/yyyy"
let resultString = dateFormatter3.string(from: date)
self.redeemed = resultString
} else {
self.redeemed = ""
self.redeemed_date = Date()
}
} else {
self.redeemed = ""
self.redeemed_date = Date()
}
}
......@@ -4410,7 +4414,26 @@ public class swiftApi {
public func setOldCouponList(_ coupons: Array<CouponItemModel>) {
// let filteredCoupons = coupons.filter { CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: $0.discount ?? "")) }
let oldCoupons = coupons.filter({ return $0.status == 0 })
var oldCoupons = coupons.filter({ return $0.status == 0 })
// sort oldCoupons by redeemed date
// let dateFormatter = DateFormatter()
// dateFormatter.dateFormat = "dd/MM/yyyy"
oldCoupons.sort(by: {
// let date1 = dateFormatter.date(from: $0.redeemed ?? "")
// let date2 = dateFormatter.date(from: $1.redeemed ?? "")
let date1 = $0.redeemed_date
let date2 = $1.redeemed_date
if ((date1 != nil) && (date2 != nil)) {
return date1!.compare(date2!) == .orderedDescending
} else {
return false
}
})
GlobalVariables.oldCouponList = oldCoupons
}
......@@ -6940,8 +6963,21 @@ public class swiftApi {
// dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS"
let sortedUnifiedCouponsArray = unifiedCouponsArray.sorted(by: { dateFormatter.date(from:$0._created)?.compare(dateFormatter.date(from:$1._created)!) == .orderedDescending })
// let sortedOldUnifiedCouponsArray = oldUnifiedCouponsArray.sorted(by: { $0.redeemed_date?.compare($1.redeemed_date!) == .orderedDescending })
swiftApi().setUnifiedCouponList(sortedUnifiedCouponsArray)
oldUnifiedCouponsArray.sort(by: {
let date1 = $0.redeemed_date
let date2 = $1.redeemed_date
if ((date1 != nil) && (date2 != nil)) {
return date1!.compare(date2!) == .orderedDescending
} else {
return false
}
})
swiftApi().setOldUnifiedCouponList(oldUnifiedCouponsArray)
successCallback(sortedUnifiedCouponsArray)
}
......