Skip to content

Commit 9bfec2e

Browse files
authored
Merge pull request #86 from Iterable/feature/MOB-95-support-userId-in-updateEmail
[MOB-95] Add support for currentUserId in updateEmail
2 parents 46ab077 + 3f548e1 commit 9bfec2e

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -731,30 +731,38 @@ public void updateEmail(final String newEmail) {
731731
* @param failureHandler Failure handler. Called when the server call failed.
732732
*/
733733
public void updateEmail(final String newEmail, final IterableHelper.SuccessHandler successHandler, IterableHelper.FailureHandler failureHandler) {
734-
if (_email != null) {
735-
JSONObject requestJSON = new JSONObject();
734+
if (!checkSDKInitialization()) {
735+
IterableLogger.e(TAG, "The Iterable SDK must be initialized with email or userId before "+
736+
"calling updateEmail");
737+
if (failureHandler != null) {
738+
failureHandler.onFailure("The Iterable SDK must be initialized with email or "+
739+
"userId before calling updateEmail", null);
740+
}
741+
return;
742+
}
743+
JSONObject requestJSON = new JSONObject();
736744

737-
try {
745+
try {
746+
if (_email != null) {
738747
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
739-
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
740-
741-
sendPostRequest(IterableConstants.ENDPOINT_UPDATE_EMAIL, requestJSON, new IterableHelper.SuccessHandler() {
742-
@Override
743-
public void onSuccess(JSONObject data) {
744-
_email = newEmail;
745-
storeEmailAndUserId();
746-
if (successHandler != null) {
747-
successHandler.onSuccess(data);
748-
}
749-
}
750-
}, failureHandler);
751-
}
752-
catch (JSONException e) {
753-
e.printStackTrace();
748+
} else {
749+
requestJSON.put(IterableConstants.KEY_CURRENT_USERID, _userId);
754750
}
755-
} else {
756-
IterableLogger.w(TAG, "updateEmail should not be called with a userId. " +
757-
"Make sure you call setEmail(String) before trying to update it");
751+
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
752+
753+
sendPostRequest(IterableConstants.ENDPOINT_UPDATE_EMAIL, requestJSON, new IterableHelper.SuccessHandler() {
754+
@Override
755+
public void onSuccess(JSONObject data) {
756+
_email = newEmail;
757+
storeEmailAndUserId();
758+
if (successHandler != null) {
759+
successHandler.onSuccess(data);
760+
}
761+
}
762+
}, failureHandler);
763+
}
764+
catch (JSONException e) {
765+
e.printStackTrace();
758766
}
759767
}
760768

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class IterableConstants {
1919
public static final String KEY_APPLICATION_NAME = "applicationName";
2020
public static final String KEY_CAMPAIGN_ID = "campaignId";
2121
public static final String KEY_CURRENT_EMAIL = "currentEmail";
22+
public static final String KEY_CURRENT_USERID = "currentUserId";
2223
public static final String KEY_DATA_FIELDS = "dataFields";
2324
public static final String KEY_DEVICE = "device";
2425
public static final String KEY_EMAIL = "email";

iterableapi/src/test/java/com/iterable/iterableapi/IterableApiTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ public void testUpdateEmailPersistence() throws Exception {
140140
assertEquals("[email protected]", IterableApi.getInstance().getEmail());
141141
}
142142

143+
@Test
144+
public void testUpdateEmailWithOldEmail() throws Exception {
145+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
146+
IterableApi.initialize(RuntimeEnvironment.application, "apiKey");
147+
IterableApi.getInstance().setEmail("[email protected]");
148+
IterableApi.getInstance().updateEmail("[email protected]");
149+
150+
RecordedRequest updateEmailRequest = server.takeRequest(1, TimeUnit.SECONDS);
151+
assertNotNull(updateEmailRequest);
152+
assertEquals("/" + IterableConstants.ENDPOINT_UPDATE_EMAIL, updateEmailRequest.getPath());
153+
JSONObject requestJson = new JSONObject(updateEmailRequest.getBody().readUtf8());
154+
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_CURRENT_EMAIL));
155+
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
156+
}
157+
158+
@Test
159+
public void testUpdateEmailWithUserId() throws Exception {
160+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
161+
IterableApi.initialize(RuntimeEnvironment.application, "apiKey");
162+
IterableApi.getInstance().setUserId("testUserId");
163+
IterableApi.getInstance().updateEmail("[email protected]");
164+
165+
RecordedRequest updateEmailRequest = server.takeRequest(1, TimeUnit.SECONDS);
166+
assertNotNull(updateEmailRequest);
167+
assertEquals("/" + IterableConstants.ENDPOINT_UPDATE_EMAIL, updateEmailRequest.getPath());
168+
JSONObject requestJson = new JSONObject(updateEmailRequest.getBody().readUtf8());
169+
assertEquals("testUserId", requestJson.getString(IterableConstants.KEY_CURRENT_USERID));
170+
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
171+
}
172+
143173
@Test
144174
public void testHandleUniversalLinkRewrite() throws Exception {
145175
IterableUrlHandler urlHandlerMock = mock(IterableUrlHandler.class);

0 commit comments

Comments
 (0)