Skip to content

Commit

Permalink
filtering out inactive tags based on configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Jul 12, 2023
1 parent 9198940 commit 6906f1e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
import com.ecwid.maleorang.method.v3_0.lists.merge_fields.GetMergeFieldsMethod;
import com.ecwid.maleorang.method.v3_0.lists.merge_fields.MergeFieldInfo;
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -174,4 +178,39 @@ public String exceptionToString(MailchimpException e) {
}
return description;
}

public static void main(String[] args) {
EnvironmentConfig.ContactTagFilters contactTagFilters = new EnvironmentConfig.ContactTagFilters();
contactTagFilters.tags = Set.of("ftag1", "ftag2");
contactTagFilters.tagPrefixes = Set.of("pref1", "pref2");

EnvironmentConfig.EmailPlatform emailPlatform = new EnvironmentConfig.EmailPlatform();
emailPlatform.secretKey = "e9bd13aeaa928b7cfd9c7fc8cba88e32-us14";
emailPlatform.contactTagFilters = contactTagFilters;

MailchimpClient mailchimpClient = new MailchimpClient(emailPlatform);

String listId = "3d215dac12";
String email = "[email protected]";

updateTags(listId, email, mailchimpClient, emailPlatform);
}

protected static void updateTags(String listId, String email, MailchimpClient mailchimpClient, EnvironmentConfig.EmailPlatform emailPlatform) {
try {
List<String> contactTags = mailchimpClient.getContactTags(listId, email);

List<String> activeTags = List.of("newtag2");

String[] tagPrefixesArray = emailPlatform.contactTagFilters.tagPrefixes.toArray(new String[]{});
List<String> inactiveTags = contactTags.stream()
.filter(tag -> !activeTags.contains(tag))
.filter(tag -> emailPlatform.contactTagFilters.tags.contains(tag) || StringUtils.startsWithAny(tag, tagPrefixesArray))
.collect(Collectors.toList());

mailchimpClient.updateContactTags(listId, email, activeTags, inactiveTags);
} catch (Exception e) {
log.error("updating tags failed for contact: {} {}", email, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,15 @@ public static class EmailList implements Serializable {
public String crmLeadFilter = "";
}

public static class ContactTagFilters implements Serializable {
public Set<String> tags = new HashSet<>();
public Set<String> tagPrefixes = new HashSet<>();
}

public static class EmailPlatform extends Platform {
public List<EmailList> lists = new ArrayList<>();
public EmailTagFilters tagFilters = new EmailTagFilters();
public ContactTagFilters contactTagFilters = new ContactTagFilters();
// Transactional email (donation receipts, notifications, etc.) need one of the email platforms to be
// designated as the conduit!
public boolean transactionalSender = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import com.impactupgrade.nucleus.model.CrmAddress;
import com.impactupgrade.nucleus.model.CrmContact;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -247,8 +248,14 @@ protected void updateTags(String listId, CrmContact crmContact, List<String> crm
MailchimpClient mailchimpClient, EnvironmentConfig.EmailPlatform mailchimpConfig) {
try {
List<String> activeTags = getContactTagsCleaned(crmContact, crmContactCampaignNames, mailchimpConfig);
List<String> inactiveTags = mailchimpClient.getContactTags(listId, crmContact.email);
inactiveTags.removeAll(activeTags);
List<String> contactTags = mailchimpClient.getContactTags(listId, crmContact.email);

String[] tagPrefixesArray = mailchimpConfig.contactTagFilters.tagPrefixes.toArray(new String[]{});
List<String> inactiveTags = contactTags.stream()
.filter(tag -> !activeTags.contains(tag))
.filter(tag ->
mailchimpConfig.contactTagFilters.tags.contains(tag) || StringUtils.startsWithAny(tag, tagPrefixesArray))
.collect(Collectors.toList());

mailchimpClient.updateContactTags(listId, crmContact.email, activeTags, inactiveTags);
} catch (Exception e) {
Expand Down

0 comments on commit 6906f1e

Please sign in to comment.