Skip to content

Commit 4913c9e

Browse files
[Feature] Merge user profiles on update email call.
Add support to allow merge user profile when updating the user email. Add and update test coverage.
1 parent 1ed0c34 commit 4913c9e

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,25 +925,72 @@ public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Null
925925
* @param newEmail New email
926926
*/
927927
public void updateEmail(final @NonNull String newEmail) {
928-
updateEmail(newEmail, null, null, null);
928+
updateEmail(newEmail, null, null, null, null);
929929
}
930930

931+
/**
932+
* Updates the current user's email.
933+
* Also updates the current email in this IterableAPI instance if the API call was successful.
934+
* @param newEmail New email
935+
* @param merge Merge user profiles in email-based projects if set.
936+
*/
937+
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge) {
938+
updateEmail(newEmail, merge, null, null, null);
939+
}
940+
941+
/**
942+
* Updates the current user's email.
943+
* Also updates the current email in this IterableAPI instance if the API call was successful.
944+
* @param newEmail New email
945+
* @param authToken Authentication token
946+
*/
931947
public void updateEmail(final @NonNull String newEmail, final @NonNull String authToken) {
932-
updateEmail(newEmail, authToken, null, null);
948+
updateEmail(newEmail, null, authToken, null, null);
933949
}
934950

951+
/**
952+
* Updates the current user's email.
953+
* Also updates the current email in this IterableAPI instance if the API call was successful.
954+
* @param newEmail New email
955+
* @param merge Merge user profiles in email-based projects if set.
956+
* @param authToken Authentication token
957+
*/
958+
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge, final @NonNull String authToken) {
959+
updateEmail(newEmail, merge, authToken, null, null);
960+
}
961+
962+
/**
963+
* Updates the current user's email.
964+
* Also updates the current email in this IterableAPI instance if the API call was successful.
965+
* @param newEmail New email
966+
* @param successHandler Success handler. Called when the server returns a success code.
967+
* @param failureHandler Failure handler. Called when the server call failed.
968+
*/
935969
public void updateEmail(final @NonNull String newEmail, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
936-
updateEmail(newEmail, null, successHandler, failureHandler);
970+
updateEmail(newEmail, null, null, successHandler, failureHandler);
971+
}
972+
973+
/**
974+
* Updates the current user's email.
975+
* Also updates the current email in this IterableAPI instance if the API call was successful.
976+
* @param newEmail New email
977+
* @param merge Merge user profiles in email-based projects if set.
978+
* @param successHandler Success handler. Called when the server returns a success code.
979+
* @param failureHandler Failure handler. Called when the server call failed.
980+
*/
981+
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
982+
updateEmail(newEmail, merge, null, successHandler, failureHandler);
937983
}
938984

939985
/**
940986
* Updates the current user's email.
941987
* Also updates the current email and authToken in this IterableAPI instance if the API call was successful.
942988
* @param newEmail New email
989+
* @param merge Optional. Merge user profiles in email-based projects if set.
943990
* @param successHandler Success handler. Called when the server returns a success code.
944991
* @param failureHandler Failure handler. Called when the server call failed.
945992
*/
946-
public void updateEmail(final @NonNull String newEmail, final @Nullable String authToken, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
993+
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge, final @Nullable String authToken, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
947994
if (!checkSDKInitialization()) {
948995
IterableLogger.e(TAG, "The Iterable SDK must be initialized with email or userId before " +
949996
"calling updateEmail");
@@ -955,7 +1002,7 @@ public void updateEmail(final @NonNull String newEmail, final @Nullable String a
9551002
return;
9561003
}
9571004

958-
apiClient.updateEmail(newEmail, new IterableHelper.SuccessHandler() {
1005+
apiClient.updateEmail(newEmail, merge, new IterableHelper.SuccessHandler() {
9591006
@Override
9601007
public void onSuccess(@NonNull JSONObject data) {
9611008
if (_email != null) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Null
142142
}
143143
}
144144

145-
public void updateEmail(final @NonNull String newEmail, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
145+
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
146146
JSONObject requestJSON = new JSONObject();
147147

148148
try {
@@ -151,8 +151,13 @@ public void updateEmail(final @NonNull String newEmail, final @Nullable Iterable
151151
} else {
152152
requestJSON.put(IterableConstants.KEY_CURRENT_USERID, authProvider.getUserId());
153153
}
154+
154155
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
155156

157+
if (merge != null) {
158+
requestJSON.put(IterableConstants.KEY_MERGE, merge);
159+
}
160+
156161
sendPostRequest(IterableConstants.ENDPOINT_UPDATE_EMAIL, requestJSON, successHandler, failureHandler);
157162
} catch (JSONException e) {
158163
e.printStackTrace();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class IterableConstants {
3434
public static final String KEY_EVENT_NAME = "eventName";
3535
public static final String KEY_ITEMS = "items";
3636
public static final String KEY_NEW_EMAIL = "newEmail";
37+
public static final String KEY_MERGE = "merge";
3738
public static final String KEY_PACKAGE_NAME = "packageName";
3839
public static final String KEY_PLATFORM = "platform";
3940
public static final String KEY_PREFER_USER_ID = "preferUserId";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public void testAuthTokenPresentInRequest() throws Exception {
371371
// assertEquals(HEADER_SDK_AUTH_FORMAT + expiredJWT, getMessagesAuthenticatedRequest2.getHeader("Authorization"));
372372

373373
doReturn(validJWT).when(authHandler).onAuthTokenRequested();
374-
IterableApi.getInstance().updateEmail("[email protected]", null, null);
374+
IterableApi.getInstance().updateEmail("[email protected]", null, null, null);
375375
shadowOf(getMainLooper()).runToEndOfTasks();
376376
shadowOf(getMainLooper()).idle();
377377
RecordedRequest updateEmailRequest = server.takeRequest(1, TimeUnit.SECONDS);

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void testUpdateEmailPersistence() throws Exception {
155155

156156
IterableApi.getInstance().updateEmail(newEmail);
157157
shadowOf(getMainLooper()).idle();
158-
verify(mockApiClient).updateEmail(eq(newEmail), nullable(IterableHelper.SuccessHandler.class), nullable(IterableHelper.FailureHandler.class));
158+
verify(mockApiClient).updateEmail(eq(newEmail), nullable(Boolean.class), nullable(IterableHelper.SuccessHandler.class), nullable(IterableHelper.FailureHandler.class));
159159
server.takeRequest(1, TimeUnit.SECONDS);
160160
assertEquals("[email protected]", IterableApi.getInstance().getEmail());
161161

@@ -213,6 +213,23 @@ public void testUpdateEmailWithOldEmail() throws Exception {
213213
JSONObject requestJson = new JSONObject(updateEmailRequest.getBody().readUtf8());
214214
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_CURRENT_EMAIL));
215215
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
216+
assertFalse(requestJson.has(IterableConstants.KEY_MERGE));
217+
}
218+
219+
@Test
220+
public void testUpdateEmailWithMergeUserProfiles() throws Exception {
221+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
222+
IterableApi.initialize(getContext(), "apiKey");
223+
IterableApi.getInstance().setEmail("[email protected]");
224+
IterableApi.getInstance().updateEmail("[email protected]", true);
225+
226+
RecordedRequest updateEmailRequest = server.takeRequest(1, TimeUnit.SECONDS);
227+
assertNotNull(updateEmailRequest);
228+
assertEquals("/" + IterableConstants.ENDPOINT_UPDATE_EMAIL, updateEmailRequest.getPath());
229+
JSONObject requestJson = new JSONObject(updateEmailRequest.getBody().readUtf8());
230+
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_CURRENT_EMAIL));
231+
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
232+
assertTrue(requestJson.getBoolean(IterableConstants.KEY_MERGE));
216233
}
217234

218235
@Test
@@ -230,6 +247,7 @@ public void testUpdateEmailWithUserId() throws Exception {
230247
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
231248
assertNull(IterableApi.getInstance().getEmail());
232249
assertEquals("testUserId", IterableApi.getInstance().getUserId());
250+
assertFalse(requestJson.has(IterableConstants.KEY_MERGE));
233251
}
234252

235253
@Ignore

0 commit comments

Comments
 (0)