Skip to content

Commit ba552e6

Browse files
committed
Allow users to be removed from companies
1 parent 955084f commit ba552e6

File tree

4 files changed

+117
-14
lines changed

4 files changed

+117
-14
lines changed

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,36 @@ class CompanyUpdateBuilder {
1111
/**
1212
* Provide restrictions on the company data that can be sent via a user update
1313
*/
14-
static List<CompanyWithStringPlan> buildUserUpdateCompanies(CompanyCollection c) {
14+
static List<CompanyWithStringPlan> buildUserUpdateCompanies(CompanyCollection add, CompanyCollection remove) {
15+
1516
final ArrayList<CompanyWithStringPlan> updatableCompanies = Lists.newArrayList();
16-
if (c != null) {
17-
final List<Company> companies = c.getPage();
17+
if (add != null) {
18+
final List<Company> companies = add.getPage();
19+
for (Company company : companies) {
20+
updatableCompanies.add(prepareUpdatableCompany(company));
21+
}
22+
}
23+
24+
if (remove != null) {
25+
final List<Company> companies = remove.getPage();
1826
for (Company company : companies) {
19-
final CompanyWithStringPlan updatableCompany = new CompanyWithStringPlan();
20-
updatableCompany.setCompanyID(company.getCompanyID());
21-
updatableCompany.setName(company.getName());
22-
updatableCompany.setSessionCount(company.getSessionCount());
23-
updatableCompany.setMonthlySpend(company.getMonthlySpend());
24-
updatableCompany.setRemoteCreatedAt(company.getRemoteCreatedAt());
25-
if (company.getPlan() != null) {
26-
updatableCompany.setPlan(company.getPlan().getName());
27-
}
28-
updatableCompanies.add(updatableCompany);
27+
updatableCompanies.add(prepareUpdatableCompany(company).setRemove(Boolean.TRUE));
2928
}
3029
}
30+
3131
return updatableCompanies;
3232
}
33+
34+
private static CompanyWithStringPlan prepareUpdatableCompany(Company company) {
35+
final CompanyWithStringPlan updatableCompany = new CompanyWithStringPlan();
36+
updatableCompany.setCompanyID(company.getCompanyID());
37+
updatableCompany.setName(company.getName());
38+
updatableCompany.setSessionCount(company.getSessionCount());
39+
updatableCompany.setMonthlySpend(company.getMonthlySpend());
40+
updatableCompany.setRemoteCreatedAt(company.getRemoteCreatedAt());
41+
if (company.getPlan() != null) {
42+
updatableCompany.setPlan(company.getPlan().getName());
43+
}
44+
return updatableCompany;
45+
}
3346
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class CompanyWithStringPlan extends TypedData {
3737
@JsonProperty("custom_attributes")
3838
private Map<String, CustomAttribute> customAttributes = Maps.newHashMap();
3939

40+
@JsonProperty("remove")
41+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
42+
private Boolean remove;
43+
4044
public CompanyWithStringPlan() {
4145
}
4246

@@ -108,4 +112,12 @@ public void setCustomAttributes(Map<String, CustomAttribute> customAttributes) {
108112
this.customAttributes = customAttributes;
109113
}
110114

115+
public Boolean getRemove() {
116+
return remove;
117+
}
118+
119+
public CompanyWithStringPlan setRemove(Boolean remove) {
120+
this.remove = remove;
121+
return this;
122+
}
111123
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class User extends TypedData implements Replier {
2727
}
2828

2929
private static List<CompanyWithStringPlan> buildUserUpdateCompanies(User user) {
30-
return CompanyUpdateBuilder.buildUserUpdateCompanies(user.getCompanyCollection());
30+
return CompanyUpdateBuilder.buildUserUpdateCompanies(user.getCompanyCollection(), user.getRemoveCompanyCollection());
3131
}
3232

3333
public static User find(String id)
@@ -333,6 +333,8 @@ public Boolean isNewSession() {
333333

334334
private Boolean untag;
335335

336+
private CompanyCollection removeCompanyCollection = new CompanyCollection();
337+
336338
public User() {
337339
}
338340

@@ -497,6 +499,11 @@ public User addCompany(Company company) {
497499
return this;
498500
}
499501

502+
public User removeCompany(Company company) {
503+
this.removeCompanyCollection.addCompany(company);
504+
return this;
505+
}
506+
500507
public SocialProfileCollection getSocialProfileCollection() {
501508
return socialProfileCollection;
502509
}
@@ -527,6 +534,14 @@ public User setNewSession(boolean newSession) {
527534
return this;
528535
}
529536

537+
CompanyCollection getRemoveCompanyCollection() {
538+
return removeCompanyCollection;
539+
}
540+
541+
void setRemoveCompanyCollection(CompanyCollection removeCompanyCollection) {
542+
this.removeCompanyCollection = removeCompanyCollection;
543+
}
544+
530545
@Override
531546
public boolean equals(Object o) {
532547
if (this == o) return true;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.google.common.collect.Lists;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
import static org.junit.Assert.*;
11+
12+
public class CompanyUpdateBuilderTest {
13+
14+
private static ObjectMapper mapper;
15+
16+
@BeforeClass
17+
public static void beforeClass() {
18+
mapper = MapperSupport.objectMapper();
19+
}
20+
21+
22+
@Test
23+
public void testRemove() throws Exception {
24+
25+
final Company bacon = new Company().setCompanyID("bacon");
26+
final Company pancake = new Company().setCompanyID("pancake");
27+
28+
final List<CompanyWithStringPlan> cos = CompanyUpdateBuilder.buildUserUpdateCompanies(
29+
new CompanyCollection(Lists.newArrayList(pancake)),
30+
new CompanyCollection(Lists.newArrayList(bacon))
31+
);
32+
33+
Boolean baconIsRemoved = null;
34+
Boolean pancakeIsRemoved = null;
35+
36+
CompanyWithStringPlan baconCo = null;
37+
CompanyWithStringPlan pancakeCo = null;
38+
39+
for (CompanyWithStringPlan co : cos) {
40+
if (co.getCompanyID().equals("pancake")) {
41+
pancakeIsRemoved = co.getRemove();
42+
pancakeCo = co;
43+
}
44+
45+
if (co.getCompanyID().equals("bacon")) {
46+
baconIsRemoved = co.getRemove();
47+
baconCo = co;
48+
}
49+
50+
}
51+
assertNull(pancakeIsRemoved);
52+
assertTrue(baconIsRemoved);
53+
54+
final String pancakeJson = mapper.writeValueAsString(pancakeCo);
55+
assertFalse(pancakeJson.contains("remove"));
56+
assertFalse(pancakeJson.contains("true"));
57+
58+
final String baconJson = mapper.writeValueAsString(baconCo);
59+
assertTrue(baconJson.contains("remove"));
60+
assertTrue(baconJson.contains("true"));
61+
}
62+
63+
}

0 commit comments

Comments
 (0)