Manos Chorianopoulos

add sm coupons request and handling

......@@ -7,7 +7,7 @@
<key>Pods-SwiftWarplyFramework.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>
......
......@@ -7,7 +7,7 @@
<key>SwiftWarplyFramework.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>
......
......@@ -81,7 +81,7 @@
- (NSDictionary*)validateCouponWithCoupon:(NSString*)coupon;
- (NSDictionary*)loginCosmoteWithGuid:(NSString*)guid andAppUuid:(NSString*)appUuid andTicket:(NSString*)ticket;
- (void)getCouponsWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCouponsUniversalAsync:(NSString*)language :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCouponsUniversalAsync:(NSString*)language :(NSString*)couponsetType :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getUnifiedCouponsAsync:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void) getAvailableCouponsAsync:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCouponsetsAsync:(NSNumber*) active andVisible:(NSNumber*) visible andUuids:(NSArray*) uuids :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
......
......@@ -1463,10 +1463,10 @@ NSString *VERIFY_URL = @"/partners/cosmote/verify";
}];
}
- (void)getCouponsUniversalAsync:(NSString*)language :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
- (void)getCouponsUniversalAsync:(NSString*)language :(NSString*)couponsetType :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
[[Warply sharedService] getCouponsUniversalWithSuccessBlock:language :^(NSDictionary *response) {
[[Warply sharedService] getCouponsUniversalWithSuccessBlock:language :couponsetType :^(NSDictionary *response) {
if (success) {
success(response);
}
......
......@@ -371,7 +371,7 @@ WL_VERSION_INTERFACE()
- (void)getCouponsWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCouponsUniversalWithSuccessBlock:(NSString*)language :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCouponsUniversalWithSuccessBlock:(NSString*)language :(NSString*)couponsetType :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getUnifiedCouponsWithSuccessBlock:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
......
......@@ -2642,7 +2642,7 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
}];
}
- (void)getCouponsUniversalWithSuccessBlock:(NSString*)language :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
- (void)getCouponsUniversalWithSuccessBlock:(NSString*)language :(NSString*)couponsetType :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
// TODO: If pagination is needed in the future create new signature with the extra argument pagination
/*
......@@ -2665,6 +2665,9 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
[couponDictionary setValue:@"user_coupons" forKey:@"action"];
[couponDictionary setValue:@[@"merchant"] forKey:@"details"];
[couponDictionary setValue:language forKey:@"language"];
if ([couponsetType isEqual: @"supermarket"]) {
[couponDictionary setValue:@[@"supermarket"] forKey:@"couponset_types"];
}
NSMutableDictionary* dataDictionary = [[NSMutableDictionary alloc] init];
[dataDictionary setValue:couponDictionary forKey:@"coupon"];
......
......@@ -17,6 +17,7 @@ public struct GlobalVariables {
static var boxCoupons: Array<swiftApi.ActiveBoxCouponModel> = []
static var couponList: Array<swiftApi.CouponItemModel> = []
static var oldCouponList: Array<swiftApi.CouponItemModel> = []
static var smCouponList: Array<swiftApi.CouponItemModel> = []
static var smOldCouponList: Array<swiftApi.CouponItemModel> = []
static var allOldCouponList: Array<swiftApi.CouponItemModel> = []
static var couponSetList: Array<swiftApi.CouponSetItemModel> = []
......@@ -5403,6 +5404,34 @@ public class swiftApi {
public func getCouponList() -> Array<CouponItemModel> {
return GlobalVariables.couponList
}
public func setSMCouponList(_ coupons: Array<CouponItemModel>) {
// let filteredCoupons = coupons.filter { CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: $0.discount ?? "")) }
var activeCoupons = coupons.filter({ return $0.status == 1 })
// sort activeCoupons by expiration date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
activeCoupons.sort(by: {
let date1 = dateFormatter.date(from: $0.expiration ?? "")
let date2 = dateFormatter.date(from: $1.expiration ?? "")
if ((date1 != nil) && (date2 != nil)) {
return date1!.compare(date2!) == .orderedAscending
} else {
return false
}
})
GlobalVariables.smCouponList = activeCoupons
}
public func getSMCouponList() -> Array<CouponItemModel> {
return GlobalVariables.smCouponList
}
public func setOldCouponList(_ coupons: Array<CouponItemModel>) {
......@@ -8305,6 +8334,37 @@ public class swiftApi {
func getUnifiedCouponsCallback (_ couponsData: Array<swiftApi.UnifiedCouponModel>?) -> Void {
if (couponsData != nil) {
DispatchQueue.main.async {
var usedCouponsArray:Array<CouponItemModel> = []
if let uniCouponsData = couponsData {
for unifiedCoupon in uniCouponsData {
for usedCoupon in unifiedCoupon._coupons {
usedCouponsArray.append(usedCoupon)
}
}
}
swiftApi().getSMCouponsUniversalAsync(language: "el", getSMCouponsCallback, failureCallback: failureCallback)
func getSMCouponsCallback (_ smCouponsData: Array<swiftApi.CouponItemModel>?) -> Void {
if (smCouponsData != nil) {
DispatchQueue.main.async {
var notUsedCouponsArray:Array<CouponItemModel> = []
if let tempSMCouponsData = smCouponsData {
for smCoupon in tempSMCouponsData {
if (!usedCouponsArray.contains { $0.coupon == smCoupon.coupon }) {
notUsedCouponsArray.append(smCoupon)
}
}
}
swiftApi().setSMCouponList(notUsedCouponsArray)
SwiftEventBus.post("sm_coupons_fetched")
}
} else {
}
}
// self.matchOldSMCoupons()
SwiftEventBus.post("unified_coupons_fetched")
successCallback(couponsData)
......@@ -8575,7 +8635,7 @@ public class swiftApi {
public func getCouponsUniversalAsync(language: String, _ successCallback: @escaping (_ couponsData: Array<CouponItemModel>?) -> Void, failureCallback: @escaping (_ errorCode: Int) -> Void) -> Void {
let instanceOfMyApi = MyApi()
instanceOfMyApi.getCouponsUniversalAsync(language, getCouponsUniversalCallback, failureBlock: getCouponsUniversalFailureCallback)
instanceOfMyApi.getCouponsUniversalAsync(language, "", getCouponsUniversalCallback, failureBlock: getCouponsUniversalFailureCallback)
func getCouponsUniversalCallback(_ responseData: [AnyHashable: Any]?) -> Void {
var couponsArray:Array<CouponItemModel> = []
......@@ -8676,6 +8736,102 @@ public class swiftApi {
}
}
public func getSMCouponsUniversalAsync(language: String, _ successCallback: @escaping (_ couponsData: Array<CouponItemModel>?) -> Void, failureCallback: @escaping (_ errorCode: Int) -> Void) -> Void {
let instanceOfMyApi = MyApi()
instanceOfMyApi.getCouponsUniversalAsync(language, "supermarket", getCouponsUniversalCallback, failureBlock: getCouponsUniversalFailureCallback)
func getCouponsUniversalCallback(_ responseData: [AnyHashable: Any]?) -> Void {
var couponsArray:Array<CouponItemModel> = []
if let responseDataDictionary = responseData as? [String: AnyObject] {
if (responseDataDictionary["status"] as? Int == 1) {
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_success_user_sm_coupons_loyalty"
dynatraceEvent._parameters = nil
SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
if let responseDataResult = responseDataDictionary["result"] as? [[String : Any]?] {
for coupon in responseDataResult {
if let couponDictionary = coupon {
let tempCoupon = CouponItemModel(dictionary: couponDictionary)
couponsArray.append(tempCoupon)
}
}
swiftApi().setSMOldCouponList(couponsArray)
// swiftApi().setSMCouponList(couponsArray)
var activeCoupons = couponsArray.filter({ return $0.status == 1 })
// sort activeCoupons by expiration date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
activeCoupons.sort(by: {
let date1 = dateFormatter.date(from: $0.expiration ?? "")
let date2 = dateFormatter.date(from: $1.expiration ?? "")
if ((date1 != nil) && (date2 != nil)) {
return date1!.compare(date2!) == .orderedAscending
} else {
return false
}
})
// returns ALL active SM coupons (used and unused)
successCallback(activeCoupons)
} else {
successCallback(nil)
}
} else {
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_error_user_sm_coupons_loyalty"
dynatraceEvent._parameters = nil
SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
successCallback(nil)
}
} else {
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_error_user_sm_coupons_loyalty"
dynatraceEvent._parameters = nil
SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
successCallback(nil)
}
}
func getCouponsUniversalFailureCallback(_ error: Error?) -> Void {
print("getSMCouponsUniversal error: ")
print(error)
print("====================")
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_error_user_sm_coupons_loyalty"
dynatraceEvent._parameters = nil
SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
// successCallback(nil)
if let error = error as? NSError {
// if (error.code == 401) {
// let sessionEvent = swiftApi.LoyaltySDKSessionExpiredEventModel()
// sessionEvent._sessionExpired = true
// SwiftEventBus.post("sdk_session_expired", sender: sessionEvent)
// }
failureCallback(error.code)
} else {
failureCallback(-1)
}
}
}
public class ActiveBoxCouponModel {
private var value: String
......