Skip to content

Commit 5ceb3b7

Browse files
thewheatjonnyom
authored andcommitted
Add tagging to contacts (Closes #184) (#191)
Addresses #184 by adding the ability to tag contacts via a Contact object
1 parent 4a94687 commit 5ceb3b7

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@ User.create(one);
347347
User.create(two);
348348
Tag.tag(tag, one, two);
349349

350+
// tag and untag contacts
351+
Contact contact1 = Contact.findByID("5ab313046e4997e35bc13e7c");
352+
Contact contact2 = Contact.findByUserID("697ea3e0-227d-4d70-b776-1652e94f9583").untag();
353+
Tag.tag(tag, contact1, contact2);
354+
350355
// iterate over all tags
351356
final TagCollection tags = Tag.list();
352357
while (tags.hasNext()) {

intercom-java/src/main/java/io/intercom/api/Tag.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public static Tag tag(Tag tag, User... users) throws InvalidException, Authoriza
2626
return tag(tag, new UserCollection(Lists.newArrayList(users)));
2727
}
2828

29+
public static Tag tag(Tag tag, Contact... contacts) throws InvalidException, AuthorizationException {
30+
return tag(tag, new ContactCollection(Lists.newArrayList(contacts)));
31+
}
32+
2933
public static Tag tag(Tag tag, Company... companies) throws InvalidException, AuthorizationException {
3034
return tag(tag, new CompanyCollection(Lists.newArrayList(companies)));
3135
}
@@ -35,6 +39,11 @@ static Tag tag(Tag tag, UserCollection users) throws InvalidException, Authoriza
3539
return DataResource.create(taggableCollection, "tags", Tag.class);
3640
}
3741

42+
static Tag tag(Tag tag, ContactCollection contacts) throws InvalidException, AuthorizationException {
43+
TaggableCollection taggableCollection = createTagTypedCollection(tag, contacts);
44+
return DataResource.create(taggableCollection, "tags", Tag.class);
45+
}
46+
3847
static Tag tag(Tag tag, CompanyCollection companies) throws InvalidException, AuthorizationException {
3948
TaggableCollection taggableCollection = createTagTypedCollection(tag, companies);
4049
return DataResource.create(taggableCollection, "tags", Tag.class);
@@ -97,6 +106,32 @@ static TaggableCollection createTagTypedCollection(Tag tag, UserCollection users
97106
return taggableCollection;
98107
}
99108

109+
@VisibleForTesting
110+
static TaggableCollection createTagTypedCollection(Tag tag, ContactCollection contacts) {
111+
TaggableCollection taggableCollection = new TaggableCollection();
112+
taggableCollection.setName(tag.getName());
113+
taggableCollection.setId(tag.getId());
114+
final List<Map<String, Object>> contactsLite = Lists.newArrayList();
115+
final List<Contact> pageItems = contacts.getPage();
116+
for (Contact contact: pageItems) {
117+
Map<String, Object> contactMap = Maps.newHashMap();
118+
final String id = contact.getID();
119+
120+
if (contact.isUntag()) {
121+
contactMap.put("untag", true);
122+
}
123+
124+
if (!Strings.isNullOrEmpty(id)) {
125+
contactMap.put("id", id);
126+
contactsLite.add(contactMap);
127+
} else {
128+
logger.warn("no identifiers found for user tag target, skipping [" + tag + "] [" + contact.toString() + "]");
129+
}
130+
}
131+
taggableCollection.setUsers(contactsLite);
132+
return taggableCollection;
133+
}
134+
100135
@VisibleForTesting
101136
static TaggableCollection createTagTypedCollection(Tag tag, CompanyCollection companies) {
102137
TaggableCollection taggableCollection = new TaggableCollection();

intercom-java/src/test/java/io/intercom/api/TagTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ public void testTagUsers() throws Exception {
6262
assertEquals(6, match);
6363
}
6464

65+
@Test
66+
public void testTagContacts() throws Exception {
67+
String json = load("tag.json");
68+
final Tag tag = mapper.readValue(json, Tag.class);
69+
70+
final Contact contact1 = new Contact().setID("3434");
71+
final Contact contact2 = new Contact().setID("3435").untag();
72+
final Contact contact3 = new Contact().setEmail("malcolm@serenity.io");
73+
final Contact contact4 = new Contact().setEmail("wash@serenity.io").untag();
74+
final ContactCollection contactCollection = new ContactCollection(Lists.newArrayList(contact1, contact2, contact3, contact4));
75+
final Tag.TaggableCollection taggableCollection = Tag.createTagTypedCollection(tag, contactCollection);
76+
final List<Map<String, Object>> contacts = taggableCollection.getUsers();
77+
78+
int match = 0;
79+
for (Map<String, Object> contact : contacts) {
80+
if (contact.containsKey("id") && contact.get("id").equals("3434")) {
81+
match += 1;
82+
assertTrue(!contact.containsKey("untag"));
83+
}
84+
if (contact.containsKey("id") && contact.get("id").equals("3435")) {
85+
match += 1;
86+
assertTrue(contact.containsKey("untag") && contact.get("untag") == Boolean.TRUE);
87+
}
88+
}
89+
assertEquals(2, match);
90+
}
91+
6592
@Test
6693
public void testTagCompanies() throws Exception {
6794
String json = load("tag.json");
@@ -124,4 +151,4 @@ public void TestSerdes() throws Exception {
124151

125152
assertEquals(tag, tag1);
126153
}
127-
}
154+
}

0 commit comments

Comments
 (0)