Skip to content

Commit

Permalink
MC: remove ADDRESS support (too finicky) + added error details in bat…
Browse files Browse the repository at this point in the history
…ch mode
  • Loading branch information
brmeyer committed Jul 28, 2023
1 parent a9d5df3 commit ff86d38
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class MailchimpClient {
public static final String FIRST_NAME = "FNAME";
public static final String LAST_NAME = "LNAME";
public static final String PHONE_NUMBER = "PHONE";
public static final String ADDRESS = "ADDRESS";
public static final String TAGS = "tags";
public static final String TAG_COUNT = "tags_count";
public static final String TAG_NAME = "name";
Expand Down Expand Up @@ -69,22 +68,7 @@ public void upsertContact(String listId, MemberInfo contact) throws IOException,
upsertMemberMethod.merge_fields.mapping.putAll(contact.merge_fields.mapping);
upsertMemberMethod.interests.mapping.putAll(contact.interests.mapping);

try {
client.execute(upsertMemberMethod);
} catch (MailchimpException e) {
String error = exceptionToString(e);

// We're finding that address validation is SUPER picky, especially when it comes to CRMs that combine
// street1 and street2 into a single street. If the upsert fails, try it again without ADDRESS...
if (contact.merge_fields.mapping.containsKey(ADDRESS)) {
env.logJobInfo("Mailchimp upsertContact failed: {}", error);
env.logJobInfo("retrying upsertContact without ADDRESS");
upsertMemberMethod.merge_fields.mapping.remove(ADDRESS);
client.execute(upsertMemberMethod);
} else {
throw e;
}
}
client.execute(upsertMemberMethod);
}

public String upsertContactsBatch(String listId, List<MemberInfo> contacts) throws IOException, MailchimpException {
Expand Down Expand Up @@ -301,5 +285,12 @@ public static final class BatchOperationResponse {
public String type;
public String title;
public String status;
public List<Error> errors;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static final class Error {
public String field;
public String message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.impactupgrade.nucleus.client.MailchimpClient;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import com.impactupgrade.nucleus.model.CrmAddress;
import com.impactupgrade.nucleus.model.CrmContact;
import com.impactupgrade.nucleus.util.HttpClient;
import org.apache.commons.collections.CollectionUtils;
Expand All @@ -40,7 +39,6 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static com.impactupgrade.nucleus.client.MailchimpClient.ADDRESS;
import static com.impactupgrade.nucleus.client.MailchimpClient.FIRST_NAME;
import static com.impactupgrade.nucleus.client.MailchimpClient.LAST_NAME;
import static com.impactupgrade.nucleus.client.MailchimpClient.PHONE_NUMBER;
Expand Down Expand Up @@ -480,11 +478,6 @@ protected MemberInfo toMcMemberInfo(CrmContact crmContact, Map<String, Object> c
mcContact.merge_fields.mapping.put(FIRST_NAME, crmContact.firstName);
mcContact.merge_fields.mapping.put(LAST_NAME, crmContact.lastName);
mcContact.merge_fields.mapping.put(PHONE_NUMBER, crmContact.mobilePhone);
// MC only allows "complete" addresses, so only fill this out if that's the case. As a backup, we're creating
// separate, custom fields for city/state/zip/country.
if (!Strings.isNullOrEmpty(crmContact.mailingAddress.street)) {
mcContact.merge_fields.mapping.put(ADDRESS, toMcAddress(crmContact.mailingAddress));
}
mcContact.merge_fields.mapping.putAll(customFields);
mcContact.status = SUBSCRIBED;

Expand All @@ -496,17 +489,4 @@ protected MemberInfo toMcMemberInfo(CrmContact crmContact, Map<String, Object> c

return mcContact;
}

protected MailchimpObject toMcAddress(CrmAddress address) {
MailchimpObject mcAddress = new MailchimpObject();

mcAddress.mapping.put("country", address.country);
mcAddress.mapping.put("state", address.state);
mcAddress.mapping.put("city", address.city);
// TODO: CRM street does not handle multiple address lines eg. addr1 & addr2
mcAddress.mapping.put("addr1", address.street);
mcAddress.mapping.put("zip", address.postalCode);

return mcAddress;
}
}

0 comments on commit ff86d38

Please sign in to comment.