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

21677: Mailchimp SMS - Integration Update #249

Merged
merged 7 commits into from
Nov 26, 2024
Merged
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 @@ -60,6 +60,7 @@ 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 SMS_PHONE_NUMBER = "SMSPHONE";
public static final String TAGS = "tags";
public static final String TAG_NAME = "name";
public static final String TAG_STATUS = "status";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ public static class CommunicationPlatform extends Platform {
// Transactional email (donation receipts, notifications, etc.) need one of the email platforms to be
// designated as the conduit!
public boolean transactionalSender = false;
public String country;
public String countryCode;
}

public List<CommunicationPlatform> mailchimp = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
Expand All @@ -33,6 +34,7 @@
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;
import static com.impactupgrade.nucleus.client.MailchimpClient.SMS_PHONE_NUMBER;
import static com.impactupgrade.nucleus.client.MailchimpClient.SUBSCRIBED;

public class MailchimpCommunicationService extends AbstractCommunicationService {
Expand Down Expand Up @@ -144,7 +146,7 @@ protected void syncContacts(PagedResults.ResultSet<CrmContact> resultSet, Enviro
communicationList);

// run the actual contact upserts
List<MemberInfo> upsertMemberInfos = toMcMemberInfos(communicationList, contactsToUpsert, contactsCustomFields);
List<MemberInfo> upsertMemberInfos = toMcMemberInfos(communicationList, mailchimpConfig, contactsToUpsert, contactsCustomFields);
String upsertBatchId = mailchimpClient.upsertContactsBatch(communicationList.id, upsertMemberInfos);
mailchimpClient.runBatchOperations(mailchimpConfig, upsertBatchId, 0);

Expand Down Expand Up @@ -305,7 +307,7 @@ protected void upsertContact(EnvironmentConfig.CommunicationPlatform mailchimpCo
communicationList);

// run the actual contact upserts
MemberInfo upsertMemberInfo = toMcMemberInfo(crmContact, customFields, communicationList.groups);
MemberInfo upsertMemberInfo = toMcMemberInfo(mailchimpConfig, crmContact, customFields, communicationList.groups);
mailchimpClient.upsertContact(communicationList.id, upsertMemberInfo);

// update all contacts' tags
Expand Down Expand Up @@ -400,14 +402,14 @@ protected String updateTagsBatch(String listId, List<MailchimpClient.EmailContac
}
}

protected List<MemberInfo> toMcMemberInfos(EnvironmentConfig.CommunicationList communicationList, List<CrmContact> crmContacts,
protected List<MemberInfo> toMcMemberInfos(EnvironmentConfig.CommunicationList communicationList, EnvironmentConfig.CommunicationPlatform mailchimpConfig, List<CrmContact> crmContacts,
Map<String, Map<String, Object>> customFieldsMap) {
return crmContacts.stream()
.map(crmContact -> toMcMemberInfo(crmContact, customFieldsMap.get(crmContact.email), communicationList.groups))
.map(crmContact -> toMcMemberInfo(mailchimpConfig, crmContact, customFieldsMap.get(crmContact.email), communicationList.groups))
.collect(Collectors.toList());
}

protected MemberInfo toMcMemberInfo(CrmContact crmContact, Map<String, Object> customFields, Map<String, String> groups) {
protected MemberInfo toMcMemberInfo(EnvironmentConfig.CommunicationPlatform mailchimpConfig, CrmContact crmContact, Map<String, Object> customFields, Map<String, String> groups) {
if (crmContact == null) {
return null;
}
Expand All @@ -421,6 +423,15 @@ 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);

if (smsAllowed(mailchimpConfig, crmContact)) {
mcContact.consents_to_one_to_one_messaging = true;
mcContact.sms_subscription_status = SUBSCRIBED;
String phoneNumber = crmContact.phoneNumberForSMS();
mcContact.sms_phone_number = phoneNumber;
mcContact.merge_fields.mapping.put(SMS_PHONE_NUMBER, phoneNumber);
}

mcContact.merge_fields.mapping.putAll(customFields);
mcContact.status = SUBSCRIBED;

Expand All @@ -432,4 +443,25 @@ protected MemberInfo toMcMemberInfo(CrmContact crmContact, Map<String, Object> c

return mcContact;
}

private boolean smsAllowed(EnvironmentConfig.CommunicationPlatform mailchimpConfig, CrmContact crmContact) {
boolean smsOptIn = Boolean.TRUE == crmContact.smsOptIn && Boolean.TRUE != crmContact.smsOptOut;
if (!smsOptIn) {
return false;
}
String phoneNumber = crmContact.phoneNumberForSMS();
if (Strings.isNullOrEmpty(phoneNumber)) {
return false;
}

boolean smsAllowed = false;
if (!Strings.isNullOrEmpty(mailchimpConfig.countryCode) && phoneNumber.startsWith(mailchimpConfig.countryCode)) {
smsAllowed = true;
} else if (!Strings.isNullOrEmpty(mailchimpConfig.country) && !phoneNumber.startsWith("+")) {
smsAllowed = Stream.of(crmContact.account.billingAddress, crmContact.account.mailingAddress, crmContact.mailingAddress)
.filter(Objects::nonNull)
.anyMatch(crmAddress -> mailchimpConfig.country.equalsIgnoreCase(crmAddress.country));
}
return smsAllowed;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/environment-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@
"groups": {},
"crmFilter": ""
}
]
],
"country": "Australia",
"countryCode" : "+61"
}
],

Expand Down
Loading