Skip to content
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ User.create(one);
User.create(two);
Tag.tag(tag, one, two);

// tag and untag contacts
Contact contact1 = Contact.findByID("5ab313046e4997e35bc13e7c");
Contact contact2 = Contact.findByUserID("697ea3e0-227d-4d70-b776-1652e94f9583").untag();
Tag.tag(tag, contact1, contact2);

// iterate over all tags
final TagCollection tags = Tag.list();
while (tags.hasNext()) {
Expand Down
35 changes: 35 additions & 0 deletions intercom-java/src/main/java/io/intercom/api/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static Tag tag(Tag tag, User... users) throws InvalidException, Authoriza
return tag(tag, new UserCollection(Lists.newArrayList(users)));
}

public static Tag tag(Tag tag, Contact... contacts) throws InvalidException, AuthorizationException {
return tag(tag, new ContactCollection(Lists.newArrayList(contacts)));
}

public static Tag tag(Tag tag, Company... companies) throws InvalidException, AuthorizationException {
return tag(tag, new CompanyCollection(Lists.newArrayList(companies)));
}
Expand All @@ -35,6 +39,11 @@ static Tag tag(Tag tag, UserCollection users) throws InvalidException, Authoriza
return DataResource.create(taggableCollection, "tags", Tag.class);
}

static Tag tag(Tag tag, ContactCollection contacts) throws InvalidException, AuthorizationException {
TaggableCollection taggableCollection = createTagTypedCollection(tag, contacts);
return DataResource.create(taggableCollection, "tags", Tag.class);
}

static Tag tag(Tag tag, CompanyCollection companies) throws InvalidException, AuthorizationException {
TaggableCollection taggableCollection = createTagTypedCollection(tag, companies);
return DataResource.create(taggableCollection, "tags", Tag.class);
Expand Down Expand Up @@ -97,6 +106,32 @@ static TaggableCollection createTagTypedCollection(Tag tag, UserCollection users
return taggableCollection;
}

@VisibleForTesting
static TaggableCollection createTagTypedCollection(Tag tag, ContactCollection contacts) {
TaggableCollection taggableCollection = new TaggableCollection();
taggableCollection.setName(tag.getName());
taggableCollection.setId(tag.getId());
final List<Map<String, Object>> contactsLite = Lists.newArrayList();
final List<Contact> pageItems = contacts.getPage();
for (Contact contact: pageItems) {
Map<String, Object> contactMap = Maps.newHashMap();
final String id = contact.getID();

if (contact.isUntag()) {
contactMap.put("untag", true);
}

if (!Strings.isNullOrEmpty(id)) {
contactMap.put("id", id);
contactsLite.add(contactMap);
} else {
logger.warn("no identifiers found for user tag target, skipping [" + tag + "] [" + contact.toString() + "]");
}
}
taggableCollection.setUsers(contactsLite);
return taggableCollection;
}

@VisibleForTesting
static TaggableCollection createTagTypedCollection(Tag tag, CompanyCollection companies) {
TaggableCollection taggableCollection = new TaggableCollection();
Expand Down
29 changes: 28 additions & 1 deletion intercom-java/src/test/java/io/intercom/api/TagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ public void testTagUsers() throws Exception {
assertEquals(6, match);
}

@Test
public void testTagContacts() throws Exception {
String json = load("tag.json");
final Tag tag = mapper.readValue(json, Tag.class);

final Contact contact1 = new Contact().setID("3434");
final Contact contact2 = new Contact().setID("3435").untag();
final Contact contact3 = new Contact().setEmail("malcolm@serenity.io");
final Contact contact4 = new Contact().setEmail("wash@serenity.io").untag();
final ContactCollection contactCollection = new ContactCollection(Lists.newArrayList(contact1, contact2, contact3, contact4));
final Tag.TaggableCollection taggableCollection = Tag.createTagTypedCollection(tag, contactCollection);
final List<Map<String, Object>> contacts = taggableCollection.getUsers();

int match = 0;
for (Map<String, Object> contact : contacts) {
if (contact.containsKey("id") && contact.get("id").equals("3434")) {
match += 1;
assertTrue(!contact.containsKey("untag"));
}
if (contact.containsKey("id") && contact.get("id").equals("3435")) {
match += 1;
assertTrue(contact.containsKey("untag") && contact.get("untag") == Boolean.TRUE);
}
}
assertEquals(2, match);
}

@Test
public void testTagCompanies() throws Exception {
String json = load("tag.json");
Expand Down Expand Up @@ -124,4 +151,4 @@ public void TestSerdes() throws Exception {

assertEquals(tag, tag1);
}
}
}