Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Allow merge user profiles on update email call. #641

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -925,25 +925,72 @@ public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Null
* @param newEmail New email
*/
public void updateEmail(final @NonNull String newEmail) {
updateEmail(newEmail, null, null, null);
updateEmail(newEmail, null, null, null, null);
}

/**
* Updates the current user's email.
* Also updates the current email in this IterableAPI instance if the API call was successful.
* @param newEmail New email
* @param merge Merge user profiles in email-based projects if set.
*/
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge) {
updateEmail(newEmail, merge, null, null, null);
}

/**
* Updates the current user's email.
* Also updates the current email in this IterableAPI instance if the API call was successful.
* @param newEmail New email
* @param authToken Authentication token
*/
public void updateEmail(final @NonNull String newEmail, final @NonNull String authToken) {
updateEmail(newEmail, authToken, null, null);
updateEmail(newEmail, null, authToken, null, null);
}

/**
* Updates the current user's email.
* Also updates the current email in this IterableAPI instance if the API call was successful.
* @param newEmail New email
* @param merge Merge user profiles in email-based projects if set.
* @param authToken Authentication token
*/
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge, final @NonNull String authToken) {
updateEmail(newEmail, merge, authToken, null, null);
}

/**
* Updates the current user's email.
* Also updates the current email in this IterableAPI instance if the API call was successful.
* @param newEmail New email
* @param successHandler Success handler. Called when the server returns a success code.
* @param failureHandler Failure handler. Called when the server call failed.
*/
public void updateEmail(final @NonNull String newEmail, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
updateEmail(newEmail, null, successHandler, failureHandler);
updateEmail(newEmail, null, null, successHandler, failureHandler);
}

/**
* Updates the current user's email.
* Also updates the current email in this IterableAPI instance if the API call was successful.
* @param newEmail New email
* @param merge Merge user profiles in email-based projects if set.
* @param successHandler Success handler. Called when the server returns a success code.
* @param failureHandler Failure handler. Called when the server call failed.
*/
public void updateEmail(final @NonNull String newEmail, final @Nullable Boolean merge, final @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
updateEmail(newEmail, merge, null, successHandler, failureHandler);
}

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

apiClient.updateEmail(newEmail, new IterableHelper.SuccessHandler() {
apiClient.updateEmail(newEmail, merge, new IterableHelper.SuccessHandler() {
@Override
public void onSuccess(@NonNull JSONObject data) {
if (_email != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Null
}
}

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

try {
Expand All @@ -151,8 +151,13 @@ public void updateEmail(final @NonNull String newEmail, final @Nullable Iterable
} else {
requestJSON.put(IterableConstants.KEY_CURRENT_USERID, authProvider.getUserId());
}

requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);

if (merge != null) {
requestJSON.put(IterableConstants.KEY_MERGE, merge);
}

sendPostRequest(IterableConstants.ENDPOINT_UPDATE_EMAIL, requestJSON, successHandler, failureHandler);
} catch (JSONException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class IterableConstants {
public static final String KEY_EVENT_NAME = "eventName";
public static final String KEY_ITEMS = "items";
public static final String KEY_NEW_EMAIL = "newEmail";
public static final String KEY_MERGE = "merge";
public static final String KEY_PACKAGE_NAME = "packageName";
public static final String KEY_PLATFORM = "platform";
public static final String KEY_PREFER_USER_ID = "preferUserId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public void testAuthTokenPresentInRequest() throws Exception {
// assertEquals(HEADER_SDK_AUTH_FORMAT + expiredJWT, getMessagesAuthenticatedRequest2.getHeader("Authorization"));

doReturn(validJWT).when(authHandler).onAuthTokenRequested();
IterableApi.getInstance().updateEmail("[email protected]", null, null);
IterableApi.getInstance().updateEmail("[email protected]", null, null, null);
shadowOf(getMainLooper()).runToEndOfTasks();
shadowOf(getMainLooper()).idle();
RecordedRequest updateEmailRequest = server.takeRequest(1, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void testUpdateEmailPersistence() throws Exception {

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

Expand Down Expand Up @@ -213,6 +213,23 @@ public void testUpdateEmailWithOldEmail() throws Exception {
JSONObject requestJson = new JSONObject(updateEmailRequest.getBody().readUtf8());
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_CURRENT_EMAIL));
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
assertFalse(requestJson.has(IterableConstants.KEY_MERGE));
}

@Test
public void testUpdateEmailWithMergeUserProfiles() throws Exception {
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
IterableApi.initialize(getContext(), "apiKey");
IterableApi.getInstance().setEmail("[email protected]");
IterableApi.getInstance().updateEmail("[email protected]", true);

RecordedRequest updateEmailRequest = server.takeRequest(1, TimeUnit.SECONDS);
assertNotNull(updateEmailRequest);
assertEquals("/" + IterableConstants.ENDPOINT_UPDATE_EMAIL, updateEmailRequest.getPath());
JSONObject requestJson = new JSONObject(updateEmailRequest.getBody().readUtf8());
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_CURRENT_EMAIL));
assertEquals("[email protected]", requestJson.getString(IterableConstants.KEY_NEW_EMAIL));
assertTrue(requestJson.getBoolean(IterableConstants.KEY_MERGE));
}

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

@Ignore
Expand Down