Skip to content

Commit 2e6f7e2

Browse files
committed
support bulk user/company untag
1 parent 6b02118 commit 2e6f7e2

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,13 @@ public String toString() {
193193
@JsonProperty("tags")
194194
private TagCollection tagCollection = new TagCollection();
195195

196-
@JsonProperty("untag")
197-
private boolean untag;
196+
private Boolean untag;
198197

199198
public Company() {
200199
}
201200

202201
public Company untag() {
203-
untag = true;
202+
untag = Boolean.TRUE;
204203
return this;
205204
}
206205

@@ -306,6 +305,10 @@ public TagCollection getTagCollection() {
306305
return tagCollection;
307306
}
308307

308+
boolean isUntag() {
309+
return untag == null ? false : untag;
310+
}
311+
309312
@Override
310313
public boolean equals(Object o) {
311314
if (this == o) return true;
@@ -317,7 +320,6 @@ public boolean equals(Object o) {
317320
if (Float.compare(company.monthlySpend, monthlySpend) != 0) return false;
318321
if (remoteCreatedAt != company.remoteCreatedAt) return false;
319322
if (sessionCount != company.sessionCount) return false;
320-
if (untag != company.untag) return false;
321323
if (updatedAt != company.updatedAt) return false;
322324
if (companyID != null ? !companyID.equals(company.companyID) : company.companyID != null) return false;
323325
if (customAttributes != null ? !customAttributes.equals(company.customAttributes) : company.customAttributes != null)
@@ -330,6 +332,7 @@ public boolean equals(Object o) {
330332
if (tagCollection != null ? !tagCollection.equals(company.tagCollection) : company.tagCollection != null)
331333
return false;
332334
if (!type.equals(company.type)) return false;
335+
if (untag != null ? !untag.equals(company.untag) : company.untag != null) return false;
333336
//noinspection RedundantIfStatement
334337
if (userCount != null ? !userCount.equals(company.userCount) : company.userCount != null) return false;
335338

@@ -352,7 +355,7 @@ public int hashCode() {
352355
result = 31 * result + (customAttributes != null ? customAttributes.hashCode() : 0);
353356
result = 31 * result + (segmentCollection != null ? segmentCollection.hashCode() : 0);
354357
result = 31 * result + (tagCollection != null ? tagCollection.hashCode() : 0);
355-
result = 31 * result + (untag ? 1 : 0);
358+
result = 31 * result + (untag != null ? untag.hashCode() : 0);
356359
return result;
357360
}
358361

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

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ public class Tag extends TypedData {
2222
private static final Logger logger = LoggerFactory.getLogger("intercom-java");
2323
private static final HashMap<String, String> SENTINEL = Maps.newHashMap();
2424

25+
public static Tag tag(Tag tag, User... users) throws InvalidException, AuthorizationException {
26+
return tag(tag, new UserCollection(Lists.newArrayList(users)));
27+
}
2528

2629
public static Tag tag(Tag tag, UserCollection users) throws InvalidException, AuthorizationException {
27-
TagTypedCollection tagTypedCollection = createTagTypedCollection(tag, users);
28-
return DataResource.create(tagTypedCollection, "tags", Tag.class);
30+
TaggableCollection taggableCollection = createTagTypedCollection(tag, users);
31+
return DataResource.create(taggableCollection, "tags", Tag.class);
32+
}
33+
34+
public static Tag tag(Tag tag, Company... companies) throws InvalidException, AuthorizationException {
35+
return tag(tag, new CompanyCollection(Lists.newArrayList(companies)));
2936
}
3037

3138
public static Tag tag(Tag tag, CompanyCollection companies) throws InvalidException, AuthorizationException {
32-
TagTypedCollection tagTypedCollection = createTagTypedCollection(tag, companies);
33-
return DataResource.create(tagTypedCollection, "tags", Tag.class);
39+
TaggableCollection taggableCollection = createTagTypedCollection(tag, companies);
40+
return DataResource.create(taggableCollection, "tags", Tag.class);
3441
}
3542

3643
public static Tag create(Tag tag) throws InvalidException, AuthorizationException {
@@ -57,17 +64,22 @@ public static TagCollection list() throws InvalidException, AuthorizationExcepti
5764
}
5865

5966
@VisibleForTesting
60-
static TagTypedCollection createTagTypedCollection(Tag tag, UserCollection users) {
61-
TagTypedCollection tagTypedCollection = new TagTypedCollection();
62-
tagTypedCollection.setName(tag.getName());
63-
tagTypedCollection.setId(tag.getId());
64-
final List<Map<String,String>> usersLite = Lists.newArrayList();
67+
static TaggableCollection createTagTypedCollection(Tag tag, UserCollection users) {
68+
TaggableCollection taggableCollection = new TaggableCollection();
69+
taggableCollection.setName(tag.getName());
70+
taggableCollection.setId(tag.getId());
71+
final List<Map<String, Object>> usersLite = Lists.newArrayList();
6572
final List<User> pageItems = users.getPageItems();
6673
for (User user : pageItems) {
67-
Map<String, String> userMap = Maps.newHashMap();
74+
Map<String, Object> userMap = Maps.newHashMap();
6875
final String id = user.getId();
6976
final String email = user.getEmail();
7077
final String userId = user.getUserId();
78+
79+
if (user.isUntag()) {
80+
userMap.put("untag", true);
81+
}
82+
7183
if (!Strings.isNullOrEmpty(id)) {
7284
userMap.put("id", id);
7385
usersLite.add(userMap);
@@ -81,64 +93,69 @@ static TagTypedCollection createTagTypedCollection(Tag tag, UserCollection users
8193
logger.warn("no identifiers found for user tag target, skipping [" + tag + "] [" + user.toString() + "]");
8294
}
8395
}
84-
tagTypedCollection.setUsers(usersLite);
85-
return tagTypedCollection;
96+
taggableCollection.setUsers(usersLite);
97+
return taggableCollection;
8698
}
8799

88100
@VisibleForTesting
89-
static TagTypedCollection createTagTypedCollection(Tag tag, CompanyCollection companies) {
90-
TagTypedCollection tagTypedCollection = new TagTypedCollection();
91-
tagTypedCollection.setName(tag.getName());
92-
tagTypedCollection.setId(tag.getId());
101+
static TaggableCollection createTagTypedCollection(Tag tag, CompanyCollection companies) {
102+
TaggableCollection taggableCollection = new TaggableCollection();
103+
taggableCollection.setName(tag.getName());
104+
taggableCollection.setId(tag.getId());
93105

94-
final List<Map<String, String>> companiesLite = Lists.newArrayList();
106+
final List<Map<String, Object>> companiesLite = Lists.newArrayList();
95107
final List<Company> pageItems = companies.getPageItems();
96108
for (Company company : pageItems) {
97-
Map<String, String> companyMap = Maps.newHashMap();
109+
Map<String, Object> companyMap = Maps.newHashMap();
98110
final String companyID = company.getCompanyID();
99-
final String id1 = company.getId();
111+
final String id = company.getId();
100112
final String name = company.getName();
113+
114+
if(company.isUntag()) {
115+
companyMap.put("untag", true);
116+
}
117+
101118
if(!Strings.isNullOrEmpty(companyID)) {
102119
companyMap.put("company_id", companyID);
103120
companiesLite.add(companyMap);
104-
} else if(!Strings.isNullOrEmpty(id1)) {
105-
companyMap.put("id", id1);
121+
} else if(!Strings.isNullOrEmpty(id)) {
122+
companyMap.put("id", id);
106123
companiesLite.add(companyMap);
107124
} else if(!Strings.isNullOrEmpty(name)) {
108125
companyMap.put("name", name);
109126
companiesLite.add(companyMap);
110127
} else {
111128
logger.warn("no identifiers found for company tag target, skipping [" + tag + "] [" + company.toString() + "]");
112129
}
113-
tagTypedCollection.setCompanies(companiesLite);
130+
taggableCollection.setCompanies(companiesLite);
114131
}
115-
return tagTypedCollection;
132+
return taggableCollection;
116133
}
117134

118135
@SuppressWarnings("UnusedDeclaration")
119136
@JsonIgnoreProperties(ignoreUnknown = true)
120137
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
121-
static class TagTypedCollection extends Tag {
138+
static class TaggableCollection extends Tag {
122139

123140
@JsonProperty("users")
124-
private List<Map<String, String>> users;
141+
private List<Map<String, Object>> users;
125142

126143
@JsonProperty("companies")
127-
private List<Map<String, String>> companies;
144+
private List<Map<String, Object>> companies;
128145

129-
public List<Map<String, String>> getUsers() {
146+
public List<Map<String, Object>> getUsers() {
130147
return users;
131148
}
132149

133-
public void setUsers(List<Map<String, String>> usersLite) {
150+
public void setUsers(List<Map<String, Object>> usersLite) {
134151
this.users = usersLite;
135152
}
136153

137-
public List<Map<String, String>> getCompanies() {
154+
public List<Map<String, Object>> getCompanies() {
138155
return companies;
139156
}
140157

141-
public void setCompanies(List<Map<String, String>> companies) {
158+
public void setCompanies(List<Map<String, Object>> companies) {
142159
this.companies = companies;
143160
}
144161
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,20 @@ public Boolean isNewSession() {
284284
@JsonProperty("new_session")
285285
private boolean newSession;
286286

287-
@JsonProperty("untag")
288-
private boolean untag;
287+
private Boolean untag;
289288

290289
public User() {
291290
}
292291

293292
public User untag() {
294-
untag = true;
293+
untag = Boolean.TRUE;
295294
return this;
296295
}
297296

297+
boolean isUntag() {
298+
return untag == null ? false : untag;
299+
}
300+
298301
public String getReplyType() {
299302
return getType() + "_reply";
300303
}
@@ -477,7 +480,6 @@ public boolean equals(Object o) {
477480
if (remoteCreatedAt != user.remoteCreatedAt) return false;
478481
if (sessionCount != user.sessionCount) return false;
479482
if (unsubscribedFromEmails != user.unsubscribedFromEmails) return false;
480-
if (untag != user.untag) return false;
481483
if (updateLastRequestAt != user.updateLastRequestAt) return false;
482484
if (updatedAt != user.updatedAt) return false;
483485
if (avatar != null ? !avatar.equals(user.avatar) : user.avatar != null) return false;
@@ -497,6 +499,7 @@ public boolean equals(Object o) {
497499
if (tagCollection != null ? !tagCollection.equals(user.tagCollection) : user.tagCollection != null)
498500
return false;
499501
if (!type.equals(user.type)) return false;
502+
if (untag != null ? !untag.equals(user.untag) : user.untag != null) return false;
500503
if (userAgentData != null ? !userAgentData.equals(user.userAgentData) : user.userAgentData != null)
501504
return false;
502505
//noinspection RedundantIfStatement
@@ -529,7 +532,7 @@ public int hashCode() {
529532
result = 31 * result + (tagCollection != null ? tagCollection.hashCode() : 0);
530533
result = 31 * result + (updateLastRequestAt ? 1 : 0);
531534
result = 31 * result + (newSession ? 1 : 0);
532-
result = 31 * result + (untag ? 1 : 0);
535+
result = 31 * result + (untag != null ? untag.hashCode() : 0);
533536
return result;
534537
}
535538

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import static io.intercom.api.TestSupport.load;
1313
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertTrue;
1415

1516
public class TagTest {
1617

@@ -26,23 +27,24 @@ public void testTagUsers() throws Exception {
2627
String json = load("tag.json");
2728
final Tag tag = mapper.readValue(json, Tag.class);
2829

29-
final User user1 = new User().setEmail("wash@serenity.io");
30+
final User user1 = new User().setEmail("wash@serenity.io").untag();
3031
final User user2 = new User().setUserId("22");
3132
final User user3 = new User().setId("3434");
3233
final User user4 = new User().setEmail("malcolm@serenity.io");
3334
final User user5 = new User().setUserId("23");
3435
final User user6 = new User().setId("3435");
3536
final UserCollection uc = new UserCollection(Lists.newArrayList(user1, user2, user3, user4, user5, user6));
36-
final Tag.TagTypedCollection tagTypedCollection = Tag.createTagTypedCollection(tag, uc);
37-
final List<Map<String, String>> users = tagTypedCollection.getUsers();
37+
final Tag.TaggableCollection taggableCollection = Tag.createTagTypedCollection(tag, uc);
38+
final List<Map<String, Object>> users = taggableCollection.getUsers();
3839

3940
int match = 0;
40-
for (Map<String, String> user : users) {
41+
for (Map<String, Object> user : users) {
4142
if (user.containsKey("id") && user.get("id").equals("3434")) {
4243
match += 1;
4344
}
4445
if (user.containsKey("email") && user.get("email").equals("wash@serenity.io")) {
4546
match += 1;
47+
assertTrue(user.containsKey("untag") && user.get("untag") == Boolean.TRUE);
4648
}
4749
if (user.containsKey("user_id") && user.get("user_id").equals("22")) {
4850
match += 1;
@@ -65,19 +67,20 @@ public void testTagCompanies() throws Exception {
6567
String json = load("tag.json");
6668
final Tag tag = mapper.readValue(json, Tag.class);
6769

68-
final Company c1 = new Company().setCompanyID("c1");
70+
final Company c1 = new Company().setCompanyID("c1").untag();
6971
final Company c2 = new Company().setName("name1");
7072
final Company c3 = new Company().setCompanyID("c3");
7173
final Company c4 = new Company().setName("name4");
7274
final Company c5 = new Company().setId("1");
7375
final Company c6 = new Company().setId("2");
7476
final CompanyCollection uc = new CompanyCollection(Lists.newArrayList(c1, c2, c3, c4, c5, c6));
75-
final Tag.TagTypedCollection tagTypedCollection = Tag.createTagTypedCollection(tag, uc);
76-
final List<Map<String, String>> companies = tagTypedCollection.getCompanies();
77+
final Tag.TaggableCollection taggableCollection = Tag.createTagTypedCollection(tag, uc);
78+
final List<Map<String, Object>> companies = taggableCollection.getCompanies();
7779
int match = 0;
78-
for (Map<String, String> company : companies) {
80+
for (Map<String, Object> company : companies) {
7981
if (company.containsKey("company_id") && company.get("company_id").equals("c1")) {
8082
match += 1;
83+
assertTrue(company.containsKey("untag") && company.get("untag") == Boolean.TRUE);
8184
}
8285
if (company.containsKey("name") && company.get("name").equals("name1")) {
8386
match += 1;

0 commit comments

Comments
 (0)