Skip to content

Commit 8f1a85c

Browse files
Call createUserForUserId if userId is set for registerToken.
1 parent 59c3f30 commit 8f1a85c

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Tests/swift-sdk-swift-tests/IterableAutoRegistrationTests.swift

+18-11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class IterableAutoRegistrationTests: XCTestCase {
9090
}
9191

9292
func testDoNotCallRegisterForRemoteNotificationsWhenNotificationsAreDisabled() {
93+
let expectation0 = expectation(description: "Call create user")
9394
let expectation1 = expectation(description: "Call registerToken API endpoint")
9495
let expectation2 = expectation(description: "Disable API endpoint called")
9596
let expectation3 = expectation(description: "do not call registerForRemoteNotifications")
@@ -107,23 +108,29 @@ class IterableAutoRegistrationTests: XCTestCase {
107108
IterableAPI.userId = "userId1"
108109
let token = "zeeToken".data(using: .utf8)!
109110
networkSession.callback = {(_, _, _) in
110-
// first call back will be called on register
111-
expectation1.fulfill()
112-
TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: .ITBL_ENDPOINT_API, path: .ITBL_PATH_REGISTER_DEVICE_TOKEN, queryParams: [(name: AnyHashable.ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)])
111+
expectation0.fulfill()
112+
TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: .ITBL_ENDPOINT_API, path: .ITBL_PATH_CREATE_USER, queryParams: [(name: AnyHashable.ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)])
113+
let body = networkSession.getRequestBody() as! [String : Any]
114+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
113115
networkSession.callback = {(_, _, _) in
114-
// second call back is for disable when we set new user id
115-
TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: .ITBL_ENDPOINT_API, path: .ITBL_PATH_DISABLE_DEVICE, queryParams: [(name: AnyHashable.ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)])
116-
let body = networkSession.getRequestBody() as! [String : Any]
117-
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body)
118-
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
119-
expectation2.fulfill()
116+
// first call back will be called on register
117+
expectation1.fulfill()
118+
TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: .ITBL_ENDPOINT_API, path: .ITBL_PATH_REGISTER_DEVICE_TOKEN, queryParams: [(name: AnyHashable.ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)])
119+
networkSession.callback = {(_, _, _) in
120+
// second call back is for disable when we set new user id
121+
TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: .ITBL_ENDPOINT_API, path: .ITBL_PATH_DISABLE_DEVICE, queryParams: [(name: AnyHashable.ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests.apiKey)])
122+
let body = networkSession.getRequestBody() as! [String : Any]
123+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body)
124+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
125+
expectation2.fulfill()
126+
}
127+
IterableAPI.userId = "userId2"
120128
}
121-
IterableAPI.userId = "userId2"
122129
}
123130
IterableAPI.register(token: token)
124131

125132
// only wait for small time, supposed to error out
126-
wait(for: [expectation1, expectation2, expectation3], timeout: 1.0)
133+
wait(for: [expectation0, expectation1, expectation2, expectation3], timeout: 1.0)
127134
}
128135

129136
func testDoNotCallDisableOrEnableWhenAutoPushIsOff() {

swift-sdk/Internal/IterableAPIInternal.swift

+18-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
5353
_userId = newValue
5454
_email = nil
5555
storeEmailAndUserId()
56-
createUserForUserId(onSuccess: IterableAPIInternal.defaultOnSucess(identifier: "createUserForUserId"), onFailure: IterableAPIInternal.defaultOnFailure(identifier: "createUserForUserId"))
5756

5857
enableDeviceForCurrentUser()
5958
}
@@ -111,7 +110,22 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
111110
return
112111
}
113112

114-
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
113+
if let userId = userId {
114+
createUser(
115+
withUserId: userId,
116+
onSuccess: { (_) in
117+
self.register(token: token, appName: appName, pushServicePlatform: self.config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
118+
},
119+
onFailure: {(errorMessage, _) in
120+
if let errorMessage = errorMessage {
121+
ITBError("Could not create user: \(errorMessage)")
122+
} else {
123+
ITBError()
124+
}
125+
})
126+
} else {
127+
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
128+
}
115129
}
116130

117131
private func register(token: Data, appName: String, pushServicePlatform: PushServicePlatform, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
@@ -221,14 +235,8 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
221235
onFailure: onFailure)
222236
}
223237
}
224-
225-
func createUserForUserId(onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
226-
guard userId != nil else {
227-
ITBError("UserId is nil")
228-
onFailure?("UserId is nil", nil)
229-
return
230-
}
231-
238+
239+
func createUser(withUserId userId: String, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
232240
var args = [AnyHashable : Any]()
233241
args[.ITBL_KEY_USER_ID] = userId
234242

0 commit comments

Comments
 (0)