11package io .intercom .api ;
22
3-
3+ import com .google .common .base .Objects ;
4+ import com .google .common .base .Predicate ;
5+ import com .google .common .collect .Iterables ;
46import com .google .common .collect .Lists ;
57
6- import java .util .ArrayList ;
78import java .util .List ;
89
910class CompanyUpdateBuilder {
@@ -13,11 +14,13 @@ class CompanyUpdateBuilder {
1314 */
1415 static List <CompanyWithStringPlan > buildUserUpdateCompanies (CompanyCollection add , CompanyCollection remove ) {
1516
16- final ArrayList <CompanyWithStringPlan > updatableCompanies = Lists .newArrayList ();
17+ final List <CompanyWithStringPlan > updatableCompanies = Lists .newArrayList ();
1718 if (add != null ) {
1819 final List <Company > companies = add .getPage ();
1920 for (Company company : companies ) {
20- updatableCompanies .add (prepareUpdatableCompany (company ));
21+ if (!isCompanyInList (company , remove )) {
22+ updatableCompanies .add (prepareUpdatableCompany (company ));
23+ }
2124 }
2225 }
2326
@@ -31,14 +34,29 @@ static List<CompanyWithStringPlan> buildUserUpdateCompanies(CompanyCollection ad
3134 return updatableCompanies ;
3235 }
3336
37+ private static boolean isCompanyInList (final Company company , CompanyCollection companyCollection ) {
38+ if (companyCollection == null ) {
39+ return false ;
40+ }
41+
42+ return Iterables .any (companyCollection .getPage (), new Predicate <Company >() {
43+ @ Override
44+ public boolean apply (Company e ) {
45+ return Objects .equal (company .getCompanyID (), e .getCompanyID ())
46+ || Objects .equal (company .getId (), e .getId ());
47+ }
48+ });
49+ }
50+
3451 private static CompanyWithStringPlan prepareUpdatableCompany (Company company ) {
3552 final CompanyWithStringPlan updatableCompany = new CompanyWithStringPlan ();
53+ updatableCompany .setId (company .getId ());
3654 updatableCompany .setCompanyID (company .getCompanyID ());
3755 updatableCompany .setName (company .getName ());
3856 updatableCompany .setSessionCount (company .getSessionCount ());
3957 updatableCompany .setMonthlySpend (company .getMonthlySpend ());
4058 updatableCompany .setRemoteCreatedAt (company .getRemoteCreatedAt ());
41- if (company .getCustomAttributes () != null ) {
59+ if (company .getCustomAttributes () != null ) {
4260 updatableCompany .getCustomAttributes ().putAll (company .getCustomAttributes ());
4361 }
4462 if (company .getPlan () != null ) {
0 commit comments